exec*
Objectives
We would like to :
- Look at exec
Notes
-
int execve(const char *pathname, char *const _Nullable argv[],
char *const _Nullable envp[]);
- Needs unistd.h
- pathname must point to an executable file: binary or script.
- If it is a script it must start with #!interpreter
- Argv as discussed, but argv[0] should be the name of the program executed.
- envp as discussed.
- The man page for this is great and you should read it.
-
execl, execlp, execle, execv, execvp, execvpe
- Library functions to make exec easier
- If tehre is an l:
- argvs are passed as individual parameters.
- The last arg must be NULL, (0), or nullptr.
- If there is a v:
- Args are passed as a vector.
- The first should be the name of the program.
- The last must contain a NULL, 0, or nullptr
- If threre is an e:
- A new environment is passed
- Null terminated list of pointers.
- Otherwise the current environment is used.
- If there is a p: the file name is given not the full path, and the environment variable PATH is searched for the executable.
- This is ignored if the name contains a /
- The current environment, not the environment passed.
- It will continue if it finds a non-executable version
- If it doesn't know the interpreter it will use /bin/sh
-
-
int system(const char * command)
- Will spawn a child process (shell) and run the command.
- The man page says
execl("/bin/sh", "sh", "-c", command, (char *) NULL);
- If the command is NULL, return value indicates if there is a shell available.
- 0 no shell
- not zero, shell
- Otherwise no return, or -1 on error.
- This is inefficient.
- But a quick way to run a process