[or-cvs] forward-port NT service patch

Nick Mathewson nickm at seul.org
Sat Feb 5 21:40:27 UTC 2005


Update of /home/or/cvsroot/tor/src/or
In directory moria.mit.edu:/tmp/cvs-serv32015/src/or

Modified Files:
	config.c main.c 
Log Message:
forward-port NT service patch

Index: config.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/config.c,v
retrieving revision 1.309
retrieving revision 1.310
diff -u -d -r1.309 -r1.310
--- config.c	3 Feb 2005 22:58:22 -0000	1.309
+++ config.c	5 Feb 2005 21:40:24 -0000	1.310
@@ -411,6 +411,9 @@
     } else if (!strcmp(argv[i],"--list-fingerprint")) {
       i += 1; /* command-line option. ignore it. */
       continue;
+    } else if (!strcmp(argv[i],"--nt-service")) {
+      i += 1;
+      continue;
     }
 
     new = tor_malloc(sizeof(struct config_line_t));

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.440
retrieving revision 1.441
diff -u -d -r1.440 -r1.441
--- main.c	3 Feb 2005 07:30:00 -0000	1.440
+++ main.c	5 Feb 2005 21:40:25 -0000	1.441
@@ -88,6 +88,7 @@
 #include <tchar.h>
 #define GENSRV_SERVICENAME  TEXT("tor-"VERSION)
 #define GENSRV_DISPLAYNAME  TEXT("Tor "VERSION" Win32 Service")
+#define GENSRV_DESCRIPTION  TEXT("Provides an anonymous Internet communication system")
 SERVICE_STATUS service_status;
 SERVICE_STATUS_HANDLE hStatus;
 static char **backup_argv;
@@ -1372,7 +1373,7 @@
 
 #ifdef MS_WINDOWS_SERVICE
 /** If we're compile to run as an NT service, and the service has been
- * shut down, then change our current status and return 1.  Else 
+ * shut down, then change our current status and return 1.  Else
  * return 0.
  */
 static int
@@ -1489,6 +1490,7 @@
 
   SC_HANDLE hSCManager = NULL;
   SC_HANDLE hService = NULL;
+  SERVICE_DESCRIPTION sdBuff;
   TCHAR szPath[_MAX_PATH];
   TCHAR szDrive[_MAX_DRIVE];
   TCHAR szDir[_MAX_DIR];
@@ -1504,13 +1506,13 @@
 
   /* Account for the extra quotes */
   //len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
-  len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 4;
+  len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2) + 64;
   command = tor_malloc(len);
 
   /* Create a quoted command line, like "c:\with spaces\tor.exe" -f
    * "c:\with spaces\tor.exe"
    */
-  if (tor_snprintf(command, len, "\"%s\" -f \"%s%storrc\"",
+  if (tor_snprintf(command, len, "\"%s\" --nt-service -f \"%s%storrc\"",
                    szPath,  szDrive, szDir)<0) {
     printf("Failed: tor_snprinf()\n");
     free(command);
@@ -1540,10 +1542,25 @@
 
   /* Start the service initially, so you don't have to muck with it in the SCM
    */
-  if(!StartService(hService, 0, NULL))
-    printf("Service installed, but not started.\n");
-  else
-    printf("Service installed and started successfully!\n");
+  /* Set the service's description */
+  sdBuff.lpDescription = GENSRV_DESCRIPTION;
+  ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &sdBuff);
+
+  /* Start the service, so you don't have to muck with it in the SCM */
+  if (StartService(hService, 0, NULL)) {
+    /* Loop until the service has finished attempting to start */
+    while (QueryServiceStatus(hService, &service_status) &&
+           service_status.dwCurrentState == SERVICE_START_PENDING)
+      Sleep(500);
+
+    /* Check if it started successfully or not */
+    if (service_status.dwCurrentState == SERVICE_RUNNING)
+      printf("Service installed and started successfully.\n");
+    else
+      printf("Service installed, but failed to start.\n");
+  } else {
+    printf("Service installed, but failed to start.\n");
+  }
 
   CloseServiceHandle(hService);
   CloseServiceHandle(hSCManager);
@@ -1567,6 +1584,7 @@
   if ((hService = OpenService(hSCManager, GENSRV_SERVICENAME, SERVICE_ALL_ACCESS)) == NULL) {
     printf("Failed: OpenService()\n");
     CloseServiceHandle(hSCManager);
+    return 0;
   }
 
   result = ControlService(hService, SERVICE_CONTROL_STOP, &service_status);
@@ -1579,13 +1597,13 @@
         break;
     }
     if (DeleteService(hService))
-      printf("Remove service successfully\n");
+      printf("Removed service successfully\n");
     else
       printf("Failed: DeleteService()\n");
   } else {
     result = DeleteService(hService);
     if (result)
-      printf("Remove service successfully\n");
+      printf("Removed service successfully\n");
     else
       printf("Failed: DeleteService()\n");
   }
@@ -1605,8 +1623,10 @@
     return nt_service_install();
   if ((argc >= 2) && !strcmp(argv[1], "-remove"))
     return nt_service_remove();
-  nt_service_main();
-  return 0;
+  if ((argc >= 2) && !strcmp(argv[1], "--nt-service")) {
+    nt_service_main();
+    return 0;
+  }
 #else
   if (tor_init(argc, argv)<0)
     return -1;



More information about the tor-commits mailing list