From 0c9b166db5329119b6553e0f38fe486521f1352f Mon Sep 17 00:00:00 2001
From: Alvaro Herrera <alvherre@alvh.no-ip.org>
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 31a1262fca4..29bea24cdd4 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
    <arg>-U <replaceable>username</replaceable></arg>
    <arg>-P <replaceable>password</replaceable></arg>
    <arg>-D <replaceable>datadir</replaceable></arg>
+   <arg>-S
+     <group choice="plain">
+       <arg>a[uto]</arg>
+       <arg>d[emand]</arg>
+     </group>
+   </arg>
    <arg>-w</arg>
    <arg>-t <replaceable>seconds</replaceable></arg>
    <arg>-o <replaceable>options</replaceable></arg>
@@ -203,7 +209,9 @@ PostgreSQL documentation
 
   <para>
    <option>register</option> mode allows you to register a system service
-   on <productname>Microsoft Windows</>.
+   on <productname>Microsoft Windows</>.  The <option>-S</option> option
+   allows selection of service start type, either <quote>auto</quote> (start
+   service automatically on system startup), on <quote>demand</quote>.
   </para>
 
   <para>
@@ -261,7 +269,7 @@ PostgreSQL documentation
         Specifies the shutdown mode.  <replaceable>mode</replaceable>
         can be <literal>smart</literal>, <literal>fast</literal>, or
         <literal>immediate</literal>, or the first letter of one of
-        these three.
+        these three.  If this is omitted, <literal>smart</literal> is used.
        </para>
       </listitem>
      </varlistentry>
@@ -384,6 +392,18 @@ PostgreSQL documentation
       </para>
      </listitem>
     </varlistentry>
+
+    <varlistentry>
+     <term><option>-S <replaceable class="parameter">start-type</replaceable></option></term>
+     <listitem>
+      <para>
+       Start type of the system service to register.  start-type can
+       be <literal>auto</literal>, or <literal>demand</literal>, or
+       the first letter of one of these two. If this is omitted,
+       <literal>auto</literal> is used.
+      </para>
+     </listitem>
+    </varlistentry>
    </variablelist>
   </refsect2>
 
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index 4af639bfee4..14d36b5fac1 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 <pgsql-bugs@postgresql.org>.\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);
-- 
GitLab