Kaynağa Gözat

ncd: modules: add reboot

ambrop7 13 yıl önce
ebeveyn
işleme
3c08174cbc

+ 1 - 0
blog_channels.txt

@@ -121,3 +121,4 @@ ncd_netmask 4
 ncd_implode 4
 ncd_call2 4
 ncd_assert 4
+ncd_reboot 4

+ 4 - 0
generated/blog_channel_ncd_reboot.h

@@ -0,0 +1,4 @@
+#ifdef BLOG_CURRENT_CHANNEL
+#undef BLOG_CURRENT_CHANNEL
+#endif
+#define BLOG_CURRENT_CHANNEL BLOG_CHANNEL_ncd_reboot

+ 2 - 1
generated/blog_channels_defines.h

@@ -121,4 +121,5 @@
 #define BLOG_CHANNEL_ncd_implode 120
 #define BLOG_CHANNEL_ncd_call2 121
 #define BLOG_CHANNEL_ncd_assert 122
-#define BLOG_NUM_CHANNELS 123
+#define BLOG_CHANNEL_ncd_reboot 123
+#define BLOG_NUM_CHANNELS 124

+ 1 - 0
generated/blog_channels_list.h

@@ -121,3 +121,4 @@
 {.name = "ncd_implode", .loglevel = 4},
 {.name = "ncd_call2", .loglevel = 4},
 {.name = "ncd_assert", .loglevel = 4},
+{.name = "ncd_reboot", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -141,6 +141,7 @@ add_executable(badvpn-ncd
     modules/implode.c
     modules/call2.c
     modules/assert.c
+    modules/reboot.c
     ${NCD_ADDITIONAL_SOURCES}
 )
 target_link_libraries(badvpn-ncd system flow flowextra dhcpclient arpprobe ncdvalue ncdval ncdvalcompat ncdvaluegenerator ncdvalueparser ncdconfigparser udevmonitor ncdinterfacemonitor ncdrequest)

+ 2 - 0
ncd/modules/modules.h

@@ -102,6 +102,7 @@ extern const struct NCDModuleGroup ncdmodule_netmask;
 extern const struct NCDModuleGroup ncdmodule_implode;
 extern const struct NCDModuleGroup ncdmodule_call2;
 extern const struct NCDModuleGroup ncdmodule_assert;
+extern const struct NCDModuleGroup ncdmodule_reboot;
 
 static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_var,
@@ -172,6 +173,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_implode,
     &ncdmodule_call2,
     &ncdmodule_assert,
+    &ncdmodule_reboot,
     NULL
 };
 

+ 105 - 0
ncd/modules/reboot.c

@@ -0,0 +1,105 @@
+/**
+ * @file reboot.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the
+ *    names of its contributors may be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * 
+ * @section DESCRIPTION
+ * 
+ * Synopsis:
+ *   hard_reboot()
+ *   hard_poweroff()
+ */
+
+#include <unistd.h>
+#include <sys/reboot.h>
+
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_reboot.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+static void func_new_hard_reboot (NCDModuleInst *i)
+{
+    // check arguments
+    if (!NCDVal_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
+    }
+    
+    // reboot
+    if (reboot(RB_AUTOBOOT) < 0) {
+        ModuleLog(i, BLOG_ERROR, "reboot(RB_AUTOBOOT) failed");
+        goto fail0;
+    }
+    
+    // signal up
+    NCDModuleInst_Backend_Up(i);
+    return;
+    
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static void func_new_hard_poweroff (NCDModuleInst *i)
+{
+    // check arguments
+    if (!NCDVal_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
+    }
+    
+    // power off
+    if (reboot(RB_POWER_OFF) < 0) {
+        ModuleLog(i, BLOG_ERROR, "reboot(RB_POWER_OFF) failed");
+        goto fail0;
+    }
+    
+    // signal up
+    NCDModuleInst_Backend_Up(i);
+    return;
+    
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "hard_reboot",
+        .func_new = func_new_hard_reboot
+    }, {
+        .type = "hard_poweroff",
+        .func_new = func_new_hard_poweroff
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_reboot = {
+    .modules = modules
+};