int ngisSSHjobPBSdoCancel( ngisSSHjob_t *job) { ngisStringBuffer_t sBuf; int sBufInitialized = 0; char *command = NULL; int i; int ret = 1; int nExe = 0; int result; ngisCallback_t callback; ngisLog_t *log; static const char fName[] = "ngisSSHjobPBSdoCancel"; NGIS_ASSERT(job != NULL); log = job->ngsj_log; nExe = ngisSSHjobGetNexecutable(job, NGIS_EXECUTABLE_STATUS_PENDING); nExe += ngisSSHjobGetNexecutable(job, NGIS_EXECUTABLE_STATUS_ACTIVE); if (nExe == 0) { /* There is no executable. */ goto finalize; } result = ngisStringBufferInitialize(&sBuf); if (result == 0) { ngisErrorPrint(log, fName, "Can't initialize the string buffer.\n"); ret = 0; goto finalize; } sBufInitialized = 1; result = ngisStringBufferAppend(&sBuf, job->ngsj_attributes->ngsja_sshDeleteCommand); if (result == 0) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } for (i = 0;i < job->ngsj_nExecutables;++i) { switch (job->ngsj_executables[i].nge_status) { case NGIS_EXECUTABLE_STATUS_PENDING: case NGIS_EXECUTABLE_STATUS_ACTIVE: result = ngisStringBufferFormat(&sBuf, " %s", job->ngsj_executables[i].nge_identifier); if (result == 0) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } break; default: break; } } result = ngisStringBufferAppend(&sBuf, "\n"); if (result == 0) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } command = ngisStringBufferRelease(&sBuf); if (command == NULL) { ngisErrorPrint(log, fName, "Can't release string from string buffer.\n"); ret = 0; goto finalize; } callback = ngisCallbackWriteFormat( job->ngsj_stdio.ngsio_in, ngisSSHjobCancelWriteCallback, job, "%s", command); if (!ngisCallbackIsValid(callback)) { ngisErrorPrint(log, fName, "Can't send command string.\n"); ret = 0; goto finalize; } job->ngsj_stdinCallback = callback; finalize: NGIS_NULL_CHECK_AND_FREE(command); if (sBufInitialized != 0) { ngisStringBufferFinalize(&sBuf); sBufInitialized = 0; } return ret; }
/** * SSH job: Callback function for "Query exit status" */ static void ngislSSHjobExecutableDoneReadCallback( void *arg, ngisLineBuffer_t *lBuf, char *line, ngisCallbackResult_t cResult) { ngisSSHjob_t *job = arg; int exitStatus; int result; int nExit; ngisLog_t *log; ngisTokenAnalyzer_t tokenAnalyzer; int tokenAnalyzerInitialized = 0; ngisExecutable_t *exe = NULL; static const char fName[] = "ngislSSHjobExecutableDoneReadCallback"; NGIS_ASSERT(job != NULL); NGIS_ASSERT(lBuf != NULL); log = job->ngsj_log; ngisDebugPrint(log, fName, "Called.\n"); switch (cResult) { case NGIS_CALLBACK_RESULT_EOF: ngisErrorPrint(log, fName, "Unexcept EOF.\n"); goto error; case NGIS_CALLBACK_RESULT_CANCEL: ngisDebugPrint(log, fName, "Callback is canceled.\n"); return; case NGIS_CALLBACK_RESULT_FAILED: ngisDebugPrint(log, fName, "Can't read the reply.\n"); goto error; case NGIS_CALLBACK_RESULT_SUCCESS: break; default: NGIS_ASSERT_NOTREACHED(); } NGIS_ASSERT(line != NULL); ngisTokenAnalyzerInitialize(&tokenAnalyzer, line); tokenAnalyzerInitialized = 1; /* Get Exit Status */ result = ngisTokenAnalyzerGetInt(&tokenAnalyzer, &exitStatus); if (result == 0) { ngisErrorPrint(log, fName, "Can't get exit status.\n"); goto error; } /* Check End */ result = ngisTokenAnalyzerNext(&tokenAnalyzer); if (result != 0) { ngisErrorPrint(log, fName, "Unnecessary token.\n."); goto error; } tokenAnalyzerInitialized = 0; ngisTokenAnalyzerFinalize(&tokenAnalyzer); ngisDebugPrint(log, fName, "Exit status is \"%d\".\n", exitStatus); /* Set status */ exe = &job->ngsj_executables[job->ngsj_iExecutables]; exe->nge_exitCode = exitStatus; result = ngisSSHjobExecutableSetStatus( job, exe, NGIS_EXECUTABLE_STATUS_EXIT); if (result == 0) { ngisErrorPrint(log, fName, "Can't set status.\n"); goto error; } nExit = ngisSSHjobGetNexecutable(job, NGIS_EXECUTABLE_STATUS_EXIT); if (nExit >= job->ngsj_nExecutables) { ngisDebugPrint(log, fName, "All Executable exit.\n"); result = ngisSSHjobProcessAfterExit(job); if (result == 0) { ngisErrorPrint(log, fName, "Can't process after exit.\n"); goto error; } } else { ngisDebugPrint(log, fName, "Check status of the next executable.\n"); job->ngsj_iExecutables++; result = ngislSSHjobQueryStatus(job); if (result == 0) { ngisErrorPrint(log, fName, "Can't query status.\n"); goto error; } } return; error: if (tokenAnalyzerInitialized != 0) { tokenAnalyzerInitialized = 0; ngisTokenAnalyzerFinalize(&tokenAnalyzer); } result = ngisSSHjobProcessAfterExit(job); if (result == 0) { ngisErrorPrint(NULL, fName, "The job isn't able to be done.\n"); } return; }
static void ngislSSHjobPBSsubmitReadCallback( void *arg, ngisLineBuffer_t *lBuf, char *line, ngisCallbackResult_t cResult) { ngisSSHjob_t *job = arg; int result; char *jobID = NULL; int nActive; ngisLog_t *log; static const char fName[] = "ngislSSHjobPBSsubmitReadCallback"; NGIS_ASSERT(job != NULL); NGIS_ASSERT(lBuf != NULL); log = job->ngsj_log; switch (cResult) { case NGIS_CALLBACK_RESULT_CANCEL: ngisDebugPrint(log, fName, "Callback is canceled.\n"); return; case NGIS_CALLBACK_RESULT_EOF: ngisErrorPrint(log, fName, "Unexcept EOF.\n"); goto error; case NGIS_CALLBACK_RESULT_FAILED: ngisErrorPrint(log, fName, "Can't read the reply.\n"); goto error; case NGIS_CALLBACK_RESULT_SUCCESS: break; default: NGIS_ASSERT_NOTREACHED(); } /* Failed? */ if (strcmp(line, NGIS_SSH_JOB_COMMAND_FAILED) == 0) { ngisErrorPrint(log, fName, "Can't submit the job.\n"); goto next; } ngisDebugPrint(log, fName, "Job ID = %s.\n", line); jobID = strdup(line); if (jobID == NULL) { ngisErrorPrint(log, fName, "Can't copy the job ID.\n"); goto next; } next: if (jobID != NULL) { job->ngsj_executables[job->ngsj_iExecutables].nge_identifier = jobID; jobID = NULL; result = ngisSSHjobExecutableSetStatus( job, &job->ngsj_executables[job->ngsj_iExecutables], NGIS_EXECUTABLE_STATUS_PENDING); if (result == 0) { ngisErrorPrint(log, fName, "Can't set status.\n"); goto next; } } else { result = ngisSSHjobExecutableSetStatus( job, &job->ngsj_executables[job->ngsj_iExecutables], NGIS_EXECUTABLE_STATUS_FAILED); if (result == 0) { ngisErrorPrint(log, fName, "Can't set status.\n"); } } job->ngsj_iExecutables++; if (job->ngsj_iExecutables < job->ngsj_nExecutables) { result = ngislSSHjobPBSsubmit(job); if (result == 0) { ngisErrorPrint(log, fName, "Can't submit next job.\n"); goto error; } } else { nActive = ngisSSHjobGetNexecutable(job, NGIS_EXECUTABLE_STATUS_PENDING); if (nActive == 0) { ngisErrorPrint(log, fName, "Can't execute all executables.\n"); goto error; } /* Polling Start */ result = ngisSSHjobPollingStart(job); if (result == 0) { ngisErrorPrint(log, fName, "Can't start status polling.\n"); goto error; } } return; error: NGIS_NULL_CHECK_AND_FREE(jobID); result = ngisSSHjobProcessAfterExit(job); if (result == 0) { ngisErrorPrint(NULL, fName, "The job isn't able to be done.\n"); } return; }
static int ngislSSHjobDoCancel( ngisSSHjob_t* job) { ngisStringBuffer_t sBuf; int sBufInitialized = 0; char *command = NULL; int i; int ret = 1; int nActive = 0; int result; ngisCallback_t callback; ngisLog_t *log; ngisExecutable_t *exe; static const char fName[] = "ngislSSHjobDoCancel"; NGIS_ASSERT(job != NULL); log = job->ngsj_log; nActive = ngisSSHjobGetNexecutable(job, NGIS_EXECUTABLE_STATUS_ACTIVE); if (nActive == 0) { /* There is no executable. */ goto finalize; } result = ngisStringBufferInitialize(&sBuf); if (result == 0) { ngisErrorPrint(log, fName, "Can't initialize the string buffer.\n"); ret = 0; goto finalize; } sBufInitialized = 1; result = ngisStringBufferAppend(&sBuf, NGIS_KILL_COMMAND); if (result == 0) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } for (i = 0;i < job->ngsj_nExecutables;++i) { exe = &job->ngsj_executables[i]; if (exe->nge_status == NGIS_EXECUTABLE_STATUS_ACTIVE) { result = ngisStringBufferFormat(&sBuf, " %s", exe->nge_identifier); if (result == 0) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } } } result = ngisStringBufferAppend(&sBuf, "\n"); if (result == 0) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } command = ngisStringBufferRelease(&sBuf); if (command == NULL) { ngisErrorPrint(log, fName, "Can't append string to the string buffer.\n"); ret = 0; goto finalize; } callback = ngisCallbackWriteFormat(job->ngsj_stdio.ngsio_in, ngisSSHjobCancelWriteCallback, job, "%s", command); if (!ngisCallbackIsValid(callback)) { ngisErrorPrint(log, fName, "Can't register function for sending command string.\n"); ret = 0; goto finalize; } job->ngsj_stdinCallback = callback; finalize: NGIS_NULL_CHECK_AND_FREE(command); if (sBufInitialized != 0) { ngisStringBufferFinalize(&sBuf); sBufInitialized = 0; } return ret; }