From 0c9b166db5329119b6553e0f38fe486521f1352f Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 15 Oct 2010 14:30:03 -0300 Subject: [PATCH] Allow pg_ctl to register the service in either AUTO or DEMAND start type Author: Quan Zongliang Documentation updates by David Fetter --- doc/src/sgml/ref/pg_ctl-ref.sgml | 24 ++++++++++++++++++++-- src/bin/pg_ctl/pg_ctl.c | 44 +++++++++++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 9 deletions(-) diff --git a/doc/src/sgml/ref/pg_ctl-ref.sgml b/doc/src/sgml/ref/pg_ctl-ref.sgml index 31a1262fca..29bea24cdd 100644 --- a/doc/src/sgml/ref/pg_ctl-ref.sgml +++ b/doc/src/sgml/ref/pg_ctl-ref.sgml @@ -102,6 +102,12 @@ PostgreSQL documentation -U username -P password -D datadir + -S + + a[uto] + d[emand] + + -w -t seconds -o options @@ -203,7 +209,9 @@ PostgreSQL documentation mode allows you to register a system service - on Microsoft Windows. + on Microsoft Windows. The option + allows selection of service start type, either auto (start + service automatically on system startup), on demand. @@ -261,7 +269,7 @@ PostgreSQL documentation Specifies the shutdown mode. mode can be smart, fast, or immediate, or the first letter of one of - these three. + these three. If this is omitted, smart is used. @@ -384,6 +392,18 @@ PostgreSQL documentation + + + + + + Start type of the system service to register. start-type can + be auto, or demand, or + the first letter of one of these two. If this is omitted, + auto is used. + + + diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c index 4af639bfee..14d36b5fac 100644 --- a/src/bin/pg_ctl/pg_ctl.c +++ b/src/bin/pg_ctl/pg_ctl.c @@ -121,6 +121,7 @@ static void WINAPI pgwin32_ServiceMain(DWORD, LPTSTR *); static void pgwin32_doRunAsService(void); static int CreateRestrictedProcess(char *cmd, PROCESS_INFORMATION *processInfo, bool as_service); +static DWORD pgctl_start_type = SERVICE_AUTO_START; static SERVICE_STATUS status; static SERVICE_STATUS_HANDLE hStatus = (SERVICE_STATUS_HANDLE) 0; static HANDLE shutdownHandles[2]; @@ -1163,8 +1164,8 @@ pgwin32_doRegister(void) } if ((hService = CreateService(hSCM, register_servicename, register_servicename, - SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, - SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, + SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, + pgctl_start_type, SERVICE_ERROR_NORMAL, pgwin32_CommandLine(true), NULL, NULL, "RPCSS\0", register_username, register_password)) == NULL) { @@ -1603,7 +1604,7 @@ do_help(void) printf(_(" %s kill SIGNALNAME PID\n"), progname); #if defined(WIN32) || defined(__CYGWIN__) printf(_(" %s register [-N SERVICENAME] [-U USERNAME] [-P PASSWORD] [-D DATADIR]\n" - " [-w] [-t SECS] [-o \"OPTIONS\"]\n"), progname); + " [-S START-TYPE] [-w] [-t SECS] [-o \"OPTIONS\"]\n"), progname); printf(_(" %s unregister [-N SERVICENAME]\n"), progname); #endif @@ -1644,6 +1645,11 @@ do_help(void) printf(_(" -N SERVICENAME service name with which to register PostgreSQL server\n")); printf(_(" -P PASSWORD password of account to register PostgreSQL server\n")); printf(_(" -U USERNAME user name of account to register PostgreSQL server\n")); + printf(_(" -S START-TYPE service start type to register PostgreSQL server\n")); + + printf(_("\nStart types are:\n")); + printf(_(" auto start service automatically during system startup (default)\n")); + printf(_(" demand start service on demand\n")); #endif printf(_("\nReport bugs to .\n")); @@ -1708,10 +1714,26 @@ set_sig(char *signame) do_advice(); exit(1); } - } +#if defined(WIN32) || defined(__CYGWIN__) +static void +set_starttype(char *starttypeopt) +{ + if (strcmp(starttypeopt, "a") == 0 || strcmp(starttypeopt, "auto") == 0) + pgctl_start_type = SERVICE_AUTO_START; + else if (strcmp(starttypeopt, "d") == 0 || strcmp(starttypeopt, "demand") == 0) + pgctl_start_type = SERVICE_DEMAND_START; + else + { + write_stderr(_("%s: unrecognized start type \"%s\"\n"), progname, starttypeopt); + do_advice(); + exit(1); + } +} +#endif + int main(int argc, char **argv) @@ -1789,7 +1811,7 @@ main(int argc, char **argv) /* process command-line options */ while (optind < argc) { - while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:st:U:wW", long_options, &option_index)) != -1) + while ((c = getopt_long(argc, argv, "cD:l:m:N:o:p:P:sS:t:U:wW", long_options, &option_index)) != -1) { switch (c) { @@ -1836,6 +1858,15 @@ main(int argc, char **argv) case 's': silent_mode = true; break; + case 'S': +#if defined(WIN32) || defined(__CYGWIN__) + set_starttype(optarg); +#else + write_stderr(_("%s: -S option not supported on this platform\n"), + progname); + exit(1); +#endif + break; case 't': wait_seconds = atoi(optarg); break; @@ -1944,8 +1975,7 @@ main(int argc, char **argv) if (pg_data == NULL && ctl_command != KILL_COMMAND && ctl_command != UNREGISTER_COMMAND) { - write_stderr(_("%s: no database directory specified " - "and environment variable PGDATA unset\n"), + write_stderr(_("%s: no database directory specified and environment variable PGDATA unset\n"), progname); do_advice(); exit(1); -- 2.11.0