dbus_bool_t run_launch_helper (const char *bus_name, int alias_depth, DBusError *error) { BusConfigParser *parser; dbus_bool_t retval; parser = NULL; retval = FALSE; /* clear the environment, apart from a few select settings */ if (!clear_environment (error)) goto error; /* check to see if we have a valid bus name */ if (!check_bus_name (bus_name, error)) goto error; /* get the correct parser, either the test or default parser */ if (!get_correct_parser (&parser, error)) goto error; /* check we are being invoked by the correct dbus user */ if (!check_dbus_user (parser, error)) goto error_free_parser; /* launch the bus with the service defined user */ if (!launch_bus_name (bus_name, parser, alias_depth, error)) goto error_free_parser; /* woohoo! */ retval = TRUE; error_free_parser: bus_config_parser_unref (parser); error: return retval; }
pid_t work_queue_process_execute(struct work_queue_process *p, int container_mode, ...) { // make warning fflush(NULL); /* why is this necessary? */ p->output_file_name = strdup(task_output_template); p->output_fd = mkstemp(p->output_file_name); if(p->output_fd == -1) { debug(D_WQ, "Could not open worker stdout: %s", strerror(errno)); return 0; } if(p->loop_mount) { char *buf = malloc(PATH_MAX); char *pwd = getcwd(buf, PATH_MAX); char *filename = work_queue_generate_disk_alloc_full_filename(pwd, p->task->taskid); p->task->command_line = string_format("export CCTOOLS_DISK_ALLOC=%s; %s", filename, p->task->command_line); free(buf); } p->execution_start = timestamp_get(); p->pid = fork(); if(p->pid > 0) { // Make child process the leader of its own process group. This allows // signals to also be delivered to processes forked by the child process. // This is currently used by kill_task(). setpgid(p->pid, 0); debug(D_WQ, "started process %d: %s", p->pid, p->task->command_line); return p->pid; } else if(p->pid < 0) { debug(D_WQ, "couldn't create new process: %s\n", strerror(errno)); unlink(p->output_file_name); close(p->output_fd); return p->pid; } else { if(chdir(p->sandbox)) { printf("The sandbox dir is %s", p->sandbox); fatal("could not change directory into %s: %s", p->sandbox, strerror(errno)); } int fd = open("/dev/null", O_RDONLY); if(fd == -1) fatal("could not open /dev/null: %s", strerror(errno)); int result = dup2(fd, STDIN_FILENO); if(result == -1) fatal("could not dup /dev/null to stdin: %s", strerror(errno)); result = dup2(p->output_fd, STDOUT_FILENO); if(result == -1) fatal("could not dup pipe to stdout: %s", strerror(errno)); result = dup2(p->output_fd, STDERR_FILENO); if(result == -1) fatal("could not dup pipe to stderr: %s", strerror(errno)); close(p->output_fd); clear_environment(); /* overwrite CORES, MEMORY, or DISK variables, if the task used specify_* */ specify_resources_vars(p); export_environment(p); va_list arg_lst; if(container_mode == NONE) { execlp("sh", "sh", "-c", p->task->command_line, (char *) 0); _exit(127); // Failed to execute the cmd. } else if(container_mode == UMBRELLA) { fatal("UMBRELLA mode have not been implemented\n"); } else { // Write task command into a shell script char *tmp_ptr = p->task->command_line; int cmd_line_size = 0; while(*(++tmp_ptr) != '\0') cmd_line_size++; FILE *script_fn = fopen(TMP_SCRIPT, "w"); fprintf(script_fn, "%s\n%s", DEFAULT_EXE_APP, p->task->command_line); fclose(script_fn); chmod(TMP_SCRIPT, 0755); uid_t uid = getuid(); char uid_str[MAX_BUFFER_SIZE]; sprintf(uid_str, "%d", uid); // Get path to sandbox char curr_wrk_dir[MAX_BUFFER_SIZE]; char *wrk_space; if((wrk_space = getenv("WORK_QUEUE_SANDBOX")) != NULL) { sprintf(curr_wrk_dir, "%s/%s", wrk_space, p->sandbox); } else { perror("getenv() error"); } if(container_mode == DOCKER) { va_start(arg_lst, container_mode); char img_name[MAX_BUFFER_SIZE]; strncpy(img_name, va_arg(arg_lst, const char *), MAX_BUFFER_SIZE); va_end(arg_lst); char mnt_flg_val[MAX_BUFFER_SIZE]; sprintf(mnt_flg_val, "%s:%s", curr_wrk_dir, DEFAULT_WORK_DIR); // cmd for running the shell script char run_cmd[SMALL_BUFFER_SIZE]; sprintf(run_cmd, "./%s", TMP_SCRIPT); execl("/usr/bin/docker", "/usr/bin/docker", "run", "--rm", "-v", mnt_flg_val, "-w", DEFAULT_WORK_DIR, "-u", uid_str, "-m", "1g", img_name, run_cmd, (char *) 0); _exit(127); // Failed to execute the cmd. } else { // DOCKER_PRESERVE mode va_start(arg_lst, container_mode); char container_name[MAX_BUFFER_SIZE]; strncpy(container_name, va_arg(arg_lst, const char *), MAX_BUFFER_SIZE); va_end(arg_lst); char sub_proc_sh_fn[MAX_BUFFER_SIZE]; char sub_proc_sh_fn_path[MAX_BUFFER_SIZE]; sprintf(sub_proc_sh_fn, "tmp_%s.sh", p->sandbox); sprintf(sub_proc_sh_fn_path, "%s/%s", wrk_space, sub_proc_sh_fn); FILE *sub_proc_script_fn = fopen(sub_proc_sh_fn_path, "w"); fprintf(sub_proc_script_fn, "%s\ncd %s\n./%s", DEFAULT_EXE_APP, p->sandbox, TMP_SCRIPT); fclose(sub_proc_script_fn); chmod(sub_proc_sh_fn_path, 0755); char run_sh_fn[MAX_BUFFER_SIZE]; sprintf(run_sh_fn, "./%s", sub_proc_sh_fn); execl("/usr/bin/docker", "/usr/bin/docker", "exec", container_name, run_sh_fn, (char *) 0); _exit(127); // Failed to execute the cmd. } } } return 0; }