/**
 * Gets the information about a job. 
 *
 * It stores the info in a module variable.
 * In order to retrieve it, use @see readJobInfo.
 * @param jobid is the PID assigned by the queue
 * @return 0 if correct, non-zero if error
 */
int rm_getJobInfo(struct soap* soap, char* jobid, char* user, 
		struct jobcard** jobInfo )
{
   //! stores the status of a job
   struct batch_status* status;
   int connectionIdentifier;
   struct jobcard* job;

   connectionIdentifier = pbs_connect(server);
   if(!connectionIdentifier)
      return BESE_BACKEND;
   status = pbs_statjob(connectionIdentifier, jobid, NULL, NULL);
   pbs_disconnect(connectionIdentifier);
   if(status == NULL)
      return BESE_NO_ACTIVITY;
   job = (struct jobcard*)soap_malloc(soap, sizeof(struct jobcard));
   if (!job)
	   return BESE_MEM_ALLOC;
   memset(job, 0, sizeof(struct jobcard));

   fillJobStatusDefaults(job);
   convertJobInfo(soap, job, status);
   *jobInfo = job;
   pbs_statfree(status);
   return BESE_OK;
}
/**
 * Prints in stderr the error message of the last error. 
 *
 * It gets the error 
 * description of the last error that happened in the PBS queue server.
 * @param userMessage message to append to the output. It may contain 
 * additional information from the main program
 */
void printError(char* userMessage)
{
    char *errorMessage;
    int connectionIdentifier = pbs_connect(server);
    errorMessage = pbs_geterrmsg(connectionIdentifier);
    pbs_disconnect(connectionIdentifier);
    fprintf(stderr, "%s\n%s\n", userMessage, errorMessage);
}
/**
 * Gets the status of the job. 
 *
 * It maps the different states of PBS jobs to
 * pending and running. It does not make a difference between finished, 
 * cancelled, terminated and unknown jobs since PBS does not store this info.
 * @param jobid is the PID assigned by the queue
 * @return 0 if correct, non-zero if error
 */
int rm_getJobStatus(struct soap* s, char* jobid, char* user, struct bes__ActivityStatusType** jobStatus)
{
   struct bes__ActivityStatusType *activityStatus;
   int connectionIdentifier;
   //! stores the status of a job
   struct batch_status* status;

   if (!jobid || !jobStatus) {
      return BESE_BAD_ARG;
   }
   connectionIdentifier = pbs_connect(server);
   if (!connectionIdentifier)
	   return BESE_BACKEND;
   status = pbs_statjob(connectionIdentifier,jobid,NULL,NULL);
   pbs_disconnect(connectionIdentifier);
   if(status == NULL)
   {
      return BESE_NO_ACTIVITY;
   }
   activityStatus = (struct bes__ActivityStatusType*)soap_malloc(s, sizeof(struct bes__ActivityStatusType));
   if (activityStatus == NULL) {
      return BESE_MEM_ALLOC;
   }
   memset(activityStatus, 0, sizeof(struct bes__ActivityStatusType));
   struct attrl* attrList = status->attribs;
   while (attrList != NULL)
   {
      if(!strcmp(attrList->name, ATTR_state))
      {
        if(!strcmp(attrList->value, "T")) {
           activityStatus->state = Pending;
        }
        else if(!strcmp(attrList->value, "Q")) {
           activityStatus->state = Pending;
        }         
        else if(!strcmp(attrList->value,"H")) {
           activityStatus->state = Pending;
	}         
        else if(!strcmp(attrList->value,"W")){
           activityStatus->state = Pending;
        }         
        else if(!strcmp(attrList->value,"R")){
           activityStatus->state = Running;
        }
        else if(!strcmp(attrList->value,"E")) {
           activityStatus->state = Finished;
        }
        pbs_statfree(status);
	*jobStatus = activityStatus;
        return BESE_OK;
     }
     attrList = attrList->next;
  }
  pbs_statfree(status);
  return BESE_NO_ACTIVITY;
}
/**
 * Terminates a job. 
 * @param jobid is the PID assigned by the queue
 * @return 0 if correct, non-zero if error
 */
int rm_terminateJob(struct soap* s, char* jobid, char* user)
{
   int connectionIdentifier = pbs_connect(server);
   if (connectionIdentifier < 1 )
	   return BESE_BACKEND;
   int rc = pbs_deljob(connectionIdentifier, jobid, NULL);
   updateErrorNo();
   pbs_disconnect(connectionIdentifier);
   return BESE_OK;
}
Example #5
0
/**
 * This routine is not used but was implemented to
 * support qsub.
 *
 * @param server_name_ptr A pointer to a server name or server name list.
 * @param retry_seconds The period of time for which retrys should be attempted.
 * @returns A file descriptor number.
 */
int pbs_connect_with_retry(char *server_name_ptr, int retry_seconds)
  {
  int n_times_to_try = retry_seconds / CNTRETRYDELAY;
  int connect = -1;
  int n;

  for (n = 0; n < n_times_to_try; n++)  /* This is the retry loop */
    {
    if ((connect = pbs_connect(server_name_ptr)) >= 0)
      return(connect);  /* Success, we have a connection, return it. */

    sleep(CNTRETRYDELAY);
    }

  return(connect);
  }
Example #6
0
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() */
/**
 * Connects to the PBS queue server and loads cluster info.
 * @param soap is used to allocate memory
 * @param serverName is a string with the hostname in which the queue is running
 * @return 0 if connection established, 1 if error
 */
int rm_initialize(struct soap* soap, char* serverName){
   int connectionIdentifier;
   int error_code = BESE_OK;

   if (!serverName)
	   return BESE_BAD_ARG;
   server = (char*) malloc(strlen(serverName) + 1);
   nresources = (int*) malloc(sizeof(int));
   strcpy(server,serverName);
   connectionIdentifier = pbs_connect(serverName);
   if (connectionIdentifier <= 0 )
	   return BESE_BACKEND;
   pbs_disconnect(connectionIdentifier);
   error_code = rm_getClusterInfo(soap, &clusterInfo);
   if (error_code != BESE_OK)
      return error_code;
   else {
      printf("Looking for resources now");
      error_code = rm_getResourceList(soap, NULL, &resourceList, nresources);
      return error_code;
   }
}
Example #8
0
int main(

  int    ArgC,  /* I */
  char **ArgV)  /* I */

  {
  const char *OptString = "c:Cd:f:h:lp:q:r:sv";

  char  HostList[65536];

  char *HPtr;


  int c;
  int rc = PBSE_NONE;
  int local_errno = 0;

  int HostCount;
  int FailCount;

  /* initialize */

  HostList[0]     = '\0';
  ConfigBuf[0] = '\0';

  if (IamRoot() == 0)
    {
    exit(EXIT_FAILURE);
    }

  while ((c = getopt(ArgC, ArgV, OptString)) != EOF)
    {
    switch (c)
      {

      case 'c':

        /* clear job */

        JPtr = optarg;

        CmdIndex = momClear;

        break;

      case 'C':

        /* force cycle */

        CmdIndex = momQuery;

        Query[QueryI] = strdup("cycle");

        QueryI++;

        break;

      case 'd':

        /* diagnose */
        /* FORMAT:  momctl -d<X> */

        CmdIndex = momQuery;

        if ((Query[QueryI] = (char *)calloc(strlen(DiagPtr) + 3, sizeof(char))) == NULL)
      	  {
          fprintf(stderr,"ERROR:    could not calloc %d bytes!\n",
            (int)strlen(DiagPtr) + 3);

          exit(EXIT_FAILURE);
      	  }

        if (optarg == NULL)
          {
          strncpy(Query[QueryI],DiagPtr,strlen(DiagPtr));
          }
        else
          {
          snprintf(Query[QueryI],strlen(DiagPtr) + 2,"%s%s",
            DiagPtr,
            optarg);
          }

        QueryI++;

        break;

      case 'f':

        {
        int   rc;

        FILE *fp;

        long   size;

        if ((fp = fopen(optarg, "r")) == NULL)
          {
          fprintf(stderr, "ERROR:    cannot open file '%s', errno: %d (%s)\n",
            optarg,
            errno,
            strerror(errno));

          exit(EXIT_FAILURE);
          }

        rc = fread(HostList, sizeof(HostList), 1, fp);

        if ((rc == 0) && (!feof(fp)))
          {
          fprintf(stderr, "ERROR:    cannot read file '%s', errno: %d (%s)\n",
            optarg,
            errno,
            strerror(errno));

          exit(EXIT_FAILURE);
          }

        if ((size = ftell(fp)) < 0)
          size = 0;

        HostList[MIN(size,(long)sizeof(HostList) - 1)] = '\0';

        fclose(fp);
        }  /* END BLOCK */

      break;

      case 'h':

        /* connect to specified host */

        snprintf(HostList, sizeof(HostList), "%s", optarg);

        break;

      case 'l':

        CmdIndex = momLayout;

        break;

      case 'p':

        /* port */

        if (optarg == NULL)
          MCShowUsage("port not specified");

        MOMPort = (int)strtol(optarg, NULL, 10);

        if (MOMPort == 0)
          MCShowUsage("invalid port specified");

        break;

      case 'q':

        /* query resources */

        if (optarg == NULL)
          {
          MCShowUsage("query not specified");

          Query[QueryI] = strdup(DiagPtr);
          }
        else
          {
          Query[QueryI] = strdup(optarg);
          }

        QueryI++;

        CmdIndex = momQuery;

        break;

      case 'r':

        /* reconfigure */

        {
        CmdIndex = momReconfig;

        /* NOTE:  specify remote file to load -> 'fname' */
        /*        specify local file to stage -> 'LOCAL:fname' */

        if (optarg == NULL)
          MCShowUsage("file not specified");

        if (!strncmp(optarg, "LOCAL:", strlen("LOCAL:")))
          {
          FILE *fp;

          int   size;

          int   rc;

          char *ptr;
          char *cptr;

          strcpy(ConfigBuf, "CONFIG:");

          cptr = ConfigBuf + strlen(ConfigBuf);

          ptr = optarg + strlen("LOCAL:");

          if ((fp = fopen(ptr, "r")) == NULL)
            {
            fprintf(stderr, "ERROR:    cannot open file '%s', errno: %d (%s)\n",
                    optarg,
                    errno,
                    strerror(errno));

            exit(EXIT_FAILURE);
            }

          rc = fread(cptr, sizeof(ConfigBuf) - strlen(ConfigBuf), 1, fp);

          if ((rc == 0) && (!feof(fp)))
            {
            fprintf(stderr, "ERROR:    cannot read file '%s', errno: %d (%s)\n",
                    optarg,
                    errno,
                    strerror(errno));

            exit(EXIT_FAILURE);
            }

          size = ftell(fp);

          ConfigBuf[MIN(size + strlen("CONFIG:"),sizeof(ConfigBuf) - 1)] = '\0';

          fclose(fp);
          }
        else
          {
          snprintf(ConfigBuf, sizeof(ConfigBuf), "%s", optarg);
          }
        }  /* END (case 'r') */

      break;

      case 's':

        /* shutdown */

        CmdIndex = momShutdown;

        break;

      case 'v':

        /* report verbose logging */

        IsVerbose = TRUE;

        break;
      }  /* END switch (c) */
    }    /* END while (c = getopt()) */

  if (CmdIndex == momNONE)
    {
    MCShowUsage("no command specified");
    }

  if (HostList[0] == '\0')
    snprintf(HostList, sizeof(HostList), "%s", LocalHost);

  HPtr = strtok(HostList, ", \t\n");

  HostCount = 0;

  FailCount = 0;

  /* at this point, all args processing and setup is completed ...
   * ... now we run through each comma-delimited word in HPtr */

  while (HPtr != NULL)
    {
    if ((*HPtr == ':') && (*(HPtr + 1) != '\0'))
      {
      /* finds nodes with this property */

      int con;
      char *def_server, *pserver, *servername;

      struct batch_status *bstatus, *pbstat;

      struct attrl *nodeattrs;

      def_server = pbs_default();

      if ((pserver = strchr(HPtr,'@')) != NULL)
        {
        *pserver = '\0';
        servername = pserver + 1;
        }
      else
        {
        servername = def_server;
        }

      con = pbs_connect(servername);

      if (con < 0)
        {
        fprintf(stderr,"failed to connect to pbs_server:%s\n",
          servername);

        exit(EXIT_FAILURE);
        }

      /* get a batch_status entry for each node in ":property" */

      bstatus = pbs_statnode_err(con,HPtr,NULL,NULL, &local_errno);

      if (bstatus != NULL)
        {
        for (pbstat = bstatus;pbstat != NULL;pbstat = pbstat->next)
          {
          /* check state first, only do_mom() if not down */

          for (nodeattrs = pbstat->attribs;nodeattrs != NULL; nodeattrs = nodeattrs->next)
            {
            if (!strcmp(nodeattrs->name, ATTR_NODE_state))
              {
              if (!strstr(nodeattrs->value, ND_down))
                {
                if ((rc = perform_communications_with_retry(pbstat->name, MOMPort, &FailCount)) == PBSE_NONE)
                  HostCount++;
                }
              else
                {
                fprintf(stderr,"%12s:   skipping down node\n",
                  pbstat->name);
                }

              break;
              }  /* END if (attrib name eq state) */
            }    /* END for (nodeattrs) */
          }      /* END for (pbstat) */

        pbs_statfree(bstatus);
        }  /* END if (bstatus != NULL) */
      else
        {
        fprintf(stderr,"no nodes found in %s on %s\n",
          HPtr,
          servername);
        }

      pbs_disconnect(con);

      if (pserver != NULL)
        *pserver = '@';
      }
    else
      {
      if ((rc = perform_communications_with_retry(HPtr, MOMPort, &FailCount)) == PBSE_NONE)
        HostCount++;
      } /* END if (*HPtr == ':') */

    HPtr = strtok(NULL, ", \t\n");
    }  /* END while (HPtr != NULL) */


  if (IsVerbose == TRUE)
    {
    fprintf(stdout, "Node Summary:  %d Successful  %d Failed\n",
            HostCount,
            FailCount);
    }

  /* test success of do_mom before returning success */
  if (rc != PBSE_NONE)
    exit(EXIT_FAILURE);
    
  /* SUCCESS */

  exit(EXIT_SUCCESS);
  }  /* END main() */
Example #9
0
int cnt2server(

  const char *SpecServer)    /* I (optional) */

  {
  int connect;
  time_t firsttime = 0, thistime = 0;

  char Server[1024];

  if (cnt2server_retry > 0)
    {
    firsttime = time(NULL);
    }

  memset(Server, 0, sizeof(Server));

  if ((SpecServer != NULL) && (SpecServer[0] != '\0'))
    {
    snprintf(Server, sizeof(Server)-1, "%s", SpecServer);
    }

  /* NOTE:  env vars PBS_DEFAULT and PBS_SERVER will be checked and applied w/in pbs_connect() */

start:

  connect = pbs_connect(Server);

  if (connect <= 0)
    {
    /* PBSE_ * -1 is returned if applicable */
    if ((connect * -1) > PBSE_)
      {
      switch (connect * -1)
        {
        case PBSE_BADHOST:

          if (Server[0] == '\0')
            {
            fprintf(stderr, "Cannot resolve default server host '%s' - check server_name file.\n",
              pbs_default());
            }
          else
            {
            fprintf(stderr, "Cannot resolve specified server host '%s'.\n",
              Server);
            }

          break;

        case PBSE_NOCONNECTS:

          if (thistime == 0)
            fprintf(stderr, "Too many open connections.\n");

          if (cnt2server_retry != 0)
            goto retry;

          break;

        case PBSE_NOSERVER:

          fprintf(stderr, "No default server name - check server_name file.\n");

          break;

        case PBSE_SYSTEM:

          if (thistime == 0)
            fprintf(stderr, "System call failure.\n");

          if (cnt2server_retry != 0)
            goto retry;

          break;

        case PBSE_PERM:

          if (thistime == 0)
            fprintf(stderr, "No Permission.\n");

          if (cnt2server_retry != 0)
            goto retry;

          break;
          
        case PBSE_IFF_NOT_FOUND:
        
          fprintf(stderr, "pbs_iff command not found.\n");
          break;

        case PBSE_PROTOCOL:

          fprintf(stderr, "protocol failure.\n");
          break;

        default:

          if (thistime == 0)
            fprintf(stderr, "Communication failure.\n");

          if (cnt2server_retry != 0)
            goto retry;

          break;
        }
      }    /* END if (a PBSE_ was reported) */
    else /* These represent system errors (errno numbers) */
      {
      if (thistime == 0)
        {
        if ((connect *-1) == ECONNREFUSED)
          {
          if (Server[0] == '\0')
            {
            char *fbserver;

            fbserver = pbs_fbserver();

            if ((fbserver != NULL) && (fbserver[0] != '\0'))
              {
              snprintf(Server, sizeof(Server), "%s", fbserver);

              if (getenv("PBSDEBUG") != NULL)
                {
                fprintf(stderr, "attempting fallback server %s\n",
                  fbserver);
                }

              goto start;
              }

            fprintf(stderr, "Cannot connect to default server host '%s' - check pbs_server daemon and/or trqauthd.\n",
              pbs_default());
            }
          else
            {
            fprintf(stderr, "Cannot connect to specified server host '%s'.\n",
              Server);
            }
          }
        else
          {
          pbs_strerror(connect *-1);
          }
        }

      if (cnt2server_retry != 0)
        goto retry;
      }
    }    /* END if (connect <= 0) */

  return(connect);

retry:

  if (thistime == 0)
    {
    fprintf(stderr, "Retrying for %d seconds\n",
            (int)cnt2server_retry);
    }

  thistime = time(NULL);

  if (cnt2server_retry > 0) /* negative is infinite */
    {
    if ((thistime - firsttime) > cnt2server_retry)
      {
      return(connect);
      }

    if (getenv("PBSDEBUG") != NULL)
      {
      fprintf(stderr, "seconds remaining: %d\n",
        (int)(cnt2server_retry - (thistime - firsttime)));
      }
    }
  else
    {
    if (getenv("PBSDEBUG") != NULL)
      fprintf(stderr, "retrying...\n");
    }

  sleep(CNTRETRYDELAY);

  goto start;
  }  /* END cnt2server() */
Example #10
0
int main(int argc, char **argv) {
    char *server = NULL;
    char *jobid = NULL;
    char *var = NULL;
    char *value = NULL;
    int server_fd = 0;
    int ret = 0;
    int c = 0;
    struct batch_status *job = NULL;
    struct attrl *attribute = NULL;
    char *var_string = NULL;
    struct option prg_options[] = {
        {"help",    no_argument, 0, 'h'},
        {"version", no_argument, 0, 'V'},
    };

    for ( ; ; ) {
        int option_index = 0;
        c = getopt_long(argc, argv, "s:hV",
            prg_options, &option_index
        );
        if (c == -1) break;
        switch (c) {
            case 'h':
                usage(0);
                break;
            case 'V':
                printf("qsetenv version: %s; for torque version %s\n", QSETENV_VERSION, TORQUE_VERSION);
                exit(0);
                break;
            case 's':
                server = optarg;
                break;
        }
    }
    for (c = optind; c != argc; c++) {
        switch (c-optind) {
            case 0:
                jobid = argv[c];
                break;
            case 1:
                var = argv[c];
                break;
            case 2:
                value = argv[c];
                break;
            default:
                printf("Too many arguments!\n");
                usage(1);
                break;
        }
    }
    if (value == NULL) {
        printf("Too few arguments!\n");
        usage(1);
    }

    if (server == NULL) {
        server = pbs_get_server_list();
    }

    char *tok_server = server;
    char *tgt_server = NULL;
    while ((tgt_server = strtok(tok_server, ",")) != NULL) {
        tok_server = NULL;
        server_fd = pbs_connect(tgt_server);
        if (server_fd > 0) {
            break;
        }
    }
    if (server_fd <= 0) {
        fprintf(stderr, "Failed to connect to PBS server!\n");
        exit(1);
    }
    printf("Querying job %s\n", jobid);
    job = pbs_statjob(server_fd, jobid, NULL, 0);
    if (job != NULL) {
        printf("job name: %s\n", job->name);
        var_string = job_setenv_varstr(job, var, value);

        attribute = (struct attrl *) malloc(sizeof(struct attrl));
        memset(attribute, 0, sizeof(struct attrl));
        attribute->name = ATTR_v;
        attribute->value = var_string;
        attribute->next = NULL;

        ret = pbs_alterjob(server_fd, jobid, attribute, NULL);

        if (ret != 0) {
            printf("Got error: %s\n", pbs_strerror(pbs_errno));
        }

        free(attribute);
        attribute = NULL;
    }

    if (var_string != NULL) {
        free(var_string);
    }
    if (job != NULL) {
        pbs_statfree(job);
        job = NULL;
    }
    pbs_disconnect(server_fd);

    if (ret != 0) {
        return 1;
    }
    return 0;
}
/**
 * Gets the factory attributes. 
 *
 * This function uses @see loadResourceFile 
 * and also queries the PBS queue.
 * @param soap is needed to allocate memory that can be deallocated by the 
 * gsoap library after.
 * @param clusterInf a struct of type clusterInfo with the information needed for the
 * factory attributes document
 */
int rm_getClusterInfo(struct soap*soap, struct rm_clusterInfo** clusterInf
		/*,int compactResources*/)
{
   char outputFile[256];   
   FILE* fd;
   int rc;
   char resource[128];
   int connectionIdentifier = pbs_connect(server);
   struct rm_clusterInfo* clusterInfo;
   struct rm_resource* resourcesInfo;
   struct batch_status* status;


   if (!clusterInf) {
      return BESE_BAD_ARG;
   }
   clusterInfo = (struct rm_clusterInfo*) soap_malloc(soap,
		   sizeof(struct rm_clusterInfo));
   if (clusterInfo == NULL)
      return BESE_MEM_ALLOC;
   memset(clusterInfo, 0, sizeof(struct rm_clusterInfo));
   
   //First, contact the PBS queue
   status = pbs_statserver(connectionIdentifier,NULL,NULL);
   if(status != NULL)
   {
   //Loop over the list of attributes returned
      struct attrl* attributeList = status->attribs;
      while(attributeList != NULL)
      {
	  //Server_host for the CommonName element
         if(!strcmp(attributeList->name, "server_host"))
         {
            clusterInfo->CommonName = soap_strdup(soap,
			   attributeList->value);
         }
	 //Server_state for the IsAcceptingNewActivities element
         else if(!strcmp(attributeList->name, "server_state"))
         {
            if(!strcmp(attributeList->value, "Active"))
               clusterInfo->IsAcceptingNewActivities = true_;
            else
               clusterInfo->IsAcceptingNewActivities = false_;
         }//total_jobs for the TotalNumberOfActivities element
         else if(!strcmp(attributeList->name, "total_jobs"))
         {
            //clusterInfo->TotalNumberOfActivities = 
	//	    atoi(attributeList->value);
         }//pbs_version for the LocalResourceManagerType element
         else if(!strcmp(attributeList->name, "pbs_version"))
         {
            char* pbsStr = (char*) soap_malloc(soap, strlen(PBS) + 
			              strlen(attributeList->value) + 10);
	    sprintf(pbsStr, "%s %s %s", PBS, "Version", attributeList->value);
            clusterInfo->LocalResourceManagerType = pbsStr;
         }
         //fprintf(stderr,"Attribute: %s - Value: %s\n",attributeList->name,attributeList->value);
         attributeList = attributeList->next;
      }
   }
 
   pbs_statfree(status);
   pbs_disconnect(connectionIdentifier); 
   *clusterInf = clusterInfo;

   return BESE_OK;
}
Example #12
0
/** Creates DRMAA session and opens connection with DRM. */
int
drmaa_create(drmaa_session_t **pc, const char *contact, char *errmsg, size_t errlen)
  {
  drmaa_session_t *c;

  c = malloc(sizeof(drmaa_session_t));

  if (c == NULL)
    RAISE_NO_MEMORY();

  c->pbs_conn = -1;

  c->contact = NULL;

  c->jt_list = NULL;

  c->job_hashtab = NULL;

  c->next_time_label = 0;

  pthread_mutex_init(&c->conn_mutex, NULL);

  pthread_mutex_init(&c->jobs_mutex, NULL);

  c->jt_list = (drmaa_job_template_t*)malloc(sizeof(drmaa_job_template_t));

  if (c->jt_list == NULL)
    {
    drmaa_destroy(c, errmsg, errlen);
    RAISE_NO_MEMORY();
    }

  c->jt_list->next = c->jt_list->prev = c->jt_list;

  c->job_hashtab = (drmaa_job_t**)calloc(HASHTAB_SIZE, sizeof(drmaa_job_t*));

  if (c->job_hashtab == NULL)
    {
    drmaa_destroy(c, errmsg, errlen);
    RAISE_NO_MEMORY();
    }

  c->pbs_conn = pbs_connect((char*)contact);

  DEBUG(("pbs_connect(%s)=%d", contact, c->pbs_conn));

  if (c->pbs_conn < 0)
    {
    drmaa_destroy(c, errmsg, errlen);
    RAISE_PBS();
    }

  if (contact)
    c->contact = strdup(contact);
  else
    c->contact = strdup(pbs_server);

  if (c->contact == NULL)
    {
    drmaa_destroy(c, errmsg, errlen);
    RAISE_NO_MEMORY();
    }

  *pc = c;

  return DRMAA_ERRNO_SUCCESS;
  }
Example #13
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;
}