Ejemplo n.º 1
0
pbs_queue *que_recov_xml(

  char *filename)

  {
  int          fds;
  int          rc;
  pbs_queue   *pq;
  char         namebuf[MAXPATHLEN];
  char         buf[MAXLINE<<10];
  char        *parent;
  char        *child;

  char        *current;
  char        *begin;
  char        *end;
  char         log_buf[LOCAL_LOG_BUF_SIZE];
  time_t       time_now = time(NULL);

  pq = que_alloc(filename, TRUE);  /* allocate & init queue structure space */

  if (pq == NULL)
    {
    log_err(-1, __func__, "que_alloc failed");

    return(NULL);
    }

  snprintf(namebuf, sizeof(namebuf), "%s%s", path_queues, filename);

  fds = open(namebuf, O_RDONLY, 0);

  if (fds < 0)
    {
    log_err(errno, __func__, "open error");

    que_free(pq, TRUE);

    return(NULL);
    }

  /* read in queue save sub-structure */
  if (read_ac_socket(fds,buf,sizeof(buf)) < 0)
    {
    snprintf(log_buf,sizeof(log_buf),
      "Unable to read from queue file %s",
      filename);
    log_err(errno, __func__, log_buf);
    
    close(fds);

    return(NULL);
    }

  current = begin = buf;

  /* advance past the queue tag */
  current = strstr(current,"<queue>");
  if (current == NULL)
    {
    log_event(PBSEVENT_SYSTEM,
      PBS_EVENTCLASS_SERVER,
      __func__,
      "Cannot find a queue tag, attempting to load legacy format");
    que_free(pq, TRUE);
    
    close(fds);

    return(que_recov(filename));
    }

  end = strstr(current,"</queue>");

  if (end == NULL)
    {
    log_err(-1, __func__, "No queue tag found in the queue file???");
    que_free(pq, TRUE);
    close(fds);
    return(NULL);
    }

  /* move past the queue tag */
  current += strlen("<queue>");
  /* adjust the end for the newline preceeding the close queue tag */
  end--;

  while (current < end)
    {
    if (get_parent_and_child(current,&parent,&child,&current))
      {
      /* ERROR */
      snprintf(log_buf,sizeof(log_buf),
        "Bad XML in the queue file at: %s",
        current);
      log_err(-1, __func__, log_buf);

      que_free(pq, TRUE);
      close(fds);
      return(NULL);
      }

    if (!strcmp(parent,"modified"))
      pq->qu_qs.qu_modified = atoi(child);
    else if (!strcmp(parent,"type"))
      pq->qu_qs.qu_type = atoi(child);
    else if (!strcmp(parent,"create_time"))
      pq->qu_qs.qu_ctime = atoi(child);
    else if (!strcmp(parent,"modify_time"))
      pq->qu_qs.qu_mtime = atoi(child);
    else if (!strcmp(parent,"name"))
      snprintf(pq->qu_qs.qu_name,sizeof(pq->qu_qs.qu_name),"%s",child);
    else if (!strcmp(parent,"attributes"))
      {
      char *attr_ptr = child;
      char *child_parent;
      char *child_attr;

      while (*attr_ptr != '\0')
        {
        if (get_parent_and_child(attr_ptr,&child_parent,&child_attr,&attr_ptr))
          {
          /* ERROR */
          snprintf(log_buf,sizeof(log_buf),
            "Bad XML in the queue file at: %s",
            current);
          log_err(-1, __func__, log_buf);
          
          que_free(pq, TRUE);
          close(fds);
          return(NULL);
          }

        if ((rc = str_to_attr(child_parent,child_attr,pq->qu_attr,que_attr_def)))
          {
          /* ERROR */
          snprintf(log_buf,sizeof(log_buf),
            "Error creating attribute %s",
            child_parent);
          log_err(rc, __func__, log_buf);

          que_free(pq, TRUE);
          close(fds);
          return(NULL);
          }
        }
      }
    } 

  /* all done recovering the queue */

  close(fds);

  if ((pq->qu_attr[QA_ATR_MTime].at_flags & ATR_VFLAG_SET) == 0)
    {
    /* if we are recovering a pre-2.1.2 queue, save a new mtime */

    pq->qu_attr[QA_ATR_MTime].at_val.at_long = time_now;
    pq->qu_attr[QA_ATR_MTime].at_flags = ATR_VFLAG_SET;

    que_save(pq);
    }

  return(pq);
  } /* END que_recov_xml() */
Ejemplo n.º 2
0
/*
 * Parse the configuration file and set up variables
 */
int parse_rc(void)
{
  int i, l = 1, parse, fg, bg, hl;
  unsigned char str[MAX_LEN+1], *var, *value, *tempptr;
  FILE *rc_file = NULL;

  /*
   *
   *  At start, 'dialog' determines the settings to use as follows:
   *
   *  a) if environment variable DIALOGRC is set, it's value determines the
   *     name of the configuration file.
   *
   *  b) if the file in (a) can't be found, use the file $HOME/.dialogrc
   *     as the configuration file.
   *
   *  c) if the file in (b) can't be found, use compiled in defaults.
   *
   */

  if ((tempptr = getenv("DIALOGRC")) != NULL)
    rc_file = fopen(tempptr, "rt");

  if (tempptr == NULL || rc_file == NULL) {    /* step (a) failed? */
    /* try step (b) */
    if ((tempptr = getenv("HOME")) == NULL)
      return 0;    /* step (b) failed, use default values */

    if (tempptr[0] == '\0' || lastch(tempptr) == '/')
      sprintf(str, "%s%s", tempptr, DIALOGRC);
    else
      sprintf(str, "%s/%s", tempptr, DIALOGRC);

    if ((rc_file = fopen(str, "rt")) == NULL)
      return 0;    /* step (b) failed, use default values */
  }

  /* Scan each line and set variables */
  while (fgets(str, MAX_LEN, rc_file) != NULL) {
    if (lastch(str) != '\n') {    /* ignore rest of file if line too long */
      fprintf(stderr, "\nParse error: line %d of configuration file too long.\n", l);
      fclose(rc_file);
      return -1;    /* parse aborted */
    }
    else {
      lastch(str) = '\0';
      parse = parse_line(str, &var, &value);    /* parse current line */

      switch (parse) {
	case LINE_BLANK:    /* ignore blank lines and comments */
        case LINE_COMMENT:
          break;
        case LINE_OK:
          /* search table for matching config variable name */
          for (i = 0; i < VAR_COUNT && strcmp(vars[i].name, var); i++);

          if (i == VAR_COUNT) {    /* no match */
            fprintf(stderr, "\nParse error: unknown variable at line %d of configuration file.\n", l);
            return -1;    /* parse aborted */
          }
          else {    /* variable found in table, set run time variables */
            switch (vars[i].type) {
              case VAL_INT:
                *((int *) vars[i].var) = atoi(value);
                break;
              case VAL_STR:
                if (!isquote(value[0]) || !isquote(lastch(value)) || strlen(value) < 2) {
                  fprintf(stderr, "\nParse error: string value expected at line %d of configuration file.\n", l);
                  return -1;    /* parse aborted */
                }
                else {
                  /* remove the (") quotes */
                  value++;
                  lastch(value) = '\0';
                  strcpy((unsigned char *) vars[i].var, value);
		}
                break;
              case VAL_BOOL:
                if (!strcasecmp(value, "ON"))
                  *((bool *) vars[i].var) = TRUE;
                else if (!strcasecmp(value, "OFF"))
                  *((bool *) vars[i].var) = FALSE;
                else {
                  fprintf(stderr, "\nParse error: boolean value expected at line %d of configuration file.\n", l);
                  return -1;    /* parse aborted */
                }
                break;
              case VAL_ATTR:
                if (str_to_attr(value, &fg, &bg, &hl) == -1) {
                  fprintf(stderr, "\nParse error: attribute value expected at line %d of configuration file.\n", l);
                  return -1;    /* parse aborted */
                }
                ((int *) vars[i].var)[0] = fg;
                ((int *) vars[i].var)[1] = bg;
                ((int *) vars[i].var)[2] = hl;
                break;
            }
          }
          break;
        case LINE_ERROR:
          fprintf(stderr, "\nParse error: syntax error at line %d of configuration file.\n", l);
          return -1;    /* parse aborted */
      }
    }

    l++;    /* next line */
  }

  fclose(rc_file);
  return 0;    /* parse successful */
}
Ejemplo n.º 3
0
/*
 * Parse the configuration file and set up variables
 */
int
dlg_parse_rc(void)
{
    int i;
    int l = 1;
    PARSE_LINE parse;
    char str[MAX_LEN + 1];
    char *var;
    char *value;
    char *tempptr;
    int result = 0;
    FILE *rc_file = 0;
    char *params;

    /*
     *  At startup, dialog determines the settings to use as follows:
     *
     *  a) if the environment variable $DIALOGRC is set, its value determines
     *     the name of the configuration file.
     *
     *  b) if the file in (a) can't be found, use the file $HOME/.dialogrc
     *     as the configuration file.
     *
     *  c) if the file in (b) can't be found, try using the GLOBALRC file.
     *     Usually this will be /etc/dialogrc.
     *
     *  d) if the file in (c) cannot be found, use the compiled-in defaults.
     */

    /* try step (a) */
    if ((tempptr = getenv("DIALOGRC")) != NULL)
        rc_file = fopen(tempptr, "rt");

    if (rc_file == NULL) {	/* step (a) failed? */
        /* try step (b) */
        if ((tempptr = getenv("HOME")) != NULL
                && strlen(tempptr) < MAX_LEN - (sizeof(DIALOGRC) + 3)) {
            if (tempptr[0] == '\0' || lastch(tempptr) == '/')
                sprintf(str, "%s%s", tempptr, DIALOGRC);
            else
                sprintf(str, "%s/%s", tempptr, DIALOGRC);
            rc_file = fopen(tempptr = str, "rt");
        }
    }

    if (rc_file == NULL) {	/* step (b) failed? */
        /* try step (c) */
        strcpy(str, GLOBALRC);
        if ((rc_file = fopen(tempptr = str, "rt")) == NULL)
            return 0;		/* step (c) failed, use default values */
    }

    DLG_TRACE(("opened rc file \"%s\"\n", tempptr));
    /* Scan each line and set variables */
    while ((result == 0) && (fgets(str, MAX_LEN, rc_file) != NULL)) {
        DLG_TRACE(("rc:%s", str));
        if (*str == '\0' || lastch(str) != '\n') {
            /* ignore rest of file if line too long */
            fprintf(stderr, "\nParse error: line %d of configuration"
                    " file too long.\n", l);
            result = -1;	/* parse aborted */
            break;
        }

        lastch(str) = '\0';
        if (begins_with(str, "bindkey", &params)) {
            if (!dlg_parse_bindkey(params)) {
                fprintf(stderr, "\nParse error: line %d of configuration\n", l);
                result = -1;
            }
            continue;
        }
        parse = parse_line(str, &var, &value);	/* parse current line */

        switch (parse) {
        case LINE_EMPTY:	/* ignore blank lines and comments */
            break;
        case LINE_EQUALS:
            /* search table for matching config variable name */
            if ((i = find_vars(var)) >= 0) {
                switch (vars[i].type) {
                case VAL_INT:
                    *((int *) vars[i].var) = atoi(value);
                    break;
                case VAL_STR:
                    if (!isquote(value[0]) || !isquote(lastch(value))
                            || strlen(value) < 2) {
                        fprintf(stderr, "\nParse error: string value "
                                "expected at line %d of configuration "
                                "file.\n", l);
                        result = -1;	/* parse aborted */
                    } else {
                        /* remove the (") quotes */
                        value++;
                        lastch(value) = '\0';
                        strcpy((char *) vars[i].var, value);
                    }
                    break;
                case VAL_BOOL:
                    if (!dlg_strcmp(value, "ON"))
                        *((bool *) vars[i].var) = TRUE;
                    else if (!dlg_strcmp(value, "OFF"))
                        *((bool *) vars[i].var) = FALSE;
                    else {
                        fprintf(stderr, "\nParse error: boolean value "
                                "expected at line %d of configuration "
                                "file (found %s).\n", l, value);
                        result = -1;	/* parse aborted */
                    }
                    break;
                }
#ifdef HAVE_COLOR
            } else if ((i = find_color(var)) >= 0) {
                int fg = 0;
                int bg = 0;
                int hl = 0;
                if (str_to_attr(value, &fg, &bg, &hl) == -1) {
                    fprintf(stderr, "\nParse error: attribute "
                            "value expected at line %d of configuration "
                            "file.\n", l);
                    result = -1;	/* parse aborted */
                } else {
                    dlg_color_table[i].fg = fg;
                    dlg_color_table[i].bg = bg;
                    dlg_color_table[i].hilite = hl;
                }
            } else {
#endif /* HAVE_COLOR */
                fprintf(stderr, "\nParse error: unknown variable "
                        "at line %d of configuration file:\n\t%s\n", l, var);
                result = -1;	/* parse aborted */
            }
            break;
        case LINE_ERROR:
            fprintf(stderr, "\nParse error: syntax error at line %d of "
                    "configuration file.\n", l);
            result = -1;	/* parse aborted */
            break;
        }
        l++;			/* next line */
    }

    (void) fclose(rc_file);
    return result;
}
Ejemplo n.º 4
0
int svr_recov_xml(

  char *svrfile,  /* I */
  int   read_only)  /* I */

  {
  int   sdb;
  int   bytes_read;
  int   errorCount = 0;
  int   rc;

  char  buffer[MAXLINE<<10];
  char *parent;
  char *child;

  char *current;
  char *begin;
  char *end;
  char  log_buf[LOCAL_LOG_BUF_SIZE];

  sdb = open(svrfile, O_RDONLY, 0);

  if (sdb < 0)
    {
    if (errno == ENOENT)
      {
      snprintf(log_buf,sizeof(log_buf),
        "cannot locate server database '%s' - use 'pbs_server -t create' to create new database if database has not been initialized.",
        svrfile);

      log_err(errno, __func__, log_buf);
      }
    else
      {
      log_err(errno, __func__, msg_svdbopen);
      }

    return(-1);
    }

  bytes_read = read_ac_socket(sdb,buffer,sizeof(buffer));

  if (bytes_read < 0)
    {
    snprintf(log_buf,sizeof(log_buf),
      "Unable to read from serverdb file - %s",
      strerror(errno));

    log_err(errno, __func__, log_buf);
    close(sdb);

    return(-1);
    }

  /* start reading the serverdb file */
  current = begin = buffer;

  /* advance past the server tag */
  current = strstr(current,"<server_db>");
  if (current == NULL)
    {
    /* no server tag - check if this is the old format */
    log_event(PBSEVENT_SYSTEM,
      PBS_EVENTCLASS_SERVER,
      __func__,
      "Cannot find a server tag, attempting to load legacy format\n");

    close(sdb);
    rc = svr_recov(svrfile,read_only);

    return(rc);
    }
  end = strstr(current,"</server_db>");

  if (end == NULL)
    {
    /* no server tag???? */
    log_err(-1, __func__, "No server tag found in the database file???");
    close(sdb);

    return(-1);
    }

  /* adjust to not process server tag */
  current += strlen("<server_db>");
  /* adjust end for the newline character preceeding the close server tag */
  end--;

  lock_sv_qs_mutex(server.sv_qs_mutex, __func__);

  server.sv_qs.sv_numjobs = 0; 
  server.sv_qs.sv_numque = server.sv_qs.sv_jobidnumber = 0;

  while (current < end)
    {
    if (get_parent_and_child(current,&parent,&child,&current))
      {
      /* ERROR */
      errorCount++;

      break;
      }

    if (!strcmp("numjobs",parent))
      {
      server.sv_qs.sv_numjobs = atoi(child);
      }
    else if (!strcmp("numque",parent))
      {
      server.sv_qs.sv_numque = atoi(child);
      }
    else if (!strcmp("nextjobid",parent))
      {
      server.sv_qs.sv_jobidnumber = atoi(child);
      }
    else if (!strcmp("savetime",parent))
      {
      server.sv_qs.sv_savetm = atol(child);
      }
    else if (!strcmp("attributes",parent))
      {
      char *attr_ptr = child;
      char *child_parent;
      char *child_attr;

      while (*attr_ptr != '\0')
        {
        if (get_parent_and_child(attr_ptr,&child_parent,&child_attr,
              &attr_ptr))
          {
          /* ERROR */
          errorCount++;

          break;
          }

        if ((rc = str_to_attr(child_parent,child_attr,server.sv_attr,svr_attr_def)))
          {
          /* ERROR */
          errorCount++;
          snprintf(log_buf,sizeof(log_buf),
            "Error creating attribute %s",
            child_parent);

          log_err(rc, __func__, log_buf);

          break;
          }
        }

      if (recovered_tcp_timeout < 300)
        disable_timeout_check = TRUE;
      }
    else
      {
      /* shouldn't get here */
      }
    }

  close(sdb);
  if (errorCount)
    return -1;
    
  if (!read_only)
    {
    server.sv_attr[SRV_ATR_NextJobNumber].at_val.at_long = 
      server.sv_qs.sv_jobidnumber;
    
    server.sv_attr[SRV_ATR_NextJobNumber].at_flags |= 
      ATR_VFLAG_SET| ATR_VFLAG_MODIFY;
    }

  unlock_sv_qs_mutex(server.sv_qs_mutex, __func__);

  return(PBSE_NONE);
  } /* END svr_recov_xml() */