]>
Commit | Line | Data |
---|---|---|
b57f37fb WP |
1 | /* |
2 | * ircd-ratbox: A slightly useful ircd | |
3 | * helper.c: Starts and deals with ircd helpers | |
4 | * | |
5 | * Copyright (C) 2006 Aaron Sethman <androsyn@ratbox.org> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 | |
20 | * USA | |
21 | * | |
033be687 | 22 | * $Id: helper.c 25245 2008-04-21 18:38:23Z androsyn $ |
b57f37fb WP |
23 | */ |
24 | #include <libratbox_config.h> | |
25 | #include <ratbox_lib.h> | |
26 | #include <commio-int.h> | |
27 | ||
28 | struct _rb_helper | |
29 | { | |
30 | char *path; | |
31 | buf_head_t sendq; | |
32 | buf_head_t recvq; | |
33 | rb_fde_t *ifd; | |
34 | rb_fde_t *ofd; | |
35 | pid_t pid; | |
36 | int fork_count; | |
37 | rb_helper_cb *read_cb; | |
38 | rb_helper_cb *error_cb; | |
39 | }; | |
40 | ||
41 | ||
42 | /* setup all the stuff a new child needs */ | |
43 | rb_helper * | |
44 | rb_helper_child(rb_helper_cb *read_cb, rb_helper_cb *error_cb, log_cb *ilog, restart_cb *irestart, die_cb *idie, | |
45 | int maxcon, size_t lb_heap_size, size_t dh_size, size_t fd_heap_size) | |
46 | { | |
47 | rb_helper *helper; | |
48 | int maxfd, x = 0; | |
49 | int ifd, ofd; | |
50 | char *tifd, *tofd, *tmaxfd; | |
51 | ||
52 | tifd = getenv("IFD"); | |
53 | tofd = getenv("OFD"); | |
54 | tmaxfd = getenv("MAXFD"); | |
55 | ||
56 | if(tifd == NULL || tofd == NULL || tmaxfd == NULL) | |
57 | return NULL; | |
58 | ||
59 | helper = rb_malloc(sizeof(rb_helper)); | |
60 | ifd = (int)strtol(tifd, NULL, 10); | |
61 | ofd = (int)strtol(tofd, NULL, 10); | |
62 | maxfd = (int)strtol(tmaxfd, NULL, 10); | |
63 | ||
64 | #ifndef WIN32 | |
65 | for(x = 0; x < maxfd; x++) | |
66 | { | |
67 | if(x != ifd && x != ofd) | |
68 | close(x); | |
69 | } | |
70 | x = open("/dev/null", O_RDWR); | |
71 | if(ifd != 0 && ofd != 0) | |
72 | dup2(x, 0); | |
73 | if(ifd != 1 && ofd != 1) | |
74 | dup2(x, 1); | |
75 | if(ifd != 2 && ofd != 2) | |
76 | dup2(x, 2); | |
77 | if(x > 2) /* don't undo what we just did */ | |
78 | close(x); | |
79 | #else | |
80 | x = 0; /* shut gcc up */ | |
81 | #endif | |
82 | ||
83 | rb_lib_init(ilog, irestart, idie, 0, maxfd, dh_size, fd_heap_size); | |
84 | rb_linebuf_init(lb_heap_size); | |
85 | rb_linebuf_newbuf(&helper->sendq); | |
86 | rb_linebuf_newbuf(&helper->recvq); | |
87 | ||
88 | helper->ifd = rb_open(ifd, RB_FD_PIPE, "incoming connection"); | |
89 | helper->ofd = rb_open(ofd, RB_FD_PIPE, "outgoing connection"); | |
90 | rb_set_nb(helper->ifd); | |
91 | rb_set_nb(helper->ofd); | |
92 | ||
93 | helper->read_cb = read_cb; | |
94 | helper->error_cb = error_cb; | |
95 | return helper; | |
96 | } | |
97 | ||
98 | /* | |
99 | * start_fork_helper | |
100 | * starts a new ircd helper | |
101 | * note that this function doesn't start doing reading..thats the job of the caller | |
102 | */ | |
103 | ||
104 | rb_helper * | |
105 | rb_helper_start(const char *name, const char *fullpath, rb_helper_cb *read_cb, rb_helper_cb *error_cb) | |
106 | { | |
107 | rb_helper *helper; | |
108 | const char *parv[2]; | |
109 | char buf[128]; | |
110 | char fx[16], fy[16]; | |
111 | rb_fde_t *in_f[2]; | |
112 | rb_fde_t *out_f[2]; | |
113 | pid_t pid; | |
114 | ||
115 | if(access(fullpath, X_OK) == -1) | |
116 | return NULL; | |
117 | ||
118 | helper = rb_malloc(sizeof(rb_helper)); | |
119 | ||
120 | rb_snprintf(buf, sizeof(buf), "%s helper - read", name); | |
121 | if(rb_pipe(&in_f[0], &in_f[1], buf) < 0) | |
122 | { | |
123 | rb_free(helper); | |
124 | return NULL; | |
125 | } | |
126 | rb_snprintf(buf, sizeof(buf), "%s helper - write", name); | |
127 | if(rb_pipe(&out_f[0], &out_f[1], buf) < 0) | |
128 | { | |
129 | rb_free(helper); | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | rb_snprintf(fx, sizeof(fx), "%d", rb_get_fd(in_f[1])); | |
134 | rb_snprintf(fy, sizeof(fy), "%d", rb_get_fd(out_f[0])); | |
135 | ||
136 | rb_set_nb(in_f[0]); | |
137 | rb_set_nb(in_f[1]); | |
138 | rb_set_nb(out_f[0]); | |
139 | rb_set_nb(out_f[1]); | |
140 | ||
141 | setenv("IFD", fy, 1); | |
142 | setenv("OFD", fx, 1); | |
143 | setenv("MAXFD", "256", 1); | |
144 | ||
145 | rb_snprintf(buf, sizeof(buf), "-ircd %s daemon", name); | |
146 | parv[0] = buf; | |
147 | parv[1] = NULL; | |
148 | ||
149 | #ifdef WIN32 | |
150 | SetHandleInformation((HANDLE)rb_get_fd(in_f[1]), HANDLE_FLAG_INHERIT, 1); | |
151 | SetHandleInformation((HANDLE)rb_get_fd(out_f[0]), HANDLE_FLAG_INHERIT, 1); | |
152 | #endif | |
153 | ||
154 | pid = rb_spawn_process(fullpath, (const char **)parv); | |
155 | ||
156 | if(pid == -1) | |
157 | { | |
158 | rb_close(in_f[0]); | |
159 | rb_close(in_f[1]); | |
160 | rb_close(out_f[0]); | |
161 | rb_close(out_f[1]); | |
162 | rb_free(helper); | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | rb_close(in_f[1]); | |
167 | rb_close(out_f[0]); | |
168 | ||
169 | rb_linebuf_newbuf(&helper->sendq); | |
170 | rb_linebuf_newbuf(&helper->recvq); | |
171 | ||
172 | helper->ifd = in_f[0]; | |
173 | helper->ofd = out_f[1]; | |
174 | helper->read_cb = read_cb; | |
175 | helper->error_cb = error_cb; | |
176 | helper->fork_count = 0; | |
177 | helper->pid = pid; | |
178 | ||
179 | return helper; | |
180 | } | |
181 | ||
182 | ||
183 | void | |
184 | rb_helper_restart(rb_helper *helper) | |
185 | { | |
186 | helper->error_cb(helper); | |
187 | } | |
188 | ||
189 | ||
190 | static void | |
191 | rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr) | |
192 | { | |
193 | rb_helper *helper = helper_ptr; | |
194 | int retlen; | |
195 | ||
196 | if(rb_linebuf_len(&helper->sendq) > 0) | |
197 | { | |
198 | while((retlen = rb_linebuf_flush(F, &helper->sendq)) > 0) | |
199 | ;; | |
200 | if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno))) { | |
201 | rb_helper_restart(helper); | |
202 | return; | |
203 | } | |
204 | } | |
205 | ||
206 | if(rb_linebuf_len(&helper->sendq) > 0) | |
207 | rb_setselect(helper->ofd, RB_SELECT_WRITE, rb_helper_write_sendq, helper); | |
208 | } | |
209 | ||
210 | void | |
211 | rb_helper_write_queue(rb_helper *helper, const char *format, ...) | |
212 | { | |
213 | va_list ap; | |
214 | va_start(ap, format); | |
215 | rb_linebuf_putmsg(&helper->sendq, format, &ap, NULL); | |
216 | va_end(ap); | |
217 | } | |
218 | ||
219 | void | |
220 | rb_helper_write_flush(rb_helper *helper) | |
221 | { | |
222 | rb_helper_write_sendq(helper->ofd, helper); | |
223 | } | |
224 | ||
225 | ||
226 | void | |
227 | rb_helper_write(rb_helper *helper, const char *format, ...) | |
228 | { | |
229 | va_list ap; | |
230 | va_start(ap, format); | |
231 | rb_linebuf_putmsg(&helper->sendq, format, &ap, NULL); | |
232 | va_end(ap); | |
233 | rb_helper_write_flush(helper); | |
234 | } | |
235 | ||
236 | static void | |
237 | rb_helper_read_cb(rb_fde_t *F, void *data) | |
238 | { | |
239 | rb_helper *helper = (rb_helper *)data; | |
240 | static char buf[32768]; | |
241 | int length; | |
242 | if(helper == NULL) | |
243 | return; | |
244 | ||
245 | while((length = rb_read(helper->ifd, buf, sizeof(buf))) > 0) | |
246 | { | |
247 | rb_linebuf_parse(&helper->recvq, buf, length, 0); | |
248 | helper->read_cb(helper); | |
249 | } | |
250 | ||
251 | if(length == 0 || (length < 0 && !rb_ignore_errno(errno))) | |
252 | { | |
253 | rb_helper_restart(helper); | |
254 | return; | |
255 | } | |
256 | ||
257 | rb_setselect(helper->ifd, RB_SELECT_READ, rb_helper_read_cb, helper); | |
258 | } | |
259 | ||
260 | void | |
261 | rb_helper_run(rb_helper *helper) | |
262 | { | |
263 | if(helper == NULL) | |
264 | return; | |
265 | rb_helper_read_cb(helper->ifd, helper); | |
266 | } | |
267 | ||
268 | ||
269 | void | |
270 | rb_helper_close(rb_helper *helper) | |
271 | { | |
272 | if(helper == NULL) | |
273 | return; | |
033be687 | 274 | kill(helper->pid, SIGKILL); |
b57f37fb WP |
275 | rb_close(helper->ifd); |
276 | rb_close(helper->ofd); | |
277 | rb_free(helper); | |
278 | } | |
279 | ||
280 | int | |
281 | rb_helper_read(rb_helper *helper, void *buf, size_t bufsize) | |
282 | { | |
283 | return rb_linebuf_get(&helper->recvq, buf, bufsize, LINEBUF_COMPLETE, LINEBUF_PARSED); | |
284 | } | |
285 | ||
286 | void | |
287 | rb_helper_loop(rb_helper *helper, long delay) | |
288 | { | |
289 | rb_helper_run(helper); | |
290 | while(1) | |
291 | { | |
292 | rb_lib_loop(delay); | |
293 | } | |
294 | } | |
295 |