]> jfr.im git - solanum.git/blob - librb/src/helper.c
check bans and quiets for cmode -n/nonmember PRIVMSG
[solanum.git] / librb / src / helper.c
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 *
22 */
23
24 #include <librb_config.h>
25 #include <rb_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,
45 restart_cb * irestart, die_cb * idie, size_t lb_heap_size,
46 size_t dh_size, size_t fd_heap_size)
47 {
48 rb_helper *helper;
49 int maxfd, x = 0;
50 int ifd, ofd;
51 char *tifd, *tofd, *tmaxfd;
52
53 tifd = getenv("IFD");
54 tofd = getenv("OFD");
55 tmaxfd = getenv("MAXFD");
56
57 if(tifd == NULL || tofd == NULL || tmaxfd == NULL)
58 return NULL;
59
60 helper = rb_malloc(sizeof(rb_helper));
61 ifd = (int)strtol(tifd, NULL, 10);
62 ofd = (int)strtol(tofd, NULL, 10);
63 maxfd = (int)strtol(tmaxfd, NULL, 10);
64
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
80 rb_lib_init(ilog, irestart, idie, 0, maxfd, dh_size, fd_heap_size);
81 rb_linebuf_init(lb_heap_size);
82 rb_linebuf_newbuf(&helper->sendq);
83 rb_linebuf_newbuf(&helper->recvq);
84
85 helper->ifd = rb_open(ifd, RB_FD_PIPE, "incoming connection");
86 helper->ofd = rb_open(ofd, RB_FD_PIPE, "outgoing connection");
87 rb_set_nb(helper->ifd);
88 rb_set_nb(helper->ofd);
89
90 helper->read_cb = read_cb;
91 helper->error_cb = error_cb;
92 return helper;
93 }
94
95 /*
96 * start_fork_helper
97 * starts a new ircd helper
98 * note that this function doesn't start doing reading..thats the job of the caller
99 */
100
101 rb_helper *
102 rb_helper_start(const char *name, const char *fullpath, rb_helper_cb * read_cb,
103 rb_helper_cb * error_cb)
104 {
105 rb_helper *helper;
106 const char *parv[2];
107 char buf[128];
108 char fx[16], fy[16], maxfd[16];
109 rb_fde_t *in_f[2];
110 rb_fde_t *out_f[2];
111 pid_t pid;
112
113 if(access(fullpath, X_OK) == -1)
114 return NULL;
115
116 helper = rb_malloc(sizeof(rb_helper));
117
118 snprintf(buf, sizeof(buf), "%s helper - read", name);
119 if(rb_pipe(&in_f[0], &in_f[1], buf) < 0)
120 {
121 rb_free(helper);
122 return NULL;
123 }
124 snprintf(buf, sizeof(buf), "%s helper - write", name);
125 if(rb_pipe(&out_f[0], &out_f[1], buf) < 0)
126 {
127 rb_free(helper);
128 return NULL;
129 }
130
131 snprintf(fx, sizeof(fx), "%d", rb_get_fd(in_f[1]));
132 snprintf(fy, sizeof(fy), "%d", rb_get_fd(out_f[0]));
133 snprintf(maxfd, sizeof(maxfd), "%d", rb_getmaxconnect());
134
135 rb_set_nb(in_f[0]);
136 rb_set_nb(in_f[1]);
137 rb_set_nb(out_f[0]);
138 rb_set_nb(out_f[1]);
139
140 rb_setenv("IFD", fy, 1);
141 rb_setenv("OFD", fx, 1);
142 rb_setenv("MAXFD", maxfd, 1);
143
144 snprintf(buf, sizeof(buf), "-ircd %s daemon", name);
145 parv[0] = buf;
146 parv[1] = NULL;
147
148 rb_clear_cloexec(in_f[1]);
149 rb_clear_cloexec(out_f[0]);
150
151 pid = rb_spawn_process(fullpath, (const char **)parv);
152
153 if(pid == -1)
154 {
155 rb_close(in_f[0]);
156 rb_close(in_f[1]);
157 rb_close(out_f[0]);
158 rb_close(out_f[1]);
159 rb_free(helper);
160 return NULL;
161 }
162
163 rb_close(in_f[1]);
164 rb_close(out_f[0]);
165
166 rb_linebuf_newbuf(&helper->sendq);
167 rb_linebuf_newbuf(&helper->recvq);
168
169 helper->ifd = in_f[0];
170 helper->ofd = out_f[1];
171 helper->read_cb = read_cb;
172 helper->error_cb = error_cb;
173 helper->fork_count = 0;
174 helper->pid = pid;
175
176 return helper;
177 }
178
179
180 void
181 rb_helper_restart(rb_helper *helper)
182 {
183 helper->error_cb(helper);
184 }
185
186
187 static void
188 rb_helper_write_sendq(rb_fde_t *F, void *helper_ptr)
189 {
190 rb_helper *helper = helper_ptr;
191 int retlen;
192
193 if(rb_linebuf_len(&helper->sendq) > 0)
194 {
195 while((retlen = rb_linebuf_flush(F, &helper->sendq)) > 0)
196 ;
197 if(retlen == 0 || (retlen < 0 && !rb_ignore_errno(errno)))
198 {
199 rb_helper_restart(helper);
200 return;
201 }
202 }
203
204 if(rb_linebuf_len(&helper->sendq) > 0)
205 rb_setselect(helper->ofd, RB_SELECT_WRITE, rb_helper_write_sendq, helper);
206 }
207
208 void
209 rb_helper_write_queue(rb_helper *helper, const char *format, ...)
210 {
211 va_list ap;
212 rb_strf_t strings = { .format = format, .format_args = &ap, .next = NULL };
213
214 va_start(ap, format);
215 rb_linebuf_put(&helper->sendq, &strings);
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 rb_strf_t strings = { .format = format, .format_args = &ap, .next = NULL };
231
232 va_start(ap, format);
233 rb_linebuf_put(&helper->sendq, &strings);
234 va_end(ap);
235
236 rb_helper_write_flush(helper);
237 }
238
239 static void
240 rb_helper_read_cb(rb_fde_t *F __attribute__((unused)), void *data)
241 {
242 rb_helper *helper = (rb_helper *)data;
243 static char buf[32768];
244 int length;
245 if(helper == NULL)
246 return;
247
248 while((length = rb_read(helper->ifd, buf, sizeof(buf))) > 0)
249 {
250 rb_linebuf_parse(&helper->recvq, buf, length, 0);
251 helper->read_cb(helper);
252 }
253
254 if(length == 0 || (length < 0 && !rb_ignore_errno(errno)))
255 {
256 rb_helper_restart(helper);
257 return;
258 }
259
260 rb_setselect(helper->ifd, RB_SELECT_READ, rb_helper_read_cb, helper);
261 }
262
263 void
264 rb_helper_run(rb_helper *helper)
265 {
266 if(helper == NULL)
267 return;
268 rb_helper_read_cb(helper->ifd, helper);
269 }
270
271
272 void
273 rb_helper_close(rb_helper *helper)
274 {
275 if(helper == NULL)
276 return;
277 rb_kill(helper->pid, SIGKILL);
278 rb_close(helper->ifd);
279 rb_close(helper->ofd);
280 rb_free(helper);
281 }
282
283 int
284 rb_helper_read(rb_helper *helper, void *buf, size_t bufsize)
285 {
286 return rb_linebuf_get(&helper->recvq, buf, bufsize, LINEBUF_COMPLETE, LINEBUF_PARSED);
287 }
288
289 void
290 rb_helper_loop(rb_helper *helper, long delay)
291 {
292 rb_helper_run(helper);
293 while(1)
294 {
295 rb_lib_loop(delay);
296 }
297 }