BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file command_template.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 <misc/debug.h> |
||
31 | |||
32 | #include <ncd/modules/command_template.h> |
||
33 | |||
34 | #define STATE_ADDING_LOCK 1 |
||
35 | #define STATE_ADDING 2 |
||
36 | #define STATE_ADDING_NEED_DELETE 3 |
||
37 | #define STATE_DONE 4 |
||
38 | #define STATE_DELETING_LOCK 5 |
||
39 | #define STATE_DELETING 6 |
||
40 | |||
41 | static void lock_handler (command_template_instance *o); |
||
42 | static void process_handler (command_template_instance *o, int normally, uint8_t normally_exit_status); |
||
43 | static void free_template (command_template_instance *o, int is_error); |
||
44 | |||
45 | static void lock_handler (command_template_instance *o) |
||
46 | { |
||
47 | ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_DELETING_LOCK) |
||
48 | ASSERT(!(o->state == STATE_ADDING_LOCK) || o->do_exec) |
||
49 | ASSERT(!(o->state == STATE_DELETING_LOCK) || o->undo_exec) |
||
50 | |||
51 | if (o->state == STATE_ADDING_LOCK) { |
||
52 | // start process |
||
53 | if (!BProcess_Init(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, o->do_exec, CmdLine_Get(&o->do_cmdline), NULL)) { |
||
54 | NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "BProcess_Init failed"); |
||
55 | free_template(o, 1); |
||
56 | return; |
||
57 | } |
||
58 | |||
59 | // set state |
||
60 | o->state = STATE_ADDING; |
||
61 | } else { |
||
62 | // start process |
||
63 | if (!BProcess_Init(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, o->undo_exec, CmdLine_Get(&o->undo_cmdline), NULL)) { |
||
64 | NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "BProcess_Init failed"); |
||
65 | free_template(o, 1); |
||
66 | return; |
||
67 | } |
||
68 | |||
69 | // set state |
||
70 | o->state = STATE_DELETING; |
||
71 | } |
||
72 | } |
||
73 | |||
74 | static void process_handler (command_template_instance *o, int normally, uint8_t normally_exit_status) |
||
75 | { |
||
76 | ASSERT(o->state == STATE_ADDING || o->state == STATE_ADDING_NEED_DELETE || o->state == STATE_DELETING) |
||
77 | |||
78 | // release lock |
||
79 | BEventLockJob_Release(&o->elock_job); |
||
80 | |||
81 | // free process |
||
82 | BProcess_Free(&o->process); |
||
83 | |||
84 | if (!normally || normally_exit_status != 0) { |
||
85 | NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "command failed"); |
||
86 | |||
87 | free_template(o, 1); |
||
88 | return; |
||
89 | } |
||
90 | |||
91 | switch (o->state) { |
||
92 | case STATE_ADDING: { |
||
93 | // set state |
||
94 | o->state = STATE_DONE; |
||
95 | |||
96 | // signal up |
||
97 | NCDModuleInst_Backend_Up(o->i); |
||
98 | } break; |
||
99 | |||
100 | case STATE_ADDING_NEED_DELETE: { |
||
101 | if (o->undo_exec) { |
||
102 | // wait for lock |
||
103 | BEventLockJob_Wait(&o->elock_job); |
||
104 | |||
105 | // set state |
||
106 | o->state = STATE_DELETING_LOCK; |
||
107 | } else { |
||
108 | free_template(o, 0); |
||
109 | return; |
||
110 | } |
||
111 | } break; |
||
112 | |||
113 | case STATE_DELETING: { |
||
114 | // finish |
||
115 | free_template(o, 0); |
||
116 | return; |
||
117 | } break; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | void command_template_new (command_template_instance *o, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, command_template_build_cmdline build_cmdline, command_template_free_func free_func, void *user, int blog_channel, BEventLock *elock) |
||
122 | { |
||
123 | // init arguments |
||
124 | o->i = i; |
||
125 | o->free_func = free_func; |
||
126 | o->user = user; |
||
127 | o->blog_channel = blog_channel; |
||
128 | |||
129 | // build do command |
||
130 | if (!build_cmdline(o->i, params->args, 0, &o->do_exec, &o->do_cmdline)) { |
||
131 | NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "build_cmdline do callback failed"); |
||
132 | goto fail0; |
||
133 | } |
||
134 | |||
135 | // build undo command |
||
136 | if (!build_cmdline(o->i, params->args, 1, &o->undo_exec, &o->undo_cmdline)) { |
||
137 | NCDModuleInst_Backend_Log(o->i, o->blog_channel, BLOG_ERROR, "build_cmdline undo callback failed"); |
||
138 | goto fail1; |
||
139 | } |
||
140 | |||
141 | // init lock job |
||
142 | BEventLockJob_Init(&o->elock_job, elock, (BEventLock_handler)lock_handler, o); |
||
143 | |||
144 | if (o->do_exec) { |
||
145 | // wait for lock |
||
146 | BEventLockJob_Wait(&o->elock_job); |
||
147 | |||
148 | // set state |
||
149 | o->state = STATE_ADDING_LOCK; |
||
150 | } else { |
||
151 | // set state |
||
152 | o->state = STATE_DONE; |
||
153 | |||
154 | // signal up |
||
155 | NCDModuleInst_Backend_Up(o->i); |
||
156 | } |
||
157 | |||
158 | return; |
||
159 | |||
160 | fail1: |
||
161 | if (o->do_exec) { |
||
162 | free(o->do_exec); |
||
163 | CmdLine_Free(&o->do_cmdline); |
||
164 | } |
||
165 | fail0: |
||
166 | o->free_func(o->user, 1); |
||
167 | } |
||
168 | |||
169 | static void free_template (command_template_instance *o, int is_error) |
||
170 | { |
||
171 | // free lock job |
||
172 | BEventLockJob_Free(&o->elock_job); |
||
173 | |||
174 | // free undo command |
||
175 | if (o->undo_exec) { |
||
176 | free(o->undo_exec); |
||
177 | CmdLine_Free(&o->undo_cmdline); |
||
178 | } |
||
179 | |||
180 | // free do command |
||
181 | if (o->do_exec) { |
||
182 | free(o->do_exec); |
||
183 | CmdLine_Free(&o->do_cmdline); |
||
184 | } |
||
185 | |||
186 | // call free function |
||
187 | o->free_func(o->user, is_error); |
||
188 | } |
||
189 | |||
190 | void command_template_die (command_template_instance *o) |
||
191 | { |
||
192 | ASSERT(o->state == STATE_ADDING_LOCK || o->state == STATE_ADDING || o->state == STATE_DONE) |
||
193 | |||
194 | switch (o->state) { |
||
195 | case STATE_ADDING_LOCK: { |
||
196 | free_template(o, 0); |
||
197 | return; |
||
198 | } break; |
||
199 | |||
200 | case STATE_ADDING: { |
||
201 | // set state |
||
202 | o->state = STATE_ADDING_NEED_DELETE; |
||
203 | } break; |
||
204 | |||
205 | case STATE_DONE: { |
||
206 | if (o->undo_exec) { |
||
207 | // wait for lock |
||
208 | BEventLockJob_Wait(&o->elock_job); |
||
209 | |||
210 | // set state |
||
211 | o->state = STATE_DELETING_LOCK; |
||
212 | } else { |
||
213 | free_template(o, 0); |
||
214 | return; |
||
215 | } |
||
216 | } break; |
||
217 | } |
||
218 | } |