[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] Apply patch from Matt Edman for NT services, mildly tweaked...



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

Modified Files:
      Tag: tor-0_0_9-patches
	main.c config.c 
Log Message:
Apply patch from Matt Edman for NT services, mildly tweaked to Finally Do What Nick Wants.

Index: main.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/main.c,v
retrieving revision 1.407.2.10
retrieving revision 1.407.2.11
diff -u -d -r1.407.2.10 -r1.407.2.11
--- main.c	4 Feb 2005 07:16:13 -0000	1.407.2.10
+++ main.c	5 Feb 2005 21:30:41 -0000	1.407.2.11
@@ -73,6 +73,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;
@@ -99,7 +100,7 @@
   tor_assert(conn);
   tor_assert(conn->s >= 0);
 
-  if (nfds >= get_options()->_MaxConn-1) {
+  if (nfds >= get_options()->MaxConn-1) {
     log_fn(LOG_WARN,"Failing because we have %d connections already. Please raise your ulimit -n.", nfds);
     return -1;
   }
@@ -1276,6 +1277,7 @@
 
   SC_HANDLE hSCManager = NULL;
   SC_HANDLE hService = NULL;
+  SERVICE_DESCRIPTION sdBuff;
   TCHAR szPath[_MAX_PATH];
   TCHAR szDrive[_MAX_DRIVE];
   TCHAR szDir[_MAX_DIR];
@@ -1291,7 +1293,7 @@
 
   /* 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);
 
   /* 1/26/2005 mje - There was an extra '\' between end of the path and 'torrc'
@@ -1303,7 +1305,7 @@
   //strlcpy(command, szPath, len);
   strlcat(command, szPath, len);
   strlcat(command, "\"", len);
-  strlcat(command, " -f ", len);
+  strlcat(command, " --nt-service -f ", len);
   strlcat(command, "\"", len);
   strlcat(command, szDrive, len);
   strlcat(command, szDir, len);
@@ -1331,11 +1333,26 @@
     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");
+  /* 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);
@@ -1359,6 +1376,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);
@@ -1371,13 +1389,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");
   }
@@ -1397,8 +1415,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;

Index: config.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/or/config.c,v
retrieving revision 1.286.2.9
retrieving revision 1.286.2.10
diff -u -d -r1.286.2.9 -r1.286.2.10
--- config.c	4 Feb 2005 07:15:40 -0000	1.286.2.9
+++ config.c	5 Feb 2005 21:30:41 -0000	1.286.2.10
@@ -420,6 +420,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));