]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
charon-svc: When running as service, change working directory to executable
authorMartin Willi <martin@revosec.ch>
Wed, 5 Mar 2014 14:45:42 +0000 (15:45 +0100)
committerMartin Willi <martin@revosec.ch>
Wed, 4 Jun 2014 13:53:09 +0000 (15:53 +0200)
Services get executed with system32 as current working directory. This does
not work for us, as we expect paths to be relative to the executable.

src/charon-svc/charon-svc.c

index 3f4b80dcede0cf43489319ba6f447e633bd81fce..c21fc1ff55efef6cd4731cdce960e6cac439cfa7 100644 (file)
@@ -215,6 +215,32 @@ static DWORD service_handler(DWORD dwControl, DWORD dwEventType,
        }
 }
 
+/**
+ * Switch the working directory to the executable directory
+ */
+static bool switch_workingdir()
+{
+       CHAR path[MAX_PATH], *pos;
+       HMODULE module;
+
+       module = GetModuleHandle(NULL);
+       if (!module)
+       {
+               return FALSE;
+       }
+       if (!GetModuleFileName(module, path, sizeof(path)))
+       {
+               return FALSE;
+       }
+       pos = strrchr(path, '\\');
+       if (!pos)
+       {
+               return FALSE;
+       }
+       *pos = 0;
+       return SetCurrentDirectory(path);
+}
+
 /**
  * Service main routine when running as service
  */
@@ -228,7 +254,10 @@ static void service_main(DWORD dwArgc, LPTSTR *lpszArgv)
        handle = RegisterServiceCtrlHandlerEx(SERVICE_NAME, service_handler, NULL);
        if (handle)
        {
-               init_and_run(dwArgc, lpszArgv);
+               if (switch_workingdir())
+               {
+                       init_and_run(dwArgc, lpszArgv);
+               }
        }
 }