Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0c9b166

Browse files
committed
Allow pg_ctl to register the service in either AUTO or DEMAND start type
Author: Quan Zongliang Documentation updates by David Fetter
1 parent 0e7f707 commit 0c9b166

File tree

2 files changed

+59
-9
lines changed

2 files changed

+59
-9
lines changed

doc/src/sgml/ref/pg_ctl-ref.sgml

+22-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ PostgreSQL documentation
102102
<arg>-U <replaceable>username</replaceable></arg>
103103
<arg>-P <replaceable>password</replaceable></arg>
104104
<arg>-D <replaceable>datadir</replaceable></arg>
105+
<arg>-S
106+
<group choice="plain">
107+
<arg>a[uto]</arg>
108+
<arg>d[emand]</arg>
109+
</group>
110+
</arg>
105111
<arg>-w</arg>
106112
<arg>-t <replaceable>seconds</replaceable></arg>
107113
<arg>-o <replaceable>options</replaceable></arg>
@@ -203,7 +209,9 @@ PostgreSQL documentation
203209

204210
<para>
205211
<option>register</option> mode allows you to register a system service
206-
on <productname>Microsoft Windows</>.
212+
on <productname>Microsoft Windows</>. The <option>-S</option> option
213+
allows selection of service start type, either <quote>auto</quote> (start
214+
service automatically on system startup), on <quote>demand</quote>.
207215
</para>
208216

209217
<para>
@@ -261,7 +269,7 @@ PostgreSQL documentation
261269
Specifies the shutdown mode. <replaceable>mode</replaceable>
262270
can be <literal>smart</literal>, <literal>fast</literal>, or
263271
<literal>immediate</literal>, or the first letter of one of
264-
these three.
272+
these three. If this is omitted, <literal>smart</literal> is used.
265273
</para>
266274
</listitem>
267275
</varlistentry>
@@ -384,6 +392,18 @@ PostgreSQL documentation
384392
</para>
385393
</listitem>
386394
</varlistentry>
395+
396+
<varlistentry>
397+
<term><option>-S <replaceable class="parameter">start-type</replaceable></option></term>
398+
<listitem>
399+
<para>
400+
Start type of the system service to register. start-type can
401+
be <literal>auto</literal>, or <literal>demand</literal>, or
402+
the first letter of one of these two. If this is omitted,
403+
<literal>auto</literal> is used.
404+
</para>
405+
</listitem>
406+
</varlistentry>
387407
</variablelist>
388408
</refsect2>
389409

src/bin/pg_ctl/pg_ctl.c

+37-7
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *);
121121
static void pgwin32_doRunAsService(void);
122122
static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_service);
123123

124+
static DWORD pgctl_start_type = SERVICE_AUTO_START;
124125
static SERVICE_STATUS status;
125126
static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0;
126127
static HANDLE shutdownHandles[2];
@@ -1163,8 +1164,8 @@ pgwin32_doRegister(void)
11631164
}
11641165

11651166
if ((hService = CreateService(hSCM, register_servicename, register_servicename,
1166-
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1167-
SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
1167+
SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
1168+
pgctl_start_type, SERVICE_ERROR_NORMAL,
11681169
pgwin32_CommandLine(true),
11691170
NULL, NULL, "RPCSS\0", register_username, register_password)) == NULL)
11701171
{
@@ -1603,7 +1604,7 @@ do_help(void)
16031604
printf(_(" %s kill SIGNALNAME PID\n"), progname);
16041605
#if defined(WIN32) || defined(__CYGWIN__)
16051606
printf(_(" %s register [-N SERVICENAME] [-U USERNAME] [-P PASSWORD] [-D DATADIR]\n"
1606-
" [-w] [-t SECS] [-o \"OPTIONS\"]\n"), progname);
1607+
" [-S START-TYPE] [-w] [-t SECS] [-o \"OPTIONS\"]\n"), progname);
16071608
printf(_(" %s unregister [-N SERVICENAME]\n"), progname);
16081609
#endif
16091610

@@ -1644,6 +1645,11 @@ do_help(void)
16441645
printf(_(" -N SERVICENAME service name with which to register PostgreSQL server\n"));
16451646
printf(_(" -P PASSWORD password of account to register PostgreSQL server\n"));
16461647
printf(_(" -U USERNAME user name of account to register PostgreSQL server\n"));
1648+
printf(_(" -S START-TYPE service start type to register PostgreSQL server\n"));
1649+
1650+
printf(_("\nStart types are:\n"));
1651+
printf(_(" auto start service automatically during system startup (default)\n"));
1652+
printf(_(" demand start service on demand\n"));
16471653
#endif
16481654

16491655
printf(_("\nReport bugs to <pgsql-bugs@postgresql.org>.\n"));
@@ -1708,10 +1714,26 @@ set_sig(char *signame)
17081714
do_advice();
17091715
exit(1);
17101716
}
1711-
17121717
}
17131718

17141719

1720+
#if defined(WIN32) || defined(__CYGWIN__)
1721+
static void
1722+
set_starttype(char *starttypeopt)
1723+
{
1724+
if (strcmp(starttypeopt, "a") == 0 || strcmp(starttypeopt, "auto") == 0)
1725+
pgctl_start_type = SERVICE_AUTO_START;
1726+
else if (strcmp(starttypeopt, "d") == 0 || strcmp(starttypeopt, "demand") == 0)
1727+
pgctl_start_type = SERVICE_DEMAND_START;
1728+
else
1729+
{
1730+
write_stderr(_("%s: unrecognized start type \"%s\"\n"), progname, starttypeopt);
1731+
do_advice();
1732+
exit(1);
1733+
}
1734+
}
1735+
#endif
1736+
17151737

17161738
int
17171739
main(int argc, char **argv)
@@ -1789,7 +1811,7 @@ main(int argc, char **argv)
17891811
/* process command-line options */
17901812
while (optind < argc)
17911813
{
1792-
while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:st:U:wW", long_options, &option_index)) != -1)
1814+
while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1)
17931815
{
17941816
switch (c)
17951817
{
@@ -1836,6 +1858,15 @@ main(int argc, char **argv)
18361858
case 's':
18371859
silent_mode = true;
18381860
break;
1861+
case 'S':
1862+
#if defined(WIN32) || defined(__CYGWIN__)
1863+
set_starttype(optarg);
1864+
#else
1865+
write_stderr(_("%s: -S option not supported on this platform\n"),
1866+
progname);
1867+
exit(1);
1868+
#endif
1869+
break;
18391870
case 't':
18401871
wait_seconds = atoi(optarg);
18411872
break;
@@ -1944,8 +1975,7 @@ main(int argc, char **argv)
19441975
if (pg_data == NULL &&
19451976
ctl_command != KILL_COMMAND && ctl_command != UNREGISTER_COMMAND)
19461977
{
1947-
write_stderr(_("%s: no database directory specified "
1948-
"and environment variable PGDATA unset\n"),
1978+
write_stderr(_("%s: no database directory specified and environment variable PGDATA unset\n"),
19491979
progname);
19501980
do_advice();
19511981
exit(1);

0 commit comments

Comments
 (0)