int locate_job( char *job_id, char *parent_server, char *located_server) { int connect; int local_errno = 0; char jid_server[PBS_MAXCLTJOBID + 1]; char *location; if ((connect = pbs_connect(parent_server)) >= 0) { /* SUCCESS */ strcpy(jid_server, job_id); if (notNULL(parent_server)) { strcat(jid_server, "@"); strcat(jid_server, parent_server); } location = pbs_locjob_err(connect, jid_server, NULL, &local_errno); if (location == NULL) { pbs_disconnect(connect); return(FALSE); } strcpy(located_server, location); free(location); pbs_disconnect(connect); return(TRUE); } /* FAILURE */ return(-1); } /* END locate_job() */
int main( int argc, char **argv) { int c; int errflg = 0; char *errmsg; #define MAX_OPTARG_LEN 256 #define MAX_RESOURCE_NAME_LEN 256 char optargout[MAX_OPTARG_LEN+1]; char resource_name[MAX_RESOURCE_NAME_LEN+1]; enum batch_op op; enum batch_op *pop = &op; struct attropl *select_list = 0; static char destination[PBS_MAXROUTEDEST+1] = ""; char server_out[MAXSERVERNAME] = ""; char *queue_name_out; char *server_name_out; int connect; char **selectjob_list; char *res_pos; char *pc; int u_cnt, o_cnt, s_cnt, n_cnt; time_t after; char a_value[80]; int exec_only = 0; if (getenv("PBS_QSTAT_EXECONLY") != NULL) exec_only = 1; #define GETOPT_ARGS "a:A:ec:h:l:N:p:q:r:s:u:" while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'a': check_op(optarg, pop, optargout); if ((after = cvtdate(optargout)) < 0) { fprintf(stderr, "qselect: illegal -a value\n"); errflg++; break; } sprintf(a_value, "%ld", (long)after); set_attrop(&select_list, ATTR_a, NULL, a_value, op); break; case 'e': exec_only = 1; break; case 'c': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)*pc)) pc++; if (strlen(pc) == 0) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } if (strcmp(pc, "u") == 0) { if ((op != EQ) && (op != NE)) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } } else if ((strcmp(pc, "n") != 0) && (strcmp(pc, "s") != 0) && (strcmp(pc, "c") != 0)) { if (strncmp(pc, "c=", 2) != 0) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } pc += 2; if (strlen(pc) == 0) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } while (*pc != '\0') { if (!isdigit((int)*pc)) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } pc++; } } set_attrop(&select_list, ATTR_c, NULL, optargout, op); break; case 'h': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)*pc)) pc++; if (strlen(pc) == 0) { fprintf(stderr, "qselect: illegal -h value\n"); errflg++; break; } u_cnt = o_cnt = s_cnt = n_cnt = 0; while (*pc) { if (*pc == 'u') u_cnt++; else if (*pc == 'o') o_cnt++; else if (*pc == 's') s_cnt++; else if (*pc == 'n') n_cnt++; else { fprintf(stderr, "qselect: illegal -h value\n"); errflg++; break; } pc++; } if (n_cnt && (u_cnt + o_cnt + s_cnt)) { fprintf(stderr, "qselect: illegal -h value\n"); errflg++; break; } set_attrop(&select_list, ATTR_h, NULL, optargout, op); break; case 'l': res_pos = optarg; while (*res_pos != '\0') { if (check_res_op(res_pos, resource_name, pop, optargout, &res_pos) != 0) { errflg++; break; } set_attrop(&select_list, ATTR_l, resource_name, optargout, op); } break; case 'p': check_op(optarg, pop, optargout); set_attrop(&select_list, ATTR_p, NULL, optargout, op); break; case 'q': strncpy(destination, optarg, PBS_MAXROUTEDEST); check_op(optarg, pop, optargout); set_attrop(&select_list, ATTR_q, NULL, optargout, op); break; case 'r': op = EQ; pc = optarg; while (isspace((int)(*pc))) pc++; if (strlen(pc) != 1) { fprintf(stderr, "qsub: illegal -r value\n"); errflg++; break; } if (*pc != 'y' && *pc != 'n') { fprintf(stderr, "qsub: illegal -r value\n"); errflg++; break; } set_attrop(&select_list, ATTR_r, NULL, pc, op); break; case 's': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)(*pc))) pc++; if (strlen(optarg) == 0) { fprintf(stderr, "qselect: illegal -s value\n"); errflg++; break; } while (*pc) { if (*pc != 'C' && *pc != 'E' && *pc != 'H' && *pc != 'Q' && *pc != 'R' && *pc != 'T' && *pc != 'W') { fprintf(stderr, "qselect: illegal -s value\n"); errflg++; break; } pc++; } set_attrop(&select_list, ATTR_state, NULL, optargout, op); break; case 'u': op = EQ; if (parse_at_list(optarg, FALSE, FALSE)) { fprintf(stderr, "qselect: illegal -u value\n"); errflg++; break; } set_attrop(&select_list, ATTR_u, NULL, optarg, op); break; case 'A': op = EQ; set_attrop(&select_list, ATTR_A, NULL, optarg, op); break; case 'N': op = EQ; set_attrop(&select_list, ATTR_N, NULL, optarg, op); break; default : errflg++; } if (errflg || (optind < argc)) { static char usage[] = "usage: qselect \ [-a [op]date_time] [-A account_string] [-e] [-c [op]interval] \n\ [-h hold_list] [-l resource_list] [-N name] [-p [op]priority] \n\ [-q destination] [-r y|n] [-s states] [-u user_name]\n"; fprintf(stderr,"%s", usage); exit(2); } if (notNULL(destination)) { if (parse_destination_id(destination, &queue_name_out, &server_name_out)) { fprintf(stderr, "qselect: illegally formed destination: %s\n", destination); exit(2); } else { if (notNULL(server_name_out)) { strcpy(server_out, server_name_out); } } } connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qselect: cannot connect to server %s (errno=%d) %s\n", pbs_server, pbs_errno, pbs_strerror(pbs_errno)); exit(pbs_errno); } selectjob_list = pbs_selectjob(connect, select_list, exec_only ? EXECQUEONLY : NULL); if (selectjob_list == NULL) { if (pbs_errno != PBSE_NONE) { errmsg = pbs_geterrmsg(connect); if (errmsg != NULL) { fprintf(stderr, "qselect: %s\n", errmsg); } else { fprintf(stderr, "qselect: Error (%d - %s) selecting jobs\n", pbs_errno, pbs_strerror(pbs_errno)); } exit(pbs_errno); } } else /* got some jobs ids */ { int i = 0; while (selectjob_list[i] != NULL) { printf("%s\n", selectjob_list[i++]); } free(selectjob_list); } pbs_disconnect(connect); exit(0); }
int main(int argc, char **argv, char **envp) /* qselect */ { int c; int errflg=0; char *errmsg; #define MAX_OPTARG_LEN 256 #define MAX_RESOURCE_NAME_LEN 256 char optargout[MAX_OPTARG_LEN+1]; char resource_name[MAX_RESOURCE_NAME_LEN+1]; enum batch_op op; enum batch_op *pop = &op; struct attropl *select_list = 0; static char destination[PBS_MAXQUEUENAME+1] = ""; char server_out[MAXSERVERNAME] = ""; char *queue_name_out; char *server_name_out; int connect; char **selectjob_list; char *res_pos; char *pc; time_t after; char a_value[80]; char extendopts[4] = ""; char *attr_time = NULL; struct ecl_attribute_errors *err_list; char *resc_time = NULL; #define GETOPT_ARGS "a:A:c:h:HJl:N:p:q:r:s:t:Tu:xP:" /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'a': check_op(optarg, pop, optargout); if ((after = cvtdate(optargout)) < 0) { fprintf(stderr, "qselect: illegal -a value\n"); errflg++; break; } sprintf(a_value, "%ld", (long)after); set_attrop(&select_list, ATTR_a, NULL, a_value, op); break; case 'c': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)*pc)) pc++; if (strlen(pc) == 0) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } set_attrop(&select_list, ATTR_c, NULL, optargout, op); break; case 'h': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)*pc)) pc++; set_attrop(&select_list, ATTR_h, NULL, optargout, op); break; case 'J': op = EQ; set_attrop(&select_list, ATTR_array, NULL, "True", op); break; case 'l': res_pos = optarg; while (*res_pos != '\0') { if (check_res_op(res_pos, resource_name, pop, optargout, &res_pos) != 0) { errflg++; break; } set_attrop(&select_list, ATTR_l, resource_name, optargout, op); } break; case 'p': check_op(optarg, pop, optargout); set_attrop(&select_list, ATTR_p, NULL, optargout, op); break; case 'q': strcpy(destination, optarg); check_op(optarg, pop, optargout); set_attrop(&select_list, ATTR_q, NULL, optargout, op); break; case 'r': op = EQ; pc = optarg; while (isspace((int)(*pc))) pc++; if (*pc != 'y' && *pc != 'n') { /* qselect specific check - stays */ fprintf(stderr, "qselect: illegal -r value\n"); errflg++; break; } set_attrop(&select_list, ATTR_r, NULL, pc, op); break; case 's': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)(*pc))) pc++; set_attrop(&select_list, ATTR_state, NULL, optargout, op); break; case 't': if (get_tsubopt(*optarg, &attr_time, &resc_time)) { fprintf(stderr, "qselect: illegal -t value\n"); errflg++; break; } /* 1st character possess the subopt, so send optarg++ */ optarg ++; check_op(optarg, pop, optargout); if ((after = cvtdate(optargout)) < 0) { fprintf(stderr, "qselect: illegal -t value\n"); errflg++; break; } sprintf(a_value, "%ld", (long)after); set_attrop(&select_list, attr_time, resc_time, a_value, op); break; case 'T': if (strchr(extendopts, (int)'T') == NULL) (void)strcat(extendopts, "T"); break; case 'x': if (strchr(extendopts, (int)'x') == NULL) (void)strcat(extendopts, "x"); break; case 'H': op = EQ; if (strchr(extendopts, (int)'x') == NULL) (void)strcat(extendopts, "x"); set_attrop(&select_list, ATTR_state, NULL, "FM", op); break; case 'u': op = EQ; set_attrop(&select_list, ATTR_u, NULL, optarg, op); break; case 'A': op = EQ; set_attrop(&select_list, ATTR_A, NULL, optarg, op); break; case 'P': op = EQ; set_attrop(&select_list, ATTR_project, NULL, optarg, op); break; case 'N': op = EQ; set_attrop(&select_list, ATTR_N, NULL, optarg, op); break; default : errflg++; } if (errflg || (optind < argc)) { print_usage(); exit(2); } if (notNULL(destination)) { if (parse_destination_id(destination, &queue_name_out, &server_name_out)) { fprintf(stderr, "qselect: illegally formed destination: %s\n", destination); exit(2); } else { if (notNULL(server_name_out)) { strcpy(server_out, server_name_out); } } } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qselect: unable to initialize security library.\n"); exit(2); } connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qselect: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(pbs_errno); } if (extendopts[0] == '\0') selectjob_list = pbs_selectjob(connect, select_list, NULL); else selectjob_list = pbs_selectjob(connect, select_list, extendopts); if (selectjob_list == NULL) { if ((err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(err_list); if (pbs_errno != PBSE_NONE) { errmsg = pbs_geterrmsg(connect); if (errmsg != NULL) { fprintf(stderr, "qselect: %s\n", errmsg); } else { fprintf(stderr, "qselect: Error (%d) selecting jobs\n", pbs_errno); } /* * If the server is not configured for history jobs i.e. * job_history_enable svr attr is unset/set to FALSE, qselect * command with -x/-H option is being used, then pbs_selectjob() * will return PBSE_JOBHISTNOTSET error code. But command will * exit with exit code '0'after printing the corresponding error * message. i.e. "job_history_enable is set to false" */ if (pbs_errno == PBSE_JOBHISTNOTSET) pbs_errno = 0; /*cleanup security library initializations before exiting*/ CS_close_app(); exit(pbs_errno); } } else { /* got some jobs ids */ int i = 0; while (selectjob_list[i] != NULL) { printf("%s\n", selectjob_list[i++]); } free(selectjob_list); } pbs_disconnect(connect); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(0); }
/** * Take a job ID string and parse it into * job ID and server name parts. * * @param job_id_in Input string of the form <job number>[.<parent server name>][@<host server url>] * @param job_id_out Output string containing only the job ID part of the input specification. * @param server_out Output string containing nothing or a host url if specified. */ int get_server( char *job_id_in, /* read only */ char *job_id_out, /* write only */ int job_id_out_size, /* sizeof the out buffer */ char *server_out, /* write only */ int server_out_size) /* sizeof the out buffer */ { char *seq_number; char *parent_server; char *current_server; char def_server[PBS_MAXSERVERNAME + 1]; char host_server[PBS_MAXSERVERNAME + 1]; char *c; /* parse the job_id_in into components */ if (!strcasecmp("all",job_id_in)) { snprintf(job_id_out, job_id_out_size, "%s", job_id_in); server_out[0] = '\0'; } else { if (parse_jobid(job_id_in, &seq_number, &parent_server, ¤t_server)) { return(1); } /* Apply the above rules, in order, except for the locate job request. * That request is only sent if the job is not found on the local server. */ if (notNULL(current_server)) { /* @server found */ snprintf(server_out, server_out_size, "%s", current_server); } else if (notNULL(parent_server)) { /* .server found */ snprintf(server_out, server_out_size, "%s", parent_server); } else { /* can't locate a server, so return a NULL to tell pbs_connect to use default */ server_out[0] = '\0'; } /* Make a fully qualified name of the job id. */ if (notNULL(parent_server)) { if (notNULL(current_server)) { snprintf(job_id_out, job_id_out_size, "%s.%s", seq_number, parent_server); /* parent_server might not be resolvable if current_server specified */ } else { if (get_fullhostname(parent_server, host_server, PBS_MAXSERVERNAME, NULL) != 0) { /* FAILURE */ return(1); } snprintf(job_id_out, job_id_out_size, "%s.%s", seq_number, host_server); } if ((c = strchr(parent_server, ':')) != 0) { if (*(c - 1) == '\\') c--; snprintf(job_id_out + strlen(job_id_out), job_id_out_size - strlen(job_id_out), "%s", c); } } else { parent_server = pbs_default(); if ((parent_server == (char *)NULL) || (*parent_server == '\0')) { return(1); } snprintf(def_server, PBS_MAXSERVERNAME, "%s", parent_server); c = def_server; while ((*c != '\n') && (*c != '\0')) c++; *c = '\0'; if (get_fullhostname(def_server, host_server, PBS_MAXSERVERNAME, NULL) != 0) { /* FAILURE */ return(1); } if ((c = strchr(def_server, ':')) != 0) { if (*(c - 1) == '\\') c--; snprintf(job_id_out, job_id_out_size, "%s.%s%s", seq_number, host_server, c); } else snprintf(job_id_out, job_id_out_size, "%s.%s", seq_number, host_server); } /* END else */ } return(PBSE_NONE); } /* END get_server() */
/* * int execute( char *queue, char *server ) * * queue The name of the queue to disable. * server The name of the server that manages the queue. * * Returns: * None * * File Variables: * exitstatus Set to two if an error occurs. */ static void execute( char *queue, char *server) { int ct; /* Connection to the server */ int local_errno = 0; int merr; /* Error return from pbs_manager */ char *errmsg; /* Error message from pbs_manager */ /* The disable request */ static struct attropl attr = { NULL, (char *)"enabled", NULL, (char *)"FALSE", SET }; if ((ct = cnt2server(server)) > 0) { merr = pbs_manager_err(ct, MGR_CMD_SET, MGR_OBJ_QUEUE, queue, &attr, NULL, &local_errno); if (merr != 0) { errmsg = pbs_geterrmsg(ct); if (errmsg != NULL) { fprintf(stderr, "qdisable: %s ", errmsg); free(errmsg); } else { fprintf(stderr, "qdisable: Error disabling queue: %d - %s ", local_errno, pbs_strerror(local_errno)); } if (notNULL(queue)) fprintf(stderr, "%s", queue); if (notNULL(server)) fprintf(stderr, "@%s", server); fprintf(stderr, "\n"); exitstatus = 2; } pbs_disconnect(ct); } else { fprintf(stderr, "qdisable: could not connect to server %s (%d) %s\n", server, ct * -1, pbs_strerror(ct * -1)); exitstatus = 2; } }
/** * @brief * processes input jobid according to above mentioned rules * * @param[in] job_id_in - input job id * @param[out] job_id_out - processed job id * @param[out] server_out - server name * * @return int * @retval 0 success * @retval 1 error * */ int get_server(char *job_id_in, char *job_id_out, char *server_out) { char *seq_number = NULL; char *parent_server = NULL; char *current_server = NULL; char host_server[PBS_MAXSERVERNAME+1]; if (!job_id_in || !job_id_out || !server_out) return 1; if (pbs_loadconf(0) != 1) return 1; /* parse the job_id_in into components */ if (parse_jobid(job_id_in, &seq_number, &parent_server, ¤t_server)) { free(seq_number); free(parent_server); free(current_server); return 1; } /* Apply the above rules, in order, except for the locate job request. That request is only sent if the job is not found on the local server. */ server_out[0] = '\0'; if (notNULL(current_server)) /* @server found */ strcpy(server_out, current_server); free(current_server); strcpy(job_id_out, seq_number); free(seq_number); if (notNULL(parent_server)) { /* If parent_server matches PBS_SERVER then use it */ if (pbs_conf.pbs_server_name) { if (strcasecmp(parent_server, pbs_conf.pbs_server_name) == 0) { strcat(job_id_out, "."); strcat(job_id_out, pbs_conf.pbs_server_name); free(parent_server); return 0; } } if (get_fullhostname(parent_server, host_server, PBS_MAXSERVERNAME) != 0) { free(parent_server); return 1; } strcat(job_id_out, "."); #ifdef NAS_CANON_JOBID /* localmod 086 */ strcat(job_id_out, host_server); #else strcat(job_id_out, parent_server); #endif /* localmod 086 */ if (server_out[0] == '\0') strcpy(server_out, parent_server); free(parent_server); return 0; } free(parent_server); if (pbs_conf.pbs_server_name) { strcat(job_id_out, "."); strcat(job_id_out, pbs_conf.pbs_server_name); } else { return 1; } return 0; }