[or-cvs] r9571: Do not try to load advapi32.dll except when the user asks us (in tor/trunk: . src/or)

nickm at seul.org nickm at seul.org
Mon Feb 12 21:52:34 UTC 2007


Author: nickm
Date: 2007-02-12 16:52:32 -0500 (Mon, 12 Feb 2007)
New Revision: 9571

Modified:
   tor/trunk/
   tor/trunk/ChangeLog
   tor/trunk/src/or/main.c
Log:
 r11779 at catbus:  nickm | 2007-02-12 16:52:29 -0500
 Do not try to load advapi32.dll except when the user asks us to do something with NT services.  When we _do_ try, always treat failure to load service functions as an exit-worthy error.  Needs testing. (Fixes bug 389)



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r11779] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/ChangeLog
===================================================================
--- tor/trunk/ChangeLog	2007-02-12 21:39:53 UTC (rev 9570)
+++ tor/trunk/ChangeLog	2007-02-12 21:52:32 UTC (rev 9571)
@@ -31,6 +31,8 @@
       other than file-not-found.
     - Don't warn the user when cached-routers.new doesn't exist: that's
       perfectly fine when starting up for the first time.
+    - Don't try to load NT service functions from advapi32.dll except when
+      we need them. (bug introduced in 0.1.2.7-alpha).
 
   o Minor features:
     - Warn the user when an application uses the obsolete binary v0

Modified: tor/trunk/src/or/main.c
===================================================================
--- tor/trunk/src/or/main.c	2007-02-12 21:39:53 UTC (rev 9570)
+++ tor/trunk/src/or/main.c	2007-02-12 21:52:32 UTC (rev 9571)
@@ -1781,22 +1781,22 @@
                   NULL, NULL, NULL, NULL, NULL, NULL,
                   NULL};
 
-/** Loads functions used by NT services. Returns 0 on success, or -1 on
+/** Loads functions used by NT services. Returns 0 on success, exits on
  * error. */
-static int
+static void
 nt_service_loadlibrary(void)
 {
   HMODULE library = 0;
   void *fn;
 
   if (service_fns.loaded)
-    return 0;
+    return;
 
   /* XXXX Possibly, we should hardcode the location of this DLL. */
   if (!(library = LoadLibrary("advapi32.dll"))) {
     log_err(LD_GENERAL, "Couldn't open advapi32.dll.  Are you trying to use "
             "NT services on Windows 98? That doesn't work.");
-    return -1;
+    goto err;
   }
 
 #define LOAD(f) do {                                                    \
@@ -1804,7 +1804,7 @@
       log_err(LD_BUG,                                                   \
               "Couldn't find %s in advapi32.dll! We probably got the "  \
               "name wrong.", #f);                                       \
-      return -1;                                                        \
+      goto err;                                                         \
     } else {                                                            \
       service_fns.f ## _fn = fn;                                        \
     }                                                                   \
@@ -1826,7 +1826,10 @@
 
   service_fns.loaded = 1;
 
-  return 0;
+  return;
+ err:
+  printf("Unable to load library support for NT services: exiting.\n");
+  exit(1);
 }
 
 /** If we're compiled to run as an NT service, and the service has been
@@ -1836,8 +1839,7 @@
 static int
 nt_service_is_stopped(void)
 {
-  if (nt_service_loadlibrary()<0)
-    return -1;
+  nt_service_loadlibrary();
 
   if (service_status.dwCurrentState == SERVICE_STOP_PENDING) {
     service_status.dwWin32ExitCode = 0;
@@ -1859,8 +1861,7 @@
   exit_now.tv_sec  = 0;
   exit_now.tv_usec = 0;
 
-  if (nt_service_loadlibrary()<0)
-    return;
+  nt_service_loadlibrary();
 
   switch (request) {
     case SERVICE_CONTROL_STOP:
@@ -1882,8 +1883,7 @@
 nt_service_body(int argc, char **argv)
 {
   int r;
-  if (nt_service_loadlibrary()<0)
-    return;
+  nt_service_loadlibrary();
   service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
   service_status.dwCurrentState = SERVICE_START_PENDING;
   service_status.dwControlsAccepted =
@@ -1927,8 +1927,7 @@
   SERVICE_TABLE_ENTRY table[2];
   DWORD result = 0;
   char *errmsg;
-  if (nt_service_loadlibrary()<0)
-    return;
+  nt_service_loadlibrary();
   table[0].lpServiceName = GENSRV_SERVICENAME;
   table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)nt_service_body;
   table[1].lpServiceName = NULL;
@@ -1972,8 +1971,7 @@
   SC_HANDLE hSCManager;
   char *errmsg = NULL;
 
-  if (nt_service_loadlibrary()<0)
-    return 0;
+  nt_service_loadlibrary();
   if ((hSCManager = service_fns.OpenSCManagerA_fn(
                             NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) {
     errmsg = nt_strerror(GetLastError());
@@ -1990,9 +1988,7 @@
 {
   SC_HANDLE hService;
   char *errmsg = NULL;
-
-  if (nt_service_loadlibrary()<0)
-    return 0;
+  nt_service_loadlibrary();
   if ((hService = service_fns.OpenServiceA_fn(hSCManager, GENSRV_SERVICENAME,
                               SERVICE_ALL_ACCESS)) == NULL) {
     errmsg = nt_strerror(GetLastError());
@@ -2009,8 +2005,7 @@
 {
   char *errmsg = NULL;
 
-  if (nt_service_loadlibrary()<0)
-    return -1;
+  nt_service_loadlibrary();
 
   service_fns.QueryServiceStatus_fn(hService, &service_status);
   if (service_status.dwCurrentState == SERVICE_RUNNING) {
@@ -2051,8 +2046,7 @@
 #define MAX_SERVICE_WAIT_TIME 10
   int wait_time;
   char *errmsg = NULL;
-  if (nt_service_loadlibrary()<0)
-    return -1;
+  nt_service_loadlibrary();
 
   service_fns.QueryServiceStatus_fn(hService, &service_status);
   if (service_status.dwCurrentState == SERVICE_STOPPED) {
@@ -2169,8 +2163,7 @@
   int is_win2k_or_worse = 0;
   int using_default_torrc = 0;
 
-  if (nt_service_loadlibrary()<0)
-    return -1;
+  nt_service_loadlibrary();
 
   /* Open the service control manager so we can create a new service */
   if ((hSCManager = nt_service_open_scm()) == NULL)
@@ -2282,8 +2275,7 @@
   SC_HANDLE hService = NULL;
   char *errmsg;
 
-  if (nt_service_loadlibrary()<0)
-    return -1;
+  nt_service_loadlibrary();
   if ((hSCManager = nt_service_open_scm()) == NULL)
     return -1;
   if ((hService = nt_service_open(hSCManager)) == NULL) {
@@ -2393,10 +2385,7 @@
   backup_argc = argc;
   if ((argc >= 3) &&
       (!strcmp(argv[1], "-service") || !strcmp(argv[1], "--service"))) {
-    if (nt_service_loadlibrary() < 0) {
-      printf("Unable to load library support for NT services.\n");
-      return -1;
-    }
+    nt_service_loadlibrary();
     if (!strcmp(argv[2], "install"))
       return nt_service_install(argc, argv);
     if (!strcmp(argv[2], "remove"))
@@ -2409,23 +2398,22 @@
     return -1;
   }
   if (argc >= 2) {
-    if (nt_service_loadlibrary() < 0) {
-      printf("Unable to load library support for NT services.\n");
-      return -1;
-    }
     if (!strcmp(argv[1], "-nt-service") || !strcmp(argv[1], "--nt-service")) {
+      nt_service_loadlibrary();
       nt_service_main();
       return 0;
     }
     // These values have been deprecated since 0.1.1.2-alpha; we've warned
     // about them since 0.1.2.7-alpha.
     if (!strcmp(argv[1], "-install") || !strcmp(argv[1], "--install")) {
+      nt_service_loadlibrary();
       fprintf(stderr,
             "The %s option is deprecated; use \"--service install\" instead.",
             argv[1]);
       return nt_service_install(argc, argv);
     }
     if (!strcmp(argv[1], "-remove") || !strcmp(argv[1], "--remove")) {
+      nt_service_loadlibrary();
       fprintf(stderr,
             "The %s option is deprecated; use \"--service remove\" instead.",
             argv[1]);



More information about the tor-commits mailing list