BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file runonce.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 * Imperative program execution module. On initialization, starts the process.
32 * Goes to UP state when the process terminates. When requested to die, waits for
33 * the process to terminate if it's running, optionally sending SIGTERM.
34 *
35 * Synopsis: runonce(list(string) cmd, [list opts])
36 * Arguments:
37 * cmd - Command to 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).
40 * opts - Map of options:
41 * "term_on_deinit":"true" - If we get a deinit request while the process is
42 * running, send it SIGTERM.
43 * "keep_stdout":"true" - Start the program with the same stdout as the NCD process.
44 * "keep_stderr":"true" - Start the program with the same stderr as the NCD process.
45 * "do_setsid":"true" - Call setsid() in the child before exec. This is needed to
46 * start the 'agetty' program.
47 * "username":username_string - Start the process under the permissions of the
48 * specified user.
49 * Variables:
50 * string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
51 */
52  
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <string.h>
56  
57 #include <misc/cmdline.h>
58 #include <misc/strdup.h>
59 #include <system/BProcess.h>
60 #include <ncd/extra/NCDBProcessOpts.h>
61  
62 #include <ncd/module_common.h>
63  
64 #include <generated/blog_channel_ncd_runonce.h>
65  
66 #define STATE_RUNNING 1
67 #define STATE_RUNNING_DIE 2
68 #define STATE_FINISHED 3
69  
70 struct instance {
71 NCDModuleInst *i;
72 int term_on_deinit;
73 int state;
74 BProcess process;
75 int exit_status;
76 };
77  
78 static void instance_free (struct instance *o);
79  
80 static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl)
81 {
82 ASSERT(!NCDVal_IsInvalid(cmd_arg))
83  
84 if (!NCDVal_IsList(cmd_arg)) {
85 ModuleLog(i, BLOG_ERROR, "wrong type");
86 goto fail0;
87 }
88  
89 size_t count = NCDVal_ListCount(cmd_arg);
90  
91 // read exec
92 if (count == 0) {
93 ModuleLog(i, BLOG_ERROR, "missing executable name");
94 goto fail0;
95 }
96 NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
97 if (!NCDVal_IsStringNoNulls(exec_arg)) {
98 ModuleLog(i, BLOG_ERROR, "wrong type");
99 goto fail0;
100 }
101 if (!(*exec = ncd_strdup(exec_arg))) {
102 ModuleLog(i, BLOG_ERROR, "strdup failed");
103 goto fail0;
104 }
105  
106 // start cmdline
107 if (!CmdLine_Init(cl)) {
108 ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
109 goto fail1;
110 }
111  
112 // add header
113 if (!CmdLine_Append(cl, *exec)) {
114 ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
115 goto fail2;
116 }
117  
118 // add additional arguments
119 for (size_t j = 1; j < count; j++) {
120 NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
121  
122 if (!NCDVal_IsStringNoNulls(arg)) {
123 ModuleLog(i, BLOG_ERROR, "wrong type");
124 goto fail2;
125 }
126  
127 if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
128 ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
129 goto fail2;
130 }
131 }
132  
133 // finish
134 if (!CmdLine_Finish(cl)) {
135 ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
136 goto fail2;
137 }
138  
139 return 1;
140  
141 fail2:
142 CmdLine_Free(cl);
143 fail1:
144 free(*exec);
145 fail0:
146 return 0;
147 }
148  
149 static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
150 {
151 ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
152  
153 // free process
154 BProcess_Free(&o->process);
155  
156 // if we were requested to die, die now
157 if (o->state == STATE_RUNNING_DIE) {
158 instance_free(o);
159 return;
160 }
161  
162 // remember exit status
163 o->exit_status = (normally ? normally_exit_status : -1);
164  
165 // set state
166 o->state = STATE_FINISHED;
167  
168 // set up
169 NCDModuleInst_Backend_Up(o->i);
170 }
171  
172 static int opts_func_unknown (void *user, NCDValRef key, NCDValRef val)
173 {
174 struct instance *o = user;
175  
176 if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "term_on_deinit")) {
177 if (!ncd_read_boolean(val, &o->term_on_deinit)) {
178 ModuleLog(o->i, BLOG_ERROR, "term_on_deinit: bad value");
179 return 0;
180 }
181 return 1;
182 }
183  
184 return 0;
185 }
186  
187 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
188 {
189 struct instance *o = vo;
190 o->i = i;
191  
192 // init arguments
193 o->term_on_deinit = 0;
194  
195 // read arguments
196 NCDValRef cmd_arg;
197 NCDValRef opts_arg = NCDVal_NewInvalid();
198 if (!NCDVal_ListRead(params->args, 1, &cmd_arg) && !NCDVal_ListRead(params->args, 2, &cmd_arg, &opts_arg)) {
199 ModuleLog(i, BLOG_ERROR, "wrong arity");
200 goto fail0;
201 }
202  
203 NCDBProcessOpts opts;
204  
205 // deprecated options format
206 if (!NCDVal_IsInvalid(opts_arg) && NCDVal_IsList(opts_arg)) {
207 int keep_stdout = 0;
208 int keep_stderr = 0;
209 int do_setsid = 0;
210  
211 // read options
212 size_t count = NCDVal_IsInvalid(opts_arg) ? 0 : NCDVal_ListCount(opts_arg);
213 for (size_t j = 0; j < count; j++) {
214 NCDValRef opt = NCDVal_ListGet(opts_arg, j);
215  
216 // read name
217 if (!NCDVal_IsString(opt)) {
218 ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
219 goto fail0;
220 }
221  
222 if (NCDVal_StringEquals(opt, "term_on_deinit")) {
223 o->term_on_deinit = 1;
224 }
225 else if (NCDVal_StringEquals(opt, "keep_stdout")) {
226 keep_stdout = 1;
227 }
228 else if (NCDVal_StringEquals(opt, "keep_stderr")) {
229 keep_stderr = 1;
230 }
231 else if (NCDVal_StringEquals(opt, "do_setsid")) {
232 do_setsid = 1;
233 }
234 else {
235 ModuleLog(o->i, BLOG_ERROR, "unknown option name");
236 goto fail0;
237 }
238 }
239  
240 NCDBProcessOpts_InitOld(&opts, keep_stdout, keep_stderr, do_setsid);
241 } else {
242 if (!NCDBProcessOpts_Init(&opts, opts_arg, opts_func_unknown, o, i, BLOG_CURRENT_CHANNEL)) {
243 goto fail0;
244 }
245 }
246  
247 // build cmdline
248 char *exec;
249 CmdLine cl;
250 if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
251 NCDBProcessOpts_Free(&opts);
252 goto fail0;
253 }
254  
255 // start process
256 struct BProcess_params p_params = NCDBProcessOpts_GetParams(&opts);
257 if (!BProcess_Init2(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), p_params)) {
258 ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
259 CmdLine_Free(&cl);
260 free(exec);
261 NCDBProcessOpts_Free(&opts);
262 goto fail0;
263 }
264  
265 CmdLine_Free(&cl);
266 free(exec);
267 NCDBProcessOpts_Free(&opts);
268  
269 // set state
270 o->state = STATE_RUNNING;
271 return;
272  
273 fail0:
274 NCDModuleInst_Backend_DeadError(i);
275 }
276  
277 static void instance_free (struct instance *o)
278 {
279 NCDModuleInst_Backend_Dead(o->i);
280 }
281  
282 static void func_die (void *vo)
283 {
284 struct instance *o = vo;
285 ASSERT(o->state != STATE_RUNNING_DIE)
286  
287 if (o->state == STATE_FINISHED) {
288 instance_free(o);
289 return;
290 }
291  
292 // send SIGTERM if requested
293 if (o->term_on_deinit) {
294 BProcess_Terminate(&o->process);
295 }
296  
297 o->state = STATE_RUNNING_DIE;
298 }
299  
300 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
301 {
302 struct instance *o = vo;
303 ASSERT(o->state == STATE_FINISHED)
304  
305 if (!strcmp(name, "exit_status")) {
306 char str[30];
307 snprintf(str, sizeof(str), "%d", o->exit_status);
308  
309 *out = NCDVal_NewString(mem, str);
310 return 1;
311 }
312  
313 return 0;
314 }
315  
316 static struct NCDModule modules[] = {
317 {
318 .type = "runonce",
319 .func_new2 = func_new,
320 .func_die = func_die,
321 .func_getvar = func_getvar,
322 .alloc_size = sizeof(struct instance)
323 }, {
324 .type = NULL
325 }
326 };
327  
328 const struct NCDModuleGroup ncdmodule_runonce = {
329 .modules = modules
330 };