Billy
Billy

Reputation: 1293

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

Upvotes: 15

Views: 2802

Answers (1)

Louis Langholtz
Louis Langholtz

Reputation: 3123

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!

Upvotes: 17

Related Questions