Formalize the simple_exec*() API

This commit is contained in:
Asanka Herath
2009-08-26 14:43:32 -04:00
committed by Love Hornquist Astrand
parent 7ee1695f12
commit 49b261efdc
3 changed files with 39 additions and 27 deletions

View File

@@ -288,6 +288,18 @@ SigAction signal(int iSig, SigAction pAction); /* BSD compatible */
#endif #endif
#endif #endif
#define SE_E_UNSPECIFIED (-1)
#define SE_E_FORKFAILED (-2)
#define SE_E_WAITPIDFAILED (-3)
#define SE_E_EXECTIMEOUT (-4)
#define SE_E_NOEXEC 126
#define SE_E_NOTFOUND 127
#define SE_PROCSTATUS(st) (((st) >= 0 && (st) < 126)? st: -1)
#define SE_PROCSIGNAL(st) (((st) >= 128)? (st) - 128: -1)
#define SE_IS_ERROR(st) ((st) < 0 || (st) >= 126)
#define simple_execve rk_simple_execve #define simple_execve rk_simple_execve
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
simple_execve(const char*, char*const[], char*const[]); simple_execve(const char*, char*const[], char*const[]);
@@ -475,7 +487,7 @@ rk_cloexec(int);
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
rk_cloexec_file(FILE *); rk_cloexec_file(FILE *);
int ROKEN_LIB_FUNCTION ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
ct_memcmp(const void *, const void *, size_t); ct_memcmp(const void *, const void *, size_t);
ROKEN_CPP_END ROKEN_CPP_END

View File

@@ -52,13 +52,13 @@
#define EX_NOTFOUND 127 #define EX_NOTFOUND 127
/* return values: /* return values:
-1 on `unspecified' system errors SE_E_UNSPECIFIED on `unspecified' system errors
-2 on fork failures SE_E_FORKFAILED on fork failures
-3 on waitpid errors SE_E_WAITPIDFAILED on waitpid errors
-4 exec timeout SE_E_EXECTIMEOUT exec timeout
0- is return value from subprocess 0- is return value from subprocess
126 if the program couldn't be executed SE_E_NOEXEC if the program couldn't be executed
127 if the program couldn't be found SE_E_NOTFOUND if the program couldn't be found
128- is 128 + signal that killed subprocess 128- is 128 + signal that killed subprocess
possible values `func' can return: possible values `func' can return:
@@ -98,7 +98,7 @@ wait_for_process_timed(pid_t pid, time_t (*func)(void *),
while(waitpid(pid, &status, 0) < 0) { while(waitpid(pid, &status, 0) < 0) {
if (errno != EINTR) { if (errno != EINTR) {
ret = -3; ret = SE_E_WAITPIDFAILED;
goto out; goto out;
} }
if (func == NULL) if (func == NULL)
@@ -110,7 +110,7 @@ wait_for_process_timed(pid_t pid, time_t (*func)(void *),
kill(pid, SIGTERM); kill(pid, SIGTERM);
continue; continue;
} else if (timeout == (time_t)-2) { } else if (timeout == (time_t)-2) {
ret = -4; ret = SE_E_EXECTIMEOUT;
goto out; goto out;
} }
alarm(timeout); alarm(timeout);
@@ -211,7 +211,7 @@ pipe_execv(FILE **stdin_fd, FILE **stdout_fd, FILE **stderr_fd,
close(err_fd[0]); close(err_fd[0]);
close(err_fd[1]); close(err_fd[1]);
} }
return -2; return SE_E_FORKFAILED;
default: default:
if(stdin_fd != NULL) { if(stdin_fd != NULL) {
close(in_fd[0]); close(in_fd[0]);
@@ -236,7 +236,7 @@ simple_execvp_timed(const char *file, char *const args[],
pid_t pid = fork(); pid_t pid = fork();
switch(pid){ switch(pid){
case -1: case -1:
return -2; return SE_E_FORKFAILED;
case 0: case 0:
execvp(file, args); execvp(file, args);
exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
@@ -259,7 +259,7 @@ simple_execve_timed(const char *file, char *const args[], char *const envp[],
pid_t pid = fork(); pid_t pid = fork();
switch(pid){ switch(pid){
case -1: case -1:
return -2; return SE_E_FORKFAILED;
case 0: case 0:
execve(file, args, envp); execve(file, args, envp);
exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC); exit((errno == ENOENT) ? EX_NOTFOUND : EX_NOEXEC);
@@ -285,7 +285,7 @@ simple_execlp(const char *file, ...)
argv = vstrcollect(&ap); argv = vstrcollect(&ap);
va_end(ap); va_end(ap);
if(argv == NULL) if(argv == NULL)
return -1; return SE_E_UNSPECIFIED;
ret = simple_execvp(file, argv); ret = simple_execvp(file, argv);
free(argv); free(argv);
return ret; return ret;
@@ -304,7 +304,7 @@ simple_execle(const char *file, ... /* ,char *const envp[] */)
envp = va_arg(ap, char **); envp = va_arg(ap, char **);
va_end(ap); va_end(ap);
if(argv == NULL) if(argv == NULL)
return -1; return SE_E_UNSPECIFIED;
ret = simple_execve(file, argv, envp); ret = simple_execve(file, argv, envp);
free(argv); free(argv);
return ret; return ret;

View File

@@ -51,7 +51,7 @@ RCSID("$Id$");
* the callback function is called. THe possible return values * the callback function is called. THe possible return values
* from the callback function are: * from the callback function are:
* *
* - ((time_t) -2) Exit loop without killing child and return -4. * - ((time_t) -2) Exit loop without killing child and return SE_E_EXECTIMEOUT.
* - ((time_t) -1) Kill child with SIGTERM and wait for child to exit. * - ((time_t) -1) Kill child with SIGTERM and wait for child to exit.
* - 0 Don't timeout again * - 0 Don't timeout again
* - n Seconds to next timeout * - n Seconds to next timeout
@@ -60,12 +60,12 @@ RCSID("$Id$");
* *
* @param[in] timeout Seconds to first timeout. * @param[in] timeout Seconds to first timeout.
* *
* @retval -1 Unspecified system error * @retval SE_E_UNSPECIFIED Unspecified system error
* @retval -2 Fork failure (not applicable for _WIN32 targets) * @retval SE_E_FORKFAILED Fork failure (not applicable for _WIN32 targets)
* @retval -3 waitpid errors * @retval SE_E_WAITPIDFAILED waitpid errors
* @retval -4 exec timeout * @retval SE_E_EXECTIMEOUT exec timeout
* @retval 0- Return value from subprocess * @retval 0 <= Return value from subprocess
* @retval 126 The program coudln't be found * @retval SE_E_NOTFOUND The program coudln't be found
* @retval 128- The signal that killed the subprocess +128. * @retval 128- The signal that killed the subprocess +128.
*/ */
ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
@@ -78,7 +78,7 @@ wait_for_process_timed(pid_t pid, time_t (*func)(void *),
int rv = 0; int rv = 0;
if (hProcess == NULL) if (hProcess == NULL)
return -4; return SE_E_WAITPIDFAILED;
dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000); dtimeout = (DWORD) ((timeout == 0)? INFINITE: timeout * 1000);
@@ -106,12 +106,12 @@ wait_for_process_timed(pid_t pid, time_t (*func)(void *),
dtimeout = INFINITE; dtimeout = INFINITE;
continue; continue;
} }
rv = -1; rv = SE_E_UNSPECIFIED;
break; break;
} else if (timeout == (time_t) -2) { } else if (timeout == (time_t) -2) {
rv = -4; rv = SE_E_EXECTIMEOUT;
break; break;
} else { } else {
@@ -123,7 +123,7 @@ wait_for_process_timed(pid_t pid, time_t (*func)(void *),
} else { } else {
rv = -1; rv = SE_E_UNSPECIFIED;
break; break;
} }
@@ -403,7 +403,7 @@ simple_execlp(const char *file, ...)
argv = vstrcollect(&ap); argv = vstrcollect(&ap);
va_end(ap); va_end(ap);
if(argv == NULL) if(argv == NULL)
return -1; return SE_E_UNSPECIFIED;
ret = simple_execvp(file, argv); ret = simple_execvp(file, argv);
free(argv); free(argv);
return ret; return ret;
@@ -423,7 +423,7 @@ simple_execle(const char *file, ... /* ,char *const envp[] */)
envp = va_arg(ap, char **); envp = va_arg(ap, char **);
va_end(ap); va_end(ap);
if(argv == NULL) if(argv == NULL)
return -1; return SE_E_UNSPECIFIED;
ret = simple_execve(file, argv, envp); ret = simple_execve(file, argv, envp);
free(argv); free(argv);
return ret; return ret;