Change the interface to lam_few() -- add a "int *status" argument to pass
back the status from the child process. The return type is now an int indicating whether the few was successful or not; if it wasn't, the exact error will be in errno. This commit was SVN r251.
Этот коммит содержится в:
родитель
b3bc80c5c5
Коммит
0df6bf703a
@ -2,44 +2,85 @@
|
|||||||
* $HEADER$
|
* $HEADER$
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "lam_config.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "lam_config.h"
|
#include "lam/constants.h"
|
||||||
#include "lam/util/few.h"
|
#include "lam/util/few.h"
|
||||||
|
|
||||||
/** @file **/
|
/** @file **/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* forks, execs and waits for a subordinate program
|
* Forks, execs, and waits for a subordinate program
|
||||||
*
|
*
|
||||||
* @param argv Argument vector, argv[0] is the program
|
* @param argv Null-terminated argument vector; argv[0] is the program
|
||||||
* @retval Status code or ERROR
|
* (same as arguments to execvp())
|
||||||
* Returns: - status code or ERROR
|
*
|
||||||
|
* @param status Upon success, will be filled with the return status
|
||||||
|
* from waitpid(2). The WIF* macros can be used to examine the value
|
||||||
|
* (see waitpid(2)).
|
||||||
|
*
|
||||||
|
* @retval LAM_SUCCESS If the child launched and exited.
|
||||||
|
* @retval LAM_ERR_IN_ERRNO If a failure occurred, errno should be
|
||||||
|
* examined for the specific error.
|
||||||
|
*
|
||||||
|
* This function forks, execs, and waits for an executable to
|
||||||
|
* complete. The input argv must be a NULL-terminated array (perhaps
|
||||||
|
* built with the lam_arr_*() interface). Upon success, LAM_SUCCESS
|
||||||
|
* is returned. This function will wait either until the child
|
||||||
|
* process has exited or waitpid() returns an error other than EINTR.
|
||||||
|
*
|
||||||
|
* Note that a return of LAM_SUCCESS does \em not imply that the child
|
||||||
|
* process exited successfully -- it simply indicates that the child
|
||||||
|
* process exited. The WIF* macros (see waitpid(2)) should be used to
|
||||||
|
* examine the status to see hold the child exited.
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
lam_few(char *argv[])
|
lam_few(char *argv[], int *status)
|
||||||
{
|
{
|
||||||
int status;
|
pid_t pid, ret;
|
||||||
int pid;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if ((pid = fork()) < 0) {
|
if ((pid = fork()) < 0) {
|
||||||
return(pid);
|
return LAM_ERR_IN_ERRNO;
|
||||||
}
|
|
||||||
/* child */
|
|
||||||
else if (0 == pid) {
|
|
||||||
execvp(argv[0], argv);
|
|
||||||
exit(errno);
|
|
||||||
}
|
|
||||||
/* parent */
|
|
||||||
else {
|
|
||||||
while ((0 != waitpid(pid, &status, 0)) &&
|
|
||||||
(! WIFEXITED(status)) && (EINTR == errno));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return status;
|
/* Child execs. If it fails to exec, exit. */
|
||||||
|
|
||||||
|
else if (0 == pid) {
|
||||||
|
execvp(argv[0], argv);
|
||||||
|
exit(errno);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Parent loops waiting for the child to die. */
|
||||||
|
|
||||||
|
else {
|
||||||
|
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 */
|
||||||
|
|
||||||
|
return LAM_ERR_IN_ERRNO;
|
||||||
|
}
|
||||||
|
} while (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return the status to the caller */
|
||||||
|
|
||||||
|
return LAM_SUCCESS;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,6 @@
|
|||||||
#ifndef LAM_FEW_H
|
#ifndef LAM_FEW_H
|
||||||
#define LAM_FEW_H
|
#define LAM_FEW_H
|
||||||
|
|
||||||
int lam_few (char *argv[]);
|
int lam_few(char *argv[], int *status);
|
||||||
|
|
||||||
#endif /* LAM_FEW_H */
|
#endif /* LAM_FEW_H */
|
||||||
|
Загрузка…
Ссылка в новой задаче
Block a user