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 36 of file exec.h.

Referenced by exec_fork(), exec_fork_background(), exec_fork_pipe(), and exec_replace().


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 28 of file exec_fork.c.

References _lucid_vasprintf(), argv_from_str(), and EXEC_MAX_ARGV.

00029 {
00030         va_list ap;
00031         va_start(ap, fmt);
00032         
00033         char *cmd;
00034         _lucid_vasprintf(&cmd, fmt, ap);
00035         
00036         va_end(ap);
00037         
00038         int argc;
00039         char *argv[EXEC_MAX_ARGV];
00040         
00041         argc = argv_from_str(cmd, argv, EXEC_MAX_ARGV);
00042         
00043         if (argc < 1) {
00044                 free(cmd);
00045                 return errno = EINVAL, -1;
00046         }
00047         
00048         pid_t pid;
00049         int status;
00050         
00051         switch ((pid = fork())) {
00052         case -1:
00053                 free(cmd);
00054                 return -1;
00055         
00056         case 0:
00057                 usleep(200);
00058                 
00059                 execvp(argv[0], argv);
00060                 
00061                 /* never get here */
00062                 exit(errno);
00063         
00064         default:
00065                 free(cmd);
00066                 
00067                 if (waitpid(pid, &status, 0) == -1)
00068                         return -1;
00069         }
00070         
00071         return status;
00072 }

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 28 of file exec_fork_background.c.

References _lucid_vasprintf(), argv_from_str(), and EXEC_MAX_ARGV.

00029 {
00030         va_list ap;
00031         va_start(ap, fmt);
00032         
00033         char *cmd;
00034         _lucid_vasprintf(&cmd, fmt, ap);
00035         
00036         va_end(ap);
00037         
00038         int argc;
00039         char *argv[EXEC_MAX_ARGV];
00040         
00041         argc = argv_from_str(cmd, argv, EXEC_MAX_ARGV);
00042         
00043         if (argc < 1) {
00044                 free(cmd);
00045                 return errno = EINVAL, -1;
00046         }
00047         
00048         pid_t pid;
00049         int i;
00050         
00051         switch ((pid = fork())) {
00052         case -1:
00053                 return -1;
00054         
00055         case 0:
00056                 usleep(200);
00057                 
00058                 for (i = 0; i < 100; i++)
00059                         close(i);
00060                 
00061                 execvp(argv[0], argv);
00062         
00063         default:
00064                 signal(SIGCHLD, SIG_IGN);
00065         }
00066         
00067         free(cmd);
00068         return 0;
00069 }

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 29 of file exec_fork_pipe.c.

References _lucid_vasprintf(), argv_from_str(), EXEC_MAX_ARGV, and io_read_eof().

00030 {
00031         va_list ap;
00032         va_start(ap, fmt);
00033         
00034         char *cmd;
00035         _lucid_vasprintf(&cmd, fmt, ap);
00036         
00037         va_end(ap);
00038         
00039         int argc;
00040         char *argv[EXEC_MAX_ARGV];
00041         
00042         argc = argv_from_str(cmd, argv, EXEC_MAX_ARGV);
00043         
00044         if (argc < 1) {
00045                 free(cmd);
00046                 return errno = EINVAL, -1;
00047         }
00048         
00049         int outfds[2];
00050         
00051         if (pipe(outfds) == -1)
00052                 return -1;
00053         
00054         pid_t pid;
00055         int status;
00056         
00057         switch ((pid = fork())) {
00058         case -1:
00059                 free(cmd);
00060                 return -1;
00061         
00062         case 0:
00063                 usleep(200);
00064                 
00065                 close(outfds[0]);
00066                 
00067                 dup2(outfds[1], STDOUT_FILENO);
00068                 dup2(outfds[1], STDERR_FILENO);
00069                 
00070                 execvp(argv[0], argv);
00071                 
00072                 /* never get here */
00073                 exit(errno);
00074         
00075         default:
00076                 free(cmd);
00077                 
00078                 close(outfds[1]);
00079                 
00080                 if (out && io_read_eof(outfds[0], out) == -1)
00081                         return -1;
00082                 
00083                 close(outfds[0]);
00084                 
00085                 if (waitpid(pid, &status, 0) == -1)
00086                         return -1;
00087         }
00088         
00089         return status;
00090 }

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 27 of file exec_replace.c.

References _lucid_vasprintf(), argv_from_str(), and EXEC_MAX_ARGV.

00028 {
00029         va_list ap;
00030         va_start(ap, fmt);
00031         
00032         char *cmd;
00033         _lucid_vasprintf(&cmd, fmt, ap);
00034         
00035         va_end(ap);
00036         
00037         int argc;
00038         char *argv[EXEC_MAX_ARGV];
00039         
00040         argc = argv_from_str(cmd, argv, EXEC_MAX_ARGV);
00041         
00042         if (argc < 1) {
00043                 free(cmd);
00044                 return errno = EINVAL, -1;
00045         }
00046         
00047         execvp(argv[0], argv);
00048         
00049         /* never get here */
00050         free(cmd);
00051         return -1;
00052 }


Generated on Sun Dec 3 17:46:16 2006 for lucid by  doxygen 1.5.1