BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file run.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Module for running arbitrary programs.
32 * NOTE: There is no locking - the program may run in parallel with other
33 * NCD processes and their programs.
34 *
35 * Synopsis: run(list do_cmd, list undo_cmd)
36 * Arguments:
37 * list do_cmd - Command run on startup. The first element is the full path
38 * to the executable, other elements are command line arguments (excluding
39 * the zeroth argument). An empty list is interpreted as no operation.
40 * list undo_cmd - Command run on shutdown, like do_cmd.
41 */
42  
43 #include <stdlib.h>
44 #include <string.h>
45  
46 #include <misc/strdup.h>
47 #include <ncd/extra/value_utils.h>
48 #include <ncd/modules/command_template.h>
49  
50 #include <ncd/module_common.h>
51  
52 #include <generated/blog_channel_ncd_run.h>
53  
54 static void template_free_func (void *vo, int is_error);
55  
56 struct instance {
57 NCDModuleInst *i;
58 BEventLock lock;
59 command_template_instance cti;
60 };
61  
62 static int build_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **exec, CmdLine *cl)
63 {
64 // read arguments
65 NCDValRef do_cmd_arg;
66 NCDValRef undo_cmd_arg;
67 if (!NCDVal_ListRead(args, 2, &do_cmd_arg, &undo_cmd_arg)) {
68 ModuleLog(i, BLOG_ERROR, "wrong arity");
69 goto fail0;
70 }
71 if (!NCDVal_IsList(do_cmd_arg) || !NCDVal_IsList(undo_cmd_arg)) {
72 ModuleLog(i, BLOG_ERROR, "wrong type");
73 goto fail0;
74 }
75  
76 NCDValRef list = (remove ? undo_cmd_arg : do_cmd_arg);
77 size_t count = NCDVal_ListCount(list);
78  
79 // check if there is no command
80 if (count == 0) {
81 *exec = NULL;
82 return 1;
83 }
84  
85 // read exec
86 NCDValRef exec_arg = NCDVal_ListGet(list, 0);
87 if (!NCDVal_IsStringNoNulls(exec_arg)) {
88 ModuleLog(i, BLOG_ERROR, "wrong type");
89 goto fail0;
90 }
91 if (!(*exec = ncd_strdup(exec_arg))) {
92 ModuleLog(i, BLOG_ERROR, "ncd_strdup failed");
93 goto fail0;
94 }
95  
96 // start cmdline
97 if (!CmdLine_Init(cl)) {
98 ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
99 goto fail1;
100 }
101  
102 // add header
103 if (!CmdLine_Append(cl, *exec)) {
104 ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
105 goto fail2;
106 }
107  
108 // add additional arguments
109 for (size_t j = 1; j < count; j++) {
110 NCDValRef arg = NCDVal_ListGet(list, j);
111  
112 if (!NCDVal_IsStringNoNulls(arg)) {
113 ModuleLog(i, BLOG_ERROR, "wrong type");
114 goto fail2;
115 }
116  
117 if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
118 ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
119 goto fail2;
120 }
121 }
122  
123 // finish
124 if (!CmdLine_Finish(cl)) {
125 ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
126 goto fail2;
127 }
128  
129 return 1;
130  
131 fail2:
132 CmdLine_Free(cl);
133 fail1:
134 free(*exec);
135 fail0:
136 return 0;
137 }
138  
139 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
140 {
141 struct instance *o = vo;
142 o->i = i;
143  
144 // init dummy event lock
145 BEventLock_Init(&o->lock, BReactor_PendingGroup(i->params->iparams->reactor));
146  
147 command_template_new(&o->cti, i, params, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
148 return;
149 }
150  
151 void template_free_func (void *vo, int is_error)
152 {
153 struct instance *o = vo;
154  
155 // free dummy event lock
156 BEventLock_Free(&o->lock);
157  
158 if (is_error) {
159 NCDModuleInst_Backend_DeadError(o->i);
160 } else {
161 NCDModuleInst_Backend_Dead(o->i);
162 }
163 }
164  
165 static void func_die (void *vo)
166 {
167 struct instance *o = vo;
168  
169 command_template_die(&o->cti);
170 }
171  
172 static struct NCDModule modules[] = {
173 {
174 .type = "run",
175 .func_new2 = func_new,
176 .func_die = func_die,
177 .alloc_size = sizeof(struct instance)
178 }, {
179 .type = NULL
180 }
181 };
182  
183 const struct NCDModuleGroup ncdmodule_run= {
184 .modules = modules
185 };