1 /*
2 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3 * University Research and Technology
4 * Corporation. All rights reserved.
5 * Copyright (c) 2004-2005 The University of Tennessee and The University
6 * of Tennessee Research Foundation. All rights
7 * reserved.
8 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9 * University of Stuttgart. All rights reserved.
10 * Copyright (c) 2004-2005 The Regents of the University of California.
11 * All rights reserved.
12 * $COPYRIGHT$
13 *
14 * Additional copyrights may follow
15 *
16 * $HEADER$
17 */
18
19
20 #include "opal_config.h"
21
22 #include <stdio.h>
23 #include <errno.h>
24 #ifdef HAVE_SYS_WAIT_H
25 #include <sys/wait.h>
26 #endif
27 #include <stdlib.h>
28 #ifdef HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif
31
32 #include "opal/util/few.h"
33 #include "opal/util/basename.h"
34 #include "opal/util/argv.h"
35 #include "opal/constants.h"
36
37 int opal_few(char *argv[], int *status)
38 {
39 #if defined(HAVE_FORK) && defined(HAVE_EXECVE) && defined(HAVE_WAITPID)
40 pid_t pid, ret;
41
42 if ((pid = fork()) < 0) {
43 return OPAL_ERR_IN_ERRNO;
44 }
45
46 /* Child execs. If it fails to exec, exit. */
47
48 else if (0 == pid) {
49 execvp(argv[0], argv);
50 exit(errno);
51 }
52
53 /* Parent loops waiting for the child to die. */
54
55 else {
56 do {
57 /* If the child exited, return */
58
59 if (pid == (ret = waitpid(pid, status, 0))) {
60 break;
61 }
62
63 /* If waitpid was interrupted, loop around again */
64
65 else if (ret < 0) {
66 if (EINTR == errno) {
67 continue;
68 }
69
70 /* Otherwise, some bad juju happened -- need to quit */
71
72 return OPAL_ERR_IN_ERRNO;
73 }
74 } while (true);
75 }
76
77 /* Return the status to the caller */
78
79 return OPAL_SUCCESS;
80 #else
81 return OPAL_ERR_NOT_SUPPORTED;
82 #endif
83 }