{"id":426,"date":"2025-12-16T10:54:03","date_gmt":"2025-12-16T02:54:03","guid":{"rendered":"https:\/\/www.garfield-blog.top\/?p=426"},"modified":"2025-12-16T10:54:32","modified_gmt":"2025-12-16T02:54:32","slug":"%e3%80%90%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e3%80%91procd%e7%9b%91%e5%90%ac%e8%bf%9b%e7%a8%8b%e6%97%a0%e8%be%93%e5%87%ba%e9%97%ae%e9%a2%98%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=426","title":{"rendered":"\u3010\u6e90\u7801\u89e3\u6790\u3011procd\u76d1\u542c\u8fdb\u7a0b\u65e0\u8f93\u51fa\u95ee\u9898\u5206\u6790"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u5173\u4e8e\u88ab\u6ce8\u518c\u7684\u8fdb\u7a0b\u7684\u8f93\u51fa<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u5728openwrt\u67b6\u6784\u7684\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u5e38\u4f1a\u9047\u5230\u81ea\u5df1\u6dfb\u52a0\u7684\u6a21\u5757\u4e2d\u7684printf\u8f93\u51fa\u770b\u4e0d\u5230\u7684\u60c5\u51b5\uff0c\u6545\u67e5\u770bprocd\u6e90\u7801\u5e0c\u671b\u53ef\u4ee5\u627e\u5230\u95ee\u9898\u539f\u56e0<\/p>\n<\/blockquote>\n\n\n\n<p><strong>\u5173\u952e\u51fd\u6570\u8c03\u7528\u6808<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>rc\n    -&gt;_rc\n        -&gt;add_initd\n            -&gt;q_initd_run\n                -&gt;ustream_fd_init\n                    -&gt;ustream_fd_set_uloop\n                        -&gt;uloop_fd_add<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u88ab\u76d1\u542c\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u51fa\uff0c\u6807\u51c6\u9519\u8bef\u7684\u91cd\u5b9a\u5411<\/h2>\n\n\n\n<p><strong><code>q_initd_run<\/code><\/strong>\u51fd\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static void q_initd_run(struct runqueue *q, struct runqueue_task *t)\n{\n    struct initd *s = container_of(t, struct initd, proc.task);\n    int pipefd&#91;2];\n    pid_t pid;\n\n    clock_gettime(CLOCK_MONOTONIC_RAW, &amp;s-&gt;ts_start);\n    DEBUG(2, \"start %s %s \\n\", s-&gt;file, s-&gt;param);\n    if (pipe(pipefd) == -1) {\n        ERROR(\"Failed to create pipe: %m\\n\");\n        return;\n    }\n\n    pid = fork();\n    if (pid &lt; 0)\n        return;\n\n    if (pid) {\n        close(pipefd&#91;1]);\n        fcntl(pipefd&#91;0], F_SETFD, FD_CLOEXEC);\n        s-&gt;fd.stream.string_data = true,\n        s-&gt;fd.stream.notify_read = pipe_cb,\n        runqueue_process_add(q, &amp;s-&gt;proc, pid);\n        ustream_fd_init(&amp;s-&gt;fd, pipefd&#91;0]);\n        return;\n    }\n    close(pipefd&#91;0]);\n\n    int devnull = open(\"\/dev\/null\"c, O_RDONLY);\n    dup2(devnull, STDIN_FILENO);\n    dup2(pipefd&#91;1], STDOUT_FILENO);\n    dup2(pipefd&#91;1], STDERR_FILENO);\n\n    if (devnull &gt; STDERR_FILENO)\n        close(devnull);\n\n    execlp(s-&gt;file, s-&gt;file, s-&gt;param, NULL);\n    exit(1);\n}\n\nvoid ustream_fd_init(struct ustream_fd *sf, int fd)\n{\n    ...\n    sf-&gt;fd.fd = fd;\n    ...\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p><code>q_initd_run<\/code>\u51fd\u6570\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7ba1\u9053\uff0c\u5e76\u901a\u8fc7<code>fork<\/code>\u521b\u5efa\u4e86\u65b0\u7684\u8fdb\u7a0b<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7236\u8fdb\u7a0b\uff1a\u5173\u95ed\u4e86\u7ba1\u9053\u7684\u5199\u7aef\uff1b\u521d\u59cb\u5316\u4e86\u90e8\u5206\u5173\u952e\u53c2\u6570\uff0c\u5e76\u5c06\u8fdb\u7a0b\u6dfb\u52a0\u5230\u8fd0\u884c\u961f\u5217\u4e2d\u53bb<\/li>\n\n\n\n<li>\u5b50\u8fdb\u7a0b\uff1a\u5173\u95ed\u4e86\u7ba1\u9053\u7684\u8bfb\u7aef\uff0c\u5c06\u8fdb\u7a0b\u7684<strong>\u6807\u51c6\u9519\u8bef<\/strong>\u548c<strong>\u6807\u51c6\u8f93\u51fa<\/strong>\u7ed1\u5b9a\u5230<strong>\u7ba1\u9053\u7684\u5199\u7aef<\/strong>\u4e0a\u53bb\uff1b\u63a5\u7740\u901a\u8fc7<code>execlp<\/code>\u51fd\u6570\u6267\u884c\u6307\u5b9a\u7684\u547d\u4ee4<\/li>\n\n\n\n<li>\u901a\u8fc7<code>ustream_fd_init<\/code>\u51fd\u6570\u5c06\u7ba1\u9053\u7684\u8f93\u51fa\u7aef\u7ed1\u5b9a\u5230<code>s-&gt;fd-&gt;fd.fd<\/code>\u4e0a<\/li>\n<\/ul>\n\n\n\n<p>\u5176\u4e2d\u7236\u8fdb\u7a0b\u7684\u914d\u7f6e\u53c2\u6570\uff0c\u5176\u5728\u5185\u5b58\u4e2d\u7684\u5206\u522b\u60c5\u51b5\u5982\u56fe\uff1b\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4f20\u5165\u8fd0\u884c\u961f\u5217\u7684\u662f<code>s-&gt;proc<\/code>\u90e8\u5206\uff0c\u521d\u59cb\u5316\u7684\u662f<code>s-&gt;fd<\/code>\u90e8\u5206\uff1b<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/12\/\u8fd0\u884c\u961f\u5217\u53e5\u67c4-scaled.jpg'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"1599\" data-original=\"https:\/\/www.garfield-blog.top\/wp-content\/uploads\/2025\/12\/\u8fd0\u884c\u961f\u5217\u53e5\u67c4-scaled.jpg\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-428\"  sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ba1\u9053\u8f93\u51fa\u7aef\u7684\u6700\u7ec8\u4f7f\u7528<\/h2>\n\n\n\n<p>\u5728<code>q_initd_run<\/code>\u51fd\u6570\u4e2d\u901a\u8fc7<code>fork<\/code>\u521b\u5efa\u4e86\u65b0\u7684\u8fdb\u7a0b\u6267\u884c\u5f85\u8fd0\u884c\u7684\u547d\u4ee4\uff0c\u53c8\u901a\u8fc7\u7ba1\u9053\u5c06\u65b0\u8fdb\u7a0b\u7684\u6807\u51c6\u8f93\u51fa\u548c\u6807\u51c6\u9519\u8bef\u91cd\u5b9a\u5411<\/p>\n\n\n\n<p><strong><code>ustream_fd_init<\/code>\u51fd\u6570<\/strong><\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u7b14\u8005\u6ce8\uff1a\u5df2\u7701\u7565\u90e8\u5206\u903b\u8f91<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>static void ustream_fd_set_uloop(struct ustream *s, bool write)\n{\n    struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);\n    ...\n    uloop_fd_add(&amp;sf-&gt;fd, flags);\n}\n\nvoid ustream_fd_init(struct ustream_fd *sf, int fd)\n{\n    struct ustream *s = &amp;sf-&gt;stream;\n    ...\n    sf-&gt;fd.fd = fd;\n    ...\n    ustream_fd_set_uloop(s, false);\n}<\/code><\/pre>\n\n\n\n<p><strong>\u6838\u5fc3\u601d\u60f3<\/strong><\/p>\n\n\n\n<p>\u7ba1\u9053\u8f93\u51fa\u7aef\u7684\u53e5\u67c4\u6700\u7ec8\u662f\u901a\u8fc7<code>uloop_fd_add<\/code>\u6dfb\u52a0\u5230<code>uloop<\/code>\u4e2d\uff1b<code>uloop<\/code>\u4e2d\u7528<code>epoll<\/code>\u6765\u76d1\u542c\u8fd9\u4e2a\u53e5\u67c4<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e\u88ab\u6ce8\u518c\u7684\u8fdb\u7a0b\u7684\u8f93\u51fa \u7b14\u8005\u6ce8\uff1a\u5728openwrt\u67b6\u6784\u7684\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u5e38\u5e38\u4f1a\u9047\u5230\u81ea\u5df1\u6dfb\u52a0\u7684\u6a21\u5757\u4e2d\u7684printf\u8f93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-426","post","type-post","status-publish","format-standard","hentry","category-source-code-analysis"],"_links":{"self":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/426","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=426"}],"version-history":[{"count":2,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/426\/revisions"}],"predecessor-version":[{"id":429,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/426\/revisions\/429"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}