All pastes #29640 Raw Edit

SaTaN

public c v1 · immutable
#29640 ·published 2005-11-20 16:46 UTC
rendered paste body
static int initng_s_launch(active_h * service, process_h * process){    char *e = NULL;    char *t = NULL;    s_entry *EXEC = NULL;    s_entry *EXEC_ARGS = NULL;    assert(service);    assert(service->name);    /* set variables depending on launch type */    switch (process->pt)    {        case T_START:            EXEC = &START;            EXEC_ARGS = &START_ARGS;            break;        case T_STOP:            EXEC = &STOP;            EXEC_ARGS = &STOP_ARGS;            break;        case T_DAEMON:            EXEC = &DAEMON;            EXEC_ARGS = &DAEMON_ARGS;            break;        case T_KILL:            EXEC = &KILL;            EXEC_ARGS = &KILL_ARGS;            break;        case T_CRON:            EXEC = &RUN_CRON;            EXEC_ARGS = &RUN_CRON_ARGS;            break;        default:            F_("Don't know how to handle launch type %i.\n", process->pt);            return (FALSE);    }    /* WE ARE EXECUTING A START FILE */    if (active_db_is(EXEC, service))    {        while ((e = active_db_get_next_string(EXEC, service, e)))        {            struct stat test;            t = fix_variables(e, service);	    /* 11/20/2005 SaTaN0rX: preliminary support for searching the path */	    if (t[0] != '/')	    {#define MAX_FILENAME 1000		char filename[MAX_FILENAME + 3];		char* tmp;		int tok_len;		const char *PATH = getenv("PATH");		printf("initng_s_launch: %s contains is not absolute path, searchin $PATH\n", t);		if (! PATH)		{		    printf("using default path\n");		    PATH = "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin";		}		printf("PATH determined to be %s\n", PATH);                /* no absolute path, so search $PATH */		while (PATH && *PATH)		{                    /* fill filename with '\0's, so that all strings are properly terminated. */		    memset(filename, '\0', MAX_FILENAME + 3);                    tmp = strchr(PATH, ':');		    if (tmp)                        tok_len = tmp - PATH;		    else		    {                        tok_len = strlen(PATH);			tmp = (char *) -1; /* at the end of this loop, PATH will be set to tmp + 1, which is NULL */		    }		    if (tok_len >= MAX_FILENAME)                        continue; /* PATH segement too long; skip over to next one */		    		    strncpy(filename, PATH, tok_len);		    if (filename[tok_len - 1] != '/')                    {                        filename[tok_len] = '/';		    }		    		    strncat(filename, t, MAX_FILENAME - tok_len);		    		    printf("Trying %s\n", filename);		    if (stat(filename, &test) == -1)                    {                        D_("Stat failed in the inner loop (PATH) failed on %s\n", filename);                    }		    else if (test.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR | S_ISVTX | S_ISGID | S_ISUID))		    {                        /* filename is an executable */			D_("Found %s as Executable\n", filename);			/* t is malloced, free it */			free(t);			/* strdup will call malloc */			t = strdup(filename);			break; /* exit loop */		    }		    PATH = tmp + 1;		}		/* end of loop: either thru break above (in this case, t will have an absolute path pointing		 * to an executable,		 * or thru loop end: in this case, t has the same contents as it had before, and stat will fail */	    }            if (stat(t, &test) == -1)            {                F_("Stat failed on %s\n", t);                free(t);                continue;            }            if (simple_exec(process, service, t,                            active_db_get_string(EXEC_ARGS, service)))            {                free(t);                return (TRUE);            }            D_("%s did not work\n", t);            free(t);        }        return (FAIL);    }    return (FALSE);}