commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jfcl...@apache.org
Subject cvs commit: jakarta-commons-sandbox/daemon/src/native/nt/procrun procrun.c
Date Mon, 17 Feb 2003 16:42:01 GMT
jfclere     2003/02/17 08:42:01

  Modified:    daemon/src/native/nt/procrun procrun.c
  Log:
  Add support for win9x.
  
  Revision  Changes    Path
  1.7       +218 -100  jakarta-commons-sandbox/daemon/src/native/nt/procrun/procrun.c
  
  Index: procrun.c
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/daemon/src/native/nt/procrun/procrun.c,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- procrun.c	13 Feb 2003 17:48:35 -0000	1.6
  +++ procrun.c	17 Feb 2003 16:42:00 -0000	1.7
  @@ -1568,32 +1568,35 @@
       return (err != ERROR_SUCCESS);
   }
   
  -static int procrun_install_service(process_t *proc, int argc, char **argv)
  +/* from src/os/win32/service.c (httpd-1.3!) */
  + 
  +static BOOL isWindowsNT(void)
   {
  -    SC_HANDLE service;
  -    SC_HANDLE manager;
  -    char *argp;
  -    char path[MAX_PATH+1];
  -    char *display = NULL;
  -    char *java = NULL;
  -    int arglen = 0;
  -
  -    int i, n;
  -    if (!proc->service.name) {
  -        return -1;
  -    }
  -    if (!GetModuleFileName(NULL, path, MAX_PATH - 
  -                           strlen(proc->service.name) - 7)) {
  -        return -1;
  +    static BOOL once = FALSE;
  +    static BOOL isNT = FALSE;
  + 
  +    if (!once)
  +    {
  +        OSVERSIONINFO osver;
  +        osver.dwOSVersionInfoSize = sizeof(osver);
  +        if (GetVersionEx(&osver))
  +            if (osver.dwPlatformId == VER_PLATFORM_WIN32_NT)
  +                isNT = TRUE;
  +        once = TRUE;
       }
  -    strcat(path, " " PROC_ARG_RUN_SERVICE);
  -    strcat(path, proc->service.name);
  -    DBPRINTF1("Installing service %s\n", path);
  +    return isNT;
  +}
  +
  +/*
  + * Process the arguments.
  + */
  +static int process_args(process_t *proc, int argc, char **argv,
  +                        char *display, char *java, char *path)
  +{
  +    int arglen = 0;
  +    char *argp;
  +    int i,n;
   
  -    manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  -    if (!manager) {    
  -        return -1;
  -    }
       /* parse command line */
       for (i = 2; i < argc; i++) {
           DBPRINTF2("Parsing %d [%s]\n", i, argv[i]);
  @@ -1681,35 +1684,23 @@
           display = proc->service.name;
       if (!proc->service.startup)
           proc->service.startup = SERVICE_AUTO_START;
  -    service = CreateService(manager, 
  -                            proc->service.name,
  -                            display,
  -                            SERVICE_ALL_ACCESS,
  -                            SERVICE_WIN32_OWN_PROCESS,
  -                            proc->service.startup,
  -                            SERVICE_ERROR_NORMAL,
  -                            path,
  -                            NULL,
  -                            NULL,
  -                            SERVICE_DEPENDENCIES,
  -                            NULL,
  -                            NULL);
  -    if (service) {
  -        CloseServiceHandle(service);
  -        CloseServiceHandle(manager);
  -    }
  -    else {
  -        DBPRINTF0(NULL);
  -        CloseServiceHandle(manager);
  -        return -1;
  -    }
  +    return 0;
  +}
  +
  +/*
  + * Save the parameters in registry
  + */
  +static void save_service_params(process_t *proc, char *java)
  +{
  +    int i;
  +
       if (proc->argw)
           set_service_param(proc, PROCRUN_PARAMS_CMDARGS, proc->argw, 0, 0);
       if (proc->service.description) {
           set_service_param(proc, PROCRUN_PARAMS_DESCRIPTION,
                             proc->service.description, 0, 1);
           set_service_param(proc, PROCRUN_PARAMS_DESCRIPTION,
  -                          proc->service.description, 0, 0);
  +                          proc->service.description, 0, 1);
       }
       if (proc->service.image)
           set_service_param(proc, PROCRUN_PARAMS_IMAGE,
  @@ -1754,10 +1745,137 @@
           set_service_param(proc, PROCRUN_PARAMS_JVM_OPTS,
                             proc->java.opts, l + 2, 0);
       }
  +}
  +
  +static int procrun_install_service(process_t *proc, int argc, char **argv)
  +{
  +    SC_HANDLE service;
  +    SC_HANDLE manager;
  +    char path[MAX_PATH+1];
  +    char *display = NULL;
  +    char *java = NULL;
  +
  +    int i, n;
  +    if (!proc->service.name) {
  +        return -1;
  +    }
  +
  +    if (!process_args(proc, argc, argv, display, java, path)) {
  +         DBPRINTF0("Installing service: process_args failed\n");
  +         return -1;
  +    }
  +     
  +
  +    DBPRINTF1("Installing service %s\n", path);
  +
  +    manager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
  +    if (!manager) {    
  +        return -1;
  +    }
  +
  +    service = CreateService(manager, 
  +                            proc->service.name,
  +                            display,
  +                            SERVICE_ALL_ACCESS,
  +                            SERVICE_WIN32_OWN_PROCESS,
  +                            proc->service.startup,
  +                            SERVICE_ERROR_NORMAL,
  +                            path,
  +                            NULL,
  +                            NULL,
  +                            SERVICE_DEPENDENCIES,
  +                            NULL,
  +                            NULL);
  +    if (service) {
  +        CloseServiceHandle(service);
  +        CloseServiceHandle(manager);
  +    }
  +    else {
  +        DBPRINTF0(NULL);
  +        CloseServiceHandle(manager);
  +        return -1;
  +    }
  +
  +    /* Save parameters in registry */
  +    save_service_params(proc,java);
  +
       SetEvent(proc->events[0]);
       return 0;
   }
   
  +/* Install service on win9x */
  +static int procrun_install_service9x(process_t *proc, int argc, char **argv)
  +{
  +    HKEY        hkey;
  +    DWORD rv;
  +    char szPath[MAX_PATH+1];
  +
  +    char path[MAX_PATH+1];
  +    char *display = NULL;
  +    char *java = NULL;
  +
  +    DBPRINTF0( "InstallSvc for non-NT\r\n");
  +
  +    if (!proc->service.name) {
  +        return -1;
  +    }
  +
  +    rv = RegCreateKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows"
  +              "\\CurrentVersion\\RunServices", &hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF0( "Could not open the RunServices registry key\r\n");
  +        return -1;
  +    }
  +
  +    if (!process_args(proc, argc, argv, display, java, path)) {
  +         DBPRINTF0("Installing service: process_args failed\n");
  +         return -1;
  +    }
  +
  +    DBPRINTF1("Installing service %s\n", path);
  +
  +    rv = RegSetValueEx(hkey, proc->service.name, 0, REG_SZ,
  +               (unsigned char *) path,
  +               strlen(path) + 1);
  +    RegCloseKey(hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF2( "Could not add %s:%s to RunServices Registry Key\r\n",
  +                  proc->service.name, path);
  +        return -1;
  +    }
  +
  +    strcpy(szPath,
  +         "SYSTEM\\CurrentControlSet\\Services\\");
  +    strcat(szPath,proc->service.name);
  +    rv = RegCreateKey(HKEY_LOCAL_MACHINE, szPath, &hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF1( "Could not create/open the %s registry key\r\n",
  +            szPath);
  +        return -1;
  +    }
  +    rv = RegSetValueEx(hkey, "ImagePath", 0, REG_SZ,
  +               (unsigned char *) path,
  +               strlen(path) + 1);
  +    if (rv != ERROR_SUCCESS) {
  +        RegCloseKey(hkey);
  +        DBPRINTF0( "Could not add ImagePath to our Registry Key\r\n");
  +        return -1;
  +    }
  +    rv = RegSetValueEx(hkey, "DisplayName", 0, REG_SZ,
  +               (unsigned char *) display,
  +               strlen(display) + 1);
  +    RegCloseKey(hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF0( "Could not add DisplayName to our Registry Key\r\n");
  +        return -1;
  +    }
  +
  +    /* Save parameters in registry */
  +    save_service_params(proc,java);
  +
  +    return 0;
  +}
  +
   static int procrun_update_service(process_t *proc, int argc, char **argv)
   {
       SC_HANDLE service;
  @@ -1893,57 +2011,7 @@
       CloseServiceHandle(service);
       CloseServiceHandle(manager);
   
  -    if (proc->argw)
  -        set_service_param(proc, PROCRUN_PARAMS_CMDARGS, proc->argw, 0, 0);
  -    if (proc->service.description) {
  -        set_service_param(proc, PROCRUN_PARAMS_DESCRIPTION,
  -                          proc->service.description, 0, 1);
  -        set_service_param(proc, PROCRUN_PARAMS_DESCRIPTION,
  -                          proc->service.description, 0, 1);
  -    }
  -    if (proc->service.image)
  -        set_service_param(proc, PROCRUN_PARAMS_IMAGE,
  -                          proc->service.image, 0, 0);
  -    if (proc->service.path)
  -        set_service_param(proc, PROCRUN_PARAMS_WORKPATH,
  -                          proc->service.path, 0, 0);
  -    if (proc->service.inname)
  -        set_service_param(proc, PROCRUN_PARAMS_STDINFILE,
  -                          proc->service.inname, 0, 0);
  -    if (proc->service.outname)
  -        set_service_param(proc, PROCRUN_PARAMS_STDOUTFILE,
  -                          proc->service.outname, 0, 0);
  -    if (proc->service.errname)
  -        set_service_param(proc, PROCRUN_PARAMS_STDERRFILE,
  -                          proc->service.errname, 0, 0);
  -    if (java)
  -        set_service_param(proc, PROCRUN_PARAMS_JVM,
  -                          java, 0, 0);
  -    if (proc->java.start_class)
  -        set_service_param(proc, PROCRUN_PARAMS_STARTCLASS,
  -                          proc->java.start_class, 0, 0);
  -    if (proc->java.stop_class)
  -        set_service_param(proc, PROCRUN_PARAMS_STOPCLASS,
  -                          proc->java.stop_class, 0, 0);
  -    if (proc->service.account)
  -        set_service_param(proc, PROCRUN_PARAMS_ACCOUNT,
  -                          proc->service.account, 0, 0);
  -    if (proc->service.password) {
  -        unsigned char b[256];
  -        simple_encrypt(100, proc->service.password, b);
  -        set_service_param(proc, PROCRUN_PARAMS_PASSWORD,
  -                          b, 256, 2);
  -    }
  -    if (proc->java.opts) {
  -        int l = strlen(proc->java.opts);
  -        /* change the string to zero separated for MULYI_SZ */
  -        for (i = 0; i < l; i ++) {
  -            if (proc->java.opts[i] == '#')
  -                proc->java.opts[i] = '\0';
  -        }
  -        set_service_param(proc, PROCRUN_PARAMS_JVM_OPTS,
  -                          proc->java.opts, l + 2, 0);
  -    }
  +    save_service_params(proc,java);
   
       SetEvent(proc->events[0]);
       return 0;
  @@ -1993,6 +2061,44 @@
       SetEvent(proc->events[0]);
       return 0;
   }
  + 
  +/* remove service (non NT) stopping it looks ugly!!! so we let it run. */
  +static int procrun_delete_service9x(process_t *proc)
  +{
  +    HKEY hkey;
  +    DWORD rv;
  + 
  +    if (!proc->service.name) {
  +        return -1;
  +    }
  +    DBPRINTF1("Deleting service %s", proc->service.name);
  +
  +    rv = RegOpenKey(HKEY_LOCAL_MACHINE,
  +        "Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",
  +        &hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF0( "Could not open the RunServices registry key.\r\n");
  +        return -1;
  +    }
  +    rv = RegDeleteValue(hkey, proc->service.name);
  +    RegCloseKey(hkey);
  +    if (rv != ERROR_SUCCESS)
  +        DBPRINTF0( "Could not delete the RunServices entry.\r\n");
  + 
  +    rv = RegOpenKey(HKEY_LOCAL_MACHINE,
  +        "SYSTEM\\CurrentControlSet\\Services", &hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF0( "Could not open the Services registry key.\r\n");
  +        return -1;
  +    }
  +    rv = RegDeleteKey(hkey, proc->service.name);
  +    RegCloseKey(hkey);
  +    if (rv != ERROR_SUCCESS) {
  +        DBPRINTF0( "Could not delete the Services registry key.\r\n");
  +        return -1;
  +    }
  +    return 0;
  +}
   
   int report_service_status(DWORD dwCurrentState,
                             DWORD dwWin32ExitCode,
  @@ -2233,13 +2339,25 @@
               service_main(argc, argv);
               break;
           case PROCRUN_CMD_INSTALL_SERVICE:
  -            rv = procrun_install_service(env->m, argc, argv);
  +            if (isWindowsNT())
  +              rv = procrun_install_service(env->m, argc, argv);
  +            else
  +              rv = procrun_install_service9x(env->m, argc, argv);
               break;
           case PROCRUN_CMD_UPDATE_SERVICE:
  -            rv = procrun_update_service(env->m, argc, argv);
  +            if (isWindowsNT())
  +              rv = procrun_update_service(env->m, argc, argv);
  +            else {
  +              // rv = procrun_update_service9x(env->m, argc, argv);
  +              rv = -1;
  +              DBPRINTF0("UPDATE SERVICE is unimplemented on 9x for now");
  +            }
               break;
           case PROCRUN_CMD_DELETE_SERVICE:
  -            rv = procrun_delete_service(env->m);
  +            if (isWindowsNT())
  +              rv = procrun_delete_service(env->m);
  +            else
  +              rv = procrun_delete_service9x(env->m);
               break;
           case PROCRUN_CMD_STOP_SERVICE:
               rv = -1;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message