Ejemplo n.º 1
0
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);
  }
Ejemplo n.º 2
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);
}