Search code examples
processforkexec

Why does sudo call fork() and exec() rather than just exec()?


Why does sudo (among other similar commands) start a new process by calling fork() before exec(), rather than just running the command within the same process? sudo doesn't do anything after it runs the command. (Or does it?)


Solution

  • If sudo merely called exec, then sudo couldn't have done things like run any cleanup tasks after the exec'd code completed. Take pam_open_session and pam_close_session for example.

    Hope this helps!