4 ms·
if I want to exec() something with specific env vars then I have to go through /usr/bin/env. Sigh. Why not use execve[0] to set up a specific environment f
by AdieuToLogic 9y ago
if I want to exec() something with specific
env vars then I have to go through
/usr/bin/env. Sigh.
Why not use execve[0] to set up a specific environment for child processes?
0 - https://www.freebsd.org/cgi/man.cgi?query=execve&apropos=0&sektion=0&manpath=FreeBSD+11.1-RELEASE+and+Ports&arch=default&format=html https://www.freebsd.org/cgi/man.cgi?query=execve&apropos=0&s...
- 0xcde4c3db 9y agoMaybe a tangent, but I never really understood how the memory pointed to by envp is supposed to be managed. Does fork() copy the allocator state such that you can just do malloc(), fork(), then free() in the parent process, then execve() blows away the whole memory map in the child?
- AdieuToLogic 9y agoYes, the flow you describe is pretty much how it goes. The OS will "overlay" the fork'ed child process if exec succeeds in loading the program specified. If not, errno is set and typically the child process reports the problem (using something like strerror_r[0]) then exits. In either case, the parent process can free the memory allocated for the execve call once it returns. 0 - https://www.freebsd.org/cgi/man.cgi?query=strerror_r&apropos=0&sektion=3&manpath=FreeBSD+11.1-RELEASE+and+Ports&arch=default&format=html https://www.freebsd.org/cgi/man.cgi?query=strerror_r&apropos...
- cryptonector 9y agoNothing special is needed here. execve() does blow away the address space of the process and replaces it. As to fork(), it copies the address space. Of course, you can't putenv() on the child-side of fork() if the parent was multi-threaded, and you may not even be able to getenv() in that case either (check the standards and your C library's fork() man page).
- JdeBP 9y agoThis is also the principle underpinning Bernstein's pathexec. * https://cr.yp.to/lib/pathexec.html https://cr.yp.to/lib/pathexec.html