示例#1
0
static void start_one_job(const char *user, CronLine *line)
{
	const char *shell;
	struct passwd *pas;
	pid_t pid;

	pas = getpwnam(user);
	if (!pas) {
		bb_error_msg("can't get uid for %s", user);
		goto err;
	}

	/* Prepare things before vfork */
	shell = line->cl_shell ? line->cl_shell : DEFAULT_SHELL;
	set_env_vars(pas, shell);

	/* Fork as the user in question and run program */
	pid = vfork();
	if (pid == 0) {
		/* CHILD */
		/* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
		change_user(pas);
		log5("child running %s", shell);
		/* crond 3.0pl1-100 puts tasks in separate process groups */
		bb_setpgrp();
		execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
		bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
	}
	if (pid < 0) {
		bb_perror_msg("vfork");
 err:
		pid = 0;
	}
	line->cl_pid = pid;
}
示例#2
0
文件: util.c 项目: AEliu/calibre
void initialize_interpreter(const char **ENV_VARS, const char **ENV_VAR_VALS,
        char *PROGRAM, const char *MODULE, const char *FUNCTION, const char *PYVER, int IS_GUI,
        const char* exe_path, const char *rpath, int argc, const char **argv) {
    PyObject *pargv, *v;
    int i;
    Py_OptimizeFlag = 2;
    Py_NoSiteFlag = 1;
    Py_DontWriteBytecodeFlag = 1;
    Py_IgnoreEnvironmentFlag = 1;
    Py_NoUserSiteDirectory = 1;
    Py_HashRandomizationFlag = 1;

    //Py_VerboseFlag = 1;
    //Py_DebugFlag = 1;
    
    Py_SetProgramName(PROGRAM);

    char pyhome[1000];
    snprintf(pyhome, 1000, "%s/Python", rpath);
    Py_SetPythonHome(pyhome);

    set_env_vars(ENV_VARS, ENV_VAR_VALS, exe_path);

    //printf("Path before Py_Initialize(): %s\r\n\n", Py_GetPath());
    Py_Initialize();

    char *dummy_argv[1] = {""};
    PySys_SetArgv(1, dummy_argv);
    //printf("Path after Py_Initialize(): %s\r\n\n", Py_GetPath());
    char path[3000];
    snprintf(path, 3000, "%s/lib/python%s:%s/lib/python%s/lib-dynload:%s/site-packages", pyhome, PYVER, pyhome, PYVER, pyhome);

    PySys_SetPath(path);
    //printf("Path set by me: %s\r\n\n", path);

    PySys_SetObject("calibre_basename", PyBytes_FromString(PROGRAM));
    PySys_SetObject("calibre_module", PyBytes_FromString(MODULE));
    PySys_SetObject("calibre_function", PyBytes_FromString(FUNCTION));
    PySys_SetObject("calibre_is_gui_app", ((IS_GUI) ? Py_True : Py_False));
    PySys_SetObject("resourcepath", PyBytes_FromString(rpath));
    snprintf(path, 3000, "%s/site-packages", pyhome);
    PySys_SetObject("site_packages", PyBytes_FromString(pyhome));


    pargv = PyList_New(argc);
    if (pargv == NULL) exit(report_error(ERR_OOM));
    for (i = 0; i < argc; i++) {
        v = PyBytes_FromString(argv[i]);
        if (v == NULL) exit(report_error(ERR_OOM));
        PyList_SetItem(pargv, i, v);
    }
    PySys_SetObject("argv", pargv);

}
int cogen_stmt_compound(FILE *fp,stmt_t s,env_t env,label_gen_t lg)
{
  int sz;
  int i;
  env = set_env_vars(s->u.c.decls,env);//宣言された変数を環境に登録、ここではメモリ確保必要ないはず
  sz = stmt_list_sz(s->u.c.body);
  for(i=0;i<sz;i++)
    {
      stmt_t x = stmt_list_get(s->u.c.body,i);
      cogen_stmt(fp,x,env,lg);
    }
  delete_env_vars(env);//スコープ脱出の際に一番上のスタックを環境から削除
  return 0;
}
示例#4
0
static pid_t
fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
{
	struct passwd *pas;
	const char *shell, *prog;
	smallint sv_logmode;
	pid_t pid;

	/* prepare things before vfork */
	pas = getpwnam(user);
	if (!pas) {
		bb_error_msg("can't get uid for %s", user);
		goto err;
	}

	shell = line->cl_shell ? line->cl_shell : DEFAULT_SHELL;
	prog = run_sendmail ? SENDMAIL : shell;

	set_env_vars(pas, shell);

	sv_logmode = logmode;
	pid = vfork();
	if (pid == 0) {
		/* CHILD */
		/* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
		change_user(pas);
		log5("child running %s", prog);
		if (mailFd >= 0) {
			xmove_fd(mailFd, run_sendmail ? 0 : 1);
			dup2(1, 2);
		}
		/* crond 3.0pl1-100 puts tasks in separate process groups */
		bb_setpgrp();
		if (!run_sendmail)
			execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
		else
			execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
		/*
		 * I want this error message on stderr too,
		 * even if other messages go only to syslog:
		 */
		logmode |= LOGMODE_STDIO;
		bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
	}
	logmode = sv_logmode;

	if (pid < 0) {
		bb_perror_msg("vfork");
 err:
		pid = 0;
	} /* else: PARENT, FORK SUCCESS */

	/*
	 * Close the mail file descriptor.. we can't just leave it open in
	 * a structure, closing it later, because we might run out of descriptors
	 */
	if (mailFd >= 0) {
		close(mailFd);
	}
	return pid;
}
示例#5
0
/*
 * Main application. This is where the requests come in and routed.
 */
void handle_request(void)
{
	bool logged_in = false;
	char *request_uri;
	struct timespec stp;
	struct timespec etp;

	clock_gettime(CLOCK_REALTIME, &stp);

	qvars = NULL;
	avars = NULL;
	u_files = NULL;

	set_env_vars();
	set_vars();
	request_uri = strdupa(env_vars.request_uri);

	/* Initialise the database connection */
	conn = db_conn();
	if (!conn)
		goto out2;

	/*
	 * Some routes need to come before the login / session stuff as
	 * they can't be logged in and have no session.
	 */
	if (match_uri(request_uri, "//")) {
		/* function call goes here */
		goto out2;
	}

	if (match_uri(request_uri, "/login/")) {
		login();
		goto out2;
	}

	logged_in = is_logged_in();
	if (!logged_in) {
		printf("Location: /login/\r\n\r\n");
		goto out2;
	}

	/* Logged in, set-up the user_session structure */
	set_user_session();

	/* Add new url handlers after here */

	if (match_uri(request_uri, "/logout/")) {
		logout();
		goto out;
	}

	/* Default location */
	printf("Location: /login/\r\n\r\n");

out:
	free_user_session();

out2:
	free_vars(qvars);
	free_avars();
	free_u_files();
	clock_gettime(CLOCK_REALTIME, &etp);
	d_fprintf(access_log, "Got request from %s for %s (%s), %ums\n",
			env_vars.remote_addr, request_uri,
			env_vars.request_method,
			(unsigned int)((etp.tv_sec * 1000 +
					etp.tv_nsec / NS_MSEC) -
				(stp.tv_sec * 1000 + stp.tv_nsec / NS_MSEC)));
	free_env_vars();
	mysql_close(conn);
}
示例#6
0
文件: fsh.c 项目: poltak/Coursework
int run_shell()
{
    char *command;
    char *commandtmp;
    char *cmd_token;
    char *arg_token;
    int cmd_arr_length = sizeof(cmd_arr) / sizeof(char *);  /* gets length of command array */
    int prompt = '$';
    int i;
    int invalid_cmd;
    int bytes_read;
    int nbytes = BYTE_LIM;

    set_env_vars();

    printf("Welcome to failshell!\n");

    /* allocates appropriate mem to command, limiting user's command and setting up temporary container string to split */
    command = (char *) malloc(nbytes);
    commandtmp = (char *) malloc(sizeof(command));
    if (commandtmp == NULL)     return OOM;     /* if no space left to allocate (highly unlikely...) */

    /* the main loop that powers the shell */
    while (run_signal != EOF)
    {
        invalid_cmd = TRUE;
        printf("%s %s %c ", user, pwd, prompt);

        /* get command string from user and place into address pointed at by *command */
        bytes_read = getline(&command, (size_t *) &nbytes, stdin);
        if (bytes_read == EOF)      /* bytes_read will only = EOF if some IO error or interrupt (including Ctrl+D) */
            return EOF;

        strcpy(commandtmp, command);

        cmd_token = (char *) malloc(sizeof(command));
        arg_token = (char *) malloc(sizeof(command));

        /* split entered command into tokens for command argument and second argument i.e., $0 and $1 */
        cmd_token = strtok(commandtmp, DELIMITER);
        arg_token = strtok(NULL, DELIMITER);

        /* had problems with '\n' value being put into strings, which messed up comparisons later; this takes them out if they exist */
        if (arg_token == NULL)  trim_line_break(cmd_token);   /* if no argument */
        else                    trim_line_break(arg_token);

        /* compares entered command with all valid commands, allowing handler to execute command if valid */
        for (i = 0; i < cmd_arr_length; i++)
        {
            if (strcmp(cmd_token, cmd_arr[i]) == 0)
            {
                invalid_cmd = FALSE;
                cmd_handler(i, arg_token);
            }
        }

        /* don't print error message if user entered valid command or if they didn't enter anything */
        if (invalid_cmd && strlen(cmd_token) > 1)
            printf("Command \"%s\" is invalid. Type \"%s\" for assistance.\n", cmd_token, cmd_arr[4]);
    }
    return 0;
}