Command execution wrappers


Detailed Description

The exec family of functions provide convenient wrappers around fork(2), execve(2), waitpid(2) and pipe(2).

These functions combine one or more of the above system calls in one function, thus allowing fast and simple process creation in applications.


Defines

#define EXEC_MAX_ARGV   64
 maximum number of arguments that will be converted for execvp(2)

Functions

int exec_fork (const char *fmt,...)
 fork, execvp and wait
int exec_fork_background (const char *fmt,...)
 fork, execvp and ignore child
int exec_fork_pipe (char **out, const char *fmt,...)
 pipe, fork, execvp and wait
int exec_replace (const char *fmt,...)
 plain execvp


Define Documentation

#define EXEC_MAX_ARGV   64

maximum number of arguments that will be converted for execvp(2)

Definition at line 35 of file exec.h.


Function Documentation

int exec_fork ( const char *  fmt,
  ... 
)

fork, execvp and wait

Parameters:
[in] fmt format string passed to printf
[in] ... variable number of arguments according to fmt
Returns:
status obtained by wait(2) or -1 with errno set
See also:
Formatted output conversion

execvp(2)

Definition at line 27 of file exec_fork.c.

References _lucid_vasprintf(), mem_alloc(), mem_free(), strtok_count(), strtok_free(), strtok_init_str(), and strtok_toargv().

00028 {
00029         va_list ap;
00030         va_start(ap, fmt);
00031 
00032         char *cmd;
00033 
00034         if (_lucid_vasprintf(&cmd, fmt, ap) == -1) {
00035                 va_end(ap);
00036                 return -1;
00037         }
00038 
00039         va_end(ap);
00040 
00041         strtok_t _st, *st = &_st;
00042 
00043         if (!strtok_init_str(st, cmd, " ", 0)) {
00044                 mem_free(cmd);
00045                 return -1;
00046         }
00047 
00048         mem_free(cmd);
00049 
00050         int argc    = strtok_count(st);
00051         char **argv = mem_alloc((argc + 1) * sizeof(char *));
00052 
00053         if (!argv) {
00054                 strtok_free(st);
00055                 return -1;
00056         }
00057 
00058         if (strtok_toargv(st, argv) < 1) {
00059                 mem_free(argv);
00060                 strtok_free(st);
00061                 return -1;
00062         }
00063 
00064         pid_t pid;
00065         int status;
00066 
00067         switch ((pid = fork())) {
00068         case -1:
00069                 return -1;
00070 
00071         case 0:
00072                 usleep(200);
00073                 execvp(argv[0], argv);
00074 
00075                 /* never get here */
00076                 exit(1);
00077 
00078         default:
00079                 mem_free(argv);
00080                 strtok_free(st);
00081 
00082                 if (waitpid(pid, &status, 0) == -1)
00083                         return -1;
00084         }
00085 
00086         return status;
00087 }

int exec_fork_background ( const char *  fmt,
  ... 
)

fork, execvp and ignore child

Parameters:
[in] fmt format string passed to printf
[in] ... variable number of arguments according to fmt
Returns:
0 on success or -1 with errno set
See also:
Formatted output conversion

execvp(2)

Note:
this function closes file descriptors 0-100 before execvp

Definition at line 26 of file exec_fork_background.c.

References _lucid_vasprintf(), mem_alloc(), mem_free(), strtok_count(), strtok_free(), strtok_init_str(), and strtok_toargv().

00027 {
00028         va_list ap;
00029         va_start(ap, fmt);
00030 
00031         char *cmd;
00032 
00033         if (_lucid_vasprintf(&cmd, fmt, ap) == -1) {
00034                 va_end(ap);
00035                 return -1;
00036         }
00037 
00038         va_end(ap);
00039 
00040         strtok_t _st, *st = &_st;
00041 
00042         if (!strtok_init_str(st, cmd, " ", 0)) {
00043                 mem_free(cmd);
00044                 return -1;
00045         }
00046 
00047         mem_free(cmd);
00048 
00049         int argc    = strtok_count(st);
00050         char **argv = mem_alloc((argc + 1) * sizeof(char *));
00051 
00052         if (!argv) {
00053                 strtok_free(st);
00054                 return -1;
00055         }
00056 
00057         if (strtok_toargv(st, argv) < 1) {
00058                 mem_free(argv);
00059                 strtok_free(st);
00060                 return -1;
00061         }
00062 
00063         pid_t pid;
00064         int i;
00065 
00066         switch ((pid = fork())) {
00067         case -1:
00068                 return -1;
00069 
00070         case 0:
00071                 usleep(200);
00072 
00073                 for (i = 0; i < 100; i++)
00074                         close(i);
00075 
00076                 execvp(argv[0], argv);
00077 
00078         default:
00079                 mem_free(argv);
00080                 strtok_free(st);
00081                 signal(SIGCHLD, SIG_IGN);
00082         }
00083 
00084         return 0;
00085 }

int exec_fork_pipe ( char **  out,
const char *  fmt,
  ... 
)

pipe, fork, execvp and wait

Parameters:
[out] out empty pointer to store combined stdout/stderr
[in] fmt format string passed to printf
[in] ... variable number of arguments according to fmt
Returns:
status obtained by wait(2) or -1 with errno set
Note:
The caller should free obtained memory for out using free(3)
See also:
Formatted output conversion

malloc(3)

free(3)

execvp(2)

Definition at line 28 of file exec_fork_pipe.c.

References _lucid_vasprintf(), mem_alloc(), mem_free(), str_readfile(), strtok_count(), strtok_free(), strtok_init_str(), and strtok_toargv().

00029 {
00030         va_list ap;
00031         va_start(ap, fmt);
00032 
00033         char *cmd;
00034 
00035         if (_lucid_vasprintf(&cmd, fmt, ap) == -1) {
00036                 va_end(ap);
00037                 return -1;
00038         }
00039 
00040         va_end(ap);
00041 
00042         strtok_t _st, *st = &_st;
00043 
00044         if (!strtok_init_str(st, cmd, " ", 0)) {
00045                 mem_free(cmd);
00046                 return -1;
00047         }
00048 
00049         mem_free(cmd);
00050 
00051         int argc    = strtok_count(st);
00052         char **argv = mem_alloc((argc + 1) * sizeof(char *));
00053 
00054         if (!argv) {
00055                 strtok_free(st);
00056                 return -1;
00057         }
00058 
00059         if (strtok_toargv(st, argv) < 1) {
00060                 mem_free(argv);
00061                 strtok_free(st);
00062                 return -1;
00063         }
00064 
00065         int outfds[2];
00066 
00067         if (pipe(outfds) == -1) {
00068                 mem_free(argv);
00069                 strtok_free(st);
00070                 return -1;
00071         }
00072 
00073         pid_t pid;
00074         int status;
00075 
00076         switch ((pid = fork())) {
00077         case -1:
00078                 mem_free(argv);
00079                 strtok_free(st);
00080                 close(outfds[0]);
00081                 close(outfds[1]);
00082                 return -1;
00083 
00084         case 0:
00085                 usleep(200);
00086 
00087                 close(outfds[0]);
00088 
00089                 dup2(outfds[1], STDOUT_FILENO);
00090                 dup2(outfds[1], STDERR_FILENO);
00091 
00092                 execvp(argv[0], argv);
00093 
00094                 mem_free(argv);
00095                 strtok_free(st);
00096 
00097                 /* never get here */
00098                 exit(1);
00099 
00100         default:
00101                 mem_free(argv);
00102                 strtok_free(st);
00103 
00104                 close(outfds[1]);
00105 
00106                 if (out && str_readfile(outfds[0], out) == -1)
00107                         return -1;
00108 
00109                 close(outfds[0]);
00110 
00111                 if (waitpid(pid, &status, 0) == -1)
00112                         return -1;
00113         }
00114 
00115         return status;
00116 }

int exec_replace ( const char *  fmt,
  ... 
)

plain execvp

Parameters:
[in] fmt format string passed to printf
[in] ... variable number of arguments according to fmt
Returns:
only returns on error with errno set
See also:
Formatted output conversion

execvp(2)

Definition at line 25 of file exec_replace.c.

References _lucid_vasprintf(), mem_alloc(), mem_free(), strtok_count(), strtok_free(), strtok_init_str(), and strtok_toargv().

00026 {
00027         va_list ap;
00028         va_start(ap, fmt);
00029 
00030         char *cmd;
00031 
00032         if (_lucid_vasprintf(&cmd, fmt, ap) == -1) {
00033                 va_end(ap);
00034                 return -1;
00035         }
00036 
00037         va_end(ap);
00038 
00039         strtok_t _st, *st = &_st;
00040 
00041         if (!strtok_init_str(st, cmd, " ", 0)) {
00042                 mem_free(cmd);
00043                 return -1;
00044         }
00045 
00046         mem_free(cmd);
00047 
00048         int argc    = strtok_count(st);
00049         char **argv = mem_alloc((argc + 1) * sizeof(char *));
00050 
00051         if (!argv) {
00052                 strtok_free(st);
00053                 return -1;
00054         }
00055 
00056         if (strtok_toargv(st, argv) < 1) {
00057                 mem_free(argv);
00058                 strtok_free(st);
00059                 return -1;
00060         }
00061 
00062         execvp(argv[0], argv);
00063 
00064         /* never get here */
00065         mem_free(argv);
00066         strtok_free(st);
00067         return -1;
00068 }


Generated on Tue Jun 19 20:38:45 2007 for lucid by  doxygen 1.5.2