2004-01-11 00:18:25 +03:00
|
|
|
/*
|
2005-11-05 22:57:48 +03:00
|
|
|
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
|
|
|
|
* University Research and Technology
|
|
|
|
* Corporation. All rights reserved.
|
|
|
|
* Copyright (c) 2004-2005 The University of Tennessee and The University
|
|
|
|
* of Tennessee Research Foundation. All rights
|
|
|
|
* reserved.
|
2004-11-28 23:09:25 +03:00
|
|
|
* Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
|
|
|
|
* University of Stuttgart. All rights reserved.
|
2005-03-24 15:43:37 +03:00
|
|
|
* Copyright (c) 2004-2005 The Regents of the University of California.
|
|
|
|
* All rights reserved.
|
2004-11-22 04:38:40 +03:00
|
|
|
* $COPYRIGHT$
|
|
|
|
*
|
|
|
|
* Additional copyrights may follow
|
|
|
|
*
|
2004-01-11 00:18:25 +03:00
|
|
|
* $HEADER$
|
|
|
|
*/
|
|
|
|
|
2004-01-11 07:37:28 +03:00
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal_config.h"
|
2005-07-04 04:13:44 +04:00
|
|
|
|
2004-01-11 00:18:25 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
2004-01-11 00:18:25 +03:00
|
|
|
#include <sys/wait.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-01-11 00:18:25 +03:00
|
|
|
#include <stdlib.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-01-11 00:18:25 +03:00
|
|
|
#include <unistd.h>
|
2004-10-20 05:03:09 +04:00
|
|
|
#endif
|
2004-01-11 00:18:25 +03:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
#include "opal/util/few.h"
|
2006-08-26 03:22:35 +04:00
|
|
|
#include "opal/util/basename.h"
|
|
|
|
#include "opal/util/argv.h"
|
2006-02-12 04:33:29 +03:00
|
|
|
#include "opal/constants.h"
|
2004-01-11 00:18:25 +03:00
|
|
|
|
2005-07-04 04:13:44 +04:00
|
|
|
int opal_few(char *argv[], int *status)
|
2004-01-11 00:18:25 +03:00
|
|
|
{
|
2005-12-13 01:01:51 +03:00
|
|
|
#ifndef __WINDOWS__
|
2005-07-14 22:05:30 +04:00
|
|
|
#if defined(HAVE_FORK) && defined(HAVE_EXECVE) && defined(HAVE_WAITPID)
|
2004-01-11 07:37:28 +03:00
|
|
|
pid_t pid, ret;
|
2004-01-11 00:18:25 +03:00
|
|
|
|
|
|
|
if ((pid = fork()) < 0) {
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_IN_ERRNO;
|
2004-01-11 07:37:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Child execs. If it fails to exec, exit. */
|
|
|
|
|
2004-01-11 00:18:25 +03:00
|
|
|
else if (0 == pid) {
|
2004-01-11 07:37:28 +03:00
|
|
|
execvp(argv[0], argv);
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parent loops waiting for the child to die. */
|
|
|
|
|
2004-01-11 00:18:25 +03:00
|
|
|
else {
|
2004-01-11 07:37:28 +03:00
|
|
|
do {
|
|
|
|
/* If the child exited, return */
|
|
|
|
|
|
|
|
if (pid == (ret = waitpid(pid, status, 0))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If waitpid was interrupted, loop around again */
|
|
|
|
|
|
|
|
else if (ret < 0) {
|
|
|
|
if (EINTR == errno) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, some bad juju happened -- need to quit */
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_IN_ERRNO;
|
2004-01-11 07:37:28 +03:00
|
|
|
}
|
|
|
|
} while (true);
|
2004-01-11 00:18:25 +03:00
|
|
|
}
|
2004-01-11 07:37:28 +03:00
|
|
|
|
|
|
|
/* Return the status to the caller */
|
|
|
|
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_SUCCESS;
|
2005-07-14 22:05:30 +04:00
|
|
|
#else
|
2006-02-12 04:33:29 +03:00
|
|
|
return OPAL_ERR_NOT_SUPPORTED;
|
2005-07-14 22:05:30 +04:00
|
|
|
#endif
|
|
|
|
|
2004-10-22 20:06:05 +04:00
|
|
|
#else
|
|
|
|
|
2006-08-23 08:19:07 +04:00
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
DWORD process_stat;
|
|
|
|
char* command = argv[0];
|
|
|
|
char* exec_command;
|
|
|
|
|
|
|
|
ZeroMemory (&si, sizeof(si));
|
|
|
|
ZeroMemory (&pi, sizeof(pi));
|
|
|
|
|
|
|
|
_flushall(); /* Push all output */
|
|
|
|
|
|
|
|
GetStartupInfo (&si);
|
|
|
|
argv[0] = opal_basename( command );
|
|
|
|
exec_command = opal_argv_join( argv, ' ' );
|
|
|
|
free( argv[0] );
|
|
|
|
argv[0] = command;
|
|
|
|
if (!CreateProcess (argv[0],
|
|
|
|
(LPSTR)exec_command,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
TRUE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi)){
|
|
|
|
*status = (int)GetLastError();
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for child to die */
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
if( 0 == GetExitCodeProcess(pi.hProcess, &process_stat) ) {
|
|
|
|
*status = (int)GetLastError();
|
|
|
|
return OPAL_ERROR;
|
|
|
|
}
|
|
|
|
*status = (int)process_stat;
|
|
|
|
|
|
|
|
return OPAL_SUCCESS;
|
2004-10-22 20:06:05 +04:00
|
|
|
#endif
|
2004-01-11 00:18:25 +03:00
|
|
|
}
|