BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file sleep.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 | * Synopsis: |
||
32 | * sleep(string ms_start [, string ms_stop]) |
||
33 | * |
||
34 | * Description: |
||
35 | * On init, sleeps 'ms_start' milliseconds then goes up, or goes up immediately |
||
36 | * if 'ms_start' is an empty string. |
||
37 | * On deinit, sleeps 'ms_stop' milliseconds then dies, or dies immediately if |
||
38 | * 'ms_stop' is an empty string or is not provided. If a deinit is requested while |
||
39 | * the init sleep is still in progress, the init sleep is aborted and the deinit |
||
40 | * sleep is started immediately (if any). |
||
41 | */ |
||
42 | |||
43 | #include <stdlib.h> |
||
44 | #include <string.h> |
||
45 | #include <stdio.h> |
||
46 | #include <inttypes.h> |
||
47 | #include <limits.h> |
||
48 | |||
49 | #include <ncd/module_common.h> |
||
50 | |||
51 | #include <generated/blog_channel_ncd_sleep.h> |
||
52 | |||
53 | struct instance { |
||
54 | NCDModuleInst *i; |
||
55 | btime_t ms_stop; |
||
56 | BTimer timer; |
||
57 | int dying; |
||
58 | }; |
||
59 | |||
60 | static void instance_free (struct instance *o); |
||
61 | |||
62 | static void timer_handler (void *vo) |
||
63 | { |
||
64 | struct instance *o = vo; |
||
65 | |||
66 | if (!o->dying) { |
||
67 | // signal up |
||
68 | NCDModuleInst_Backend_Up(o->i); |
||
69 | } else { |
||
70 | // die |
||
71 | instance_free(o); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
76 | { |
||
77 | struct instance *o = vo; |
||
78 | o->i = i; |
||
79 | |||
80 | // check arguments |
||
81 | NCDValRef ms_start_arg; |
||
82 | NCDValRef ms_stop_arg = NCDVal_NewInvalid(); |
||
83 | if (!NCDVal_ListRead(params->args, 1, &ms_start_arg) && |
||
84 | !NCDVal_ListRead(params->args, 2, &ms_start_arg, &ms_stop_arg)) { |
||
85 | ModuleLog(o->i, BLOG_ERROR, "wrong arity"); |
||
86 | goto fail0; |
||
87 | } |
||
88 | |||
89 | uintmax_t ms; |
||
90 | btime_t ms_start; |
||
91 | |||
92 | if (NCDVal_IsString(ms_start_arg) && NCDVal_StringEqualsId(ms_start_arg, NCD_STRING_EMPTY)) { |
||
93 | ms_start = -1; |
||
94 | } else { |
||
95 | if (!ncd_read_uintmax(ms_start_arg, &ms) || ms > INT64_MAX) { |
||
96 | ModuleLog(o->i, BLOG_ERROR, "wrong start time"); |
||
97 | goto fail0; |
||
98 | } |
||
99 | ms_start = ms; |
||
100 | } |
||
101 | |||
102 | if (NCDVal_IsInvalid(ms_stop_arg) || (NCDVal_IsString(ms_stop_arg) && NCDVal_StringEqualsId(ms_stop_arg, NCD_STRING_EMPTY))) { |
||
103 | o->ms_stop = -1; |
||
104 | } else { |
||
105 | if (!ncd_read_uintmax(ms_stop_arg, &ms) || ms > INT64_MAX) { |
||
106 | ModuleLog(o->i, BLOG_ERROR, "wrong stop time"); |
||
107 | goto fail0; |
||
108 | } |
||
109 | o->ms_stop = ms; |
||
110 | } |
||
111 | |||
112 | // init timer |
||
113 | BTimer_Init(&o->timer, 0, timer_handler, o); |
||
114 | |||
115 | // set not dying |
||
116 | o->dying = 0; |
||
117 | |||
118 | if (ms_start < 0) { |
||
119 | // go up |
||
120 | NCDModuleInst_Backend_Up(i); |
||
121 | } else { |
||
122 | // set timer |
||
123 | BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, ms_start); |
||
124 | } |
||
125 | |||
126 | return; |
||
127 | |||
128 | fail0: |
||
129 | NCDModuleInst_Backend_DeadError(i); |
||
130 | } |
||
131 | |||
132 | void instance_free (struct instance *o) |
||
133 | { |
||
134 | // free timer |
||
135 | BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer); |
||
136 | |||
137 | NCDModuleInst_Backend_Dead(o->i); |
||
138 | } |
||
139 | |||
140 | static void func_die (void *vo) |
||
141 | { |
||
142 | struct instance *o = vo; |
||
143 | |||
144 | if (o->ms_stop < 0) { |
||
145 | // die immediately |
||
146 | instance_free(o); |
||
147 | return; |
||
148 | } |
||
149 | |||
150 | // set dying |
||
151 | o->dying = 1; |
||
152 | |||
153 | // set timer |
||
154 | BReactor_SetTimerAfter(o->i->params->iparams->reactor, &o->timer, o->ms_stop); |
||
155 | } |
||
156 | |||
157 | static struct NCDModule modules[] = { |
||
158 | { |
||
159 | .type = "sleep", |
||
160 | .func_new2 = func_new, |
||
161 | .func_die = func_die, |
||
162 | .alloc_size = sizeof(struct instance) |
||
163 | }, { |
||
164 | .type = NULL |
||
165 | } |
||
166 | }; |
||
167 | |||
168 | const struct NCDModuleGroup ncdmodule_sleep = { |
||
169 | .modules = modules |
||
170 | }; |