{"id":298,"date":"2025-11-02T20:15:27","date_gmt":"2025-11-02T12:15:27","guid":{"rendered":"https:\/\/www.garfield-blog.top\/?p=298"},"modified":"2025-11-02T20:18:04","modified_gmt":"2025-11-02T12:18:04","slug":"%e3%80%90%e6%ba%90%e7%a0%81%e8%a7%a3%e6%9e%90%e3%80%91ubus1%e7%bc%96%e8%af%91","status":"publish","type":"post","link":"https:\/\/www.garfield-blog.top\/?p=298","title":{"rendered":"\u3010\u6e90\u7801\u89e3\u6790\u3011ubus(1)\u7f16\u8bd1"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u7f16\u8bd1\u73af\u5883<\/h2>\n\n\n\n<p>\u7f16\u8bd1\u5668\u9ed8\u8ba4\u4f7f\u7528<code>gcc<\/code><br><code>cmake version&gt;=3.13<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4f9d\u8d56\u5173\u7cfb<\/h2>\n\n\n\n<p><code>ubus<\/code>\u7f16\u8bd1\u4f9d\u8d56<code>json-c<\/code>\u548c<code>libubox<\/code><\/p>\n\n\n\n<p><code>libubox<\/code>\u4f9d\u8d56<code>json-c<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u6e90\u7801\u4e0b\u8f7d\u8def\u5f84<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>git clone https:\/\/github.com\/json-c\/json-c.git\ngit clone https:\/\/github.com\/openwrt\/libubox\ngit clone https:\/\/github.com\/openwrt\/ubus<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u8bd1\u65b9\u6cd5<\/h2>\n\n\n\n<p>\u4e0b\u8f7d<code>json-c\u3001libubox\u3001ubus<\/code>\u6e90\u4ee3\u7801\uff0c\u89e3\u538b\u91cd\u5e76\u547d\u540d\u6587\u4ef6\u5939(\u4e0d\u8981\u4fee\u6539\u6e90\u7801)\uff0c\u4fdd\u8bc1\u6587\u4ef6\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b\u3002<strong>\u540c\u65f6\u4fdd\u8bc1<code>cmake<\/code>\u6587\u4ef6\u5177\u6709\u53ef\u6267\u884c\u6743\u9650<\/strong><\/p>\n\n\n\n<p>\u590d\u5236\u7c98\u8d34\u8fd0\u884c\u672c\u6587\u6863\u4e2d\u7684\u7f16\u8bd1\u811a\u672c\uff0c\u811a\u672c\u8fd0\u884c\u53c2\u6570<code>all<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>.\n\u251c\u2500\u2500 build.sh\n\u251c\u2500\u2500 json-c\n\u251c\u2500\u2500 libubox\n\u2514\u2500\u2500 ubus<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u8bd1\u811a\u672c<\/h2>\n\n\n\n<p>\u7b14\u8005\u6d4b\u8bd5\u7528\u8f6f\u4ef6\u7248\u672c<br><code>json-c<\/code>\u7248\u672c\uff1a<code>json-c-0.17-20230812<\/code><br><code>libubox<\/code>\u7248\u672c\uff1a<code>2024-5-17 eb9bcb64185ac155c02cc1a604692c4b00368324<\/code><br><code>ubus<\/code>\u7248\u672c\uff1a<code>2023-12-18 65bb027054def3b94a977229fd6ad62ddd32345b<\/code><\/p>\n\n\n\n<p><strong>\u7f16\u8bd1\u4ea7\u7269<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7f16\u8bd1\u5b8c\u6210\u540e\u4f1a\u521b\u5efa\u4e00\u4e2a<code>products_and_depends<\/code>\u540d\u79f0\u7684\u6587\u4ef6\u5939\uff0c\u7528\u4e8e\u5b58\u653e\u7f16\u8bd1\u4ea7\u7269<code>ubus<\/code>\u3001<code>ubusd<\/code>\u548c\u540e\u7eed\u53ef\u80fd\u4f7f\u7528\u4f9d\u8d56\u5e93\u548c\u5934\u6587\u4ef6<\/li>\n\n\n\n<li><code>ubus<\/code>\u6e90\u7801\u76ee\u5f55\u4e0b\u7684<code>examples<\/code>\u4e5f\u4f1a\u4e00\u8d77\u88ab\u7f16\u8bd1\uff0c\u8fd9\u4e2a\u662f\u4e00\u4e2a\u5b98\u65b9<code>demo<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\nbase_path=$(pwd)\n\njsonC_path=$base_path\/json-c\njsonC_build_path=$base_path\/jsonC_build\n\nlibubox_path=$base_path\/libubox\nlibubox_build_path=$base_path\/libubox_build\n\nubus_path=$base_path\/ubus\nubus_build_path=$base_path\/ubus_build\nubus_examples_build_path=$ubus_path\/examples\/build\n\ndepend_path=$base_path\/products_and_depends\ndepend_libs_path=$depend_path\/lib\ndepend_inc_path=$depend_path\/include\n\n# json-c\u5e93\u7f16\u8bd1\njsonC_build() {\n    echo \"Start compiling jsonc\"\n\n    mkdir $jsonC_build_path\n    cd $jsonC_build_path\n\n    $jsonC_path\/cmake-configure --prefix=.\/ --enable-shared\n\n    make &amp;&amp;\n\n    # \u62f7\u8d1d\u7f16\u8bd1\u4ea7\u7269\n    if &#91; ! -d \"$depend_libs_path\" ]; then\n        mkdir -p $depend_libs_path\n    fi\n\n    cp $jsonC_build_path\/libjson-c* $depend_libs_path\n}\n\n# libubox\u5e93\u7f16\u8bd1\nlibubox_build() {\n    echo \"Start compiling libubox\"\n\n    mkdir $libubox_build_path\n    cd $libubox_build_path\n\n    # \u62f7\u8d1d\u4f9d\u8d56\u6587\u4ef6\n    if &#91; ! -d \"$depend_inc_path\/json\" ]; then\n        mkdir -p $depend_inc_path\/json\n    fi\n\n    cp $jsonC_build_path\/json.h $depend_inc_path\/json &amp;&amp;\n    cp $jsonC_build_path\/*.h $depend_inc_path &amp;&amp;\n    cp $jsonC_path\/*.h $depend_inc_path &amp;&amp;\n\n    cmake -Djson=$depend_libs_path\/libjson-c.so -DCMAKE_C_FLAGS=\"-I $depend_inc_path\"\\\n        -DBUILD_LUA:BOOL=OFF -DBUILD_EXAMPLES:BOLL=OFF -DBUILD_STATIC=ON $libubox_path -DCMAKE_BUILD_TYPE=Debug\n\n    make &amp;&amp;\n\n    # \u62f7\u8d1d\u7f16\u8bd1\u4ea7\u7269\n    if &#91; ! -d \"$depend_libs_path\" ]; then\n        mkdir -p $depend_libs_path\n    fi\n\n    cp $libubox_build_path\/libblobmsg_json* $depend_libs_path &amp;&amp;\n    cp $libubox_build_path\/libjson_script* $depend_libs_path &amp;&amp;\n    cp $libubox_build_path\/libubox* $depend_libs_path\n}\n\n# ubus\u5e93\u7f16\u8bd1\nubus_build() {\n    echo \"Start compiling ubus\"\n\n    mkdir $ubus_build_path\n    cd $ubus_build_path\n\n    # \u62f7\u8d1d\u4f9d\u8d56\u6587\u4ef6\n    if &#91; ! -d \"$depend_inc_path\/libubox\" ]; then\n        mkdir -p $depend_inc_path\/libubox\n    fi\n    cp $libubox_path\/*.h $depend_inc_path\/libubox &amp;&amp;\n\n    cmake -Djson=$depend_libs_path\/libjson-c.so -Dubox_library=$depend_libs_path\/libubox.so \\\n        -Dblob_library=$depend_libs_path\/libblobmsg_json.so -Dubox_include_dir:PATH=$depend_inc_path -DBUILD_LUA=OFF\\\n        -DBUILD_STATIC=ON $ubus_path -DCMAKE_BUILD_TYPE=Debug\n\n    make &amp;&amp;\n\n    # \u62f7\u8d1d\u7f16\u8bd1\u4ea7\u7269\n    if &#91; ! -d \"$depend_libs_path\" ]; then\n        mkdir -p $depend_libs_path\n    fi\n\n    cp $ubus_path\/*.h $depend_inc_path &amp;&amp;\n    cp $ubus_build_path\/ubus $depend_path &amp;&amp;\n    cp $ubus_build_path\/ubusd $depend_path &amp;&amp;\n    cp $ubus_build_path\/libubus.* $depend_libs_path &amp;&amp;\n    cp $ubus_build_path\/libubusd_library.* $depend_libs_path\n\n    ubus_examples_build\n}\n\n# ubus\u5b98\u65b9demo\u7f16\u8bd1\nubus_examples_build() {\n    if &#91; ! -d \"$ubus_examples_build_path\" ]; then\n        mkdir -p $ubus_examples_build_path\n    fi\n\n    cd $ubus_examples_build_path\n    cmake .. -DBUILD_EXAMPLES=ON -DCMAKE_PREFIX_PATH=$depend_path -Dubox_library=ubox -DCMAKE_BUILD_TYPE=Debug\n    make\n}\n\nclean() {\n    echo \"Clean up old files\"\n    rm -rf $jsonC_build_path &amp;&amp;\n    rm -rf $libubox_build_path &amp;&amp;\n    rm -rf $ubus_build_path &amp;&amp;\n    rm -rf $depend_path &amp;&amp;\n    rm -rf $ubus_examples_build_path\n}\n\nbuild_all() {\n    jsonC_build\n    libubox_build\n    ubus_build\n}\n\n# \u68c0\u67e5\u4f20\u9012\u7684\u53c2\u6570\u5e76\u8c03\u7528\u76f8\u5e94\u7684\u51fd\u6570\ncase \"$1\" in\n    jsonC)\n        jsonC_build\n        ;;\n    libubox)\n        libubox_build\n        ;;\n    ubus)\n        ubus_build\n        ;;\n    all)\n        build_all\n        ;;\n    clean)\n        clean\n        ;;\n    *)\n        echo \"Usage: $0 &#91;jsonC|libubox|ubus|all|clean]\"\n        exit 1\n        ;;\nesac<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728Ubuntu\u4e0a\u8fd0\u884cubus<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728Ubuntu\u4e0a\u5b89\u88c5\u76f8\u5173\u5e93<\/h3>\n\n\n\n<p>\u5728\u5b8c\u6210\u4e0a\u6587\u4e2d\u7684\u7f16\u8bd1\u6b65\u9aa4\u540e\uff0c\u4f9d\u6b21\u8fdb\u5165<code>jsonC_build<\/code>\u3001<code>libubox_build<\/code>\u548c<code>ubus_build<\/code>\u6587\u4ef6\u5939\u6267\u884c<code>sudo make install<\/code>\u547d\u4ee4\u5728<code>Ubuntu<\/code>\u4e0a\u5b89\u88c5<code>json-c<\/code>\u3001<code>libubox<\/code>\u548c<code>ubus<\/code><\/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\u6b64\u5904\u5efa\u8bae\u4f7f\u7528\u5305\u7ba1\u7406\u5de5\u5177\u5148\u5bf9\u7f16\u8bd1\u6210\u751f\u4ea7\u7684\u4e8c\u8fdb\u5236\u8fdb\u884c\u6253\u5305\uff0c\u7136\u540e\u518d\u5b89\u88c5\u3002\u8fd9\u6837\u53ef\u4ee5\u4f7f\u7528\u5305\u7ba1\u7406\u5668\u8fdb\u884c\u7ba1\u7406<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728\u7ec8\u7aef\u624b\u52a8\u6267\u884cubus<\/h3>\n\n\n\n<p>\u521b\u5efa\u4e09\u4e2a\u7ec8\u7aef<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7ec8\u7aef1\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  su root\n\n  ubusd    #ubusd\u8fdb\u7a0b<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7ec8\u7aef2\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  su root\n\n  ubus listen test    #\u76d1\u542cubus<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7ec8\u7aef3\uff1a<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  su root\n\n  ubus send test '{\"hi!\":\"hello!\"}'    #\u53d1\u9001ubus<\/code><\/pre>\n\n\n\n<p>\u5982\u4e0a\u8ff0\u64cd\u4f5c\u5931\u8d25\uff0c\u53ef\u5c1d\u8bd5\u5c06<code>json-c<\/code>\u3001<code>libubox<\/code>\u548c<code>ubus<\/code>\u7684\u52a8\u6001\u5e93\u5e93\u8def\u5f84\u6dfb\u52a0\u81f3\u7cfb\u7edf\u52a8\u6001\u5e93\u641c\u7d22\u8def\u5f84<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e34\u65f6\u8bbe\u7f6e\u52a8\u6001\u5e93\u52a0\u8f7d\u8def\u5f84 <code>#\u8bbe\u7f6e\u4e34\u65f6\u52a8\u6001\u5e93\u52a0\u8f7d\u8def\u5f84 export LD_LIBRARY_PATH=\/path\/to\/lib:$LD_LIBRARY_PATH<\/code><\/li>\n\n\n\n<li>\u957f\u671f\u8bbe\u7f6e\u52a8\u6001\u5e93\u52a0\u8f7d\u8def\u5f84\n<ol class=\"wp-block-list\">\n<li>\u5207\u6362<code>root<\/code>\u6267\u884c \uff1a<code>su root<\/code><\/li>\n\n\n\n<li>\u5c06\u52a8\u6001\u5e93\u8def\u5f84\u6dfb\u52a0\u81f3\u7cfb\u7edf\u52a8\u6001\u5e93\u641c\u7d22\u8def\u5f84\uff1a<code>echo [\u4f9d\u8d56\u5e93\u8def\u5f84] &gt;&gt; \/etc\/ld.so.conf<\/code><\/li>\n\n\n\n<li>\u66f4\u65b0\u52a8\u6001\u5e93\u641c\u7d22\u8def\u5f84\uff1a<code>ldconfig<\/code><\/li>\n<\/ol>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u8fd0\u884c\u5b98\u65b9demo<\/h3>\n\n\n\n<p>\u5728<code>ubus<\/code>\u6e90\u7801\u4e2d\u5b58\u5728\u4e00\u4e2a<code>examples<\/code>\u6587\u4ef6\u5939\u5b58\uff0c\u91cc\u9762\u662f\u4e00\u4e2a\u5b98\u65b9<code>demo<\/code>\uff0c\u4e0a\u6587\u4e2d\u7684\u7f16\u8bd1\u811a\u672c\u4e5f\u4f1a\u7f16\u8bd1\u8fd9\u4e2a<code>demo<\/code><\/p>\n\n\n\n<p><strong>\u8fd0\u884c\u65b9\u5f0f<\/strong><\/p>\n\n\n\n<p>\u521b\u5efa\u4e09\u4e2a\u7ec8\u7aef<\/p>\n\n\n\n<p><strong>\u7ec8\u7aef1<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>su root\n\n.\/ubusd                     #\u6267\u884c\u5728\u7f16\u8bd1\u4ea7\u7269\uff08products_and_depends\u6587\u4ef6\u5939\uff09\u4e2d\u7684ubusd<\/code><\/pre>\n\n\n\n<p><strong>\u7ec8\u7aef2<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>su root\n\n.\/server                   #\u542f\u52a8ubus\/examples\u6587\u4ef6\u5939\u4e2d\u7684server\u7aef<\/code><\/pre>\n\n\n\n<p>\u5728<code>clinet<\/code>\u7aef\u4e5f\u6210\u529f\u6267\u884c\u540e<code>server<\/code>\u4fa7\u7684\u7ec8\u7aef\u4f1a\u51fa\u73b0\u5982\u4e0b\u6253\u5370<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Watching object dd66ce76: Success\nObject dd66ce76 went away<\/code><\/pre>\n\n\n\n<p><strong>\u7ec8\u7aef3\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>su root\n\n.\/client                  #\u542f\u52a8ubus\/examples\u6587\u4ef6\u5939\u4e2d\u7684client\u7aef<\/code><\/pre>\n\n\n\n<p>\u5728\u542f\u52a8<code>clinet<\/code>\u7aef\u540e\u7ec8\u7aef\u4f1a\u51fa\u73b0\u5982\u4e0b\u6253\u5370\uff0c\u5e76\u4e0d\u65ad\u65b0\u589e\u76f4\u5230<code>client<\/code>\u7aef\u8fdb\u7a0b\u7ed3\u675f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Avg time per iteration: 111 usec\nSubscribers active: 1\nGot fd from the server, watching...\ncompleted request, ret: 0\nAvg time per iteration: 111 usec\nGot line: msg1: test received a message: blah\nSending count up to '100100'; string has length '592926'\nServer validated our count up to '100100'\nGot line: msg2: test received a message: blah\nAvg time per iteration: 116 usec\nGot line: msg3: test received a message: blah\nAvg time per iteration: 121 usec\nGot line: msg4: test received a message: blah\nSending count up to '100200'; string has length '593629'\nServer validated our count up to '100200'<\/code><\/pre>\n\n\n\n<p><strong>\u5982\u679c\u8fd0\u884c\u65f6\u63d0\u793a\u627e\u4e0d\u5230\u4f9d\u8d56\u5e93\u5219\u9700\u624b\u52a8\u914d\u7f6e\u73af\u5883\u4f9d\u8d56\uff08\u8bbe\u7f6e <code>LD_LIBRARY_PATH<\/code>\uff09<\/strong><\/p>\n\n\n\n<p>\u4f8b\u5982 <code>libubox.so<\/code> \u4f4d\u4e8e <code>\/usr\/local\/lib<\/code> \u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1b<\/p>\n\n\n\n<p><strong>PS\uff1a<\/strong>\u5982\u679c\u4f7f\u7528\u4e0a\u6587\u4e2d\u7684\u7f16\u8bd1\u811a\u672c\u6267\u884c<code>ubus<\/code>\u7f16\u8bd1\u5219<code>libubox.so<\/code>\u4f1a\u5728\u7f16\u8bd1\u65f6\u5b58\u653e\u5728<code>products_and_depends\/lib<\/code>\u6587\u4ef6\u4e2d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export LD_LIBRARY_PATH=\/usr\/local\/lib:$LD_LIBRARY_PATH<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7f16\u8bd1\u73af\u5883 \u7f16\u8bd1\u5668\u9ed8\u8ba4\u4f7f\u7528gcccmake version&gt;=3.13 \u4f9d\u8d56\u5173\u7cfb ubus\u7f16\u8bd1\u4f9d\u8d56json [&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-298","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\/298","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=298"}],"version-history":[{"count":2,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions"}],"predecessor-version":[{"id":301,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=\/wp\/v2\/posts\/298\/revisions\/301"}],"wp:attachment":[{"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=298"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=298"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garfield-blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=298"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}