/** * ObserveThread: Initialize */ static int ngcllObserveThreadInitialize( ngclContext_t *context, ngcliObserveThread_t *observe, int *error) { static const char fName[] = "ngcllObserveThreadInitialize"; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Initialize the Observe Thread module.\n", fName); ngcllObserveThreadInitializeMember(observe); result = ngcllObserveThreadInitializeMutexAndCond( context, observe, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't initialize the Mutex and Condition Variable.\n", fName); return 0; } /* Success */ return 1; }
/** * RefreshCredentials: Initialize */ int ngcliRefreshCredentialsInitialize( ngclContext_t *context, int *error) { static const char fName[] = "ngcliRefreshCredentialsInitialize"; ngcliObserveItem_t *observeItem; int result, interval; /* Check the arguments */ assert(context != NULL); context->ngc_refreshCredentialsInfo = NULL; /* Get the refresh credentials interval */ result = ngcllRefreshCredentialsIntervalGet(context, &interval, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't get the refresh credentials interval.\n", fName); return 0; } if (interval <= 0) { /* not used */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_INFORMATION, NULL, "%s: refresh credentials feature is not used.\n", fName); return 1; } /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Initialize the Refresh Credentials module.\n", fName); /* Construct */ observeItem = ngcliObserveItemConstruct( context, &context->ngc_observe, error); if (observeItem == NULL) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't construct the ObserveItem.\n", fName); return 0; } context->ngc_refreshCredentialsInfo = observeItem; observeItem->ngoi_eventTime = time(NULL) + interval; observeItem->ngoi_interval = interval; observeItem->ngoi_eventFunc = ngcllRefreshCredentialsEvent; observeItem->ngoi_eventTimeSetFunc = ngcllRefreshCredentialsEventTimeSet; /* Success */ return 1; }
/** * ObserveItem: EventTime change request */ int ngcliObserveItemEventTimeChangeRequest( ngclContext_t *context, ngcliObserveThread_t *observe, ngcliObserveItem_t *observeItem, int *error) { static const char fName[] = "ngcliObserveItemEventTimeChangeRequest"; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); assert(observeItem != NULL); #if 0 ngLogPrintf(context->ngc_log, NG_LOG_LEVEL_FATAL, NG_LOG_LEVEL_FATAL, NULL, "%s start...\n", fName); #endif #ifdef NG_PTHREAD result = ngcllObserveThreadEventTimeChangeRequest( context, observe, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Changing ObserveItem eventTime failed.\n", fName); return 0; } #else /* NG_PTHREAD */ /* Do nothing */ result = 1; assert(result != 0); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is not supported for this GlobusToolkit flavor.\n", fName); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is supported only for pthread version.\n", fName); #endif /* NG_PTHREAD */ /* Success */ return 1; }
/** * ObserveItem: Unregister */ static int ngcllObserveItemUnregister( ngclContext_t *context, ngcliObserveThread_t *observe, ngcliObserveItem_t *observeItem, int *error) { ngcliObserveItem_t **tail; static const char fName[] = "ngcllObserveItemUnregister"; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); assert(observeItem != NULL); /* Find the observeItem */ tail = &observe->ngot_item_head; while ((*tail != NULL) && (*tail != observeItem)) { tail = &(*tail)->ngoi_next; } if (*tail == NULL) { NGI_SET_ERROR(error, NG_ERROR_NOT_EXIST); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't find the ObserveItem in context.\n", fName); return 0; } /* Unregister the observeItem */ *tail = (*tail)->ngoi_next; /* Success */ return 1; }
/** * RefreshCredentials: ObserveThread event arrived */ static int ngcllRefreshCredentialsEvent( ngclContext_t *context, ngcliObserveItem_t *observeItem, time_t now, int *error) { static const char fName[] = "ngcllRefreshCredentialsEvent"; int result; /* Check the arguments */ assert(context != NULL); assert(observeItem != NULL); /* Perform the refreshCredentialsInfo */ result = ngcllRefreshCredentials(context, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Refreshing credentials failed.\n", fName); return 0; } /* Success */ return 1; }
/** * ObserveItem: Construct */ ngcliObserveItem_t * ngcliObserveItemConstruct( ngclContext_t *context, ngcliObserveThread_t *observe, int *error) { static const char fName[] = "ngcliObserveItemConstruct"; ngcliObserveItem_t *observeItem; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); /* Allocate */ observeItem = ngcllObserveItemAllocate(context, error); if (observeItem == NULL) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't allocate the storage for ObserveItem.\n", fName); return NULL; } /* Initialize */ result = ngcllObserveItemInitialize(context, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't initialize the ObserveItem.\n", fName); return NULL; } /* Register */ result = ngcllObserveItemRegister( context, observe, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't register the ObserveItem.\n", fName); return NULL; } /* Success */ return observeItem; }
/** * ObserveItem: Destruct */ int ngcliObserveItemDestruct( ngclContext_t *context, ngcliObserveThread_t *observe, ngcliObserveItem_t *observeItem, int *error) { static const char fName[] = "ngcliObserveItemDestruct"; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); /* Unregister */ result = ngcllObserveItemUnregister( context, observe, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unregister the ObserveItem.\n", fName); return 0; } /* Finalize */ result = ngcllObserveItemFinalize(context, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't finalize the ObserveItem.\n", fName); return 0; } /* Deallocate */ result = ngcllObserveItemFree(context, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't deallocate the storage for ObserveItem.\n", fName); return 0; } /* Success */ return 1; }
/** * ObserveThread: Finalize the Mutex and Condition Variable. */ static int ngcllObserveThreadFinalizeMutexAndCond( ngclContext_t *context, ngcliObserveThread_t *observe, int *error) { static const char fName[] = "ngcllObserveThreadFinalizeMutexAndCond"; ngLog_t *log; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); log = context->ngc_log; /* Destroy the condition variable */ if (observe->ngot_condInitialized != 0) { result = ngiCondDestroy(&observe->ngot_cond, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't destroy the Condition variable.\n", fName); } } observe->ngot_condInitialized = 0; /* Destroy the mutex */ if (observe->ngot_mutexInitialized != 0) { result = ngiMutexDestroy(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't destroy the Mutex.\n", fName); } } observe->ngot_mutexInitialized = 0; /* Success */ return 1; }
/** * ObserveThread: Finalize */ static int ngcllObserveThreadFinalize( ngclContext_t *context, ngcliObserveThread_t *observe, int *error) { static const char fName[] = "ngcllObserveThreadFinalize"; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Finalize the Observe Thread module.\n", fName); if (observe->ngot_continue != 0) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Observe Thread is not stopped.\n", fName); return 0; } result = ngcllObserveThreadFinalizeMutexAndCond( context, &context->ngc_observe, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't finalize the Mutex and Condition Variable.\n", fName); return 0; } ngcllObserveThreadInitializeMember(observe); /* Success */ return 1; }
/** * RefreshCredentials: Finalize */ int ngcliRefreshCredentialsFinalize( ngclContext_t *context, int *error) { static const char fName[] = "ngcliRefreshCredentialsFinalize"; ngcliObserveItem_t *observeItem; int result; /* Check the arguments */ assert(context != NULL); observeItem = context->ngc_refreshCredentialsInfo; if (observeItem == NULL) { /* not used */ return 1; } /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Finalize the Refresh Credentials module.\n", fName); context->ngc_refreshCredentialsInfo = NULL; /* Destruct */ result = ngcliObserveItemDestruct( context, &context->ngc_observe, observeItem, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't destruct the ObserveItem.\n", fName); return 0; } /* Success */ return 1; }
/** * RefreshCredentials: ObserveThread eventTime setup */ static int ngcllRefreshCredentialsEventTimeSet( ngclContext_t *context, ngcliObserveItem_t *observeItem, time_t now, int *error) { static const char fName[] = "ngcllRefreshCredentialsEventTimeSet"; /* Check the arguments */ assert(context != NULL); assert(observeItem != NULL); if (observeItem->ngoi_eventTimeChangeRequested != 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: interval won't be changed.\n", fName); } /* Check interval */ if (observeItem->ngoi_interval <= 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_WARNING, NULL, "%s: interval is wrong (%d).\n", fName, observeItem->ngoi_interval); } /* Set next eventTime */ if (observeItem->ngoi_eventExecuted != 0) { observeItem->ngoi_eventTime = now + observeItem->ngoi_interval; } /* Success */ return 1; }
/** * ObserveThread: Proceed events on ObserveItems if necessary */ static int ngcllObserveThreadProceedEvents( ngclContext_t *context, ngcliObserveThread_t *observe, time_t now, int *error) { static const char fName[] = "ngcllObserveThreadProceedEvents"; ngcliObserveItemEvent_t eventFunc; ngcliObserveItem_t *observeItem; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); observeItem = NULL; /* retrieve head item */ while ((observeItem = ngcliObserveItemGetNext( context, observe, observeItem, error)) != NULL) { assert(observeItem != NULL); observeItem->ngoi_eventExecuted = 0; /* Skip if disabled */ if (observeItem->ngoi_eventTime == 0) { continue; } /* Invoke the event */ if (observeItem->ngoi_eventTime <= now) { eventFunc = observeItem->ngoi_eventFunc; result = (*eventFunc)(context, observeItem, now, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_WARNING, NULL, "%s: Observe event function returned by error.\n", fName); } observeItem->ngoi_eventExecuted = 1; } } /* Success */ return 1; }
/** * ObserveThread: Proceed eventTime change on ObserveItems if necessary */ static int ngcllObserveThreadProceedEventTimeChange( ngclContext_t *context, ngcliObserveThread_t *observe, time_t now, int *error) { static const char fName[] = "ngcllObserveThreadProceedEventTimeChange"; ngcliObserveItemEvent_t eventFunc; ngcliObserveItem_t *observeItem; int result; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); observeItem = NULL; /* retrieve head item */ while ((observeItem = ngcliObserveItemGetNext( context, observe, observeItem, error)) != NULL) { assert(observeItem != NULL); /* Invoke the eventTime setup function */ if ((observeItem->ngoi_eventExecuted != 0) || (observeItem->ngoi_eventTimeChangeRequested != 0)) { eventFunc = observeItem->ngoi_eventTimeSetFunc; result = (*eventFunc)(context, observeItem, now, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_WARNING, NULL, "%s: Observe eventTime setup function returned by error.\n", fName); } } /* Reset the flag */ observeItem->ngoi_eventExecuted = 0; observeItem->ngoi_eventTimeChangeRequested = 0; } /* Success */ return 1; }
/** * ObserveItem: Allocate */ static ngcliObserveItem_t * ngcllObserveItemAllocate( ngclContext_t *context, int *error) { ngcliObserveItem_t *observeItem; static const char fName[] = "ngcllObserveItemAllocate"; /* Allocate */ observeItem = globus_libc_calloc(1, sizeof(ngcliObserveItem_t)); if (observeItem == NULL) { NGI_SET_ERROR(error, NG_ERROR_MEMORY); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't allocate the storage for ObserveItem.\n", fName); return NULL; } /* Success */ return observeItem; }
/** * ObserveThread: Finalize */ int ngcliObserveThreadFinalize( ngclContext_t *context, int *error) { static const char fName[] = "ngcliObserveThreadFinalize"; int result; /* Check the arguments */ assert(context != NULL); result = ngcllObserveThreadFinalize( context, &context->ngc_observe, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't clear the Observe Thread.\n", fName); return 0; } /* Success */ return 1; }
/** * ObserveThread: EventTime change request */ static int ngcllObserveThreadEventTimeChangeRequest( ngclContext_t *context, ngcliObserveThread_t *observe, ngcliObserveItem_t *observeItem, int *error) { static const char fName[] = "ngcllObserveThreadEventTimeChangeRequest"; int requireLock, result; ngLog_t *log; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); assert(observeItem != NULL); log = context->ngc_log; requireLock = 1; /** * Tell the Observe thread that event time was changed */ /* Check if this thread is Observe thread */ result = globus_thread_equal( observe->ngot_thread, globus_thread_self()); if (result != 0) { /** * This thread is Observe thread itself, * and executing ngcllObserveThreadProceedEvents(). * Thus, no need to lock and cond_signal. */ requireLock = 0; } /* Lock */ if (requireLock != 0) { result = ngiMutexLock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't lock the Mutex.\n", fName); return 0; } } /* Set the status */ observeItem->ngoi_eventTimeChangeRequested = 1; /* Notify signal */ if (requireLock != 0) { result = ngiCondSignal(&observe->ngot_cond, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't signal the Condition Variable.\n", fName); goto error; } } /* Unlock */ if (requireLock != 0) { result = ngiMutexUnlock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return 0; } } /* Success */ return 1; error: /* Unlock */ if (requireLock != 0) { result = ngiMutexUnlock(&observe->ngot_mutex, log, NULL); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return 0; } } /* Failed */ return 0; }
/** * RefreshCredentials: Perform Refresh Credentials */ static int ngcllRefreshCredentials( ngclContext_t *context, int *error) { static const char fName[] = "ngcllRefreshCredentials"; ngcliJobManager_t *jobManager; int result; static gss_cred_id_t ng_gss_credential = GSS_C_NO_CREDENTIAL; /* Check the arguments */ assert(context != NULL); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_INFORMATION, NULL, "%s: refresh credentials.\n", fName); /* acquire credential */ result = ngcliGlobusAcquireCredential(&ng_gss_credential, context->ngc_log, error); if (result != 1) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't acquire proxy credential.\n", fName); return 0; } /* Reset credential */ result = ngcliJobSetCredential(ng_gss_credential, error); if (result != 1) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't set proxy credential.\n", fName); return 0; } /* lock the list of JobManager */ result = ngcliContextJobManagerListReadLock(context, context->ngc_log, error); if (result != 1) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't lock the list of JobManager.\n", fName); return 0; } jobManager = NULL; /* retrieve head item */ while ((jobManager = ngcliContextGetNextJobManager( context, jobManager, error)) != NULL) { /* refresh credential of JobManager */ result = ngcliJobRefreshCredential(jobManager, error); if (result != 1) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_WARNING, NULL, "%s: Failed to refresh credential.\n", fName); } } result = ngcliContextJobManagerListReadUnlock(context, context->ngc_log, error); if (result != 1) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the list of JobManager.\n", fName); return 0; } ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: refresh credentials done.\n", fName); /* Success */ return 1; }
/** * ObserveThread: Get minimum sleep time from ObserveItems */ static int ngcllObserveThreadGetSleepTime( ngclContext_t *context, ngcliObserveThread_t *observe, int *sleepSec, int *error) { static const char fName[] = "ngcllObserveThreadGetSleepTime"; ngcliObserveItem_t *observeItem; time_t minTime, now; int found; /* Check the arguments */ assert(context != NULL); assert(observe != NULL); assert(sleepSec != NULL); *sleepSec = 0; found = 0; minTime = 0; observeItem = NULL; /* retrieve head item */ while ((observeItem = ngcliObserveItemGetNext( context, observe, observeItem, error)) != NULL) { assert(observeItem != NULL); /* Skip */ if (observeItem->ngoi_eventTime == 0) { continue; } if ((found == 0) || (observeItem->ngoi_eventTime < minTime)) { minTime = observeItem->ngoi_eventTime; } found = 1; } now = time(NULL); /* To detect busy loop caused by eventTime no-refresh bug */ if ((found != 0) && ((minTime + 60) <= now)) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_WARNING, NULL, "%s: Already passed eventTime detected. event time=%ld (now=%ld)\n", fName, minTime, now); } if (found == 0) { /* No need to wakeup, but wakeup long long later */ *sleepSec = NGCLL_OBSERVE_THREAD_NO_EVENT_SLEEP; } else { assert(minTime > 0); if (minTime <= now) { *sleepSec = 0; } else { *sleepSec = minTime - now; } } /* Success */ return 1; }
/** * RefreshCredentials: Get the refresh interval */ static int ngcllRefreshCredentialsIntervalGet( ngclContext_t *context, int *interval, int *error) { static const char fName[] = "ngcllRefreshCredentialsIntervalGet"; ngclLocalMachineInformationManager_t *lmInfoMng; ngLog_t *log; int result; /* Check the arguments */ assert(context != NULL); assert(interval != NULL); log = context->ngc_log; *interval = 0; /* Lock the Local Machine Information */ result = ngcliLocalMachineInformationListReadLock(context, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't lock the list of Local Machine Information.\n", fName); return 0; } /* Get refresh proxy cred interval */ lmInfoMng = ngcliLocalMachineInformationCacheGet(context, error); if (lmInfoMng == NULL) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't get the Local Machine Information.\n", fName); goto error; } /* set the interval of refresh proxy cert */ *interval = lmInfoMng->nglmim_info.nglmi_refreshInterval; /* Unlock the Local Machine Information */ result = ngcliLocalMachineInformationListReadUnlock( context, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the list of Local Machine Information.\n", fName); return 0; } /* Success */ return 1; /* Error occurred */ error: /* Unlock */ result = ngcliLocalMachineInformationListReadUnlock( context, log, NULL); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the list of Local Machine Information.\n", fName); return 0; } /* Failed */ return 0; }
/** * ObserveThread: main proceeding */ static void * ngcllObserveThread(void *threadArgument) { static const char fName[] = "ngcllObserveThread"; ngcliObserveThread_t *observe; int errorEntity, *error; int result, wasTimedOut, sleepSec; ngclContext_t *context; ngLog_t *log; time_t now; /* Check the arguments */ assert(threadArgument != NULL); context = (ngclContext_t *)threadArgument; observe = &context->ngc_observe; log = context->ngc_log; error = &errorEntity; sleepSec = 0; now = 0; /* Check if the flag is valid */ assert(observe->ngot_stopped == 0); /** * Do observing. */ /* Lock */ result = ngiMutexLock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't lock the Mutex.\n", fName); return NULL; } #if 0 ngLogPrintf(context->ngc_log, NG_LOG_LEVEL_FATAL, NG_LOG_LEVEL_FATAL, NULL, "%s start...\n", fName); #endif /* Get current time */ now = time(NULL); /* Proceed event time change if necessary */ result = ngcllObserveThreadProceedEventTimeChange( context, observe, now, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Proceeding Observe Thread event time change failed.\n", fName); goto error; } /* Wait the status */ while (observe->ngot_continue != 0) { /* Get sleep time */ result = ngcllObserveThreadGetSleepTime( context, observe, &sleepSec, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Getting Observe Thread sleep time failed.\n", fName); goto error; } /* Cond wait sleep */ result = ngiCondTimedWait( &observe->ngot_cond, &observe->ngot_mutex, sleepSec, &wasTimedOut, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't wait the Condition Variable.\n", fName); goto error; } if (observe->ngot_continue == 0) { break; } /* Get current time */ now = time(NULL); /* Proceed events if necessary */ result = ngcllObserveThreadProceedEvents( context, observe, now, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Proceeding Observe Thread event failed.\n", fName); goto error; } /* Proceed event time change if necessary */ result = ngcllObserveThreadProceedEventTimeChange( context, observe, now, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Proceeding Observe Thread event time change failed.\n", fName); goto error; } } /* Unlock */ result = ngiMutexUnlock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return NULL; } /** * Tell the Main Thread that, Observe Thread was stopped. */ /* Lock */ result = ngiMutexLock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't lock the Mutex.\n", fName); return NULL; } /* Set the status */ observe->ngot_stopped = 1; /* TRUE */ /* Notify signal */ result = ngiCondSignal(&observe->ngot_cond, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't signal the Condition Variable.\n", fName); goto error; } /* Unlock */ result = ngiMutexUnlock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return NULL; } /* Success */ return NULL; error: /* Thread is stopped now */ observe->ngot_stopped = 1; /* TRUE */ /* Unlock */ result = ngiMutexUnlock(&observe->ngot_mutex, log, NULL); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return NULL; } /* Failed */ return NULL; }
/** * ObserveThread: Stop checking */ int ngcliObserveThreadStop( ngclContext_t *context, int *error) { static const char fName[] = "ngcliObserveThreadStop"; ngcliObserveThread_t *observe; int result; /* Check the arguments */ assert(context != NULL); observe = &context->ngc_observe; /* Check the arguments */ if (observe->ngot_stopped != 0) { NGI_SET_ERROR(error, NG_ERROR_INVALID_ARGUMENT); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Observe Thread was already stopped.\n", fName); return 0; } /* Check if the Observe Thread is working */ if (observe->ngot_continue == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Observe Thread is not working.\n", fName); /* Do nothing */ return 1; } #ifdef NG_PTHREAD /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Stop the Observe Thread.\n", fName); result = ngcllObserveThreadStop(context, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't stop the thread to observe.\n", fName); return 0; } #else /* NG_PTHREAD */ /* Do nothing */ result = 1; assert(result != 0); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing not supported for this GlobusToolkit flavor.\n", fName); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is supported only for pthread version.\n", fName); #endif /* NG_PTHREAD */ /* Success */ return 1; }
/** * ObserveThread: Start observing */ int ngcliObserveThreadStart( ngclContext_t *context, int *error) { static const char fName[] = "ngcliObserveThreadStart"; ngcliObserveThread_t *observe; int result, enabled; /* Check the arguments */ assert(context != NULL); observe = &context->ngc_observe; enabled = 0; if (observe->ngot_item_head != NULL) { enabled = 1; } if (enabled == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_INFORMATION, NULL, "%s: Observe Thread is unnecessary.\n", fName); /* Do nothing */ observe->ngot_continue = 0; /* FALSE */ observe->ngot_stopped = 0; /* FALSE */ return 1; } #ifdef NG_PTHREAD observe->ngot_continue = 1; /* TRUE */ observe->ngot_stopped = 0; /* FALSE */ /* log */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_DEBUG, NULL, "%s: Start the Observe Thread.\n", fName); /* Create the Observe Thread */ result = globus_thread_create( &observe->ngot_thread, NULL, ngcllObserveThread, context); if (result != 0) { NGI_SET_ERROR(error, NG_ERROR_GLOBUS); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't create the thread for observe.\n", fName); return 0; } #else /* NG_PTHREAD */ /* Do nothing */ result = 1; assert(result != 0); observe->ngot_continue = 0; /* FALSE */ observe->ngot_stopped = 0; /* FALSE */ ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is not supported for this GlobusToolkit flavor.\n", fName); ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: observing is supported only for pthread version.\n", fName); #endif /* NG_PTHREAD */ /* Success */ return 1; }
/** * ObserveThread: Stop the thread */ static int ngcllObserveThreadStop( ngclContext_t *context, int *error) { static const char fName[] = "ngcllObserveThreadStop"; ngcliObserveThread_t *observe; ngLog_t *log; int result; /* Check the arguments */ assert(context != NULL); log = context->ngc_log; observe = &context->ngc_observe; /* Check if the flag is valid */ assert(observe->ngot_continue == 1); /** * Tell the Observe thread to stop */ /* Lock */ result = ngiMutexLock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't lock the Mutex.\n", fName); return 0; } /* Set the status */ observe->ngot_continue = 0; /* to stop */ /* Notify signal */ result = ngiCondSignal(&observe->ngot_cond, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't signal the Condition Variable.\n", fName); goto error; } /** * Suppress unlock and lock, to ignore CondSignal(stopped) issue, * before the CondWait(stopped) is in the process. */ /** * Wait the Observe thread to stop */ /* Suppress lock. already locked */ /* Wait the status */ while (observe->ngot_stopped == 0) { result = ngiCondWait( &observe->ngot_cond, &observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't wait the Condition Variable.\n", fName); goto error; } } /* Unlock */ result = ngiMutexUnlock(&observe->ngot_mutex, log, error); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return 0; } /* Success */ return 1; error: /* Unlock */ result = ngiMutexUnlock(&observe->ngot_mutex, log, NULL); if (result == 0) { ngclLogPrintfContext(context, NG_LOG_CATEGORY_NINFG_PURE, NG_LOG_LEVEL_ERROR, NULL, "%s: Can't unlock the Mutex.\n", fName); return 0; } /* Failed */ return 0; }