ambrop7 14 лет назад
Родитель
Сommit
c540227778

+ 1 - 0
blog_channels.txt

@@ -19,6 +19,7 @@ ncd_print 4
 ncd_blocker 4
 ncd_ip_in_network 4
 ncd_run 4
+ncd_runonce 4
 ncd_synchronous_process 4
 ncd_process_manager 4
 ncd_net_backend_waitdevice 4

+ 4 - 0
generated/blog_channel_ncd_runonce.h

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

+ 64 - 63
generated/blog_channels_defines.h

@@ -19,66 +19,67 @@
 #define BLOG_CHANNEL_ncd_blocker 18
 #define BLOG_CHANNEL_ncd_ip_in_network 19
 #define BLOG_CHANNEL_ncd_run 20
-#define BLOG_CHANNEL_ncd_synchronous_process 21
-#define BLOG_CHANNEL_ncd_process_manager 22
-#define BLOG_CHANNEL_ncd_net_backend_waitdevice 23
-#define BLOG_CHANNEL_ncd_net_backend_waitlink 24
-#define BLOG_CHANNEL_ncd_net_backend_badvpn 25
-#define BLOG_CHANNEL_ncd_net_backend_wpa_supplicant 26
-#define BLOG_CHANNEL_ncd_net_backend_rfkill 27
-#define BLOG_CHANNEL_ncd_net_up 28
-#define BLOG_CHANNEL_ncd_net_dns 29
-#define BLOG_CHANNEL_ncd_net_iptables 30
-#define BLOG_CHANNEL_ncd_net_ipv4_addr 31
-#define BLOG_CHANNEL_ncd_net_ipv4_route 32
-#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 33
-#define BLOG_CHANNEL_ncd_net_watch_interfaces 34
-#define BLOG_CHANNEL_ncd_sys_evdev 35
-#define BLOG_CHANNEL_ncd_sys_watch_directory 36
-#define BLOG_CHANNEL_StreamPeerIO 37
-#define BLOG_CHANNEL_DatagramPeerIO 38
-#define BLOG_CHANNEL_BReactor 39
-#define BLOG_CHANNEL_BSignal 40
-#define BLOG_CHANNEL_FragmentProtoAssembler 41
-#define BLOG_CHANNEL_BPredicate 42
-#define BLOG_CHANNEL_ServerConnection 43
-#define BLOG_CHANNEL_Listener 44
-#define BLOG_CHANNEL_DataProto 45
-#define BLOG_CHANNEL_FrameDecider 46
-#define BLOG_CHANNEL_BSocksClient 47
-#define BLOG_CHANNEL_BDHCPClientCore 48
-#define BLOG_CHANNEL_BDHCPClient 49
-#define BLOG_CHANNEL_NCDIfConfig 50
-#define BLOG_CHANNEL_BUnixSignal 51
-#define BLOG_CHANNEL_BProcess 52
-#define BLOG_CHANNEL_PRStreamSink 53
-#define BLOG_CHANNEL_PRStreamSource 54
-#define BLOG_CHANNEL_PacketProtoDecoder 55
-#define BLOG_CHANNEL_DPRelay 56
-#define BLOG_CHANNEL_BThreadWork 57
-#define BLOG_CHANNEL_DPReceive 58
-#define BLOG_CHANNEL_BInputProcess 59
-#define BLOG_CHANNEL_NCDUdevMonitorParser 60
-#define BLOG_CHANNEL_NCDUdevMonitor 61
-#define BLOG_CHANNEL_NCDUdevCache 62
-#define BLOG_CHANNEL_NCDUdevManager 63
-#define BLOG_CHANNEL_BTime 64
-#define BLOG_CHANNEL_BEncryption 65
-#define BLOG_CHANNEL_SPProtoDecoder 66
-#define BLOG_CHANNEL_LineBuffer 67
-#define BLOG_CHANNEL_BTap 68
-#define BLOG_CHANNEL_lwip 69
-#define BLOG_CHANNEL_NCDConfigParser 70
-#define BLOG_CHANNEL_nsskey 71
-#define BLOG_CHANNEL_addr 72
-#define BLOG_CHANNEL_PasswordListener 73
-#define BLOG_CHANNEL_NCDInterfaceMonitor 74
-#define BLOG_CHANNEL_NCDRfkillMonitor 75
-#define BLOG_CHANNEL_udpgw 76
-#define BLOG_CHANNEL_UdpGwClient 77
-#define BLOG_CHANNEL_SocksUdpGwClient 78
-#define BLOG_CHANNEL_BNetwork 79
-#define BLOG_CHANNEL_BConnection 80
-#define BLOG_CHANNEL_BSSLConnection 81
-#define BLOG_CHANNEL_BDatagram 82
-#define BLOG_NUM_CHANNELS 83
+#define BLOG_CHANNEL_ncd_runonce 21
+#define BLOG_CHANNEL_ncd_synchronous_process 22
+#define BLOG_CHANNEL_ncd_process_manager 23
+#define BLOG_CHANNEL_ncd_net_backend_waitdevice 24
+#define BLOG_CHANNEL_ncd_net_backend_waitlink 25
+#define BLOG_CHANNEL_ncd_net_backend_badvpn 26
+#define BLOG_CHANNEL_ncd_net_backend_wpa_supplicant 27
+#define BLOG_CHANNEL_ncd_net_backend_rfkill 28
+#define BLOG_CHANNEL_ncd_net_up 29
+#define BLOG_CHANNEL_ncd_net_dns 30
+#define BLOG_CHANNEL_ncd_net_iptables 31
+#define BLOG_CHANNEL_ncd_net_ipv4_addr 32
+#define BLOG_CHANNEL_ncd_net_ipv4_route 33
+#define BLOG_CHANNEL_ncd_net_ipv4_dhcp 34
+#define BLOG_CHANNEL_ncd_net_watch_interfaces 35
+#define BLOG_CHANNEL_ncd_sys_evdev 36
+#define BLOG_CHANNEL_ncd_sys_watch_directory 37
+#define BLOG_CHANNEL_StreamPeerIO 38
+#define BLOG_CHANNEL_DatagramPeerIO 39
+#define BLOG_CHANNEL_BReactor 40
+#define BLOG_CHANNEL_BSignal 41
+#define BLOG_CHANNEL_FragmentProtoAssembler 42
+#define BLOG_CHANNEL_BPredicate 43
+#define BLOG_CHANNEL_ServerConnection 44
+#define BLOG_CHANNEL_Listener 45
+#define BLOG_CHANNEL_DataProto 46
+#define BLOG_CHANNEL_FrameDecider 47
+#define BLOG_CHANNEL_BSocksClient 48
+#define BLOG_CHANNEL_BDHCPClientCore 49
+#define BLOG_CHANNEL_BDHCPClient 50
+#define BLOG_CHANNEL_NCDIfConfig 51
+#define BLOG_CHANNEL_BUnixSignal 52
+#define BLOG_CHANNEL_BProcess 53
+#define BLOG_CHANNEL_PRStreamSink 54
+#define BLOG_CHANNEL_PRStreamSource 55
+#define BLOG_CHANNEL_PacketProtoDecoder 56
+#define BLOG_CHANNEL_DPRelay 57
+#define BLOG_CHANNEL_BThreadWork 58
+#define BLOG_CHANNEL_DPReceive 59
+#define BLOG_CHANNEL_BInputProcess 60
+#define BLOG_CHANNEL_NCDUdevMonitorParser 61
+#define BLOG_CHANNEL_NCDUdevMonitor 62
+#define BLOG_CHANNEL_NCDUdevCache 63
+#define BLOG_CHANNEL_NCDUdevManager 64
+#define BLOG_CHANNEL_BTime 65
+#define BLOG_CHANNEL_BEncryption 66
+#define BLOG_CHANNEL_SPProtoDecoder 67
+#define BLOG_CHANNEL_LineBuffer 68
+#define BLOG_CHANNEL_BTap 69
+#define BLOG_CHANNEL_lwip 70
+#define BLOG_CHANNEL_NCDConfigParser 71
+#define BLOG_CHANNEL_nsskey 72
+#define BLOG_CHANNEL_addr 73
+#define BLOG_CHANNEL_PasswordListener 74
+#define BLOG_CHANNEL_NCDInterfaceMonitor 75
+#define BLOG_CHANNEL_NCDRfkillMonitor 76
+#define BLOG_CHANNEL_udpgw 77
+#define BLOG_CHANNEL_UdpGwClient 78
+#define BLOG_CHANNEL_SocksUdpGwClient 79
+#define BLOG_CHANNEL_BNetwork 80
+#define BLOG_CHANNEL_BConnection 81
+#define BLOG_CHANNEL_BSSLConnection 82
+#define BLOG_CHANNEL_BDatagram 83
+#define BLOG_NUM_CHANNELS 84

+ 1 - 0
generated/blog_channels_list.h

@@ -19,6 +19,7 @@
 {.name = "ncd_blocker", .loglevel = 4},
 {.name = "ncd_ip_in_network", .loglevel = 4},
 {.name = "ncd_run", .loglevel = 4},
+{.name = "ncd_runonce", .loglevel = 4},
 {.name = "ncd_synchronous_process", .loglevel = 4},
 {.name = "ncd_process_manager", .loglevel = 4},
 {.name = "ncd_net_backend_waitdevice", .loglevel = 4},

+ 1 - 0
ncd/CMakeLists.txt

@@ -46,6 +46,7 @@ add_executable(badvpn-ncd
     modules/blocker.c
     modules/ip_in_network.c
     modules/run.c
+    modules/runonce.c
     modules/synchronous_process.c
     modules/process_manager.c
     modules/net_backend_waitdevice.c

+ 2 - 0
ncd/modules/modules.h

@@ -43,6 +43,7 @@ extern const struct NCDModuleGroup ncdmodule_print;
 extern const struct NCDModuleGroup ncdmodule_blocker;
 extern const struct NCDModuleGroup ncdmodule_ip_in_network;
 extern const struct NCDModuleGroup ncdmodule_run;
+extern const struct NCDModuleGroup ncdmodule_runonce;
 extern const struct NCDModuleGroup ncdmodule_synchronous_process;
 extern const struct NCDModuleGroup ncdmodule_process_manager;
 extern const struct NCDModuleGroup ncdmodule_net_backend_waitdevice;
@@ -83,6 +84,7 @@ static const struct NCDModuleGroup *ncd_modules[] = {
     &ncdmodule_blocker,
     &ncdmodule_ip_in_network,
     &ncdmodule_run,
+    &ncdmodule_runonce,
     &ncdmodule_synchronous_process,
     &ncdmodule_process_manager,
     &ncdmodule_net_backend_waitdevice,

+ 255 - 0
ncd/modules/runonce.c

@@ -0,0 +1,255 @@
+/**
+ * @file runonce.c
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * This file is part of BadVPN.
+ * 
+ * BadVPN is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ * 
+ * BadVPN is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * @section DESCRIPTION
+ * 
+ * Imperative program execution module. On initialization, starts the process.
+ * Goes to UP state when the process terminates. When requested to die, waits for
+ * the process to terminate if it's running, without sending any signals.
+ * 
+ * Synopsis: runonce(list(string) cmd)
+ * Arguments:
+ *   cmd - Command to run on startup. The first element is the full path
+ *     to the executable, other elements are command line arguments (excluding
+ *     the zeroth argument).
+ * Variables:
+ *   string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <misc/cmdline.h>
+#include <system/BProcess.h>
+#include <ncd/NCDModule.h>
+
+#include <generated/blog_channel_ncd_runonce.h>
+
+#define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
+
+#define STATE_RUNNING 1
+#define STATE_RUNNING_DIE 2
+#define STATE_FINISHED 3
+
+struct instance {
+    NCDModuleInst *i;
+    int state;
+    BProcess process;
+    int exit_status;
+};
+
+static void instance_free (struct instance *o);
+
+static int build_cmdline (NCDModuleInst *i, NCDValue *cmd_arg, char **exec, CmdLine *cl)
+{
+    if (NCDValue_Type(cmd_arg) != NCDVALUE_LIST) {
+        ModuleLog(i, BLOG_ERROR, "wrong type");
+        goto fail0;
+    }
+    
+    // read exec
+    NCDValue *exec_arg = NCDValue_ListFirst(cmd_arg);
+    if (!exec_arg) {
+        ModuleLog(i, BLOG_ERROR, "missing executable name");
+        goto fail0;
+    }
+    if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
+        ModuleLog(i, BLOG_ERROR, "wrong type");
+        goto fail0;
+    }
+    if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
+        ModuleLog(i, BLOG_ERROR, "strdup failed");
+        goto fail0;
+    }
+    
+    // start cmdline
+    if (!CmdLine_Init(cl)) {
+        ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
+        goto fail1;
+    }
+    
+    // add header
+    if (!CmdLine_Append(cl, *exec)) {
+        ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
+        goto fail2;
+    }
+    
+    // add additional arguments
+    NCDValue *arg = exec_arg;
+    while (arg = NCDValue_ListNext(cmd_arg, arg)) {
+        if (NCDValue_Type(arg) != NCDVALUE_STRING) {
+            ModuleLog(i, BLOG_ERROR, "wrong type");
+            goto fail2;
+        }
+        
+        if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
+            ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
+            goto fail2;
+        }
+    }
+    
+    // finish
+    if (!CmdLine_Finish(cl)) {
+        ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
+        goto fail2;
+    }
+    
+    return 1;
+    
+fail2:
+    CmdLine_Free(cl);
+fail1:
+    free(*exec);
+fail0:
+    return 0;
+}
+
+static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
+{
+    ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
+    
+    // free process
+    BProcess_Free(&o->process);
+    
+    // if we were requested to die, die now
+    if (o->state == STATE_RUNNING_DIE) {
+        instance_free(o);
+        return;
+    }
+    
+    // remember exit status
+    o->exit_status = (normally ? normally_exit_status : -1);
+    
+    // set state
+    o->state = STATE_FINISHED;
+    
+    // set up
+    NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
+}
+
+static void func_new (NCDModuleInst *i)
+{
+    // allocate instance
+    struct instance *o = malloc(sizeof(*o));
+    if (!o) {
+        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
+        goto fail0;
+    }
+    NCDModuleInst_Backend_SetUser(i, o);
+    
+    // init arguments
+    o->i = i;
+    
+    // read arguments
+    NCDValue *cmd_arg;
+    if (!NCDValue_ListRead(i->args, 1, &cmd_arg)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail1;
+    }
+    
+    // build cmdline
+    char *exec;
+    CmdLine cl;
+    if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
+        goto fail1;
+    }
+    
+    // start process
+    if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL)) {
+        ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
+        CmdLine_Free(&cl);
+        free(exec);
+        goto fail1;
+    }
+    
+    CmdLine_Free(&cl);
+    free(exec);
+    
+    // set state
+    o->state = STATE_RUNNING;
+    
+    return;
+    
+fail1:
+    free(o);
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void instance_free (struct instance *o)
+{
+    NCDModuleInst *i = o->i;
+    
+    // free instance
+    free(o);
+    
+    NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
+}
+
+static void func_die (void *vo)
+{
+    struct instance *o = vo;
+    ASSERT(o->state != STATE_RUNNING_DIE)
+    
+    if (o->state == STATE_FINISHED) {
+        instance_free(o);
+        return;
+    }
+    
+    o->state = STATE_RUNNING_DIE;
+}
+
+static int func_getvar (void *vo, const char *name, NCDValue *out)
+{
+    struct instance *o = vo;
+    ASSERT(o->state == STATE_FINISHED)
+    
+    if (!strcmp(name, "exit_status")) {
+        char str[30];
+        snprintf(str, sizeof(str), "%d", o->exit_status);
+        
+        if (!NCDValue_InitString(out, str)) {
+            ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
+            return 0;
+        }
+        
+        return 1;
+    }
+    
+    return 0;
+}
+
+static const struct NCDModule modules[] = {
+    {
+        .type = "runonce",
+        .func_new = func_new,
+        .func_die = func_die,
+        .func_getvar = func_getvar
+    }, {
+        .type = NULL
+    }
+};
+
+const struct NCDModuleGroup ncdmodule_runonce = {
+    .modules = modules
+};