BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file NCDBProcessOpts.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  
30 #include <stddef.h>
31  
32 #include <misc/balloc.h>
33  
34 #include <ncd/extra/value_utils.h>
35  
36 #include "NCDBProcessOpts.h"
37  
38 int NCDBProcessOpts_Init (NCDBProcessOpts *o, NCDValRef opts_arg, NCDBProcessOpts_func_unknown func_unknown, void *func_unknown_user, NCDModuleInst *i, int blog_channel)
39 {
40 return NCDBProcessOpts_Init2(o, opts_arg, func_unknown, func_unknown_user, i, blog_channel, NULL, NULL);
41 }
42  
43 int NCDBProcessOpts_Init2 (NCDBProcessOpts *o, NCDValRef opts_arg, NCDBProcessOpts_func_unknown func_unknown, void *func_unknown_user, NCDModuleInst *i, int blog_channel,
44 int *out_keep_stdout, int *out_keep_stderr)
45 {
46 if (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsMap(opts_arg)) {
47 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "options must be a map");
48 goto fail0;
49 }
50  
51 o->username = NULL;
52 o->do_setsid = 0;
53  
54 int keep_stdout = 0;
55 int keep_stderr = 0;
56  
57 if (!NCDVal_IsInvalid(opts_arg)) {
58 for (NCDValMapElem me = NCDVal_MapFirst(opts_arg); !NCDVal_MapElemInvalid(me); me = NCDVal_MapNext(opts_arg, me)) {
59 NCDValRef key = NCDVal_MapElemKey(opts_arg, me);
60 NCDValRef val = NCDVal_MapElemVal(opts_arg, me);
61  
62 if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stdout")) {
63 if (!ncd_read_boolean(val, &keep_stdout)) {
64 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "bad keep_stdout");
65 goto fail1;
66 }
67 }
68 else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "keep_stderr")) {
69 if (!ncd_read_boolean(val, &keep_stderr)) {
70 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "bad keep_stderr");
71 goto fail1;
72 }
73 }
74 else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "do_setsid")) {
75 if (!ncd_read_boolean(val, &o->do_setsid)) {
76 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "bad do_setsid");
77 goto fail1;
78 }
79 }
80 else if (NCDVal_IsString(key) && NCDVal_StringEquals(key, "username")) {
81 if (!NCDVal_IsStringNoNulls(val)) {
82 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "username must be a string without nulls");
83 goto fail1;
84 }
85 o->username = MemRef_StrDup(NCDVal_StringMemRef(val));
86 if (!o->username) {
87 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "MemRef_StrDup failed");
88 goto fail1;
89 }
90 }
91 else {
92 if (!func_unknown || !func_unknown(func_unknown_user, key, val)) {
93 NCDModuleInst_Backend_Log(i, blog_channel, BLOG_ERROR, "unknown option");
94 goto fail1;
95 }
96 }
97 }
98 }
99  
100 o->nfds = 0;
101 if (keep_stdout) {
102 o->fds[o->nfds] = 1;
103 o->fds_map[o->nfds++] = 1;
104 }
105 if (keep_stderr) {
106 o->fds[o->nfds] = 2;
107 o->fds_map[o->nfds++] = 2;
108 }
109 o->fds[o->nfds] = -1;
110  
111 if (out_keep_stdout) {
112 *out_keep_stdout = keep_stdout;
113 }
114 if (out_keep_stderr) {
115 *out_keep_stderr = keep_stderr;
116 }
117  
118 return 1;
119  
120 fail1:
121 if (o->username) {
122 BFree(o->username);
123 }
124 fail0:
125 return 0;
126 }
127  
128 void NCDBProcessOpts_InitOld (NCDBProcessOpts *o, int keep_stdout, int keep_stderr, int do_setsid)
129 {
130 o->username = NULL;
131 o->do_setsid = do_setsid;
132  
133 o->nfds = 0;
134 if (keep_stdout) {
135 o->fds[o->nfds] = 1;
136 o->fds_map[o->nfds++] = 1;
137 }
138 if (keep_stderr) {
139 o->fds[o->nfds] = 2;
140 o->fds_map[o->nfds++] = 2;
141 }
142 o->fds[o->nfds] = -1;
143 }
144  
145 void NCDBProcessOpts_Free (NCDBProcessOpts *o)
146 {
147 if (o->username) {
148 BFree(o->username);
149 }
150 }
151  
152 struct BProcess_params NCDBProcessOpts_GetParams (NCDBProcessOpts *o)
153 {
154 struct BProcess_params params;
155  
156 params.username = o->username;
157 params.fds = o->fds;
158 params.fds_map = o->fds_map;
159 params.do_setsid = o->do_setsid;
160  
161 return params;
162 }