int handle_path(

    char           *path_iter,
    int            &path_index)

{
    char  log_buf[LOCAL_LOG_BUF_SIZE];
    char *level_parent;
    char *level_child;

    int   level_index = -1;

    path_index++;

    /* iterate over each level in the path */
    while (get_parent_and_child(path_iter,&level_parent,&level_child,&path_iter) == PBSE_NONE)
    {
        if (!strncmp(level_parent,"level",strlen("level")))
        {
            handle_level(level_child, path_index, level_index);
        }
        else
        {
            /* non-fatal error */
            snprintf(log_buf, sizeof(log_buf),
                     "Found noise in the mom hierarchy file. Ignoring <%s>%s</%s>",
                     level_parent, level_child, level_parent);
            log_err(-1, __func__, log_buf);
        }
    }

    return(PBSE_NONE);
} /* END handle_path() */
void parse_mom_hierarchy(

    int fds)

{
    int             bytes_read;
    char            buffer[MAXLINE<<10];
    char           *current;
    char           *parent;
    char           *child;
    char            log_buf[LOCAL_LOG_BUF_SIZE];
    int             path_index = -1;

    memset(&buffer, 0, sizeof(buffer));

    if ((bytes_read = read_ac_socket(fds, buffer, sizeof(buffer) - 1)) < 0)
    {
        snprintf(log_buf, sizeof(log_buf),
                 "Unable to read from mom hierarchy file");
        log_err(errno, __func__, log_buf);

        return;
    }

    current = buffer;

    while (get_parent_and_child(current, &parent, &child, &current) == PBSE_NONE)

    {
        if (!strncmp(parent,"path",strlen("path")))
            handle_path(child, path_index);
        else
        {
            /* non-fatal error */
            snprintf(log_buf, sizeof(log_buf),
                     "Found noise in the mom hierarchy file. Ignoring <%s>%s</%s>",
                     parent, child, parent);
            log_err(-1, __func__, log_buf);
        }
    }
} /* END parse_mom_hierarchy() */
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() */
int str_to_attr(

  const char           *name,   /* I */
  char                 *val,    /* I */
  pbs_attribute        *attr,   /* O */
  struct attribute_def *padef,  /* I */
  int                   limit)  /* I */

  {
  int   index;
  char  buf[MAXLINE<<5];
  char  log_buf[LOCAL_LOG_BUF_SIZE];

  if ((name == NULL) ||
      (val  == NULL) ||
      (attr == NULL))
    {
    return(-10);
    }

  index = find_attr(padef,name,limit);

  if (index < 0)
    return(ATTR_NOT_FOUND);

  switch (padef[index].at_type)
    {
    case ATR_TYPE_LONG:

      attr[index].at_val.at_long = strtol(val, NULL, 10);

      break;

    case ATR_TYPE_CHAR:

      attr[index].at_val.at_char = *val;

      break;

    case ATR_TYPE_STR:

      unescape_xml(val,buf,sizeof(buf));

      attr[index].at_val.at_str = strdup(buf);

      if (attr[index].at_val.at_str == NULL)
        {
        log_err(PBSE_SYSTEM, __func__, "Cannot allocate memory\n");

        return(PBSE_SYSTEM);
        }

      break;

    case ATR_TYPE_ARST:
    case ATR_TYPE_ACL:

      {
      int   rc;

      unescape_xml(val,buf,sizeof(buf));

      if ((rc = decode_arst(attr + index,name,NULL,buf,0)))
        return(rc);
      }

      break;

    case ATR_TYPE_SIZE:

      {
      unsigned long number;

      char *unit;

      number = strtol(val, NULL, 10);

      attr[index].at_val.at_size.atsv_units = ATR_SV_BYTESZ;
      attr[index].at_val.at_size.atsv_num = number;
      attr[index].at_val.at_size.atsv_shift = 0;

      /* the string always ends with kb,mb if it has units */
      unit = val + strlen(val) - 2;

      if (unit < val)
        break;
      else if (isdigit(*val))
        break;

      switch (*unit)
        {
        case 'k':

          attr[index].at_val.at_size.atsv_shift = 10;

          break;

        case 'm':

          attr[index].at_val.at_size.atsv_shift = 20;

          break;

        case 'g':

          attr[index].at_val.at_size.atsv_shift = 30;

          break;

        case 't':

          attr[index].at_val.at_size.atsv_shift = 40;

          break;

        case 'p':

          attr[index].at_val.at_size.atsv_shift = 50;

          break;

        }
      }

      break;

    case ATR_TYPE_RESC:

      {
      char *resc_parent;
      char *resc_child;
      char *resc_ptr = val;

      int   len = strlen(resc_ptr);
      int   rc;
      int   errFlg = 0;

      while (resc_ptr - val < len)
        {
        if (get_parent_and_child(resc_ptr,&resc_parent,&resc_child,
              &resc_ptr))
          {
          errFlg = TRUE;

          break;
          }
        
        if ((rc = decode_resc(&(attr[index]),name,resc_parent,resc_child,ATR_DFLAG_ACCESS)))
          {
          snprintf(log_buf,sizeof(log_buf),
            "Error decoding resource %s, %s = %s\n",
            name,
            resc_parent,
            resc_child);
          
          errFlg = TRUE;

          log_err(rc, __func__, log_buf);
          }
        }

      if (errFlg == TRUE)
        return(-1);

      }

      break;

    /* NYI */
    case ATR_TYPE_LIST:
    case ATR_TYPE_LL:
    case ATR_TYPE_SHORT:
    case ATR_TYPE_JINFOP:

      break;
    } /* END switch (pbs_attribute type) */

  attr[index].at_flags |= ATR_VFLAG_SET;

  return(PBSE_NONE);
  } /* END str_to_attr */
Exemple #5
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() */
int str_to_attr(

  char             *name,
  char             *val,
  struct attribute *attr)

  {
  int   index;
  char *id = "str_to_attr";
  char  buf[MAXLINE<<5];

  if ((name == NULL) ||
      (val  == NULL) ||
      (attr == NULL))
    {
    log_err(-1,id,"Illegal NULL pointer argument");

    return(-10);
    }

  index = find_attr(svr_attr_def,name,SRV_ATR_LAST);

  if (index < 0)
    {
    /* couldn't find attribute */
    snprintf(log_buffer,sizeof(log_buffer),
      "Couldn't find attribute %s\n",
      name);
    log_err(-1,id,log_buffer);

    return(ATTR_NOT_FOUND);
    }

  switch (svr_attr_def[index].at_type)
    {
    case ATR_TYPE_LONG:

      attr[index].at_val.at_long = atol(val);

      break;

    case ATR_TYPE_CHAR:

      attr[index].at_val.at_char = *val;

      break;

    case ATR_TYPE_STR:

      unescape_xml(val,buf,sizeof(buf));

      attr[index].at_val.at_str = (char *)malloc(strlen(buf)+1);

      if (attr[index].at_val.at_str == NULL)
        {
        log_err(PBSE_SYSTEM,id,"Cannot allocate memory\n");

        return(PBSE_SYSTEM);
        }

      strcpy(attr[index].at_val.at_str,buf);

      break;

    case ATR_TYPE_ARST:
    case ATR_TYPE_ACL:

      {
      int   rc;

      unescape_xml(val,buf,sizeof(buf));

      if ((rc = decode_arst(attr + index,name,NULL,buf)))
        return(rc);
      }

      break;

    case ATR_TYPE_SIZE:

      {
      unsigned long number;

      char *unit;

      number = atol(val);

      attr[index].at_val.at_size.atsv_units = ATR_SV_BYTESZ;
      attr[index].at_val.at_size.atsv_num = number;
      attr[index].at_val.at_size.atsv_shift = 0;

      /* the string always ends with kb,mb if it has units */
      unit = val + strlen(val) - 2;

      if (unit < val)
        break;
      else if (isdigit(*val))
        break;

      switch (*unit)
        {
        case 'k':

          attr[index].at_val.at_size.atsv_shift = 10;

          break;

        case 'm':

          attr[index].at_val.at_size.atsv_shift = 20;

          break;

        case 'g':

          attr[index].at_val.at_size.atsv_shift = 30;

          break;

        case 't':

          attr[index].at_val.at_size.atsv_shift = 40;

          break;

        case 'p':

          attr[index].at_val.at_size.atsv_shift = 50;

          break;

        }
      }

      break;

    case ATR_TYPE_RESC:

      {
      char *resc_parent;
      char *resc_child;
      char *resc_ptr = val;

      int   len = strlen(resc_ptr);
      int   rc;
      int   errFlg = 0;

      while (resc_ptr - val < len)
        {
        if (get_parent_and_child(resc_ptr,&resc_parent,&resc_child,
              &resc_ptr))
          {
          errFlg = TRUE;

          break;
          }
        
        if ((rc = decode_resc(&(server.sv_attr[index]),name,resc_parent,resc_child)))
          {
          snprintf(log_buffer,sizeof(log_buffer),
            "Error decoding resource %s, %s = %s\n",
            name,
            resc_parent,
            resc_child);
          
          errFlg = TRUE;

          log_err(rc,id,log_buffer);
          }
        }

      if (errFlg == TRUE)
        return(-1);

      }

      break;

    /* NYI */
    case ATR_TYPE_LIST:
    case ATR_TYPE_LL:
    case ATR_TYPE_SHORT:
    case ATR_TYPE_JINFOP:

      break;
    } /* END switch (attribute type) */

  attr[index].at_flags |= ATR_VFLAG_SET;

  return(0);
  } /* END str_to_attr */