/** * Request Process JOB_DESTROY */ static int ngislRequestProcessJobDestroy( ngisiContext_t *context, char *requestName, char *id, int *error) { static const char fName[] = "ngislRequestProcessJobDestroy"; int result; /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Process %s.\n", requestName); result = ngisiJobProtocolDestroy(context, id, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Job Destroy process failed.\n"); return 0; } result = ngislReply(context, NGISI_REPLY_SUCCESS, NULL, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Reply Job Destroy failed.\n"); return 0; } /* Success */ return 1; }
/** * Request Process EXIT */ static int ngislRequestProcessExit( ngisiContext_t *context, char *requestName, int *error) { static const char fName[] = "ngislRequestProcessExit"; int result; /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Process %s.\n", requestName); result = ngislReply(context, NGISI_REPLY_SUCCESS, NULL, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Reply Exit failed.\n"); /* Not return */ } result = ngislRequestReaderStatusSet( context, NGISI_REQUEST_READER_STATUS_DONE, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Set the Request Reader status failed.\n"); } /* Success */ return 1; }
/** * Request Reader Initialize */ int ngisiRequestReaderInitialize( ngisiContext_t *context, ngisiRequestReader_t *requestReader, int *error) { static const char fName[] = "ngisiRequestReaderInitialize"; int result; /* Check the arguments */ assert(context != NULL); assert(requestReader != NULL); ngislRequestReaderInitializeMember(requestReader); result = ngisiMutexInitialize( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Initialize the mutex failed.\n"); return 0; } requestReader->ngisr_mutexInitialized = 1; result = ngisiCondInitialize( context, &requestReader->ngisr_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Initialize the cond failed.\n"); return 0; } requestReader->ngisr_condInitialized = 1; result = ngisiReadBufferInitialize( context, &requestReader->ngisr_readBuffer, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Initialize the Read Buffer failed.\n"); return 0; } result = ngislRequestReaderStatusSet( context, NGISI_REQUEST_READER_STATUS_INITIALIZED, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Set the Request Reader Status failed.\n"); return 0; } result = ngislRequestReaderStart(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Start the Request Reader failed.\n"); return 0; } /* Success */ return 1; }
/** * Reply Status */ static int ngislReplyStatus( ngisiContext_t *context, ngisiReplyResult_t replyResult, ngisiJobStatus_t status, char *message, int *error) { static const char fName[] = "ngislReplyStatus"; char buf[NGISI_PROTOCOL_MAX]; char *statusString; int result; /* Check the arguments */ assert(context != NULL); statusString = NULL; buf[0] = '\0'; if (replyResult == NGISI_REPLY_SUCCESS) { if (message != NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_WARNING, fName, "Reply success requires no message : \"%s\".\n", message); } result = ngislReplyNotifyJobStatusGetString( context, status, &statusString, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Get string for status failed\n"); return 0; } assert(statusString != NULL); snprintf(buf, sizeof(buf), "S %s", statusString); } else if (replyResult == NGISI_REPLY_FAIL) { snprintf(buf, sizeof(buf), "F%s%s", ((message != NULL) ? " " : ""), ((message != NULL) ? message : "")); } else { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Invalid Reply Result %d\n", replyResult); return 0; } /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Send Reply \"%s\".\n", buf); fprintf(NGISI_FP_REPLY, "%s%s", buf, NGISI_LINE_TERMINATOR_STR); /* Success */ return 1; }
/** * Request Reader Finalize */ int ngisiRequestReaderFinalize( ngisiContext_t *context, ngisiRequestReader_t *requestReader, int *error) { static const char fName[] = "ngisiRequestReaderFinalize"; int result; /* Check the arguments */ assert(context != NULL); assert(requestReader != NULL); result = ngislRequestReaderStopCheck(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Stop check failed.\n"); return 0; } result = ngisiReadBufferFinalize( context, &requestReader->ngisr_readBuffer, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Finalize the Read Buffer failed.\n"); return 0; } if (requestReader->ngisr_condInitialized != 0) { requestReader->ngisr_condInitialized = 0; result = ngisiCondFinalize( context, &requestReader->ngisr_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Finalize the cond failed.\n"); return 0; } } if (requestReader->ngisr_mutexInitialized != 0) { requestReader->ngisr_mutexInitialized = 0; result = ngisiMutexFinalize( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Finalize the mutex failed.\n"); return 0; } } ngislRequestReaderInitializeMember(requestReader); /* Success */ return 1; }
/** * RefreshCredential: Finalize. */ int ngisiRefreshCredentialFinalize( ngisiContext_t *context, ngisiRefreshCredential_t *ref, int *error) { static const char fName[] = "ngisiRefreshCredentialFinalize"; int result; /* Check the arguments */ assert(context != NULL); assert(ref != NULL); if ((ref->ngisrc_continue != 0) || (ref->ngisrc_working != 0)) { result = ngislRefreshCredentialThreadStop( context, ref, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Can't stop the Refresh Credential thread.\n"); return 0; } } if (ref->ngisrc_condInitialized != 0) { ref->ngisrc_condInitialized = 0; result = ngisiCondFinalize( context, &ref->ngisrc_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Finalize the cond failed.\n"); return 0; } } if (ref->ngisrc_mutexInitialized != 0) { ref->ngisrc_mutexInitialized = 0; result = ngisiMutexFinalize( context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Finalize the mutex failed.\n"); return 0; } } ngislRefreshCredentialInitializeMember(ref); /* Success */ return 1; }
/** * Request Reader Start */ static int ngislRequestReaderStart( ngisiContext_t *context, int *error) { static const char fName[] = "ngislRequestReaderStart"; ngisiRequestReader_t *requestReader; int result; /* Check the arguments */ assert(context != NULL); requestReader = &context->ngisc_requestReader; requestReader->ngisr_continue = 1; requestReader->ngisr_working = 1; /* Create the Request Reader thread */ result = globus_thread_create( &requestReader->ngisr_thread, NULL, ngislRequestReaderThread, context); if (result != 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Create the Request Reader thread failed.\n"); return 0; } /* Success */ return 1; }
/** * Notify Job Status */ int ngisiNotifyJobStatus( ngisiContext_t *context, char *jobID, ngisiJobStatus_t status, char *message, int *error) { static const char fName[] = "ngisiNotifyJobStatus"; char buf[NGISI_PROTOCOL_MAX]; char *statusString; int result; /* Check the arguments */ assert(context != NULL); assert(jobID != NULL); statusString = NULL; result = ngislReplyNotifyJobStatusGetString( context, status, &statusString, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Get string for status failed\n"); return 0; } assert(statusString != NULL); snprintf(buf, sizeof(buf), "STATUS_NOTIFY %s %s%s%s", jobID, statusString, (message != NULL ? " " : ""), (message != NULL ? message : "")); /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Send Notify \"%s\".\n", buf); fprintf(NGISI_FP_NOTIFY,"%s%s", buf, NGISI_LINE_TERMINATOR_STR); /* Success */ return 1; }
/** * Reply */ static int ngislReply( ngisiContext_t *context, ngisiReplyResult_t replyResult, char *message, int *error) { static const char fName[] = "ngislReply"; char buf[NGISI_PROTOCOL_MAX]; /* Check the arguments */ assert(context != NULL); buf[0] = '\0'; if (replyResult == NGISI_REPLY_SUCCESS) { if (message != NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_WARNING, fName, "Reply success requires no message : \"%s\".\n", message); } snprintf(buf, sizeof(buf), "S"); } else if (replyResult == NGISI_REPLY_FAIL) { snprintf(buf, sizeof(buf), "F%s%s", ((message != NULL) ? " " : ""), ((message != NULL) ? message : "")); } else { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Invalid Reply Result %d\n", replyResult); return 0; } /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Send Reply \"%s\".\n", buf); fprintf(NGISI_FP_REPLY, "%s%s", buf, NGISI_LINE_TERMINATOR_STR); /* Success */ return 1; }
/** * RefreshCredential: Initialize. */ int ngisiRefreshCredentialInitialize( ngisiContext_t *context, ngisiRefreshCredential_t *ref, int *error) { static const char fName[] = "ngisiRefreshCredentialInitialize"; int result; /* Check the arguments */ assert(context != NULL); assert(ref != NULL); ngislRefreshCredentialInitializeMember(ref); result = ngisiMutexInitialize( context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Initialize the mutex failed.\n"); return 0; } ref->ngisrc_mutexInitialized = 1; result = ngisiCondInitialize( context, &ref->ngisrc_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Initialize the cond failed.\n"); return 0; } ref->ngisrc_condInitialized = 1; ref->ngisrc_updateInterval = 0; /* disable at first */ ref->ngisrc_continue = 0; /* Thread is not invoked at first. */ ref->ngisrc_working = 0; /* Success */ return 1; }
/** * RefreshCredential: Start thread. */ static int ngislRefreshCredentialThreadStart( ngisiContext_t *context, ngisiRefreshCredential_t *ref, int *error) { static const char fName[] = "ngislRefreshCredentialThreadStart"; int result; /* Check the arguments */ assert(context != NULL); assert(ref != NULL); if (ref->ngisrc_working != 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Refresh Credential thread is already working.\n"); return 0; } /* Check if the flag is valid */ assert(ref->ngisrc_continue == 0); ref->ngisrc_continue = 1; /* TRUE */ /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Create the Refresh Credential thread.\n"); /* Create the Refresh Credential thread */ result = globus_thread_create( &ref->ngisrc_thread, NULL, ngislRefreshCredentialThread, context); if (result != 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Can't create thread for Refresh Credential.\n"); return 0; } /* Success */ return 1; }
/** * Request Reader Stop Check */ static int ngislRequestReaderStopCheck( ngisiContext_t *context, int *error) { static const char fName[] = "ngislRequestReaderStopCheck"; ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Request Reader Stop check not implemented.\n"); /* Success */ return 1; }
static void ngislCheckAttribute( ngisiContext_t *context, char *attr) { int i; static const char fName[] = "ngislCheckAttribute"; for (i = 0; ngislAttributeTable[i].attr != NULL; i++) { if (strcmp(attr, ngislAttributeTable[i].attr) == 0) { break; } } if (ngislAttributeTable[i].attr == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_WARNING, fName, "Unknown attribute \"%s\".\n", attr); } else if (ngislAttributeTable[i].supported == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_WARNING, fName, "Unsupported attribute \"%s\".\n", attr); } }
/** * Request Reader Thread */ static void * ngislRequestReaderThread( void *threadArgument) { static const char fName[] = "ngislRequestReaderThread"; ngisiRequestReader_t *requestReader; ngisiContext_t *context; int *error, errorEntity; int result; assert(threadArgument != NULL); context = (ngisiContext_t *)threadArgument; requestReader = &context->ngisc_requestReader; error = &errorEntity; /* Check if the flag is valid */ assert(requestReader->ngisr_continue != 0); requestReader->ngisr_working = 1; /* Process Request Reader */ result = ngislRequestReaderProcess(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process the Request Reader failed.\n"); } result = ngisiRequestReaderSetDone(context, error); requestReader->ngisr_working = 0; if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Set done to the Request Reader failed.\n"); } /* Success */ return NULL; }
/** * Request Process Unknown2 */ static int ngislRequestProcessDefault( ngisiContext_t *context, int *error) { static const char fName[] = "ngislRequestProcessDefault"; /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Process Unknown.\n"); /* Success */ return 1; }
/** * Request Process Connection Closed */ static int ngislRequestProcessClosed( ngisiContext_t *context, int *error) { static const char fName[] = "ngislRequestProcessClosed"; int result; /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Process Connection Closed.\n"); result = ngislRequestReaderStatusSet( context, NGISI_REQUEST_READER_STATUS_DONE, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Set the Request Reader status failed.\n"); return 0; } /* Success */ return 1; }
static int ngislReplyNotifyJobStatusGetString( ngisiContext_t *context, ngisiJobStatus_t status, char **statusString, int *error) { static const char fName[] = "ngislReplyNotifyJobStatusGetString"; /* Check the arguments */ assert(context != NULL); assert(statusString != NULL); *statusString = "unknown"; switch(status) { case NGISI_JOB_STATUS_INITIALIZED: *statusString = "PENDING"; /* Report as PENDING */ break; case NGISI_JOB_STATUS_PENDING: *statusString = "PENDING"; break; case NGISI_JOB_STATUS_ACTIVE: *statusString = "ACTIVE"; break; case NGISI_JOB_STATUS_FAILED: *statusString = "FAILED"; break; case NGISI_JOB_STATUS_DONE: *statusString = "DONE"; break; default: ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unknown Job Status %d.\n", status); return 0; } /* Success */ return 1; }
/** * Request Reader Set Done */ int ngisiRequestReaderSetDone( ngisiContext_t *context, int *error) { static const char fName[] = "ngisiRequestReaderSetDone"; ngisiRequestReader_t *requestReader; int result, locked; /* Check the arguments */ assert(context != NULL); requestReader = &context->ngisc_requestReader; locked = 0; /* Lock the mutex */ result = ngisiMutexLock( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the Request Reader mutex failed.\n"); goto error; } locked = 1; /* Set done */ requestReader->ngisr_working = 0; result = ngisiCondBroadcast(context, &requestReader->ngisr_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Broadcast the cond failed.\n"); goto error; } /* Unlock the mutex */ locked = 0; result = ngisiMutexUnlock( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the Request Reader mutex failed.\n"); goto error; } /* Success */ return 1; /* Error occurred */ error: if (locked != 0) { locked = 0; result = ngisiMutexUnlock( context, &requestReader->ngisr_mutex, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the Request Reader mutex failed.\n"); } } /* Failed */ return 0; }
/** * RefreshCredential: Set update interval. */ int ngisiRefreshCredentialUpdateIntervalSet( ngisiContext_t *context, ngisiRefreshCredential_t *ref, int newInterval, int *error) { static const char fName[] = "ngisiRefreshCredentialUpdateIntervalSet"; time_t oldEventTime, nextEventTime; int mutexLocked, result, notify; int oldInterval; /* Check the arguments */ assert(context != NULL); assert(ref != NULL); mutexLocked = 0; notify = 0; /* Lock */ result = ngisiMutexLock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the mutex failed.\n"); goto error; } mutexLocked = 1; oldInterval = ref->ngisrc_updateInterval; oldEventTime = ref->ngisrc_nextEventTime; if (newInterval != oldInterval) { notify = 1; if (newInterval > 0) { ref->ngisrc_updateInterval = newInterval; /* Select the early event time */ nextEventTime = time(NULL) + newInterval; if ((oldEventTime > 0) && (nextEventTime > oldEventTime)) { nextEventTime = oldEventTime; } ref->ngisrc_nextEventTime = nextEventTime; } else { ref->ngisrc_updateInterval = 0; ref->ngisrc_nextEventTime = 0; } /* Notify signal */ result = ngisiCondBroadcast( context, &ref->ngisrc_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Cond signal failed.\n"); goto error; } } /* Unlock */ mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); goto error; } if ((notify != 0) && (newInterval > 0) && (ref->ngisrc_continue == 0) && (ref->ngisrc_working == 0)) { /* Start the thread */ result = ngislRefreshCredentialThreadStart( context, ref, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Can't start the Refresh Credential thread.\n"); goto error; } } /* Success */ return 1; /* Error occurred */ error: /* Unlock */ if (mutexLocked != 0) { mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); return 0; } } /* Failed */ return 0; }
/** * Request Process */ static int ngislRequestProcess( ngisiContext_t *context, ngisiRequestType_t request, char *requestName, char *id, ngisiCreateAttr_t *attrs, int *finish, int *error) { static const char fName[] = "ngislRequestProcess"; int result; /* Check the arguments */ assert(context != NULL); assert(finish != NULL); *finish = 0; switch(request) { case NGISI_REQUEST_JOB_CREATE: result = ngislRequestProcessJobCreate( context, requestName, id, attrs, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Job Create failed.\n"); return 0; } break; case NGISI_REQUEST_JOB_STATUS: result = ngislRequestProcessJobStatus( context, requestName, id, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Job Status failed.\n"); return 0; } break; case NGISI_REQUEST_JOB_DESTROY: result = ngislRequestProcessJobDestroy( context, requestName, id, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Job Destroy failed.\n"); return 0; } break; case NGISI_REQUEST_QUERY_FEATURES: result = ngislRequestProcessQueryFeatures( context, requestName, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Query Features failed.\n"); return 0; } break; case NGISI_REQUEST_EXIT: result = ngislRequestProcessExit( context, requestName, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Exit failed.\n"); return 0; } *finish = 1; break; case NGISI_REQUEST_CLOSED: result = ngislRequestProcessClosed(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Connection closed failed.\n"); return 0; } *finish = 1; break; case NGISI_REQUEST_UNKNOWN: result = ngislRequestProcessUnknown(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Unknown failed.\n"); return 0; } break; default: result = ngislRequestProcessDefault(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process Default failed.\n"); return 0; } break; } /* Success */ return 1; }
/** * Create Attr Add */ static int ngislCreateAttrAdd( ngisiContext_t *context, ngisiCreateAttr_t *attr, char *name, char *value, int *error) { static const char fName[] = "ngislCreateAttrAdd"; ngisiCreateAttrElement_t *newArray, *oldArray; int i, newArraySize; char *newName, *newValue; /* Check the arguments */ assert(context != NULL); assert(attr != NULL); assert(name != NULL); /* Check and renew size */ if (attr->ngisca_nAttrs >= attr->ngisca_arraySize) { oldArray = attr->ngisca_attrs; if (attr->ngisca_arraySize <= 0) { newArraySize = NGISI_CREATE_ATTR_INITIAL_SIZE; } else { newArraySize = attr->ngisca_arraySize * 2; } newArray = (ngisiCreateAttrElement_t *)globus_libc_calloc( newArraySize, sizeof(ngisiCreateAttrElement_t)); if (newArray == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Allocate the Create Attr failed.\n"); return 0; } for (i = 0; i < newArraySize; i++) { newArray[i].ngisce_name = NULL; newArray[i].ngisce_value = NULL; newArray[i].ngisce_treated = 0; } for (i = 0; i < attr->ngisca_arraySize; i++) { newArray[i].ngisce_name = oldArray[i].ngisce_name; newArray[i].ngisce_value = oldArray[i].ngisce_value; newArray[i].ngisce_treated = oldArray[i].ngisce_treated; } attr->ngisca_arraySize = newArraySize; attr->ngisca_attrs = newArray; if (oldArray != NULL) { globus_libc_free(oldArray); } } assert(attr->ngisca_nAttrs < attr->ngisca_arraySize); /* Add to tail */ i = attr->ngisca_nAttrs; newName = strdup(name); if (newName == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "strdup(new name) failed.\n"); return 0; } if (value != NULL) { newValue = strdup(value); if (newValue == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "strdup(new value) failed.\n"); return 0; } } else { newValue = NULL; } attr->ngisca_attrs[i].ngisce_name = newName; attr->ngisca_attrs[i].ngisce_value = newValue; attr->ngisca_nAttrs++; /* Success */ return 1; }
/** * Request Attributes Read */ static int ngislRequestAttributesRead( ngisiContext_t *context, FILE *readFp, ngisiReadBuffer_t *readBuffer, ngisiCreateAttr_t **attrs, int *error) { static const char fName[] = "ngislRequestAttributesRead"; char attrNameBuf[NGISI_CREATE_ATTR_STR_MAX]; ngisiCreateAttr_t *newAttr; int result, finish, cur; char *p, *strStart; /* Check the arguments */ assert(context != NULL); assert(readFp != NULL); assert(readBuffer != NULL); assert(attrs != NULL); *attrs = NULL; newAttr = (ngisiCreateAttr_t *)globus_libc_calloc( 1, sizeof(ngisiCreateAttr_t)); if (newAttr == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Allocate the Create Attr failed.\n"); return 0; } result = ngislCreateAttrInitialize(context, newAttr, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Initialize the Create Attr failed.\n"); return 0; } *attrs = newAttr; finish = 0; do { result = ngisiReadLine( context, NGISI_FP_REQUEST, readBuffer, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Read line failed.\n"); return 0; } /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Read Attr \"%s\".\n", readBuffer->ngisrb_buf); strStart = readBuffer->ngisrb_buf; p = strStart; cur = 0; /* Get attribute name */ while (isgraph((int)*p)) { attrNameBuf[cur] = *p; p++; cur++; } attrNameBuf[cur] = '\0'; cur = 0; if (strlen(attrNameBuf) == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Attr name is 0.\n"); return 0; } /* Find the value */ while (isspace((int)*p)) { p++; } if (strcmp(attrNameBuf, NGISI_CREATE_ATTR_END) == 0) { finish = 1; } else { ngislCheckAttribute(context, attrNameBuf); result = ngislCreateAttrAdd( context, newAttr, attrNameBuf, p, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Add the Create Attr failed.\n"); return 0; } } } while (finish == 0); /* Success */ return 1; }
/** * Request Read */ static int ngislRequestRead( ngisiContext_t *context, ngisiRequestType_t *request, char **requestName, char **id, ngisiCreateAttr_t **attrs, int *error) { static const char fName[] = "ngislRequestRead"; char idBuf[NGISI_ID_STR_MAX], *requestStr, *returnIdStr, *p; ngisiReadBuffer_t *readBuffer; int result, found, i, idCur; /* Check the arguments */ assert(context != NULL); assert(request != NULL); assert(requestName != NULL); assert(id != NULL); assert(attrs != NULL); *request = NGISI_REQUEST_UNDEFINED; *requestName = NULL; *id = NULL; *attrs = NULL; p = NULL; requestStr = NULL; readBuffer = &context->ngisc_requestReader.ngisr_readBuffer; result = ngisiReadLine( context, NGISI_FP_REQUEST, readBuffer, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Read the Line failed.\n"); return 0; } if (readBuffer->ngisrb_buf == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Read buffer is NULL.\n"); return 0; } /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Read Request \"%s\".\n", readBuffer->ngisrb_buf); found = 0; for (i = 0; ngislRequestTypeTable[i].ngisrt_valid != 0; i++) { if (strncmp(readBuffer->ngisrb_buf, ngislRequestTypeTable[i].ngisrt_name, strlen(ngislRequestTypeTable[i].ngisrt_name)) == 0) { *request = ngislRequestTypeTable[i].ngisrt_type; requestStr = ngislRequestTypeTable[i].ngisrt_name; *requestName = requestStr; found = 1; break; } } if (found == 0) { if (readBuffer->ngisrb_reachEOF != 0) { *request = NGISI_REQUEST_CLOSED; } else { *request = NGISI_REQUEST_UNKNOWN; } return 1; } assert(found != 0); if ((*request == NGISI_REQUEST_EXIT) || (*request == NGISI_REQUEST_QUERY_FEATURES)) { return 1; } p = readBuffer->ngisrb_buf; p += strlen(requestStr); /* Skip space */ while (isspace((int)*p)) { p++; } /* Get the ID */ idCur = 0; while (isgraph((int)*p)) { idBuf[idCur] = *p; idCur++; p++; if (idCur + 1 >= NGISI_ID_STR_MAX) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "id overflow (%d chars).\n", NGISI_ID_STR_MAX); return 0; } } idBuf[idCur] = '\0'; if (idCur > 0) { returnIdStr = strdup(idBuf); if (returnIdStr == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "strdup(ID str) failed.\n"); return 0; } *id = returnIdStr; } else { *id = NULL; } if (*request == NGISI_REQUEST_JOB_CREATE) { result = ngislRequestAttributesRead( context, NGISI_FP_REQUEST, readBuffer, attrs, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Read the Request Attributes failed.\n"); return 0; } } /* Success */ return 1; }
/** * Request Reader Process */ static int ngislRequestReaderProcess( ngisiContext_t *context, int *error) { static const char fName[] = "ngislRequestReaderProcess"; ngisiRequestType_t request; ngisiCreateAttr_t *attrs; char *requestName, *id; int result, finish; /* Check the arguments */ assert(context != NULL); /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Request Reader started working.\n"); /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Request Reader pid=%ld.\n", (long)getpid()); do { requestName = NULL; id = NULL; attrs = NULL; finish = 0; result = ngislRequestRead( context, &request, &requestName, &id, &attrs, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Read the Request failed.\n"); goto error; } finish = 0; result = ngislRequestProcess( context, request, requestName, id, attrs, &finish, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Process the Request failed.\n"); /* Return to loop */ } requestName = NULL; if (id != NULL) { globus_libc_free(id); id = NULL; } if (attrs != NULL) { result = ngislCreateAttrFinalize(context, attrs, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Finalize the Create Attr failed.\n"); goto error; } globus_libc_free(attrs); attrs = NULL; } } while (finish == 0); /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Request Reader thread exiting.\n"); /* Success */ return 1; /* Error occurred */ error: /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Request Reader thread exiting by error.\n"); /* Failed */ return 0; }
/** * Notify Job Create */ static int ngislNotifyJobCreate( ngisiContext_t *context, char *requestID, ngisiReplyResult_t replyResult, char *jobID, char *message, int *error) { static const char fName[] = "ngislNotifyJobCreate"; char buf[NGISI_PROTOCOL_MAX]; size_t length; /* Check the arguments */ assert(context != NULL); assert(requestID != NULL); length = 0; length += snprintf(&buf[length], sizeof(buf) - length, "CREATE_NOTIFY %s ", requestID); if (replyResult == NGISI_REPLY_SUCCESS) { if (jobID == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "JobID is NULL.\n"); return 0; } if (message != NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_WARNING, fName, "Notify success requires no message : \"%s\".\n", message); } length += snprintf(&buf[length], sizeof(buf) - length, "S %s", jobID); } else if (replyResult == NGISI_REPLY_FAIL) { if (jobID != NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_WARNING, fName, "Notify failure requires no jobID : \"%s\".\n", jobID); } length += snprintf(&buf[length], sizeof(buf) - length, "F%s%s", ((message != NULL) ? " " : ""), ((message != NULL) ? message : "")); } else { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Invalid Reply Result %d\n", replyResult); return 0; } /* log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Send Notify \"%s\".\n", buf); fprintf(NGISI_FP_NOTIFY,"%s%s", buf, NGISI_LINE_TERMINATOR_STR); /* Success */ return 1; }
/** * RefreshCredential: Stop thread. */ static int ngislRefreshCredentialThreadStop( ngisiContext_t *context, ngisiRefreshCredential_t *ref, int *error) { static const char fName[] = "ngislRefreshCredentialThreadStop"; int mutexLocked, result; /* Check the arguments */ assert(context != NULL); assert(ref != NULL); mutexLocked = 0; if (ref->ngisrc_working == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Refresh Credential thread is already stopped.\n"); return 1; } /* Check if the flag is valid */ assert(ref->ngisrc_continue != 0); /** * Tell the Refresh Credential thread to stop. */ /* Lock */ result = ngisiMutexLock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the mutex failed.\n"); goto error; } mutexLocked = 1; /* Set the status */ ref->ngisrc_continue = 0; /* to stop */ /* Notify signal */ result = ngisiCondBroadcast( context, &ref->ngisrc_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Cond signal failed.\n"); goto error; } /** * Suppress unlock and lock, to ignore CondBroadcast(!working) issue, * before the CondWait(!working) starts its proceedings. */ /** * Wait the Refresh Credential thread to stop. */ /* Suppress lock. already locked */ /* Wait the status */ while (ref->ngisrc_working != 0) { result = ngisiCondWait(context, &ref->ngisrc_cond, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Can't wait the Condition Variable.\n"); goto error; } } /* Unlock */ mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); goto error; } /* Success */ return 1; /* Error occurred */ error: /* Unlock */ if (mutexLocked != 0) { mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); return 0; } } /* Failed */ return 0; }
/** * RefreshCredential: thread. */ static void * ngislRefreshCredentialThread( void *arg) { static const char fName[] = "ngislRefreshCredentialThread"; ngisiRefreshCredential_t *ref; int sleepSec, wasTimeout; ngisiContext_t *context; int mutexLocked, result; int *error, errorEntity; time_t now, timeoutTime; /* Check the arguments */ assert(arg != NULL); context = (ngisiContext_t *)arg; ref = &context->ngisc_refreshCredential; error = &errorEntity; *error = 0; /* Check if the flag is valid */ assert(ref->ngisrc_working == 0); mutexLocked = 0; ref->ngisrc_working = 1; /* TRUE */ sleepSec = 0; /* Log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Refresh Credential thread invoked.\n"); /* Lock */ result = ngisiMutexLock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the mutex failed.\n"); goto error; } mutexLocked = 1; sleepSec = 0; /* Do not wait at first. */ /* Wait the status */ while (ref->ngisrc_continue != 0) { wasTimeout = 0; /* Wait the event time. */ if (sleepSec > 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "sleep %d sec for next update.\n", sleepSec); result = ngisiCondTimedWait(context, &ref->ngisrc_cond, &ref->ngisrc_mutex, sleepSec, &wasTimeout, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Can't wait the Condition Variable.\n"); goto error; } } now = time(NULL); timeoutTime = ref->ngisrc_nextEventTime; /* Perform refresh credential. */ if ((timeoutTime > 0) && (now >= timeoutTime)) { result = ngisiJobRefreshCredential(context, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Update the credential failed.\n"); /* Not return */ } if (ref->ngisrc_updateInterval > 0) { ref->ngisrc_nextEventTime = now + ref->ngisrc_updateInterval; } else { ref->ngisrc_nextEventTime = 0; } } timeoutTime = ref->ngisrc_nextEventTime; sleepSec = NGISL_REFRESH_CREDENTIALS_SLEEP_WAIT_SEC; if (timeoutTime > 0) { sleepSec = timeoutTime - time(NULL); } } /* Unlock */ mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); goto error; } /** * Tell the Main Thread that, Refresh Credential thread was stopped. */ /* Log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Refresh Credential thread exiting.\n"); /* Lock */ result = ngisiMutexLock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the mutex failed.\n"); goto error; } mutexLocked = 1; /* Set the status */ ref->ngisrc_working = 0; /* FALSE */ /* Notify signal */ result = ngisiCondBroadcast( context, &ref->ngisrc_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Cond signal failed.\n"); goto error; } /* Unlock */ mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); goto error; } /* Success */ return NULL; /* Error occurred */ error: /* Set the status */ ref->ngisrc_working = 0; /* FALSE */ /* Log */ ngisiLogPrintf(context, NGISI_LOG_LEVEL_DEBUG, fName, "Refresh Credential thread exiting by error.\n"); /* Unlock */ if (mutexLocked != 0) { /* Notify signal */ result = ngisiCondBroadcast( context, &ref->ngisrc_cond, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Cond signal failed.\n"); } mutexLocked = 0; result = ngisiMutexUnlock(context, &ref->ngisrc_mutex, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the mutex failed.\n"); } } /* Failed */ return NULL; }
/** * Request Reader Wait Done */ int ngisiRequestReaderWaitDone( ngisiContext_t *context, int *error) { static const char fName[] = "ngisiRequestReaderWaitDone"; ngisiRequestReader_t *requestReader; int result, locked; /* Check the arguments */ assert(context != NULL); requestReader = &context->ngisc_requestReader; locked = 0; /* Lock the mutex */ result = ngisiMutexLock( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the Request Reader mutex failed.\n"); goto error; } locked = 1; /* Wait done */ while ((requestReader->ngisr_status < NGISI_REQUEST_READER_STATUS_DONE) && (requestReader->ngisr_working != 0)) { result = ngisiCondWait(context, &requestReader->ngisr_cond, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Cond Wait the Request Reader failed.\n"); goto error; } } /* Unlock the mutex */ locked = 0; result = ngisiMutexUnlock( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the Request Reader mutex failed.\n"); goto error; } /* Success */ return 1; /* Error occurred */ error: if (locked != 0) { locked = 0; result = ngisiMutexUnlock( context, &requestReader->ngisr_mutex, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the Request Reader mutex failed.\n"); } } /* Failed */ return 0; }
/** * Request Reader Status Set */ static int ngislRequestReaderStatusSet( ngisiContext_t *context, ngisiRequestReaderStatus_t status, int *error) { static const char fName[] = "ngislRequestReaderStatusSet"; ngisiRequestReader_t *requestReader; int result, locked; /* Check the arguments */ assert(context != NULL); requestReader = &context->ngisc_requestReader; locked = 0; /* Lock the mutex */ result = ngisiMutexLock( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Lock the Request Reader mutex failed.\n"); goto error; } locked = 1; /* Update the status */ requestReader->ngisr_status = status; /* Signal */ result = ngisiCondBroadcast( context, &requestReader->ngisr_cond, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Cond Signal the Request Reader failed.\n"); goto error; } /* Unlock the mutex */ locked = 0; result = ngisiMutexUnlock( context, &requestReader->ngisr_mutex, error); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the Request Reader mutex failed.\n"); goto error; } /* Success */ return 1; /* Error occurred */ error: if (locked != 0) { locked = 0; result = ngisiMutexUnlock( context, &requestReader->ngisr_mutex, NULL); if (result == 0) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Unlock the Request Reader mutex failed.\n"); } } /* Failed */ return 0; }
/** * Create Attr Get Remain * Returns attribute names which is not treated. */ int ngisiCreateAttrGetRemain( ngisiContext_t *context, ngisiCreateAttr_t *attr, int *remainCount, char ***remainTable, int *error) { static const char fName[] = "ngisiCreateAttrGetRemain"; int size, i, cur; char **newTable, *name; /* Check the arguments */ assert(context != NULL); assert(attr != NULL); assert(remainCount != NULL); assert(remainTable != NULL); *remainCount = 0; *remainTable = NULL; /* Count the remain */ size = 0; for (i = 0; i < attr->ngisca_nAttrs; i++) { assert(attr->ngisca_attrs[i].ngisce_name != NULL); if (attr->ngisca_attrs[i].ngisce_treated == 0) { size++; } } if (size == 0) { *remainCount = 0; *remainTable = NULL; /* Success */ return 1; } newTable = (char **)globus_libc_calloc(size, sizeof(char *)); if (newTable == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "Allocate the string table failed.\n"); return 0; } cur = 0; for (i = 0; i < attr->ngisca_nAttrs; i++) { assert(attr->ngisca_attrs[i].ngisce_name != NULL); if (attr->ngisca_attrs[i].ngisce_treated == 0) { name = strdup(attr->ngisca_attrs[i].ngisce_name); if (name == NULL) { ngisiLogPrintf(context, NGISI_LOG_LEVEL_ERROR, fName, "strdup() failed.\n"); return 0; } newTable[cur] = name; cur++; assert(cur <= size); } } *remainCount = size; *remainTable = newTable; /* Success */ return 1; }