Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

fork

process

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?)

like image 362
Billy Avatar asked Dec 30 '17 09:12

Billy


1 Answers

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

Hope this helps!

like image 74
Louis Langholtz Avatar answered Sep 28 '22 07:09

Louis Langholtz