pbs_queue *que_alloc( char *name, int sv_qs_mutex_held) { static char *mem_err = "no memory"; int i; pbs_queue *pq; pq = (pbs_queue *)calloc(1, sizeof(pbs_queue)); if (pq == NULL) { log_err(errno, __func__, mem_err); return(NULL); } pq->qu_qs.qu_type = QTYPE_Unset; pq->qu_mutex = calloc(1, sizeof(pthread_mutex_t)); pq->qu_jobs = calloc(1, sizeof(struct all_jobs)); pq->qu_jobs_array_sum = calloc(1, sizeof(struct all_jobs)); if ((pq->qu_mutex == NULL) || (pq->qu_jobs == NULL) || (pq->qu_jobs_array_sum == NULL)) { log_err(ENOMEM, __func__, mem_err); return(NULL); } initialize_all_jobs_array(pq->qu_jobs); initialize_all_jobs_array(pq->qu_jobs_array_sum); pthread_mutex_init(pq->qu_mutex,NULL); lock_queue(pq, "que_alloc", NULL, LOGLEVEL); strncpy(pq->qu_qs.qu_name, name, PBS_MAXQUEUENAME); insert_queue(&svr_queues,pq); if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque++; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); /* set the working attributes to "unspecified" */ for (i = 0; i < QA_ATR_LAST; i++) { clear_attr(&pq->qu_attr[i], &que_attr_def[i]); } return(pq); } /* END que_alloc() */
void que_free( pbs_queue *pq, int sv_qs_mutex_held) { int i; pbs_attribute *pattr; attribute_def *pdef; /* remove any calloc working pbs_attribute space */ for (i = 0;i < QA_ATR_LAST;i++) { pdef = &que_attr_def[i]; pattr = &pq->qu_attr[i]; pdef->at_free(pattr); /* remove any acl lists associated with the queue */ if (pdef->at_type == ATR_TYPE_ACL) { pattr->at_flags |= ATR_VFLAG_MODIFY; save_acl(pattr, pdef, pdef->at_name, pq->qu_qs.qu_name); } } /* now free the main structure */ if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque--; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); free_user_info_holder(pq->qu_uih); remove_queue(&svr_queues, pq); pq->q_being_recycled = TRUE; insert_into_queue_recycler(pq); unlock_queue(pq, "que_free", NULL, LOGLEVEL); return; } /* END que_free() */
int req_stat_svr( struct batch_request *preq) /* ptr to the decoded request */ { svrattrl *pal; struct batch_reply *preply; struct brp_status *pstat; int bad = 0; char nc_buf[128]; int numjobs; int netrates[3]; memset(netrates, 0, sizeof(netrates)); /* update count and state counts from sv_numjobs and sv_jobstates */ lock_sv_qs_mutex(server.sv_qs_mutex, __func__); numjobs = server.sv_qs.sv_numjobs; unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); pthread_mutex_lock(server.sv_attr_mutex); server.sv_attr[SRV_ATR_TotalJobs].at_val.at_long = numjobs; server.sv_attr[SRV_ATR_TotalJobs].at_flags |= ATR_VFLAG_SET; pthread_mutex_lock(server.sv_jobstates_mutex); update_state_ct( &server.sv_attr[SRV_ATR_JobsByState], server.sv_jobstates, server.sv_jobstbuf); pthread_mutex_unlock(server.sv_jobstates_mutex); netcounter_get(netrates); snprintf(nc_buf, 127, "%d %d %d", netrates[0], netrates[1], netrates[2]); if (server.sv_attr[SRV_ATR_NetCounter].at_val.at_str != NULL) free(server.sv_attr[SRV_ATR_NetCounter].at_val.at_str); server.sv_attr[SRV_ATR_NetCounter].at_val.at_str = strdup(nc_buf); if (server.sv_attr[SRV_ATR_NetCounter].at_val.at_str != NULL) server.sv_attr[SRV_ATR_NetCounter].at_flags |= ATR_VFLAG_SET; pthread_mutex_unlock(server.sv_attr_mutex); /* allocate a reply structure and a status sub-structure */ preply = &preq->rq_reply; preply->brp_choice = BATCH_REPLY_CHOICE_Status; CLEAR_HEAD(preply->brp_un.brp_status); pstat = (struct brp_status *)calloc(1, sizeof(struct brp_status)); if (pstat == NULL) { reply_free(preply); req_reject(PBSE_SYSTEM, 0, preq, NULL, NULL); pthread_mutex_unlock(server.sv_attr_mutex); return(PBSE_SYSTEM); } CLEAR_LINK(pstat->brp_stlink); strcpy(pstat->brp_objname, server_name); pstat->brp_objtype = MGR_OBJ_SERVER; CLEAR_HEAD(pstat->brp_attr); append_link(&preply->brp_un.brp_status, &pstat->brp_stlink, pstat); /* add attributes to the status reply */ pal = (svrattrl *)GET_NEXT(preq->rq_ind.rq_status.rq_attr); if (status_attrib( pal, svr_attr_def, server.sv_attr, SRV_ATR_LAST, preq->rq_perm, &pstat->brp_attr, &bad, 1)) /* IsOwner == TRUE */ { reply_badattr(PBSE_NOATTR, bad, pal, preq); } else { reply_send_svr(preq); } return(PBSE_NONE); } /* END req_stat_svr() */
int svr_save_xml( struct server *ps, int mode) { char buf[MAXLINE<<8]; int fds; int rc = PBSE_NONE; int len; time_t time_now = time(NULL); char *tmp_file = NULL; int tmp_file_len = 0; char log_buf[LOCAL_LOG_BUF_SIZE + 1]; tmp_file_len = strlen(path_svrdb) + 5; if ((tmp_file = (char *)calloc(sizeof(char), tmp_file_len)) == NULL) { rc = PBSE_MEM_MALLOC; return(rc); } else { snprintf(tmp_file, tmp_file_len - 1, "%s.tmp", path_svrdb); } lock_sv_qs_mutex(server.sv_qs_mutex, __func__); fds = open(tmp_file, O_WRONLY | O_CREAT | O_Sync | O_TRUNC, 0600); if (fds < 0) { sprintf(log_buf, "%s:1", __func__); unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); log_err(errno, __func__, msg_svdbopen); free(tmp_file); return(-1); } /* write the sv_qs info */ snprintf(buf,sizeof(buf), "<server_db>\n<numjobs>%d</numjobs>\n<numque>%d</numque>\n<nextjobid>%d</nextjobid>\n<savetime>%ld</savetime>\n", ps->sv_qs.sv_numjobs, ps->sv_qs.sv_numque, ps->sv_qs.sv_jobidnumber, time_now); len = strlen(buf); if ((rc = write_buffer(buf,len,fds))) { sprintf(log_buf, "%s:2", __func__); unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); free(tmp_file); close(fds); return(rc); } if ((rc = save_attr_xml(svr_attr_def,ps->sv_attr,SRV_ATR_LAST,fds)) != 0) { sprintf(log_buf, "%s:3", __func__); unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); free(tmp_file); close(fds); return(rc); } /* close the server_db */ snprintf(buf,sizeof(buf),"</server_db>"); if ((rc = write_buffer(buf,strlen(buf),fds))) { sprintf(log_buf, "%s:4", __func__); unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); close(fds); free(tmp_file); return(rc); } /* Some write() errors may not show up until the close() */ if ((rc = close(fds)) != 0) { log_err(rc, __func__, "PBS got an error closing the serverdb file"); sprintf(log_buf, "%s:5", __func__); unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); free(tmp_file); return(rc); } if ((rc = rename(tmp_file, path_svrdb)) == -1) { rc = PBSE_CAN_NOT_MOVE_FILE; } sprintf(log_buf, "%s:5", __func__); unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); free(tmp_file); return(PBSE_NONE); } /* END svr_save_xml */
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,¤t)) { /* 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 svr_recov( char *svrfile, /* I */ int read_only) /* I */ { int i; int sdb; char log_buf[LOCAL_LOG_BUF_SIZE]; void recov_acl(pbs_attribute *, attribute_def *, const char *, const char *); sdb = open(svrfile, O_RDONLY, 0); if (sdb < 0) { if (errno == ENOENT) { char tmpLine[LOG_BUF_SIZE]; snprintf(tmpLine, sizeof(tmpLine), "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__, tmpLine); } else { log_err(errno, __func__, msg_svdbopen); } return(-1); } /* read in server structure */ lock_sv_qs_mutex(server.sv_qs_mutex, __func__); i = read_ac_socket(sdb, (char *) & server.sv_qs, sizeof(server_qs)); if (i != sizeof(server_qs)) { unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); if (i < 0) log_err(errno, __func__, "read of serverdb failed"); else log_err(errno, __func__, "short read of serverdb"); close(sdb); return(-1); } /* Save the sv_jobidnumber field in case it is set by the attributes. */ i = server.sv_qs.sv_jobidnumber; /* read in server attributes */ if (recov_attr( sdb, &server, svr_attr_def, server.sv_attr, SRV_ATR_LAST, 0, !read_only) != 0 ) { unlock_sv_qs_mutex(server.sv_qs_mutex, log_buf); log_err(errno, __func__, "error on recovering server attr"); close(sdb); return(-1); } /* Restore the current job number and make it visible in qmgr print server commnad. */ if (!read_only) { server.sv_qs.sv_jobidnumber = i; server.sv_attr[SRV_ATR_NextJobNumber].at_val.at_long = i; server.sv_attr[SRV_ATR_NextJobNumber].at_flags |= ATR_VFLAG_SET| ATR_VFLAG_MODIFY; } unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); close(sdb); /* recover the server various acls from their own files */ for (i = 0;i < SRV_ATR_LAST;i++) { if (server.sv_attr[i].at_type == ATR_TYPE_ACL) { recov_acl( &server.sv_attr[i], &svr_attr_def[i], PBS_SVRACL, svr_attr_def[i].at_name); if ((!read_only) && (svr_attr_def[i].at_action != (int (*)(pbs_attribute*, void*, int))0)) { svr_attr_def[i].at_action( &server.sv_attr[i], &server, ATR_ACTION_RECOV); } } } /* END for (i) */ return(PBSE_NONE); } /* END svr_recov() */
pbs_queue *que_alloc( char *name, int sv_qs_mutex_held) { static char *mem_err = (char *)"no memory"; int i; pbs_queue *pq; pq = (pbs_queue *)calloc(1, sizeof(pbs_queue)); if (pq == NULL) { log_err(errno, __func__, mem_err); return(NULL); } pq->qu_qs.qu_type = QTYPE_Unset; pq->qu_mutex = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t)); pq->qu_jobs = (struct all_jobs *)calloc(1, sizeof(struct all_jobs)); pq->qu_jobs_array_sum = (struct all_jobs *)calloc(1, sizeof(struct all_jobs)); if ((pq->qu_mutex == NULL) || (pq->qu_jobs == NULL) || (pq->qu_jobs_array_sum == NULL)) { log_err(ENOMEM, __func__, mem_err); free(pq); return(NULL); } initialize_all_jobs_array(pq->qu_jobs); initialize_all_jobs_array(pq->qu_jobs_array_sum); pthread_mutex_init(pq->qu_mutex,NULL); lock_queue(pq, __func__, NULL, LOGLEVEL); snprintf(pq->qu_qs.qu_name, sizeof(pq->qu_qs.qu_name), "%s", name); insert_queue(&svr_queues,pq); if (sv_qs_mutex_held == FALSE) lock_sv_qs_mutex(server.sv_qs_mutex, __func__); server.sv_qs.sv_numque++; if (sv_qs_mutex_held == FALSE) unlock_sv_qs_mutex(server.sv_qs_mutex, __func__); /* set up the user info struct */ pq->qu_uih = (user_info_holder *)calloc(1, sizeof(user_info_holder)); initialize_user_info_holder(pq->qu_uih); /* set the working attributes to "unspecified" */ for (i = 0; i < QA_ATR_LAST; i++) { clear_attr(&pq->qu_attr[i], &que_attr_def[i]); } /* Set route_retry_thread_id in case this is a routing queue */ pq->route_retry_thread_id = -1; return(pq); } /* END que_alloc() */