Question

My question is about the exec family of C system calls. How do I predict what type of input the program I am executing is looking for since the exec family varies between a list of strings arg1, arg2, arg3 ... and a 2d char array for its parameters. Can most Linux command line programs handle both forms of input?

for example:

int execl(const char *path, const char *arg0, ... /*, (char *)0 */);
int execv(const char *path, char *const argv[]);

What would happen if I executed ls:

execl('/bin/ls', 'ls', NULL);

versus:

execv('/bin/ls', lsArgsArray);

Presumably ls can handle both forms of input, but my real question is generic about other utilities in general, is it safe to assume that you can invoke them either way? If it is not safe to invoke with either method how would something like bash distinguish which to use?

Was it helpful?

Solution

Neither the executed program nor the kernel see any difference whatever one you call: It is both converted to the same system call by your C library. The executed program comes one additional step behind the kernel, thus has even less chance to see any difference.

For the exact kernel interface, look in the sources of either the kernel or your platform C library.

The shell (bash/ash/csh/sh/...) is not in any way priviliged. Still, going directly to the API call allows you to pass a bogus program name.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top