int main(int argc, char **argv) { /* * This routine sends a Manage request to the batch server specified by * the destination. The STARTED queue attribute is set to {False}. If the * batch request is accepted, the server will stop scheduling or routing * requests for the specified queue. */ int dest; /* Index into the destination array (argv) */ char *queue; /* Queue name part of destination */ char *server; /* Server name part of destination */ /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif if (argc == 1) { fprintf(stderr, "Usage: qstop [queue][@server] ...\n"); fprintf(stderr, " qstop --version\n"); exit(1); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qstop: unable to initialize security library.\n"); exit(1); } for (dest=1; dest<argc; dest++) if (parse_destination_id(argv[dest], &queue, &server) == 0) execute(queue, server); else { fprintf(stderr, "qstop: illegally formed destination: %s\n", argv[dest]); exitstatus = 1; } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(exitstatus); }
/** * @brief * main - the entry point in hostn.c * * @param[in] argc - argument count * @param[in] argv - argument variables. * @param[in] env - environment values. * * @return int * @retval 0 : success * @retval !=0 : error code */ int main(int argc, char *argv[], char *env[]) { int i; struct hostent *host; struct hostent *hosta; struct in_addr *ina; int naddr; int vflag = 0; void prt_herrno(); extern int optind; /*the real deal or output pbs_version and exit?*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif while ((i = getopt(argc, argv, "v-:")) != EOF) { switch (i) { case 'v': vflag = 1; break; default: usage(argv[0]); return 1; } } if (optind != argc-1) { usage(argv[0]); return 1; } #ifndef WIN32 h_errno = 0; #endif i = 0; while (env[i]) { if (!strncmp(env[i], "LOCALDOMAIN", 11)) { printf("%s\n", env[i]); env[i] = ""; break; } ++i; } host = gethostbyname(argv[optind]); if (host) { if (vflag) printf("primary name: "); printf("%s", host->h_name); if (vflag) printf(" (from gethostbyname())"); printf("\n"); if (vflag) { if (host->h_aliases && *host->h_aliases) { for (i=0; host->h_aliases[i]; ++i) printf("aliases: %s\n", host->h_aliases[i]); } else { printf("aliases: -none-\n"); } printf(" address length: %d bytes\n", host->h_length); } /* need to save address because they will be over writen on */ /* next call to gethostby*() */ naddr = 0; for (i=0; host->h_addr_list[i]; ++i) { ++naddr; } ina = (struct in_addr *)malloc(sizeof(struct in_addr) * naddr); if (ina == NULL) { fprintf(stderr, "%s: out of memory\n", argv[0]); return 1; } for (i=0; i<naddr; ++i) { (void)memcpy((char *)(ina+i), host->h_addr_list[i], host->h_length); } if (vflag) { for (i=0; i<naddr; ++i) { printf(" address: %15.15s ", inet_ntoa(*(ina+i))); printf(" (%u dec) ", (int)(ina+i)->s_addr); #ifndef WIN32 h_errno = 0; #endif hosta = gethostbyaddr((char *)(ina+i), host->h_length, host->h_addrtype); if (hosta) { printf("name: %s", host->h_name); } else { printf("name: -null-"); prt_herrno(); } printf("\n"); } } } else { fprintf(stderr, "no name entry found for %s\n", argv[optind]); prt_herrno(); } return 0; }
int main(int argc, char *argv[]) { int i; int rc; pid_t pid, retpid; int status; char *program; char *pdesc; char *pc; int pw_pipe; int expire_delta; int refresh_time; /* call me redundant, but it's satisfying to see this */ handler_refreshed_context = 0; have_login_context = 0; pid = 0; /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); if (argc < 3) { fprintf(stderr, "usage: %s user program [arg(s)]\n", argv[0]); fprintf(stderr, " %s --version\n", argv[0]); exit(254); } /* In the event we inherited creds from the parent, ignore them. */ if (getenv("KRB5CCNAME") != NULL) { unsetenv("KRB5CCNAME"); } /* read password from descriptor, close descriptor */ if ((pdesc = getenv("PBS_PWPIPE")) == NULL) { fprintf(stderr, "PBS_PWPIPE not in the environment\n"); exit (254); } pw_pipe = atoi(pdesc); if (errno != 0) { fprintf(stderr, "Value of PBS_PWPIPE is bad\n"); exit (254); } for (;;) { i = read(pw_pipe, &tmp_passwd[0], sec_passwd_str_max_len); if (i == -1 && errno == EINTR) continue; break; } close(pw_pipe); username = argv[1]; program = strdup(argv[2]); /* when execv-ing a shell interpreter, cause it to be a login shell */ if (argc == 3) { if (pc = strrchr(argv[2], (int)'/')) { *pc = '-'; argv[2] = pc; } } /* Attempt to establish login context for user */ if (rc=establish_login_context(username)) { have_login_context = 0; } else { have_login_context = 1; rc=252; } /* * If we have a login context, fork the child that will become * the job. The parent sticks around to refresh the context * periodically. * * If we don't have a login context, exec the job over ourself. */ if (have_login_context) { if ((pid = fork()) == -1) { perror("fork"); (void)remove_context(); exit(254); } } if (pid == 0) { /* exec the program */ if (execv(program, &argv[2]) == -1) { /* execv system call failed */ perror("execv"); fprintf(stderr, "pbs_dcelogin: execv system call failed\n"); exit(rc); } /* child should never get here */ exit(99); } /* * go into a loop which will every so often refresh the * the DCE login context while it waits for the child to terminate */ refresh_time = compute_refresh_time(lcon); for (;;) { if ((retpid = waitpid(pid, &status, WNOHANG)) == -1) { perror("pbs_dcelogin: waitpid"); break; } else if (retpid > 0) /* child finished */ break; /* see if it is time to refresh */ if (0 < refresh_time && refresh_time <= time(NULL)) refresh_time = do_refresh(lcon); sleep(5); } /* after removing any created DCE login context and credential cache * files, pass back the exit status of the job */ (void)remove_context(); if (retpid == pid) { if (WIFEXITED(status)) { exit(WEXITSTATUS(status)); } else if (WIFSIGNALED(status)) { exit(WTERMSIG(status)); } else if (WIFSTOPPED(status)) { exit(WSTOPSIG(status)); } else { exit(253); } } else { exit(254); } }
int main(int argc, char **argv) { /* * This routine sends a Run Job request to the batch server. If the * batch request is accepted, the server will have started the execution * of the job. */ char job[PBS_MAXCLTJOBID]; /* Job Id */ char server[MAXSERVERNAME]; /* Server name */ char *location = NULL; /* Where to run the job */ static char opts[] = "H:a"; /* See man getopt */ static char *usage = "Usage: qrun [-a] [-H vnode_specification ] job_identifier_list\n" " qrun [-a] [-H - ] job_identifier_list\n" " qrun --version\n"; int s; int errflg = 0; /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif /* Command line options */ while ((s = getopt(argc, argv, opts)) != EOF) switch (s) { case 'H': if (strlen(optarg) == 0) { fprintf(stderr, "qrun: illegal -H value\n"); errflg++; break; } location = optarg; break; case 'a': async = 1; break; case '?': default: errflg++; break; } if (errflg || (optind >= argc)) { fprintf(stderr, "%s", usage); exit(1); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qrun: unable to initialize security library.\n"); exit(2); } for (; optind < argc; optind++) { if (get_server(argv[optind], job, server)) { fprintf(stderr, "qrun: illegally formed job identifier: %s\n", argv[optind]); exitstatus = 1; continue; } execute(job, server, location); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(exitstatus); }
int main(int argc, char **argv, char **envp) /* qrls */ { int c; int errflg=0; int any_failed=0; int u_cnt, o_cnt, s_cnt, n_cnt, p_cnt; char *pc; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; #define MAX_HOLD_TYPE_LEN 32 char hold_type[MAX_HOLD_TYPE_LEN+1]; #define GETOPT_ARGS "h:" /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif hold_type[0]='\0'; while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'h': while (isspace((int)*optarg)) optarg++; if (strlen(optarg) == 0) { fprintf(stderr, "qrls: illegal -h value\n"); errflg++; break; } pc = optarg; u_cnt = o_cnt = s_cnt = n_cnt = p_cnt = 0; while (*pc) { if (*pc == 'u') u_cnt++; else if (*pc == 'o') o_cnt++; else if (*pc == 's') s_cnt++; else if (*pc == 'p') p_cnt++; else if (*pc == 'n') n_cnt++; else { fprintf(stderr, "qrls: illegal -h value\n"); errflg++; break; } pc++; } if (n_cnt && (u_cnt + o_cnt + s_cnt + p_cnt)) { fprintf(stderr, "qrls: illegal -h value\n"); errflg++; break; } strcpy(hold_type, optarg); break; default : errflg++; } if (errflg || optind >= argc) { static char usage[]="usage: qrls [-h hold_list] job_identifier...\n"; static char usag2[]=" qrls --version\n"; fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usag2); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qrls: unable to initialize security library.\n"); exit(1); } for (; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qrls: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qrls: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_rlsjob(connect, job_id_out, hold_type, NULL); if (stat && (pbs_errno != PBSE_UNKJOBID)) { prt_job_err("qrls", connect, job_id_out); any_failed = pbs_errno; } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qrls", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }
/** * @Brief * This is main function of pbs_interactive process. * * @return int * * @retval 0 : On Success * */ int main(int argc, char *argv[]) { int reg = 0; int unreg = 0; SC_HANDLE SvcHandle; SC_HANDLE SvcManager; char ModuleName[MAX_PATH]; /* The real deal or output pbs_version and exit? */ PRINT_VERSION_AND_EXIT(argc, argv); if (argc > 1) { if (strcmp(argv[1], "-R") == 0) { reg = 1; } else if (strcmp(argv[1], "-U") == 0) { unreg = 1; } else { fprintf(stderr, "\nUSAGE:\n"); fprintf(stderr, "\t%s [ -R | -U ]\n", argv[0]); fprintf(stderr, "\t%s -R -> To Register PBS_INTERACTIVE Service\n", argv[0]); fprintf(stderr, "\t%s -U -> To Unregister PBS_INTERACTIVE Service\n", argv[0]); return 1; } } if (reg || unreg) { /* register or unregister service */ SvcManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); if (SvcManager == 0) { ErrorMessage("OpenSCManager"); return 1; } if (reg) { /* register service */ GetModuleFileName(0, ModuleName, sizeof(ModuleName)/sizeof(*ModuleName)); printf("Installing %s service \n", g_PbsInteractiveName); SvcHandle = CreateService(SvcManager, g_PbsInteractiveName, g_PbsInteractiveName, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, ModuleName, 0, 0, 0, NULL, NULL); if (SvcHandle) { printf("Service %s installed successfully \n", g_PbsInteractiveName); } else { if (SvcManager) CloseServiceHandle(SvcManager); ErrorMessage("CreateService"); return 1; } if (SvcHandle) { CloseServiceHandle(SvcHandle); } } else if (unreg) { /* unregister service */ printf("Uninstalling %s service \n", g_PbsInteractiveName); SvcHandle = OpenService(SvcManager, g_PbsInteractiveName, DELETE); if (SvcHandle) { if (DeleteService(SvcHandle)) { printf("Service %s uninstalled successfully \n", g_PbsInteractiveName); if (SvcHandle) CloseServiceHandle(SvcHandle); } else { if (SvcManager) CloseServiceHandle(SvcManager); if (SvcHandle) CloseServiceHandle(SvcManager); ErrorMessage("DeleteService"); return 1; } } else { if (SvcManager) CloseServiceHandle(SvcManager); ErrorMessage("OpenSevice"); return 1; } } if (SvcManager) { CloseServiceHandle(SvcManager); } } else { /* start PBS_INTERACTIVE service */ SERVICE_TABLE_ENTRY ServiceTable[] = { {(TCHAR *)g_PbsInteractiveName, pbsinteractiveMain }, { 0 } }; if (!StartServiceCtrlDispatcher(ServiceTable)) { ErrorMessage("StartServiceCntrlDispatcher"); return 1; } } return 0; }
/** * @brief * The main function in C - entry point * * @param[in] argc - argument count * @param[in] argv - pointer to argument array * @param[in] envp - pointer to environment values * * @return int * @retval 0 - success * @retval !0 - error */ int main(int argc, char *argv[], char *envp[]) { int errflg; /* command line option error */ int connect; /* return from pbs_connect */ char *errmsg; /* return from pbs_geterrmsg */ char destbuf[256]; /* buffer for option server */ struct attrl *attrib; /* the attrib list */ char *new_resvname; /* the name returned from pbs_submit_resv */ struct ecl_attribute_errors *err_list; char *interactive = NULL; /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif destbuf[0] = '\0'; errflg = process_opts(argc, argv, &attrib, destbuf); /* get cmdline options */ if (errflg || ((optind+1) < argc) || argc == 1) { print_usage(); exit(2); } /* Get any required environment variables needing to be sent. */ if (! set_resv_env(envp)) { fprintf(stderr, "pbs_rsub: can't send environment with the reservation\n"); exit(3); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "pbs_rsub: unable to initialize security library.\n"); exit(1); } /* Connect to the server */ connect = cnt2server(destbuf); if (connect <= 0) { fprintf(stderr, "pbs_rsub: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); CS_close_app(); exit(pbs_errno); } if (qmoveflg == TRUE) { qmoveflg = FALSE; interactive = get_attr(attrib, ATTR_inter, NULL); if (interactive == NULL) { set_attr_error_exit(&attrib, ATTR_inter, DEFAULT_INTERACTIVE); } else { if (atoi(interactive) > -1) { fprintf(stderr, "pbs_rsub: -I <timeout> value must be negative when used with -Wqmove option.\n"); CS_close_app(); exit(2); } } errflg = cnvrt_proc_attrib(connect, &attrib, destbuf); if (errflg) { fprintf(stderr, "pbs_rsub: can't make a reservation with the qmove option\n"); CS_close_app(); exit(2); } } pbs_errno = 0; new_resvname = pbs_submit_resv(connect, (struct attropl *)attrib, NULL); if (new_resvname == NULL) { if ((err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(err_list); errmsg = pbs_geterrmsg(connect); if (errmsg != NULL) { fprintf(stderr, "pbs_rsub: %s\n", errmsg); } else fprintf(stderr, "pbs_rsub: Error (%d) submitting reservation\n", pbs_errno); CS_close_app(); exit(pbs_errno); } else { printf("%s\n", new_resvname); free(new_resvname); } /* Disconnet from the server. */ pbs_disconnect(connect); CS_close_app(); exit(0); }
/** * @brief * The entry point of pbsfs * * @return int * @retval 0 : success * @retval 1 : something is wrong! */ int main(int argc, char *argv[]) { char path_buf[256] = {0}; char sched_name[PBS_MAXSCHEDNAME + 1] = "default"; group_info *ginfo; group_info *ginfo2; int c; int flags = FS_PRINT; int flag1 = 0; double val; char *endp; char *testp; /* the real deal or output version and exit? */ PRINT_VERSION_AND_EXIT(argc, argv); set_msgdaemonname("pbsfs"); #ifdef WIN32 if (winsock_init()) { return 1; } #endif if (pbs_loadconf(0) <= 0) exit(1); while ((c = getopt(argc, argv, "sgptdceI:-:")) != -1) switch (c) { case 'g': flags = FS_GET; break; case 's': flags = FS_SET | FS_WRITE_FILE; break; case 'p': flags = FS_PRINT; break; case 't': flags = FS_PRINT_TREE; break; case 'd': flags = FS_DECAY | FS_WRITE_FILE; break; case 'c': flags = FS_COMP; break; case 'e': flags = FS_TRIM_TREE | FS_WRITE_FILE; break; case 'I': snprintf(sched_name, sizeof(sched_name), "%s", optarg); break; case '-': flag1 = 1; break; } if (flag1 == 1) { fprintf(stderr, "Usage: pbsfs --version\n"); exit(1); } if ((flags & (FS_PRINT | FS_PRINT_TREE)) && (argc - optind) != 0) { fprintf(stderr, "Usage: pbsfs -[ptdgcs] [-I sched_name]\n"); exit(1); } else if ((flags & FS_GET) && (argc - optind) != 1) { fprintf(stderr, "Usage: pbsfs [-I sched_name] -g <fairshare_entity>\n"); exit(1); } else if ((flags & FS_SET) && (argc - optind) != 2) { fprintf(stderr, "Usage: pbsfs [-I sched_name] -s <fairshare_entity> <usage>\n"); exit(1); } else if ((flags & FS_COMP) && (argc - optind) != 2) { fprintf(stderr, "Usage: pbsfs [-I sched_name] -c <entity1> <entity2>\n"); exit(1); } if (strcmp(sched_name, "default") != 0) { int pbs_sd; struct batch_status *bs; struct batch_status *cur_bs; pbs_sd = pbs_connect(NULL); if (pbs_sd < 0) { fprintf(stderr, "Can't connect to the server\n"); exit(1); } bs = pbs_statsched(pbs_sd, NULL, NULL); for (cur_bs = bs; cur_bs != NULL; cur_bs = cur_bs->next) { if (strcmp(cur_bs->name, sched_name) == 0) { struct attrl *cur_attrl; for (cur_attrl = cur_bs->attribs; cur_attrl != NULL; cur_attrl = cur_attrl->next) { if (strcmp(cur_attrl->name, ATTR_sched_priv) == 0) { strncpy(path_buf, cur_attrl->value, sizeof(path_buf)); path_buf[sizeof(path_buf) - 1] = '\0'; break; } } if (cur_attrl == NULL) { fprintf(stderr, "Scheduler %s does not have its sched_priv set\n", sched_name); exit(1); } break; } } if (cur_bs == NULL) { fprintf(stderr, "Scheduler %s does not exist\n", sched_name); exit(1); } pbs_disconnect(pbs_sd); } else snprintf(path_buf, sizeof(path_buf), "%s/sched_priv/", pbs_conf.pbs_home_path); if (chdir(path_buf) == -1) { perror("Unable to access fairshare data"); exit(1); } init_config(); parse_config(CONFIG_FILE); if ((conf.fairshare = preload_tree()) == NULL) { fprintf(stderr, "Error in preloading fairshare information\n"); return 1; } if (parse_group(RESGROUP_FILE, conf.fairshare->root) == 0) return 1; if (flags & FS_TRIM_TREE) read_usage(USAGE_FILE, FS_TRIM, conf.fairshare); else read_usage(USAGE_FILE, 0, conf.fairshare); calc_fair_share_perc(conf.fairshare->root->child, UNSPECIFIED); calc_usage_factor(conf.fairshare); if (flags & FS_PRINT_TREE) print_fairshare(conf.fairshare->root, 0); else if (flags & FS_PRINT ) { printf("Fairshare usage units are in: %s\n", conf.fairshare_res); print_fairshare(conf.fairshare->root, -1); } else if (flags & FS_DECAY) decay_fairshare_tree(conf.fairshare->root); else if (flags & (FS_GET | FS_SET | FS_COMP)) { ginfo = find_group_info(argv[optind], conf.fairshare->root); if (ginfo == NULL) { fprintf(stderr, "Fairshare Entity %s does not exist.\n", argv[optind]); return 1; } if (flags & FS_COMP) { ginfo2 = find_group_info(argv[optind + 1], conf.fairshare->root); if (ginfo2 == NULL) { fprintf(stderr, "Fairshare Entity %s does not exist.\n", argv[optind + 1]); return 1; } switch (compare_path(ginfo->gpath, ginfo2->gpath)) { case -1: printf("%s\n", ginfo->name); break; case 0: printf("%s == %s\n", ginfo->name, ginfo2->name); break; case 1: printf("%s\n", ginfo2->name); } } else if (flags & FS_GET) print_fairshare_entity(ginfo); else { testp = argv[optind + 1]; val = strtod(testp, &endp); if (*endp == '\0') ginfo->usage = val; } } if (flags & FS_WRITE_FILE) { FILE *fp; /* make backup of database file */ remove(USAGE_FILE ".bak"); if (rename(USAGE_FILE, USAGE_FILE ".bak") < 0) perror("Could not backup usage database."); write_usage(USAGE_FILE, conf.fairshare); if ((fp = fopen(USAGE_TOUCH, "w")) != NULL) fclose(fp); } return 0; }
int main(int argc, char **argv, char **envp) { char job_id1[PBS_MAXCLTJOBID+1]; /* from the command line */ char job_id2[PBS_MAXCLTJOBID+1]; /* from the command line */ char job_id1_out[PBS_MAXCLTJOBID+1]; char job_id2_out[PBS_MAXCLTJOBID+1]; char *pn; int port1 = 0; int port2 = 0; char server_out1[MAXSERVERNAME+1]; char server_out2[MAXSERVERNAME+1]; char svrtmp[MAXSERVERNAME+1]; int connect; int stat=0; int rc = 0; extern char *PBS_get_server(); /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif if (argc != 3) { static char usage[]="usage: qorder job_identifier job_identifier\n"; static char usag2[]=" qorder --version\n"; fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usag2); exit(2); } strcpy(job_id1, argv[1]); strcpy(job_id2, argv[2]); svrtmp[0] = '\0'; if (get_server(job_id1, job_id1_out, svrtmp)) { fprintf(stderr, "qorder: illegally formed job identifier: %s\n", job_id1); exit(1); } if (*svrtmp == '\0') { if ((pn = pbs_default()) != NULL) { (void)strcpy(svrtmp, pn); } else { fprintf(stderr, "qorder: could not get default server: %s\n", job_id1); exit(1); } } if ((pn = strchr(svrtmp, (int)':')) != 0) { *pn = '\0'; port1 = atoi(pn+1); } if (get_fullhostname(svrtmp, server_out1, MAXSERVERNAME) != 0) { fprintf(stderr, "qorder: invalid server name: %s\n", job_id1); exit(1); } svrtmp[0] = '\0'; if (get_server(job_id2, job_id2_out, svrtmp)) { fprintf(stderr, "qorder: illegally formed job identifier: %s\n", job_id2); exit(1); } if (*svrtmp == '\0') { if ((pn = pbs_default()) != NULL) { (void)strcpy(svrtmp, pn); } else { fprintf(stderr, "qorder: could not get default server: %s\n", job_id1); exit(1); } } if ((pn = strchr(svrtmp, (int)':')) != 0) { *pn = '\0'; port2 = atoi(pn+1); } if (get_fullhostname(svrtmp, server_out2, MAXSERVERNAME) != 0) { fprintf(stderr, "qorder: invalid server name: %s\n", job_id2); exit(1); } if ((strcmp(server_out1, server_out2) != 0) || (port1 != port2)) { fprintf(stderr, "qorder: both jobs ids must specify the same server\n"); exit(1); } if (pn) *pn = ':'; /* restore : if it was present */ /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qorder: unable to initialize security library.\n"); exit(1); } connect = cnt2server(svrtmp); if (connect <= 0) { fprintf(stderr, "qorder: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); exit(1);; } stat = pbs_orderjob(connect, job_id1_out, job_id2_out, NULL); if (stat) { char job_id_both[PBS_MAXCLTJOBID + PBS_MAXCLTJOBID + 3]; strcpy(job_id_both, job_id1_out); strcat(job_id_both, " or "); strcat(job_id_both, job_id2_out); prt_job_err("qorder", connect, job_id_both); rc = pbs_errno; } pbs_disconnect(connect); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(rc); }
int main(int argc, char **argv, char **envp) /* qhold */ { int c; int errflg=0; int any_failed=0; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; struct ecl_attribute_errors *err_list; #define MAX_HOLD_TYPE_LEN 32 char hold_type[MAX_HOLD_TYPE_LEN+1]; #define GETOPT_ARGS "h:-:" /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif hold_type[0]='\0'; while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'h': while (isspace((int)*optarg)) optarg++; if (optarg[0] == '\0') { fprintf(stderr, "qhold: illegal -h value\n"); errflg++; } else strcpy(hold_type, optarg); break; default : errflg++; } if (errflg || optind >= argc) { print_usage(); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qhold: unable to initialize security library.\n"); exit(2); } for (; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qhold: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qhold: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_holdjob(connect, job_id_out, hold_type, NULL); if (stat && (err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(connect, err_list); if (stat && (pbs_errno != PBSE_UNKJOBID)) { prt_job_err("qhold", connect, job_id_out); any_failed = pbs_errno; } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qhold", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }
/** * @brief * The main function in C - entry point * * @param[in] argc - argument count * @param[in] argv - pointer to argument array * * @return int * @retval 0 - success * @retval !0 - error */ int main(int argc, char *argv[]) { int i, rc; char passwd[MAX_PASSWORD_LEN + 1] = {'\0'}; char passwd2[MAX_PASSWORD_LEN + 1]; char *pquoted; char pwd_file[MAXPATHLEN + 1]; char userid[LOGIN_NAME_MAX + 1]; int fd, errflg = 0; int gen_pwd = 0; char sqlbuff[1024]; int db_conn_error=0; char *db_errmsg = NULL; int pmode; int change_user = 0; char *olduser; int update_db = 0; char getopt_format[5]; char prog[]="pbs_ds_password"; char errmsg[PBS_MAX_DB_CONN_INIT_ERR + 1]; conn = NULL; pwd_file_new[0]=0; /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); /* read configuration file */ if (pbs_loadconf(0) == 0) { fprintf(stderr, "%s: Could not load pbs configuration\n", prog); return (-1); } /* backup old user name */ if ((olduser = pbs_get_dataservice_usr(errmsg, PBS_MAX_DB_CONN_INIT_ERR)) == NULL) { fprintf(stderr, "%s: Could not retrieve current data service user\n", prog); if (strlen(errmsg) > 0) fprintf(stderr, "%s\n", errmsg); return (-1); } if (pbs_conf.pbs_data_service_host == NULL) update_db = 1; userid[0]=0; /* empty user id */ strcpy(getopt_format, "rC:"); while ((i = getopt(argc, argv, getopt_format)) != EOF) { switch (i) { case 'r': gen_pwd = 1; break; case 'C': strcpy(userid, optarg); break; case '?': default: errflg++; } } if (errflg) { fprintf(stderr, "\nusage:\t%s [-r] [-C username]\n", prog); fprintf(stderr, " \t%s --version\n", prog); return (-1); } /* NOTE : This functionality is added just for the automation testing purpose. * Usage: pbs_ds_password <password> */ if (argv[optind] != NULL) { gen_pwd = 0; strncpy(passwd, argv[optind], sizeof(passwd)); passwd[sizeof(passwd) - 1] = '\0'; } /* check admin privileges */ #ifdef WIN32 if (!isAdminPrivilege(getlogin())) { fprintf(stderr, "pbs_ds_password: Must be run by Admin\n"); return (1); } #else if ((getuid() != 0) || (geteuid() != 0)) { fprintf(stderr, "%s: Must be run by root\n", prog); return (1); } #endif /* WIN32 */ change_user = 0; /* if the -C option was specified read the user from pbs.conf */ if (userid[0] != 0) { if (strcmp(olduser, userid) != 0) { change_user = 1; } } if (change_user == 1) { /* check that the supplied user-id exists (and is non-root on unix) */ if (check_user(userid) != 0) { #ifdef WIN32 fprintf(stderr, "\n%s: User-id %s does not exist\n", prog, userid); #else fprintf(stderr, "\n%s: User-id %s does not exist/is root user/home dir is not accessible\n", prog, userid); #endif return (-1); } } atexit(cleanup); if (update_db == 1) { /* then connect to database */ conn = pbs_db_init_connection(NULL, PBS_DB_CNT_TIMEOUT_NORMAL, 1, &db_conn_error, errmsg, PBS_MAX_DB_CONN_INIT_ERR); if (!conn) { get_db_errmsg(db_conn_error, &db_errmsg); fprintf(stderr, "%s: %s\n", prog, db_errmsg); if (strlen(errmsg) > 0) fprintf(stderr, "%s\n", errmsg); return -1; } db_conn_error = pbs_db_connect(conn); if (db_conn_error == PBS_DB_SUCCESS && change_user == 1) { /* able to connect ? Thats bad, PBS or dataservice is running */ fprintf(stderr, "%s: PBS Services and/or PBS Data Service is running\n", prog); fprintf(stderr, " Stop PBS and Data Services before changing Data Service user\n"); return (-1); } if (db_conn_error != PBS_DB_SUCCESS) { if (db_conn_error == PBS_DB_CONNREFUSED) { /* start db only if it was not already running */ if (pbs_startup_db(&db_errmsg) != 0) { if (db_errmsg) fprintf(stderr, "%s: Failed to start PBS dataservice:[%s]\n", prog, db_errmsg); else fprintf(stderr, "%s: Failed to start PBS dataservice\n", prog); return (-1); } started_db = 1; } db_conn_error = pbs_db_connect(conn); if (db_conn_error != PBS_DB_SUCCESS) { get_db_errmsg(db_conn_error, &db_errmsg); if (conn->conn_db_err) fprintf(stderr, "%s: Could not connect to PBS data service:%s:[%s]\n", prog, db_errmsg, (char*)conn->conn_db_err); else fprintf(stderr, "%s: Could not connect to PBS data service:%s\n", prog, db_errmsg); return (-1); } } } if (gen_pwd == 0 && passwd[0] == '\0') { /* ask user to enter password twice */ printf("Enter the password:"******"\nRe-enter the password:"******"\n\n"); if (strcmp(passwd, passwd2) != 0) { fprintf(stderr, "Entered passwords do not match\n"); return (-2); } if (strlen(passwd) == 0) { fprintf(stderr, "Blank password is not allowed\n"); return (-2); } } else if (gen_pwd == 1) { gen_password(passwd, 16); } rc = pbs_encrypt_pwd(passwd, &cred_type, &cred_buf, &cred_len); if (rc != 0) { fprintf(stderr, "%s: Failed to encrypt password\n", prog); return (-1); } /* escape password to use in sql strings later */ if ((pquoted = pbs_db_escape_str(conn, passwd)) == NULL) { fprintf(stderr, "%s: Out of memory\n", prog); return -1; } #ifdef WIN32 sprintf(pwd_file_new, "%s\\server_priv\\db_password.new", pbs_conf.pbs_home_path); sprintf(pwd_file, "%s\\server_priv\\db_password", pbs_conf.pbs_home_path); #else sprintf(pwd_file_new, "%s/server_priv/db_password.new", pbs_conf.pbs_home_path); sprintf(pwd_file, "%s/server_priv/db_password", pbs_conf.pbs_home_path); #endif /* write encrypted password to the password file */ #ifdef WIN32 pmode = _S_IWRITE | _S_IREAD; fix_perms2(pwd_file_new, pwd_file); if ((fd = open(pwd_file_new, O_WRONLY | O_TRUNC | O_CREAT | O_Sync | O_BINARY, pmode)) == -1) #else pmode = 0600; if ((fd = open(pwd_file_new, O_WRONLY | O_TRUNC | O_CREAT | O_Sync, pmode)) == -1) #endif { perror("open/create failed"); fprintf(stderr, "%s: Unable to create file %s\n", prog, pwd_file_new); return (-1); } #ifdef WIN32 secure_file(pwd_file_new, "Administrators", READS_MASK|WRITES_MASK|STANDARD_RIGHTS_REQUIRED); setmode(fd, O_BINARY); #endif if (update_db == 1) { /* change password only if this config option is not set */ if (pbs_db_begin_trx(conn, 0, 0) != 0) { fprintf(stderr, "%s: Could not start transaction\n", prog); unlink(pwd_file_new); return -1; } if (change_user == 1) { /* check whether user exists */ snprintf(sqlbuff, sizeof(sqlbuff), "select usename from pg_user where usename = '%s'", userid); if (pbs_db_execute_str(conn, sqlbuff) == 1) { /* now attempt to create new user & set the database passwd to the un-encrypted password */ snprintf(sqlbuff, sizeof(sqlbuff), "create user \"%s\" SUPERUSER ENCRYPTED PASSWORD '%s'", userid, pquoted); } else { /* attempt to alter new user & set the database passwd to the un-encrypted password */ snprintf(sqlbuff, sizeof(sqlbuff), "alter user \"%s\" SUPERUSER ENCRYPTED PASSWORD '%s'", userid, pquoted); } memset(passwd, 0, sizeof(passwd)); memset(passwd2, 0, sizeof(passwd2)); memset(pquoted, 0, (sizeof(char) * strlen(pquoted))); if (pbs_db_execute_str(conn, sqlbuff) == -1) { fprintf(stderr, "%s: Failed to create/alter user id %s\n", prog, userid); (void) pbs_db_end_trx(conn, PBS_DB_ROLLBACK); return -1; } } else { /* now attempt to set the database passwd to the un-encrypted password */ /* alter user ${user} SUPERUSER ENCRYPTED PASSWORD '${passwd}' */ sprintf(sqlbuff, "alter user \"%s\" SUPERUSER ENCRYPTED PASSWORD '%s'", olduser, pquoted); memset(passwd, 0, sizeof(passwd)); memset(passwd2, 0, sizeof(passwd2)); memset(pquoted, 0, (sizeof(char) * strlen(pquoted))); if (pbs_db_execute_str(conn, sqlbuff) == -1) { fprintf(stderr, "%s: Failed to create/alter user id %s\n", prog, userid); (void) pbs_db_end_trx(conn, PBS_DB_ROLLBACK); return -1; } } } if (write(fd, cred_buf, cred_len) != cred_len) { perror("write failed"); fprintf(stderr, "%s: Unable to write to file %s\n", prog, pwd_file_new); if (update_db == 1) { (void) pbs_db_end_trx(conn, PBS_DB_ROLLBACK); } return -1; } close(fd); free(cred_buf); #ifdef WIN32 if (MoveFileEx(pwd_file_new, pwd_file, MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH) == 0) { errno = GetLastError(); fprintf(stderr, "MoveFileEx(%s, %s) failed!", pwd_file_new, pwd_file); if (update_db == 1) { (void) pbs_db_end_trx(conn, PBS_DB_ROLLBACK); } return (-1); } #else if (rename(pwd_file_new, pwd_file) != 0) { if (update_db == 1) { (void) pbs_db_end_trx(conn, PBS_DB_ROLLBACK); } return (-1); } #endif if (update_db == 1) { /* commit to database */ (void) pbs_db_end_trx(conn, PBS_DB_COMMIT); cleanup(); /* cleanup will disconnect and delete tmp file too */ } printf("---> Updated user password\n"); if (update_db == 1 && change_user == 1) { printf("---> Updated user in datastore\n"); printf("---> Stored user password in datastore\n"); } if (change_user == 1) { char usr_file[MAXPATHLEN + 1]; #ifdef WIN32 sprintf(usr_file, "%s\\server_priv\\db_user", pbs_conf.pbs_home_path); #else sprintf(usr_file, "%s/server_priv/db_user", pbs_conf.pbs_home_path); #endif /* update PBS_HOME/server_priv/db_user file with the new user name */ if (update_db_usr(usr_file, userid) != 0) { fprintf(stderr, "Unable to update file %s\n", usr_file); return -1; } printf("---> Updated new user\n"); } if (update_db == 1 && change_user == 1) { char datastore[MAXPATHLEN + 1]; #ifndef WIN32 /* ownership is changed only for Unix users * On windows, these files are allways owned by the user who installed the database * and writable by administrators anyway */ sprintf(datastore, "%s/datastore", pbs_conf.pbs_home_path); /* change ownership of the datastore directories to the new user, so that db can be started again */ if (change_ownership(datastore, userid) != 0) { fprintf(stderr, "%s: Failed to change ownership on path %s\n", prog, datastore); return -1; } printf("---> Changed ownership of %s to user %s\n", datastore, userid); #endif /* reload configuration file */ if (pbs_loadconf(1) == 0) { fprintf(stderr, "%s: Could not load pbs configuration\n", prog); return (-1); } if (pbs_startup_db(&db_errmsg) != 0) { if (db_errmsg) fprintf(stderr, "%s: Failed to start PBS dataservice as new user:[%s]\n", prog, db_errmsg); else fprintf(stderr, "%s: Failed to start PBS dataservice as new user\n", prog); return (-1); } started_db = 1; /* connect again to drop the old user */ conn = pbs_db_init_connection(NULL, PBS_DB_CNT_TIMEOUT_NORMAL, 1, &db_conn_error, errmsg, PBS_MAX_DB_CONN_INIT_ERR); if (!conn) { get_db_errmsg(db_conn_error, &db_errmsg); fprintf(stderr, "%s: %s\n", prog, db_errmsg); if (strlen(errmsg) > 0) fprintf(stderr, "%s\n", errmsg); return -1; } db_conn_error = pbs_db_connect(conn); if (db_conn_error != PBS_DB_SUCCESS) { get_db_errmsg(db_conn_error, &db_errmsg); if (conn->conn_db_err) fprintf(stderr, "%s: Could not connect to PBS data service as new user:%s[%s]\n", prog, db_errmsg, (char*)conn->conn_db_err); else fprintf(stderr, "%s: Could not connect to PBS data service as new user:%s\n", prog, db_errmsg); return (-1); } /* delete the old user from the database */ sprintf(sqlbuff, "drop user \"%s\"", olduser); pbs_db_execute_str(conn, sqlbuff); } printf("---> Success\n"); return (0); }