以rmmod为例,在init.rc中添加命令支持

diff --git a/system/core/init/builtins.cpp b/system/core/init/builtins.cpp
index 01db4f5dabd..72cc41612ba 100644
--- a/system/core/init/builtins.cpp
+++ b/system/core/init/builtins.cpp
@@ -364,6 +364,17 @@ static Result<void> do_insmod(const BuiltinArguments& args) {
     return {};
 }
 
+static Result<void> do_rmmod(const BuiltinArguments& args) {
+    auto it = args.begin() + 1;
+
+    std::string filename = *it;
+
+    int rc = syscall(__NR_delete_module, filename.c_str(), O_NONBLOCK | O_EXCL);
+    if (rc == -1) return ErrnoError() << "delete_module for " << filename << " failed";
+
+    return {};
+}
+
 static Result<void> do_interface_restart(const BuiltinArguments& args) {
     Service* svc = ServiceList::GetInstance().FindInterface(args[1]);
     if (!svc) return Error() << "interface " << args[1] << " not found";
@@ -1398,6 +1409,7 @@ const BuiltinFunctionMap& GetBuiltinFunctionMap() {
         {"ifup",                    {1,     1,    {true,   do_ifup}}},
         {"init_user0",              {0,     0,    {false,  do_init_user0}}},
         {"insmod",                  {1,     kMax, {true,   do_insmod}}},
+        {"rmmod",                   {1,     1,    {true,   do_rmmod}}},
         {"installkey",              {1,     1,    {false,  do_installkey}}},
         {"interface_restart",       {1,     1,    {false,  do_interface_restart}}},
         {"interface_start",         {1,     1,    {false,  do_interface_start}}},

Android编译之编译可执行文件

Android.bp

cc_binary {
    name: "iwpriv",
    vendor: true,
    srcs: [
        "iwpriv.c",
        "iwlib.c",
    ],
    cflags: [
        "-D_GNU_SOURCE",
        "-DCONFIG_LIBNL20",

        // Silence some warnings for now. Needs to be fixed upstream. b/26105799
        "-Wno-unused-parameter",
        "-Wno-sign-compare",
        "-Wno-format",
        "-Wno-absolute-value",
        "-Wno-enum-conversion",
        "-Wno-self-assign",
        "-Werror",
    ],
    ldflags: ["-Wl,--no-gc-sections"],
    shared_libs: ["libnl"],
}

将源文件iwpriv.c、iwlib.c编译为iwpriv

    name: "iwpriv",
    srcs: [
        "iwpriv.c",
        "iwlib.c",
    ],

文件默认生成到/vendor/bin目录

    vendor: true,

添加编译时的cflag,忽略部分编译warning

    cflags: [
        "-D_GNU_SOURCE",
        "-DCONFIG_LIBNL20",

        // Silence some warnings for now. Needs to be fixed upstream. b/26105799
        "-Wno-unused-parameter",
        "-Wno-sign-compare",
        "-Wno-format",
        "-Wno-absolute-value",
        "-Wno-enum-conversion",
        "-Wno-self-assign",
        "-Werror",
    ],
    ldflags: ["-Wl,--no-gc-sections"],

编译依赖,编译时链接对应的库文件

    shared_libs: ["libnl"],