Example #1
0
/**
 * SSH job: Popen
 * If command is NULL, "/bin/sh" used as command.
 */
static pid_t
ngislSSHjobSSHpopen(
    ngisSSHjob_t *job,
    char *command,
    ngisStandardIO_t* sio)
{
    char *sshCommand = NULL;
    pid_t ret = -1;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobSSHpopen";
    
    NGIS_ASSERT(job != NULL);
    NGIS_ASSERT(sio != NULL);

    log = job->ngsj_log;

    sshCommand = ngisSSHjobCreateSSHcommand(job, command);
    if (sshCommand == NULL) {
        ngisErrorPrint(log, fName, "Can't create SSH command.\n");
        goto finalize;
    }

    ret = ngisPopen(sio, sshCommand);
    if (ret < 0) {
        ngisErrorPrint(log, fName, "Can't execute SSH command.\n");
        goto finalize;
    }

finalize:
    NGIS_NULL_CHECK_AND_FREE(sshCommand);

    return ret;
}
Example #2
0
static int
ngislSSHjobDisable(
    ngisSSHjob_t *job)
{
    int result;
    int ret = 1;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobDisable";
    
    NGIS_ASSERT(job != NULL);

    log = ((ngisJob_t *)job)->ngj_log;
    
    /* Stop file transfer */
    if (job->ngsj_fileTransfer != NULL) {
        result = ngisSSHjobFileTransferDestroy(job->ngsj_fileTransfer);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't destroy file transfer.\n");
            ret = 0;
        }
    }

    NGIS_NULL_CHECK_AND_DESTROY(job->ngsj_timerCallback, ngisCallbackCancel);
    NGIS_NULL_CHECK_AND_DESTROY(job->ngsj_stdinCallback, ngisCallbackCancel);
    NGIS_NULL_CHECK_AND_FREE(job->ngsj_command);
    NGIS_NULL_CHECK_AND_DESTROY(job->ngsj_lBufStdout, ngisLineBufferDestroy);
    NGIS_NULL_CHECK_AND_DESTROY(job->ngsj_lBufStderr, ngisLineBufferDestroy);

    result = ngisStandardIOclose(&job->ngsj_stdio);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't close the STDIO\n");
        ret = 0;
    }

    /* Remove Script File */
#define NGISL_SSH_JOB_FILE_DESTROY(filename) do {               \
    char *scriptName = (filename);                              \
    if (scriptName != NULL) {                                   \
        ngisDebugPrint(log, fName, "Unlink %s.\n", scriptName); \
        result = unlink(scriptName);                            \
        if (result < 0) {                                       \
            ngisErrorPrint(log, fName,                          \
                "unlink(%s): %s", scriptName, strerror(errno)); \
            ret = 0;                                            \
        }                                                       \
    }                                                           \
    NGIS_NULL_CHECK_AND_FREE(filename);                         \
}while (0)

    NGISL_SSH_JOB_FILE_DESTROY(job->ngsj_localScriptNameForArrayJob);
    NGISL_SSH_JOB_FILE_DESTROY(job->ngsj_localScriptName);

#undef NGISL_SSH_JOB_FILE_DESTROY

    return ret;
}
Example #3
0
int
ngisSSHjobPBSprepare(ngisSSHjob_t *job)
{
    int result;
    ngisLog_t *log;
    static const char fName[] = "ngisSSHjobPBSprepare";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    /* Common? */
    result = ngisSSHjobPrepareCommon(job);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't prepare.\n");
        return 0;
    }

    result = ngisSSHjobFunctionPush(job, ngislSSHjobPBScreateScript);
    if (result == 0) {
        ngisErrorPrint(log, fName, 
            "Can't register function for create PBS script.\n");
        return 0;
    }

    result = ngisSSHjobFunctionPush(job, ngisSSHjobStagingScript);
    if (result == 0) {
        ngisErrorPrint(log, fName, 
            "Can't register function for transfer PBS script.\n");
        return 0;
    }

    if ((job->ngsj_attributes->ngsja_jobBackend == NGIS_BACKEND_NORMAL) && 
        (job->ngsj_attributes->ngsja_count > 1)) {
        /* For Array Job */
        result = ngisSSHjobFunctionPush(job,
            ngisSSHjobStagingScriptForArrayJob);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't register function "
                "for transfer SGE script for array job.\n");
            return 0;
        }
    }

    result = ngisSSHjobFunctionPush(job, ngislSSHjobPBSsubmitStart);
    if (result == 0) {
        ngisErrorPrint(log, fName, 
            "Can't register function for transfer PBS script.\n");
        return 0;
    }

    return 1;
}
Example #4
0
static int
ngislSSHjobPBSqueryStatus(
    ngisSSHjob_t *job)
{
    ngisCallback_t callback;
    int index;
    ngisExecutable_t *exe;
    ngisLog_t *log;
    int result;
    static const char fName[] = "ngislSSHjobPBSqueryStatus";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    while (job->ngsj_iExecutables < job->ngsj_nExecutables) {
        index = job->ngsj_iExecutables;
        exe   = &job->ngsj_executables[index];
        ngisDebugPrint(log, fName,
            "Executable[%d] is %d\n", index, exe->nge_status);

        if (exe->nge_status <= NGIS_EXECUTABLE_STATUS_ACTIVE) {
            /* Check Status */
            job->ngsj_nextReadCallback = ngislSSHjobPBSqueryStatusReadCallback;
            callback = ngisCallbackWriteFormat(
                job->ngsj_stdio.ngsio_in,
                ngisSSHjobWriteStringCallback, job,
                "%s -f %s | %s 'job_state';%s dummy\n",
                job->ngsj_attributes->ngsja_sshStatusCommand,
                exe->nge_identifier, NGIS_GREP_COMMAND, NGIS_ECHO_COMMAND);
            if (!ngisCallbackIsValid(callback)) {
                ngisErrorPrint(log, fName,
                    "Can't register function for sending"
                    " query status command string.\n");
                return 0;
            }
            break;
        }
        job->ngsj_iExecutables++;
    }

    if (!(job->ngsj_iExecutables < job->ngsj_nExecutables)) {
        ngisDebugPrint(log, fName, "Status checking has finished.\n");
        job->ngsj_iExecutables = 0;
        result = ngisSSHjobFinishQuery(job);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't finish to query the job status.\n");
            return 0;
        }
    }
    return 1;
}
Example #5
0
/**
 * SSH job: Query status
 */
static int
ngislSSHjobQueryStatus(
    ngisSSHjob_t *job)
{
    int index;
    ngisExecutable_t *exe;
    ngisCallback_t callback;
    ngisLog_t *log = NULL;
    int result;
    static const char fName[] = "ngislSSHjobQueryStatus";

    NGIS_ASSERT(job != NULL);
    NGIS_ASSERT(job->ngsj_attributes != NULL);

    log = job->ngsj_log;

    ngisDebugPrint(log, fName, "Index is %d/%d\n",
        job->ngsj_iExecutables, job->ngsj_nExecutables);

    while (job->ngsj_iExecutables < job->ngsj_nExecutables) {
        index =  job->ngsj_iExecutables;
        exe   = &job->ngsj_executables[index];
        ngisDebugPrint(log, fName, "Executable[%d] is %s\n",
            index, ngislExecutableStatusString[exe->nge_status]);
        if (exe->nge_status == NGIS_EXECUTABLE_STATUS_ACTIVE) {
            /* Check Status */
            job->ngsj_nextReadCallback = ngislSSHjobQueryStatusReadCallback;
            callback = ngisCallbackWriteFormat(
                job->ngsj_stdio.ngsio_in, ngisSSHjobWriteStringCallback, job,
                "%s %s 2> %s && %s %s || %s %s\n",
                NGIS_QUERY_COMMAND, exe->nge_identifier, NGIS_DEV_NULL,
                NGIS_ECHO_COMMAND, NGIS_SSH_JOB_NONE,
                NGIS_ECHO_COMMAND, NGIS_SSH_JOB_EXIT);
            if (!ngisCallbackIsValid(callback)) {
                ngisErrorPrint(log, fName,
                    "Can't write command invoking a executable.\n");
            }
            break;
        }
        job->ngsj_iExecutables++;
    }

    if (!(job->ngsj_iExecutables < job->ngsj_nExecutables)) {
        ngisDebugPrint(log, fName, "Status checking has finished.\n");
        result = ngisSSHjobFinishQuery(job);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't finish to query the job status.\n");
            return 0;
        }
    }
    return 1;
}
Example #6
0
/**
 * 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;
}
Example #7
0
/**
 * SSH job: done
 */
int
ngisSSHjobDone(
    ngisSSHjob_t *job)
{
    int i;
    int result;
    ngisLog_t *log;
    ngisExecutable_t *exe = NULL;
    static const char fName[] = "ngisSSHjobDone";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;
    ngisDebugPrint(log, fName, "called.\n");

    for (i = 0;i < job->ngsj_nExecutables;++i) {
        exe = &job->ngsj_executables[i];
        switch (exe->nge_status) {
        case NGIS_EXECUTABLE_STATUS_FAILED:
        case NGIS_EXECUTABLE_STATUS_FAILED_STAGEOUT:
            break;
        default:
            result = ngisSSHjobExecutableSetStatus(
                job, exe, NGIS_EXECUTABLE_STATUS_DONE);
            if (result == 0) {
                ngisErrorPrint(log, fName, "Can't set status.\n");            
            }
            break;
        }
    }

    if (((ngisJob_t *)job)->ngj_destroyRequested != 0) {
        result = ngisJobDestroy((ngisJob_t *)job);
        if (result == 0) {
            ngisErrorPrint(NULL, fName, "Can't destroy the job.\n");            
            /* through */
        }
        job = NULL;
    } else {
        /* Disable */
        result = ngislSSHjobDisable(job);
        if (result == 0) {
            ngisErrorPrint(NULL, fName, "Can't disable the job.\n");            
            /* through */
        }
    }
    return 1;
}
Example #8
0
static int
ngislSSHjobPBSsubmit(
    ngisSSHjob_t *job)
{
    ngisCallback_t callback;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobPBSsubmit";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    ngisDebugPrint(log, fName, "Called.\n");

    job->ngsj_nextReadCallback = ngislSSHjobPBSsubmitReadCallback;
    callback = ngisCallbackWriteFormat(
        job->ngsj_stdio.ngsio_in, ngisSSHjobWriteStringCallback, job,
        "%s %s || %s %s\n",
        job->ngsj_attributes->ngsja_sshSubmitCommand,
        job->ngsj_remoteScriptName,
        NGIS_ECHO_COMMAND, NGIS_SSH_JOB_COMMAND_FAILED);
    if (!ngisCallbackIsValid(callback)) {
        ngisErrorPrint(log, fName,
            "Can't register for sending submit command string.\n");
        return 0;
    }
    job->ngsj_stdinCallback = callback;


    return 1;
}
Example #9
0
/**
 * SSH job: Start status polling
 */
int
ngisSSHjobPollingStart(
    ngisSSHjob_t *job)
{
    int interval;
    ngisCallback_t callback;
    ngisLog_t *log;
    static const char fName[] = "ngisSSHjobPollingStart";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    interval = job->ngsj_attributes->ngsja_statusPolling;
    if (interval <= 0) {
        interval = NGIS_SSH_JOB_DEFAULT_POLLING_TIME;
    }
    callback = ngisCallbackSetTimer(interval, ngisSSHjobPollingCallback, job);
    if (!ngisCallbackIsValid(callback)) {
        ngisErrorPrint(log, fName,
            "Can't register callback for status polling.\n");
        return 0;
    }
    job->ngsj_timerCallback = callback;

    return 1;
}
Example #10
0
/**
 * SSH job: Execute SSH
 */
static pid_t
ngislSSHjobExecuteSSH(
    ngisSSHjob_t *job)
{
    pid_t pid = -1;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobExecuteSSH";
    
    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    pid = ngislSSHjobSSHpopen(job, NULL, &job->ngsj_stdio);
    if (pid < 0) {
        ngisErrorPrint(log, fName, "Can't execute SSH.\n");
        return -1;
    }
    
    ngisDebugPrint(log, fName, "Child Process's STDIN  is %d.\n",
        job->ngsj_stdio.ngsio_in);
    ngisDebugPrint(log, fName, "Child Process's STDOUT is %d.\n",
        job->ngsj_stdio.ngsio_out);
    ngisDebugPrint(log, fName, "Child Process's STDERR is %d.\n",
        job->ngsj_stdio.ngsio_error);

    return pid;
}
Example #11
0
static int
ngislSSHjobPBSwriteScript(
    ngisSSHjob_t *job,
    FILE *fp)
{
    ngisSSHjobAttributes_t *attr = NULL;
    int result;
    ngisLog_t *log = NULL;
    static const char fName[] = "ngislSSHjobPBSwriteScript";

    NGIS_ASSERT(job != NULL);
    NGIS_ASSERT(fp != NULL);

    attr = job->ngsj_attributes;
    log = job->ngsj_log;

    result = ngislSSHjobPBSwriteScriptHeader(job, fp);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't write script header.\n");
        return 0;
    }

    switch (attr->ngsja_jobBackend) {
    case NGIS_BACKEND_NORMAL:
        if (attr->ngsja_count > 1) {
            result = ngisSSHjobWriteArrayJobCommandToScript(
                job, fp, NGISL_PBS_NODEFILE, attr->ngsja_sshPBSrsh);
        } else {
            result = ngisSSHjobWriteSingleJobCommandToScript(job, fp);
        }
        break;
    case NGIS_BACKEND_MPI:
    case NGIS_BACKEND_BLACS:
        result = ngisSSHjobWriteMPICommandToScript(
            job, fp, NGISL_PBS_NODEFILE);
        break;
    default:
        result = 0;
        NGIS_ASSERT_NOTREACHED();
    }
    if (result < 0) {
        ngisErrorPrint(log, fName, "Can't print command string.\n");
        return 0;
    }

    return 1;
}
Example #12
0
static int
ngislSSHjobTransferStderr(ngisSSHjob_t *job)
{
    int result;
    static const char fName[] = "ngislSSHjobTransferStderr";

    NGIS_ASSERT(job != NULL);

    result = ngislSSHjobTransferStdouterr(job, NGIS_SSH_JOB_TRANSFER_STDERR);
    if (result == 0) {
        ngisErrorPrint(job->ngsj_log, fName, "Can't transfer STDERR.\n");
    }
    return result;
}
Example #13
0
static int
ngislSSHjobPBSsubmitStart(
    ngisSSHjob_t *job)
{
    int result;
    static const char fName[] = "ngislSSHjobPBSsubmitStart";

    NGIS_ASSERT(job != NULL);

    job->ngsj_iExecutables = 0;
    result = ngisSSHjobSetStatus(job, NGIS_EXECUTABLE_STATUS_SUBMITTING);
    if (result == 0) {
        ngisErrorPrint(job->ngsj_log, fName,
            "Can't start to submit the job.\n");
        return 0;
    }

    return ngislSSHjobPBSsubmit(job);
}
Example #14
0
static int
ngislSSHjobPBSwriteResource(
    ngisSSHjob_t *job,
    FILE *fp)
{
    ngisSSHjobAttributes_t *attr;
    int ppn;
    int count;
    int fraction;
    int nNodes;
    int result = -1;
    static const char fName[] = "ngislSSHjobPBSwriteResource";

    NGIS_ASSERT(job != NULL);
    NGIS_ASSERT(fp != NULL);

    attr = job->ngsj_attributes;

    ppn      = attr->ngsja_sshPBSprocessorsPerNode;
    count    = attr->ngsja_count;
    fraction = count%ppn;
    nNodes   = count/ppn;

    if ((nNodes > 0) && (fraction > 0)) {
        result = fprintf(fp, "#PBS -l nodes=%d:ppn=%d+nodes=1:ppn=%d\n",
            nNodes, ppn, fraction);
    } else if (nNodes > 0) {
        result = fprintf(fp, "#PBS -l nodes=%d:ppn=%d\n",
            nNodes, ppn);
    } else if (fraction > 0) {
        result = fprintf(fp, "#PBS -l nodes=1:ppn=%d\n",
            fraction);
    } else {
        NGIS_ASSERT_NOTREACHED();
    }
    if (result < 0) {
        ngisErrorPrint(job->ngsj_log, fName,
            "Can't print number of executable to scriptfile.\n");
        return 0;
    }
    return 1;
}
Example #15
0
/**
 * SSH Job: Finalize
 */
static int
ngislSSHjobFinalize(
    ngisJob_t *job)
{
    int result;
    int ret = 1;
    ngisLog_t *log;
    ngisSSHjob_t *sshJob = (ngisSSHjob_t *)job;
    static const char fName[] = "ngislSSHjobFinalize";
    
    NGIS_ASSERT(sshJob != NULL);

    log = job->ngj_log;

    result = ngislSSHjobDisable(sshJob);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't disable the SSH job.");
    }
    
    /* Destroy Executables */
    if (sshJob->ngsj_executables != NULL) {
        ngislExecutableDestroy(
            sshJob->ngsj_executables, sshJob->ngsj_nExecutables);
        sshJob->ngsj_executables = NULL;
    }

    NGIS_NULL_CHECK_AND_DESTROY(
        sshJob->ngsj_attributes, ngisSSHjobAttributesDestroy);
    NGIS_NULL_CHECK_AND_FREE(sshJob->ngsj_remoteExecutablePath);
    NGIS_NULL_CHECK_AND_FREE(sshJob->ngsj_remoteTempdir);
    NGIS_NULL_CHECK_AND_FREE(sshJob->ngsj_remoteTempdirBase);
    NGIS_NULL_CHECK_AND_FREE(sshJob->ngsj_pidRemoteSh);

    if (!NGIS_LIST_IS_INVALID_VALUE(&sshJob->ngsj_funcQueue)) {
        NGIS_LIST_FINALIZE(ngisSSHjobFunc_t, &sshJob->ngsj_funcQueue);
    }

    ngislSSHjobInitializeMember(sshJob);

    return ret;
}
Example #16
0
/**
 * SSH job: Finish to query the job status
 */
int
ngisSSHjobFinishQuery(
    ngisSSHjob_t *job)
{
    int result;
    ngisLog_t *log = NULL;
    static const char fName[] = "ngisSSHjobFinishQuery";

    log = job->ngsj_log;

    NGIS_ASSERT(job != NULL);
    NGIS_ASSERT(job->ngsj_statusChecking != 0);

    job->ngsj_statusChecking = 0;
    if (job->ngsj_cancelCalled != 0) {
        result = ngisJobCancel((ngisJob_t *)job);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't cancel the job.\n");
            return 0;
        }
    }
    return 1;
}
Example #17
0
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;
}
Example #18
0
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;
}
Example #19
0
/**
 * 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;
}
Example #20
0
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;
}
Example #21
0
/**
 * SSH Job: Cancel
 */
static int
ngislSSHjobCancel(
    ngisJob_t* job)
{
    ngisSSHjob_t *sshJob = (ngisSSHjob_t *)job;
    int result = 0;
    ngisExecutableStatus_t status;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobCancel";    

    NGIS_ASSERT(sshJob != NULL);

    log = sshJob->ngsj_log;
    
    ngisDebugPrint(log, fName, "called.\n");
    sshJob->ngsj_cancelCalled = 0;

    status = ngisSSHjobGetStatus(sshJob);
    switch (status) {
    case NGIS_EXECUTABLE_STATUS_UNSUBMITTED:
        result = ngisSSHjobDone(sshJob);
        if (result == 0) {
            ngisErrorPrint(NULL, fName,
                "The job isn't able to be done.\n");
        }
        break;
    case NGIS_EXECUTABLE_STATUS_SUBMITTING:
        ngisDebugPrint(log, fName, "Delayed cancel.\n");
        sshJob->ngsj_cancelCalled = 1;
        break;
    case NGIS_EXECUTABLE_STATUS_PENDING:
    case NGIS_EXECUTABLE_STATUS_ACTIVE:
        if (sshJob->ngsj_statusChecking == 0) {
            switch (sshJob->ngsj_jobManagerType) {
            case NGIS_SSH_JOB_MANAGER_NORMAL:
                result = ngislSSHjobDoCancel(sshJob);
                break;
            case NGIS_SSH_JOB_MANAGER_SGE:
                result = ngisSSHjobSGEdoCancel(sshJob);
                break;
            case NGIS_SSH_JOB_MANAGER_PBS:
                result = ngisSSHjobPBSdoCancel(sshJob);
                break;
            default:
                NGIS_ASSERT_NOTREACHED();
            }
            if (result == 0) {
                ngisErrorPrint(log, fName, "Can't cancel the job.\n");
                return 0;
            }
        } else {
            ngisDebugPrint(log, fName, "Delayed cancel.\n");
            sshJob->ngsj_cancelCalled = 1;
        }
        break;
    case NGIS_EXECUTABLE_STATUS_DONE:
    case NGIS_EXECUTABLE_STATUS_FAILED:
    case NGIS_EXECUTABLE_STATUS_STAGEOUT:
    case NGIS_EXECUTABLE_STATUS_FAILED_STAGEOUT:
    case NGIS_EXECUTABLE_STATUS_EXIT:
        break;
    }

    return 1; 
}
Example #22
0
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;
}
Example #23
0
/**
 * SSH job: Callback for polling
 */
void
ngisSSHjobPollingCallback(
    void *arg,
    ngisCallbackResult_t cResult)
{
    ngisSSHjob_t *job = arg;
    int result;
    ngisLog_t *log;
    static const char fName[] = "ngisSSHjobPollingCallback";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;
    job->ngsj_timerCallback = NULL;

    ngisDebugPrint(log, fName, "Called.\n");

    switch (cResult) {
    case NGIS_CALLBACK_RESULT_SUCCESS:
        break;
    case NGIS_CALLBACK_RESULT_CANCEL:
        ngisDebugPrint(log, fName, "Canceled.\n");
        return;
    case NGIS_CALLBACK_RESULT_FAILED:
        ngisErrorPrint(log, fName, "Failed.\n");
        goto error;
    default:
        NGIS_ASSERT_NOTREACHED();
    }

    /* Next */
    result = ngisSSHjobPollingStart(job);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't continue status polling.\n");
        goto error;
    }

    if (job->ngsj_statusChecking != 0) {
        ngisWarningPrint(log, fName, 
            "Previous status checking still has not finished. "
            "Skip checking of this time.\n");
        /* DONOTHING */
        return;
    } else {
        /* Check Start */
        ngisSSHjobFunctionReset(job);
        job->ngsj_statusChecking = 1;
        job->ngsj_iExecutables = 0;
        switch (job->ngsj_jobManagerType) {
        case NGIS_SSH_JOB_MANAGER_NORMAL:
            result = ngislSSHjobQueryStatus(job);
            break;
        case NGIS_SSH_JOB_MANAGER_SGE:
            result = ngisSSHjobSGEqueryStatus(job);
            break;
        case NGIS_SSH_JOB_MANAGER_PBS:
            result = ngisSSHjobPBSqueryStatus(job);
            break;
        default:
            NGIS_ASSERT_NOTREACHED();
        }
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't query status.\n");
            goto error;
        }
    }
    
    return;
error:
    result = ngisSSHjobProcessAfterExit(job);
    if (result == 0) {
        ngisErrorPrint(NULL, fName, "The job isn't able to be done.\n");
    }
    return;
}
Example #24
0
static int
ngislSSHjobPBSwriteScriptHeader(
    ngisSSHjob_t *job,
    FILE *fp)
{
    int maxWallTime;
    int maxCpuTime;
    ngisSSHjobAttributes_t *attr;
    int result;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobPBSwriteScriptHeader";

    NGIS_ASSERT(job != NULL);
    NGIS_ASSERT(fp != NULL);

    attr = job->ngsj_attributes;
    log  = job->ngsj_log;

    /* SHELL */
    result = fprintf(fp, 
        "#! %s\n"
        "#PBS -S %s\n",
        attr->ngsja_sshRemoteSh,
        attr->ngsja_sshRemoteSh);
    if (result < 0) {
        ngisErrorPrint(log, fName, "Can't print shell to scriptfile.\n");
        return 0;
    }

    result = ngislSSHjobPBSwriteResource(job, fp);
    if (result == 0) {
        ngisErrorPrint(log, fName,
            "Can't print required resources to scriptfile.\n");
        return 0;
    }

    result = fprintf(fp, 
        "#PBS -e %s\n" 
        "#PBS -o %s\n",
        NGIS_DEV_NULL, NGIS_DEV_NULL);
    if (result < 0) {
        ngisErrorPrint(log, fName, "Can't print output to scriptfile.\n");
        return 0;
    }

    if (attr->ngsja_queueName != NULL) {
        result = fprintf(fp, "#PBS -q %s\n", attr->ngsja_queueName);
        if (result < 0) {
            ngisErrorPrint(log, fName,
                "Can't print queue name to scriptfile.\n");
            return 0;
        }
    }

    if (attr->ngsja_project != NULL) {
        result = fprintf(fp, "#PBS -A %s\n", attr->ngsja_project);
        if (result < 0) {
            ngisErrorPrint(log, fName,
                "Can't print project name to scriptfile.\n");
            return 0;
        }
    }

    /* Max Wall Time */
    maxWallTime = 0;
    if (attr->ngsja_maxWallTime > 0) {
        maxWallTime = attr->ngsja_maxWallTime;
    } else if (attr->ngsja_maxTime > 0) {
        maxWallTime = attr->ngsja_maxTime;
    }
    if (maxWallTime > 0) {
        result = fprintf(fp, "#PBS -l walltime=%d:00\n",
            maxWallTime);
        if (result < 0) {
            ngisErrorPrint(log, fName,
                "Can't print max wall time to scriptfile.\n");
            return 0;
        }
    }
    /* Max CPU Time */
    maxCpuTime = 0;
    if (attr->ngsja_maxCpuTime > 0) {
        maxCpuTime = attr->ngsja_maxCpuTime;
    } else if (attr->ngsja_maxTime > 0) {
        maxCpuTime = attr->ngsja_maxTime;
    }
    if (maxCpuTime > 0) {
        result = fprintf(fp, "#PBS -l pcput=%d:00\n",
            maxCpuTime);
        if (result < 0) {
            ngisErrorPrint(log, fName,
                "Can't print cpu wall time to scriptfile.\n");
            return 0;
        }
    }

    if (attr->ngsja_maxMemory > 0) {
        result = fprintf(fp, "#PBS -l mem=%dmb\n",
            attr->ngsja_maxMemory);
        if (result < 0) {
            ngisErrorPrint(log, fName,
                "Can't print max memory to scriptfile.\n");
            return 0;
        }
    }
    result = fprintf(fp, "\n");

    return 1;
}
Example #25
0
static int
ngislSSHjobTransferStdouterr(
    ngisSSHjob_t *job,
    ngisSSHjobTransferTarget_t targetType)
{
    ngisSSHjobFileTransfer_t *fileTransfer = NULL;
    int ret = 1;
    int result;
    char *remoteFileName = NULL;
    char *localFileName = NULL;
    char *remoteFileNameFormat = NULL;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobTransferStdouterr";
    
    NGIS_ASSERT(job != NULL);

    log  = job->ngsj_log;

    switch (targetType) {
    case NGIS_SSH_JOB_TRANSFER_STDOUT:
        remoteFileNameFormat = "%s/stdout.*";
        localFileName        = job->ngsj_attributes->ngsja_stdoutFile;
        break;
    case NGIS_SSH_JOB_TRANSFER_STDERR:
        remoteFileNameFormat = "%s/stderr.*";
        localFileName        = job->ngsj_attributes->ngsja_stderrFile;
        break;
    default:
        NGIS_ASSERT_NOTREACHED();
    }

    remoteFileName = ngisStrdupPrintf(
        remoteFileNameFormat, job->ngsj_remoteTempdir);
    if (remoteFileName == NULL) {
        ngisErrorPrint(log, fName,
            "Can't copy string of temporary filename.\n");
        ret = 0;
        goto finalize;
    }

    fileTransfer = ngisSSHjobFileTransferCreate(
        job, localFileName, remoteFileName,
        NGIS_SSH_JOB_FILE_TRANSFER_REMOTE_TO_LOCAL);
    if (fileTransfer == NULL) {
        ngisErrorPrint(log, fName, "Can't create file transfer.\n");
        ret = 0;
        goto finalize;
    }
    result = ngisSSHjobFileTransferStart(fileTransfer);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't start file transfer.\n");
        ret = 0;
        goto finalize;
    }
finalize:
    NGIS_NULL_CHECK_AND_FREE(remoteFileName);

    if ((ret == 0) && (fileTransfer != NULL)) {
        result = ngisSSHjobFileTransferDestroy(fileTransfer);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't destroy file transfer.\n");
        }
    }
    return ret;
}
Example #26
0
/**
 * 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;
}
Example #27
0
int
main(int argc, char *argv[])
{
    int result;
    char *logFileName = NGIS_DEV_NULL;
    int invokeServerInitialized = 0;
    int exitCode = 0;/* Success */
    int opt;
    struct sigaction sa;    
    int debug = 0;
    int i;
    static const int ignoreSignals[] = {
        SIGPIPE, 
        SIGINT,
        SIGTSTP, 
    };
    static const char fName[] = "main";

    /* Options analyze */
    while ((opt = getopt(argc, argv, "l:d")) >= 0) {
        switch (opt) {
        case 'l':
            /* LOG */
            logFileName = optarg;
            break;
        case 'd':
            debug = 1;
            break;
        case '?':
        default:
            /* Ignore arguments */
            ;
        }
    }
    
    result = ngisLogInitializeModule(logFileName, NGIS_LOG_LEVEL_DEBUG);
    if (result == 0) {
        return 1;
    }

    ngisDebugPrint(NULL, fName, "#========================================#\n");
    ngisDebugPrint(NULL, fName, "#      Invoke Server SSH Started         #\n");
    ngisDebugPrint(NULL, fName, "#========================================#\n");
    
    /* Signal */
    sa.sa_flags   = 0;
    sa.sa_handler = SIG_IGN;
    result = sigemptyset(&sa.sa_mask);
    if (result < 0) {
        ngisErrorPrint(NULL, fName, "sigemptyset: %s\n", strerror(errno));
        exitCode = 1;
        goto finalize;
    }

    for (i = 0;i < NGIS_NELEMENTS(ignoreSignals);++i) {
        result = sigaction(ignoreSignals[i], &sa, NULL);
        if (result < 0) {
            ngisErrorPrint(NULL, fName, "sigaction: %s\n", strerror(errno));
            exitCode = 1;
            goto finalize;
        }    
    }
    
    /* Invoke Server */
    result = ngInvokeServerInitialize(&ngislSSHjobTypeInformation);
    if (result == 0) {
        ngisErrorPrint(NULL, fName, "Can't Initialize Invoke Server.\n");
        exitCode = 1;
        goto finalize;
    }
    invokeServerInitialized = 1;
    
    result = ngInvokeServerRun();
    if (result == 0) {
        ngisErrorPrint(NULL, fName, "Invoke Server can't run.\n");
        exitCode = 1;
        goto finalize;
    }
    
finalize:
    if (invokeServerInitialized != 0) {
        result = ngInvokeServerFinalize();
        if (result == 0) {
            ngisErrorPrint(NULL, fName, "Can't finalize Invoke Server.\n");
            exitCode = 1;
        }
    }

    ngisDebugPrint(NULL, fName, "#========================================#\n");
    ngisDebugPrint(NULL, fName, "#      Invoke Server SSH Exit            #\n");
    ngisDebugPrint(NULL, fName, "#========================================#\n");
    
    ngisLogFinalizeModule();
    /* Ignore result */    
    
    return 0;
}
Example #28
0
static void
ngislSSHjobQueryStatusReadCallback(
    void *arg,
    ngisLineBuffer_t *lBuf,
    char *line,
    ngisCallbackResult_t cResult)
{
    ngisSSHjob_t *job = arg;
    ngisCallback_t callback;
    int result;
    char *command;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobQueryStatusReadCallback";

    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 data.\n");
        goto error;
    case NGIS_CALLBACK_RESULT_SUCCESS:
        break;
    default:
        NGIS_ASSERT_NOTREACHED();
    }

    if (strcmp(line, NGIS_SSH_JOB_NONE) == 0) {
        /* Check next executable */
        job->ngsj_iExecutables++;
        result = ngislSSHjobQueryStatus(job);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't query the status.\n");
            goto error;
        }
    } else if (strcmp(line, NGIS_SSH_JOB_EXIT) == 0) {
        /* Wait the child process and get exit status */
        job->ngsj_nextReadCallback = ngislSSHjobExecutableDoneReadCallback;
        callback = ngisCallbackWriteFormat(
            job->ngsj_stdio.ngsio_in, ngisSSHjobWriteStringCallback, job,
            "wait %s;%s $?\n",
            job->ngsj_executables[job->ngsj_iExecutables].nge_identifier,
            NGIS_ECHO_COMMAND);
        if (!ngisCallbackIsValid(callback)) {
            ngisErrorPrint(log, fName, 
                "Can't write command getting exit code.\n");
            goto error;
        }
        command = NULL;
    } else {
        NGIS_ASSERT_NOTREACHED();
    }

    return;

error:
    result = ngisSSHjobProcessAfterExit(job);
    if (result == 0) {
        ngisErrorPrint(NULL, fName,
            "The job isn't able to be done.\n");
    }
    return;
}
Example #29
0
static int
ngislSSHjobPBScreateScript(
    ngisSSHjob_t *job)
{
    char *filename = NULL;
    FILE *fp = NULL;
    int result;
    int ret = 1;
    ngisLog_t *log;
    static const char fName[] = "ngislSSHjobPBScreateScript";

    NGIS_ASSERT(job != NULL);

    log = job->ngsj_log;

    if ((job->ngsj_attributes->ngsja_jobBackend == NGIS_BACKEND_NORMAL) && 
        (job->ngsj_attributes->ngsja_count > 1)) {
        /* For Array Job */
        result = ngisSSHjobCreateScriptForArrayJob(job);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't create script for array job.\n");
            ret = 0;
            goto finalize;
        }
    }

    filename = ngisMakeTempFile(job->ngsj_attributes->ngsja_tmpDir);
    if (filename == NULL) {
        ngisErrorPrint(log, fName, "Can't make temporary file.\n");
        ret = 0;
        goto finalize;
    }
    job->ngsj_localScriptName = filename;

    fp = fopen(filename, "w");
    if (fp == NULL) {
        ngisErrorPrint(log, fName, "Can't create FILE structure.\n");
        ret = 0;
        goto finalize;
    }

    result = ngislSSHjobPBSwriteScript(job, fp);
    if (result == 0) {
        ngisErrorPrint(log, fName, "Can't write script to file.\n");
        ret = 0;
        goto finalize;
    }
    
finalize:
    /* Close */
    if (fp != NULL) {
        result = fclose(fp);
        if (result != 0) {
            ngisErrorPrint(log, fName, "fclose: %s.\n", strerror(errno));
            ret = 0;
        }
        fp = NULL;
    }

    ngisLogDumpFile(log, NGIS_LOG_LEVEL_DEBUG,
        fName, job->ngsj_localScriptName);

    if (ret != 0) {
        result = ngisSSHjobFunctionPop(job);
        if (result == 0) {
            ngisErrorPrint(log, fName, "Can't execute the next process.\n");
            ret = 0;
        }
    }
    return ret;
}
Example #30
0
int
ngisSSHjobProcessAfterExit(
    ngisSSHjob_t *job)
{
    int result;
    ngisLog_t *log = NULL;
    int i;
    ngisExecutable_t *exe = NULL;
    ngisExecutableStatus_t jobStatus;
    static const char fName[] = "ngisSSHjobProcessAfterExit";
    
    NGIS_ASSERT(job != NULL);

    log  = job->ngsj_log;
    jobStatus = ngisSSHjobGetStatus(job);

    ngisSSHjobFunctionReset(job);
    job->ngsj_iExecutables = 0;

    /* Stop polling */
    if (ngisCallbackIsValid(job->ngsj_timerCallback)) {
        ngisCallbackCancel(job->ngsj_timerCallback);
        job->ngsj_timerCallback = NULL;
    }

    if (job->ngsj_afterExit != 0) {
        goto error;
    }
    job->ngsj_afterExit = 1;

    if ((job->ngsj_attributes->ngsja_redirectEnable != 0) &&
        (jobStatus > NGIS_EXECUTABLE_STATUS_SUBMITTING)   &&
        (jobStatus < NGIS_EXECUTABLE_STATUS_STAGEOUT)) {

        for (i = 0;i < job->ngsj_nExecutables;++i) {
            exe = &job->ngsj_executables[i];
            if (exe->nge_status == NGIS_EXECUTABLE_STATUS_FAILED) {
                result = ngisSSHjobExecutableSetStatus(
                    job, exe, NGIS_EXECUTABLE_STATUS_FAILED_STAGEOUT);
            } else {
                result = ngisSSHjobExecutableSetStatus(
                    job, exe, NGIS_EXECUTABLE_STATUS_STAGEOUT);
            }
            if (result == 0) {
                ngisErrorPrint(log, fName, "Can't set status.\n");            
                goto error;
            }
        }

        /* Redirect outerr */
        result = ngisSSHjobFunctionPush(job, ngislSSHjobTransferStdout);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't register the function for transfer STDOUT.\n");
            goto error;
        }
        result = ngisSSHjobFunctionPush(job, ngislSSHjobTransferStderr);
        if (result == 0) {
            ngisErrorPrint(log, fName,
                "Can't register the function for transfer STDERR.\n");
            goto error;
        }
    } 

    result = ngisSSHjobFunctionPush(job, ngisSSHjobDone);
    if (result == 0) {
        ngisErrorPrint(log, fName,
            "Can't register the function for transfer job done.\n");
        goto error;
    }

    result = ngisSSHjobFunctionPop(job);
    if (result == 0) {
        ngisErrorPrint(NULL, fName,
            "Fails the registered function.\n");
        return 0;
    }
    
    return 1;
error:
    result = ngisSSHjobDone(job);
    if (result == 0) {
        ngisErrorPrint(NULL, fName, "Can't done the job.\n");
    }
    return 0;
}