[or-cvs] implement patch by matt edman to make nt services work and

Roger Dingledine arma at seul.org
Thu Jan 27 06:59:01 UTC 2005


Update of /home2/or/cvsroot/tor/src/or
In directory moria.mit.edu:/home2/arma/work/onion/0091/tor/src/or

Modified Files:
      Tag: tor-0_0_9-patches
	main.c 
Log Message:
implement patch by matt edman to make nt services work and
start on startup.
(can somebody try this?)


Index: main.c
===================================================================
RCS file: /home2/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.407.2.4
retrieving revision 1.407.2.5
diff -u -d -r1.407.2.4 -r1.407.2.5
--- main.c	10 Jan 2005 04:36:47 -0000	1.407.2.4
+++ main.c	27 Jan 2005 06:58:59 -0000	1.407.2.5
@@ -1102,6 +1102,7 @@
 
   /* give it somewhere to log to initially */
   add_temp_log();
+
   log_fn(LOG_NOTICE,"Tor v%s. This is experimental software. Do not rely on it for strong anonymity.",VERSION);
 
   if (network_init()<0) {
@@ -1207,7 +1208,6 @@
 void nt_service_body(int argc, char **argv)
 {
   int err;
-  FILE *f;
   service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
   service_status.dwCurrentState = SERVICE_START_PENDING;
   service_status.dwControlsAccepted =
@@ -1217,10 +1217,12 @@
   service_status.dwCheckPoint = 0;
   service_status.dwWaitHint = 1000;
   hStatus = RegisterServiceCtrlHandler(GENSRV_SERVICENAME, (LPHANDLER_FUNCTION) nt_service_control);
+
   if (hStatus == 0) {
     // failed;
     return;
   }
+
   err = tor_init(backup_argc, backup_argv); // refactor this part out of tor_main and do_main_loop
   if (err) {
     // failed.
@@ -1244,6 +1246,7 @@
   table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
   table[1].lpServiceName = NULL;
   table[1].lpServiceProc = NULL;
+
   if (!StartServiceCtrlDispatcher(table)) {
     result = GetLastError();
     printf("Error was %d\n",result);
@@ -1272,11 +1275,15 @@
 {
   /* XXXX Problems with NT services:
    * 1. The configuration file needs to be in the same directory as the .exe
+   *
    * 2. The exe and the configuration file can't be on any directory path
    *    that contains a space.
+   *    mje - you can quote the string (i.e., "c:\program files")
+   *
    * 3. Ideally, there should be one EXE that can either run as a
    *    separate process (as now) or that can install and run itself
    *    as an NT service.  I have no idea how hard this is.
+   *    mje - should be done. It can install and run itself as a service
    *
    * Notes about developing NT services:
    *
@@ -1300,14 +1307,27 @@
     return 0;
 
   _tsplitpath(szPath, szDrive, szDir, NULL, NULL);
-  len = _MAX_PATH + strlen(cmd1) + _MAX_DRIVE + _MAX_DIR + strlen(cmd2);
+
+  /* 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;
   command = tor_malloc(len);
 
-  strlcpy(command, szPath, len);
+  /* 1/26/2005 mje - There was an extra '\' between end of the path and 'torrc'
+   *               - Added quotes around the path\exe and the path\torrc
+   *     like the following:
+   *     "c:\with spaces\tor.exe" -f "c:\with spaces\tor.exe"
+   */
+  strlcpy(command, "\"", len);
+  //strlcpy(command, szPath, len);
+  strlcat(command, szPath, len);
+  strlcat(command, "\"", len);
   strlcat(command, " -f ", len);
+  strlcat(command, "\"", len);
   strlcat(command, szDrive, len);
   strlcat(command, szDir, len);
-  strlcat(command, "\\torrc", len);
+  strlcat(command, "torrc", len);
+  strlcat(command, "\"", len);
 
   if ((hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
     printf("Failed: OpenSCManager()\n");
@@ -1315,22 +1335,31 @@
     return 0;
   }
 
+  /* 1/26/2005 mje
+   * - changed the service start type to auto
+   * - and changed the lpPassword param to "" instead of NULL as per an
+   *   MSDN article.
+   */
   if ((hService = CreateService(hSCManager, GENSRV_SERVICENAME, GENSRV_DISPLAYNAME,
                                 SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
-                                SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, command,
-                                NULL, NULL, NULL, NULL, NULL)) == NULL) {
+                                SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, command,
+                                NULL, NULL, NULL, NULL, "")) == NULL) {
     printf("Failed: CreateService()\n");
     CloseServiceHandle(hSCManager);
     free(command);
     return 0;
   }
 
+  /* 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");
+
   CloseServiceHandle(hService);
   CloseServiceHandle(hSCManager);
   free(command);
 
-  printf("Install service successfully\n");
-
   return 0;
 }
 



More information about the tor-commits mailing list