/****** uti/string/sge_jobname() *********************************************** * NAME * sge_jobname() -- get jobname of command line string * * SYNOPSIS * const char* sge_jobname(const char *name) * * FUNCTION * Determine the jobname of a command line. The following definition is used * for the jobname: * - take everything up to the first semicolon * - take everything up to the first whitespace * - take the basename * * INPUTS * const char *name - contains the input string (command line) * * RESULT * const char* - pointer to the jobname * NULL if name is NULL or only '\0' * * EXAMPLE * Command line jobname * ---------------------------------------------- * "cd /home/me/5five; hostname" --> cd * "/home/me/4Ujob" --> 4Ujob (invalid, will be denied) * "cat /tmp/5five" --> cat * "bla;blub" --> bla * "a b" --> a * * * NOTES * MT-NOTE: sge_jobname() is not MT safe * * SEE ALSO * sge_basename() *******************************************************************************/ const char *sge_jobname(const char *name) { const char *cp = NULL; DENTER(BASIS_LAYER, "sge_jobname"); if (name && name[0] != '\0' ) { cp = sge_strtok(name, ";"); cp = sge_strtok(cp, " "); cp = sge_basename(cp, '/'); } DRETURN(cp); }
static void qevent_start_trigger_script(int qevent_event, const char* script_file, lListElem *event ) { u_long jobid, taskid; const char* event_name; int pid; char buffer[MAX_STRING_SIZE]; char buffer2[MAX_STRING_SIZE]; DENTER(TOP_LAYER, "qevent_start_trigger_script"); jobid = lGetUlong(event, ET_intkey); taskid = lGetUlong(event, ET_intkey2); event_name = qevent_get_event_name(qevent_event); /* test if script is executable and valid file */ if (!sge_is_file(script_file)) { ERROR((SGE_EVENT, "no script file: "SFQ"\n", script_file)); DEXIT; return; } /* is file executable ? */ if (!sge_is_executable(script_file)) { ERROR((SGE_EVENT, "file not executable: "SFQ"\n", script_file)); DEXIT; return; } pid = fork(); if (pid < 0) { ERROR((SGE_EVENT, "fork() error\n")); DEXIT; return; } if (pid > 0) { int exit_status; #if !(defined(CRAY) || defined(INTERIX)) struct rusage rusage; #endif #if defined(SVR3) || defined(_BSD) union wait status; #else int status; #endif #if defined(CRAY) || defined(INTERIX) waitpid(pid, &status, 0); #else wait3(&status, 0, &rusage); #endif #if defined(SVR3) || defined(_BSD) exit_status = status.w_retcode; #else exit_status = status; #endif if ( WEXITSTATUS(exit_status) == 0 ) { INFO((SGE_EVENT,"exit status of script: "sge_U32CFormat"\n", sge_u32c(WEXITSTATUS(exit_status)))); } else { ERROR((SGE_EVENT,"exit status of script: "sge_U32CFormat"\n", sge_u32c(WEXITSTATUS(exit_status)))); } DEXIT; return; } else { const char *basename = sge_basename( script_file, '/' ); /* SETPGRP; */ /* sge_close_all_fds(NULL); */ sprintf(buffer ,sge_U32CFormat,sge_u32c(jobid)); sprintf(buffer2 ,sge_U32CFormat,sge_u32c(taskid)); execlp(script_file, basename, event_name, buffer, buffer2, (char *)0); } exit(1); }
/****** Interactive/qrsh/startJob() *************************************** * * NAME * startJob() -- start a shell with commands to execute * * SYNOPSIS * static int startJob(char *command, char *wrapper, int noshell); * * FUNCTION * Starts the commands and arguments to be executed as * specified in parameter <command>. * If the parameter noshell is set to 1, the command is directly called * by exec. * If a wrapper is specified (parameter wrapper, set by environment * variable QRSH_WRAPPER), this wrapper is called and is passed the * command to execute as commandline parameters. * If neither noshell nor wrapper is set, a users login shell is called * with the parameters -c <command>. * The child process creates an own process group. * The pid of the child process is written to a pid file in $TMPDIR. * * INPUTS * command - commandline to be executed * wrapper - name and path of a wrapper script * noshell - if != 0, call the command directly without shell * * RESULT * status of the child process after it terminated * or EXIT_FAILURE, if the process of starting the child * failed because of one of the following error situations: * - fork failed * - the pid of the child process cannot be written to pid file * - the name of actual user cannot be determined * - info about the actual user cannot be determined (getpwnam) * - necessary memory cannot be allocated * - executing the shell failed * * SEE ALSO * Interactive/qrsh/write_pid_file() * Interactive/qrsh/split_command() * Interactive/qrsh/join_command() * **************************************************************************** */ static int startJob(char *command, char *wrapper, int noshell) { child_pid = fork(); if(child_pid == -1) { qrsh_error(MSG_QRSH_STARTER_CANNOTFORKCHILD_S, strerror(errno)); return EXIT_FAILURE; } if(child_pid) { /* parent */ int status; #if defined(LINUX) int ttyfd; #endif signal(SIGINT, forward_signal); signal(SIGQUIT, forward_signal); signal(SIGTERM, forward_signal); /* preserve pseudo terminal */ #if defined(LINUX) ttyfd = open("/dev/tty", O_RDWR); if (ttyfd != -1) { tcsetpgrp(ttyfd, child_pid); close(ttyfd); } #endif while(waitpid(child_pid, &status, 0) != child_pid && errno == EINTR); return(status); } else { /* child */ char *buffer = NULL; int size; struct passwd pw_struct; char *shell = NULL; char *userName = NULL; int argc = 0; const char **args = NULL; char *cmd = NULL; int cmdargc; char **cmdargs = NULL; int i; if(!write_pid_file(getpid())) { exit(EXIT_FAILURE); } cmdargc = split_command(command, &cmdargs); if(cmdargc == 0) { qrsh_error(MSG_QRSH_STARTER_INVALIDCOMMAND); exit(EXIT_FAILURE); } if(!noshell) { struct passwd *pw = NULL; if((userName = search_conf_val("job_owner")) == NULL) { qrsh_error(MSG_QRSH_STARTER_CANNOTGETLOGIN_S, strerror(errno)); exit(EXIT_FAILURE); } size = get_pw_buffer_size(); buffer = sge_malloc(size); if ((pw = sge_getpwnam_r(userName, &pw_struct, buffer, size)) == NULL) { qrsh_error(MSG_QRSH_STARTER_CANNOTGETUSERINFO_S, strerror(errno)); exit(EXIT_FAILURE); } shell = pw->pw_shell; if(shell == NULL) { qrsh_error(MSG_QRSH_STARTER_CANNOTDETERMSHELL_S, "/bin/sh"); shell = "/bin/sh"; } } if((args = malloc((cmdargc + 3) * sizeof(char *))) == NULL) { qrsh_error(MSG_QRSH_STARTER_MALLOCFAILED_S, strerror(errno)); exit(EXIT_FAILURE); } if(wrapper == NULL) { if(noshell) { cmd = cmdargs[0]; for(i = 0; i < cmdargc; i++) { args[argc++] = cmdargs[i]; } } else { cmd = shell; args[argc++] = sge_basename(shell, '/'); args[argc++] = "-c"; args[argc++] = join_command(cmdargc, cmdargs); } } else { cmd = wrapper; args[argc++] = sge_basename(wrapper, '/'); for(i = 0; i < cmdargc; i++) { args[argc++] = cmdargs[i]; } } args[argc++] = NULL; #if 0 { /* debug code */ int i; fflush(stdout) ; fflush(stderr); printf("qrsh_starter: executing %s\n", cmd); for(i = 1; args[i] != NULL; i++) { printf("args[%d] = %s\n", i, args[i]); } printf("\n"); fflush(stdout) ; fflush(stderr); } #endif SETPGRP; execvp(cmd, (char *const *)args); /* exec failed */ fprintf(stderr, MSG_QRSH_STARTER_EXECCHILDFAILED_S, args[0], strerror(errno)); fprintf(stderr, "\n"); exit(EXIT_FAILURE); } /* will never be reached */ return EXIT_FAILURE; }
int main(int argc, char **argv) { int ret = STATUS_OK; lList *alp = NULL; lList *request_list = NULL; lList *cmdline = NULL; lListElem *aep; int all_jobs = 0; int all_users = 0; u_long32 gdi_cmd = SGE_GDI_MOD; int tmp_ret; int me_who; sge_gdi_ctx_class_t *ctx = NULL; DENTER_MAIN(TOP_LAYER, "qalter"); prof_mt_init(); /* ** get command name: qalter or qresub */ if (!strcmp(sge_basename(argv[0], '/'), "qresub")) { DPRINTF(("QRESUB\n")); me_who = QRESUB; } else if (!strcmp(sge_basename(argv[0], '/'), "qhold")) { DPRINTF(("QHOLD\n")); me_who = QHOLD; } else if (!strcmp(sge_basename(argv[0], '/'), "qrls")) { DPRINTF(("QRLS\n")); me_who = QRLS; } else { DPRINTF(("QALTER\n")); me_who = QALTER; } log_state_set_log_gui(1); sge_setup_sig_handlers(me_who); if (sge_gdi2_setup(&ctx, me_who, MAIN_THREAD, &alp) != AE_OK) { answer_list_output(&alp); SGE_EXIT((void**)&ctx, 1); } /* ** begin to work */ opt_list_append_opts_from_qalter_cmdline(me_who, &cmdline, &alp, argv + 1, environ); tmp_ret = answer_list_print_err_warn(&alp, MSG_QALTER, MSG_QALTER, MSG_QALTERWARNING); if (tmp_ret > 0) { SGE_EXIT((void**)&ctx, tmp_ret); } /* handling the case that no command line parameter was specified */ if ((me_who == QHOLD || me_who == QRLS) && lGetNumberOfElem(cmdline) == 1) { /* -h option is set implicitly for QHOLD and QRLS */ sge_usage(me_who, stderr); fprintf(stderr, "%s\n", MSG_PARSE_NOOPTIONARGUMENT); SGE_EXIT((void**)&ctx, 1); } else if ((me_who == QRESUB || me_who == QALTER) && lGetNumberOfElem(cmdline) == 0) { /* qresub and qalter have nothing set */ sge_usage(me_who, stderr); fprintf(stderr, "%s\n", MSG_PARSE_NOOPTIONARGUMENT); SGE_EXIT((void**)&ctx, 1); } else if (opt_list_has_X(cmdline, "-help")) { /* -help was specified */ sge_usage(me_who, stdout); SGE_EXIT((void**)&ctx, 0); } alp = qalter_parse_job_parameter(me_who, cmdline, &request_list, &all_jobs, &all_users); DPRINTF(("all_jobs = %d, all_user = %d\n", all_jobs, all_users)); if (request_list && verify) { /* got a request list containing one element for each job to be modified save jobid all fields contain the same fields so we may use show_job() with the first job in our list The jobid's in our request list get printed before show_job() */ cull_show_job(lFirst(request_list), FLG_QALTER, false); sge_prof_cleanup(); SGE_EXIT((void**)&ctx, 0); } tmp_ret = answer_list_print_err_warn(&alp, NULL, NULL, MSG_WARNING); if (tmp_ret > 0) { SGE_EXIT((void**)&ctx, tmp_ret); } if ((me_who == QALTER) || (me_who == QHOLD) || (me_who == QRLS) ) { DPRINTF(("QALTER\n")); gdi_cmd = SGE_GDI_MOD; } else if (me_who == QRESUB){ DPRINTF(("QRESUB\n")); gdi_cmd = SGE_GDI_COPY; } else { printf("unknown binary name.\n"); SGE_EXIT((void**)&ctx, 1); } if (all_jobs) gdi_cmd |= SGE_GDI_ALL_JOBS; if (all_users) gdi_cmd |= SGE_GDI_ALL_USERS; alp = ctx->gdi(ctx, SGE_JB_LIST, gdi_cmd, &request_list, NULL, NULL); for_each (aep, alp) { printf("%s\n", lGetString(aep, AN_text)); if (ret == STATUS_OK) { ret = lGetUlong(aep, AN_status); } }