{"id":94778,"date":"2024-04-29T07:10:10","date_gmt":"2024-04-28T23:10:10","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=94778"},"modified":"2024-04-29T07:10:10","modified_gmt":"2024-04-28T23:10:10","slug":"analysis-linux-patron","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/analysis-linux-patron.html","title":{"rendered":"\u5256\u6790Linux\u7684\u5b88\u62a4\u795e"},"content":{"rendered":"
<\/p>\n
\u5b88\u62a4\u8fdb\u7a0b(Daemon)\u662f\u5728\u540e\u53f0\u8fd0\u884c\u7684\u4e00\u79cd\u7279\u6b8a\u8fdb\u7a0b\uff0c\u5b83\u8131\u79bb\u4e8e\u7ec8\u7aef\uff0c\u4ece\u800c\u8fd9\u53ef\u907f\u514d\u8fdb\u7a0b\u88ab\u4efb\u4f55\u7ec8\u7aef\u6240\u4ea7\u751f\u7684\u4fe1\u53f7\u6253\u65ad\uff0c\u5b83\u5728\u6267\u884c\u8fdb\u7a0b\u4e2d\u7684\u4ea7\u751f\u4fe1\u606f\u4e5f\u4e0d\u5728\u4efb\u4f55\u7ec8\u7aef\u4e0a\u663e\u793a\u3002\u5b88\u62a4\u8fdb\u7a0b\u5468\u671f\u6027\u5730\u6267\u884c\u67d0\u79cd\u4efb\u52a1\u6216\u7b49\u5f85\u5904\u7406\u67d0\u4e9b\u53d1\u751f\u7684\u4e8b\u4ef6\uff0cLinux\u7684\u5927\u591a\u6570\u670d\u52a1\u5668\u5c31\u662f\u7528\u5b88\u62a4\u8fdb\u7a0b\u5b9e\u73b0\u7684\u3002<\/p>\n
\/* \u5904\u7406\u53ef\u80fd\u7684\u7ec8\u7aef\u4fe1\u53f7 *\/\r\nsignal(SIGTTOU, SIG_IGN);\r\nsignal(SIGTTIN, SIG_IGN);\r\nsignal(SIGTSTP, SIG_IGN);\r\nsignal(SIGHUP , SIG_IGN);\r\n<\/pre>\n<\/li>\n
\/* \u662f\u7236\u8fdb\u7a0b\uff0c\u7ed3\u675f\u7236\u8fdb\u7a0b\uff0c\u5b50\u8fdb\u7a0b\u7ee7\u7eed *\/\r\nif(fork())\r\n exit(0);\r\n<\/pre>\n<\/li>\n
\u57fa\u4e8e\u4ee5\u4e0a\u539f\u56e0\uff0c\u9700\u8981\u8ba9\u4e3a\u4e2a\u5b50\u8fdb\u7a0b\u5f7b\u5e95\u6446\u8131\u8be5\u7ec8\u7aef\u7684\u5f71\u54cd\uff0c\u9700\u8981\u8c03\u7528setsid()\u4f7f\u5b50\u8fdb\u7a0b\u6210\u4e3a\u65b0\u7684\u4f1a\u8bdd\u7ec4\u957f\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
setsid();<\/pre>\nsetsid()\u8c03\u7528\u6210\u529f\u540e\uff0c\u8c03\u7528\u6b64\u51fd\u6570\u7684\u8fdb\u7a0b\u6210\u4e3a\u65b0\u7684\u4f1a\u8bdd\u7ec4\u957f\u548c\u65b0\u7684\u8fdb\u7a0b\u7ec4\u957f\uff0c\u5e76\u4e0e\u539f\u6765\u7684\u8fdb\u7a0b\u7ec4\u8131\u79bb\u5173\u7cfb\u3002\u7531\u4e8e\u4f1a\u8bdd\u8fc7\u7a0b\u5bf9\u63a7\u5236\u7ec8\u7aef\u7684\u72ec\u5360\u6027\uff0c\u8fdb\u7a0b\u540c\u65f6\u4e0e\u63a7\u5236\u7ec8\u7aef\u8131\u79bb\u3002<\/li>\n
\/* \u7ed3\u675f\u7b2c\u4e00\u5b50\u8fdb\u7a0b\uff0c\u7b2c\u4e8c\u5b50\u8fdb\u7a0b\u7ee7\u7eed *\/\r\nif(fork())\r\n exit(0);\r\n<\/pre>\n<\/li>\n
#define NOFILE 256\r\n\r\nfor(i=0; i<\/pre>\n<\/li>\n
chdir(\"\/tmp\");<\/pre>\n<\/li>\n
umask(0);\r\n<\/pre>\n<\/li>\n
\/* \u5c06\u5b50\u8fdb\u7a0b\u9000\u51fa\u4fe1\u53f7\u8bbe\u4e3aSIG_IGN\uff0c\u8ba9\u7cfb\u7edf\u5e2e\u52a9\u56de\u6536\u8fdb\u7a0b\u8d44\u6e90 *\/\r\nsignal(SIGCHLD, SIG_IGN);<\/pre>\n\u6574\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
#define NOFILE 256\r\n\r\nvoid DaemonMode()\r\n{\r\n int num = 0;\r\n int fd0, fd1, fd2;\r\n\r\n \/* \u5c4f\u853d\u53ef\u80fd\u7684\u4fe1\u53f7 *\/\r\n signal(SIGTTOU, SIG_IGN);\r\n signal(SIGTTIN, SIG_IGN);\r\n signal(SIGTSTP, SIG_IGN);\r\n signal(SIGHUP , SIG_IGN);\r\n\r\n if(fork())\r\n exit(0);\r\n\r\n setsid();\r\n\r\n if(fork())\r\n exit(0);\r\n\r\n chdir(\"\/tmp\/httpd\");\r\n\r\n umask(0);\r\n\r\n for(; num<\/pre>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n
\u8865\u5145 setsid() \u51fd\u6570\u529f\u80fd\uff1a<\/strong><\/span><\/div>\n\u5982\u679c\u8c03\u7528\u8fdb\u7a0b\u5df2\u7ecf\u662f\u4e00\u4e2a\u8fdb\u7a0b\u7ec4\u7684\u7ec4\u957f\uff0c\u5219\u6b64\u51fd\u6570\u8fd4\u56de\u9519\u8bef\u3002\u4e3a\u4e86\u675c\u7edd\u8fd9\u79cd\u60c5\u51b5\uff0c\u901a\u5e38\u5148\u8c03\u7528fork()\u521b\u5efa\u5b50\u8fdb\u7a0b\uff0c\u7136\u540e\u4f7f\u5176\u7236\u8fdb\u7a0b\u7ec8\u6b62\uff0c\u800c\u5b50\u8fdb\u7a0b\u7ee7\u7eed\uff0c\u5728\u5b50\u8fdb\u7a0b\u4e2d\u8c03\u7528\u6b64\u51fd\u6570\u3002<\/p>\n
\u5982\u679c\u8c03\u7528\u6b64\u51fd\u6570\u7684\u8fdb\u7a0b\u4e0d\u662f\u4e00\u4e2a\u8fdb\u7a0b\u7ec4\u7ec4\u957f\uff0c\u5219\u6b64\u51fd\u6570\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u4f1a\u8bdd\uff0c\u8c03\u7528setsid()\u51fd\u6570\u7684\u8fdb\u7a0b\u6210\u4e3a\u65b0\u7684\u4f1a\u8bdd\u7684\u9886\u5934\u8fdb\u7a0b\uff0c\u5e76\u4e0e\u5176\u7236\u8fdb\u7a0b\u7684\u4f1a\u8bdd\u7ec4\u548c\u8fdb\u7a0b\u7ec4\u8131\u79bb\u3002\u7531\u4e8e\u4f1a\u8bdd\u5bf9\u63a7\u5236\u7ec8\u7aef\u7684\u72ec\u5360\u6027\uff0c\u8fdb\u7a0b\u540c\u65f6\u4e0e\u63a7\u5236\u7ec8\u7aef\u8131\u79bb\u3002<\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttp:\/\/www.linuxidc.com\/Linux\/2018-02\/150773.htm<\/a><\/p>\n