/** * SSH Job: Callback function called when a line is read from standard error */ void ngisSSHjobErrorCallback( void *arg, ngisLineBuffer_t *lineBuffer, char *line, ngisCallbackResult_t cResult) { ngisSSHjob_t *job = arg; int result; ngisLog_t *log = NULL; static const char fName[] = "ngisSSHjobErrorCallback"; NGIS_ASSERT(job != NULL); NGIS_ASSERT(lineBuffer != NULL); NGIS_ASSERT(lineBuffer == job->ngsj_lBufStderr); log = job->ngsj_log; switch (cResult) { case NGIS_CALLBACK_RESULT_EOF: ngisDebugPrint(log, fName, "Called with EOF.\n"); goto error; case NGIS_CALLBACK_RESULT_CANCEL: ngisDebugPrint(log, fName, "Called because callback was canceled.\n"); return; case NGIS_CALLBACK_RESULT_FAILED: ngisErrorPrint(log, fName, "Called because error occurred on reading from STDERR.\n"); goto error; case NGIS_CALLBACK_RESULT_SUCCESS: break; default: NGIS_ASSERT_NOTREACHED(); } NGIS_ASSERT(line != NULL); ngisErrorPrint(log, fName, "\"%s\".\n", line); /* Next Line */ result = ngisLineBufferReadLine(lineBuffer, ngisSSHjobErrorCallback, job); if (result == 0) { ngisErrorPrint(log, fName, "Can't register callback for reading STDERR.\n"); goto error; } return; error: ngisLineBufferDestroy(job->ngsj_lBufStderr); job->ngsj_lBufStderr = NULL; return; }
static void ngislSSHjobPBSqueryStatusReadCallback( void *arg, ngisLineBuffer_t *lBuf, char *line, ngisCallbackResult_t cResult) { ngisSSHjob_t *job = arg; int result; int i; ngisTokenAnalyzer_t tokenAnalyzer; int tokenAnalyzerInitialized = 0; char *status = NULL; ngisLog_t *log; static const char fName[] = "ngislSSHjobPBSqueryStatusReadCallback"; NGIS_ASSERT(job != NULL); NGIS_ASSERT(lBuf != NULL); log = job->ngsj_log; 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: 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, "dummy") == 0) { /* Done */ job->ngsj_executables[0].nge_exitCode = 0;/* Success */ result = ngisSSHjobExecutableSetStatus( job, &job->ngsj_executables[0], NGIS_EXECUTABLE_STATUS_EXIT); if (result == 0) { ngisErrorPrint(log, fName, "Can't set status.\n"); goto error; } result = ngisSSHjobProcessAfterExit(job); if (result == 0) { ngisErrorPrint(log, fName, "Can't process after exit.\n"); goto error; } return; } ngisTokenAnalyzerInitialize(&tokenAnalyzer, line); tokenAnalyzerInitialized = 0; /* job_state = [STATUS] */ for (i = 0;i < 2;++i) { result = ngisTokenAnalyzerNext(&tokenAnalyzer); if (result == 0) { ngisErrorPrint(log, fName, "Can't get status.\n."); goto error; } } /* Get Status */ status = ngisTokenAnalyzerGetString(&tokenAnalyzer); if (status == NULL) { ngisErrorPrint(log, fName, "Can't get status.\n"); goto error; } ngisDebugPrint(log, fName, "Status string is \"%s\".\n", status); /* Check Status */ switch (*status) { case 'Q': case 'W': case 'T': break; case 'B': case 'E': case 'R': case 'X': /* t(ransfering), r(unning) */ job->ngsj_executables[0].nge_status = NGIS_EXECUTABLE_STATUS_ACTIVE; break; case 'H': /* Do nothing */ break; case 'S': break; case 'U': default: /* DONOTHING */; } free(status); status = NULL; ngisTokenAnalyzerFinalize(&tokenAnalyzer); tokenAnalyzerInitialized = 0; result = ngisLineBufferReadLine(job->ngsj_lBufStdout, ngisSSHjobQueryStatusReadDummyCallback, job); if (result == 0) { ngisErrorPrint(log, fName, "Can't register the function for reading the line.\n"); goto error; } return; error: NGIS_NULL_CHECK_AND_FREE(status); if (tokenAnalyzerInitialized != 0) { ngisTokenAnalyzerFinalize(&tokenAnalyzer); tokenAnalyzerInitialized = 0; } result = ngisSSHjobProcessAfterExit(job); if (result == 0) { ngisErrorPrint(NULL, fName, "The job isn't able to be done.\n"); } return; }
/** * SSH Job: Initialize */ static int ngislSSHjobInitialize( ngisJob_t *job, ngisOptionContainer_t *opts) { ngisSSHjob_t *sshJob = (ngisSSHjob_t *)job; ngisSSHjobAttributes_t *attr = NULL; char *jobID = NULL; int result; pid_t pid = -1; ngisLog_t *log = NULL; static const char fName[] = "ngislSSHjobInitialize"; NGIS_ASSERT(sshJob != NULL); NGIS_ASSERT(opts != NULL); log = job->ngj_log; ngislSSHjobInitializeMember(sshJob); /* Function Queue */ NGIS_LIST_INITIALIZE(ngisSSHjobFunc_t, &sshJob->ngsj_funcQueue); /* Create SSH Job Attribute from Invoke Server Options */ attr = ngisSSHjobAttributesCreate(opts); if (attr == NULL) { ngisErrorPrint(log, fName, "Can't create SSH job attributes.\n"); goto error; } sshJob->ngsj_attributes = attr; ngislSSHjobWarnIgnoredOptions(sshJob, opts); /* Set JobManager*/ if ((attr->ngsja_jobManager == NULL) || (strcmp(attr->ngsja_jobManager, "jobmanager-fork") == 0)) { sshJob->ngsj_jobManagerType = NGIS_SSH_JOB_MANAGER_NORMAL; } else if (strcmp(attr->ngsja_jobManager, "jobmanager-sge") == 0) { sshJob->ngsj_jobManagerType = NGIS_SSH_JOB_MANAGER_SGE; } else if (strcmp(attr->ngsja_jobManager, "jobmanager-pbs") == 0) { sshJob->ngsj_jobManagerType = NGIS_SSH_JOB_MANAGER_PBS; } else { ngisErrorPrint(log, fName, "Invalid job manager.\n"); goto error; } /* Number of the job of queuing system */ switch (attr->ngsja_jobBackend) { case NGIS_BACKEND_NORMAL: switch(sshJob->ngsj_jobManagerType) { case NGIS_SSH_JOB_MANAGER_PBS: case NGIS_SSH_JOB_MANAGER_SGE: sshJob->ngsj_nExecutables = 1; break; case NGIS_SSH_JOB_MANAGER_NORMAL: sshJob->ngsj_nExecutables = attr->ngsja_count; break; } break; case NGIS_BACKEND_MPI: case NGIS_BACKEND_BLACS: sshJob->ngsj_nExecutables = 1; break; default: NGIS_ASSERT_NOTREACHED(); } /* Create Executable Handle */ sshJob->ngsj_executables = ngislExecutableCreate(sshJob->ngsj_nExecutables, log); if (sshJob->ngsj_executables == NULL) { ngisErrorPrint(log, fName, "Can't create the executables.\n"); goto error; } /* Execute SSH */ pid = ngislSSHjobExecuteSSH(sshJob); if (pid < 0) { ngisErrorPrint(log, fName, "Can't execute SSH.\n"); goto error; } /* Create Line Buffer */ sshJob->ngsj_lBufStdout = ngisLineBufferCreate(sshJob->ngsj_stdio.ngsio_out, "\n"); if (sshJob->ngsj_lBufStdout == NULL) { ngisErrorPrint(log, fName, "Can't create line buffer for the STDOUT.\n"); goto error; } sshJob->ngsj_lBufStderr = ngisLineBufferCreate(sshJob->ngsj_stdio.ngsio_error, "\n"); if (sshJob->ngsj_lBufStderr == NULL) { ngisErrorPrint(log, fName, "Can't create line buffer for the STDERR.\n"); goto error; } /* Register function for reading STDERR */ result = ngisLineBufferReadLine( sshJob->ngsj_lBufStderr, ngisSSHjobErrorCallback, sshJob); if (result == 0) { ngisErrorPrint(log, fName, "Can't register callback for reading STDERR.\n"); goto error; } /* Prepare submitting the job */ switch (sshJob->ngsj_jobManagerType) { case NGIS_SSH_JOB_MANAGER_NORMAL: result = ngisSSHjobPrepare(sshJob); break; case NGIS_SSH_JOB_MANAGER_SGE: result = ngisSSHjobSGEprepare(sshJob); break; case NGIS_SSH_JOB_MANAGER_PBS: result = ngisSSHjobPBSprepare(sshJob); break; default: NGIS_ASSERT_NOTREACHED(); } if (result == 0) { ngisErrorPrint(log, fName, "Can't prepare.\n"); goto error; } /* Job ID is process ID of SSH */ jobID = ngisStrdupPrintf("%ld", (long)pid); if (jobID == NULL) { ngisErrorPrint(log, fName, "Can't create Job ID.\n"); goto error; } result = ngisJobRegisterID(job, jobID); if (result == 0) { ngisErrorPrint(log, fName, "Can't register Job ID to protocol.\n"); goto error; } result = ngisSSHjobFunctionPop(sshJob); if (result == 0) { ngisErrorPrint(log, fName, "Can't execute the next process.\n"); goto error; } NGIS_NULL_CHECK_AND_FREE(jobID); return 1; /* Error occurred */ error: NGIS_NULL_CHECK_AND_FREE(jobID); result = ngislSSHjobFinalize(job); if (result == 0) { ngisErrorPrint(log, fName, "Can't finalize the ssh job.\n"); } return 0; }