execve(2) system call has a number of helper functions giving users many options to specify the
command line args. (e.g. execl, execlp, execle, execv, execvp, execvpe...).
A way to specify the args as one string (as args are given when typing a command using a shell) is missing.
More precisely, it was missing, because the library s2argv-execs has
filled the lack.
- because otherwise programmers use the unsafe
system(3) - or (even worse) use
fork/execof/bin/ssh -c "..." - wise programmers must survive code wrestling using strtok
the syntax is clear and follows the standard naming scheme of exec* functions:
int execs(const char *path, const char *args);
int execse(const char *path, const char *args, char *const envp[]);
int execsp(const char *args);
int execspe(const char *args, char *const envp[]);Notes:
- Command arguments in args are delimited by space characters (blank, tabs or new lines). Single or
double quotes can be used to delimitate command arguments including spaces and a non quoted back‐
slash (
\) is the escape character to protect the next char. execspdoes not need any pathname, it usesargv[0]as parsed fromargs.- args is const, i.e.
exec*functions do not modify it. execs*functions do not use dynamic allocation (allocate memory on the stack)execs*functions are thread safe- the library provides also eexecs functions (using less memory, but modifying args)
- for lazy programmers, the library includes replacements for
system(3)andpopen(3)(namedsystem_noshandpopen_noshrespectively) using execs instead of starting a shell. system_safeis a safe version ofsystem(3). It solves the security problems ofsystem: attacks using environment variables (e.g. PATH), command separators (like;||&&) or redirections.
Virtualsquare uses execs in many places. An incomplete list includes:
- cado/scado.c: to start the command
- libvdestack/vdestack.c: uses a more advanced interface provided by the library
s2multiargv(mutiple commands, semicolon;separated.)