下面這段 vsftpd 的 source code ,就是使 vsftpd 啟動後,在背景執行的方法:
5455 /* 56 * tracing date : 2006-10-17 57 */58 if (tunable_background)59 {60 /* 61 * fork() 62 */63 int forkret = vsf_sysutil_fork();64 if (forkret > 0)65 {66 /* Parent, just exit */67 vsf_sysutil_exit(0);68 }6970 /* 71 * 2006-10-17 - so.. setsid() 72 */73 vsf_sysutil_make_session_leader();74 }75
方法是,透過 fork() child process,以 setsid() 讓 child process 成為 process leader,有自己的 process group,
parent process 單純結束,下面是 vsf_sysutil_make_session_leader()
vsftpd-2.0.3/sysutil.c
24632464 void2465 vsf_sysutil_make_session_leader(void)2466 {2467 /* This makes us the leader if we are not already */2468 (void) setsid();2469 /* Check we're the leader */2470 if (getpid() != getpgrp())2471 {2472 die("not session leader");2473 }2474 }2475
參考文獻
Douglas E. Comer, David L. Stevens, Internetworking with TCP/IP, Vol. III: Client-Server Programming and Applications, Linux/Posix Sockets Version, Vol. 3, Chap. 30, 2000.