Debian Codegraph @

libmowgli-2/src/libmowgli/eventloop/helper.c

raw
1 /*
2 * Copyright (c) 2012 William Pitcock <[email protected]>.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "mowgli.h"
22
23 typedef struct
24 {
25 mowgli_eventloop_helper_start_fn_t *start_fn;
26 void *userdata;
27 mowgli_descriptor_t fd;
28 } mowgli_helper_create_req_t;
29
30 static void
31 mowgli_helper_trampoline(mowgli_helper_create_req_t *req)
32 {
33 mowgli_eventloop_helper_proc_t *helper;
34
35 #ifndef _WIN32
36 int i, x;
37 #endif
38
39 return_if_fail(req != NULL);
40 return_if_fail(req->start_fn != NULL);
41
42 helper = mowgli_alloc(sizeof *helper);
43 helper->type.type = MOWGLI_EVENTLOOP_TYPE_HELPER;
44 helper->fd = req->fd;
45
46 #ifndef _WIN32
47
48 for (i = 0; i < 1024; i++)
49 if (i != req->fd)
50 close(i);
51
52 x = open("/dev/null", O_RDWR);
53
54 for (i = 0; i < 2; i++)
55 if (req->fd != i)
56 dup2(x, i);
57
58 if (x > 2)
59 close(x);
60
61 #endif
62
63 helper->eventloop = mowgli_eventloop_create();
64 helper->pfd = mowgli_pollable_create(helper->eventloop, helper->fd, helper);
65 helper->userdata = req->userdata;
66
67 mowgli_pollable_set_nonblocking(helper->pfd, true);
68
69 req->start_fn(helper, helper->userdata);
70 }
71
72 mowgli_eventloop_helper_proc_t *
73 mowgli_helper_create(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_start_fn_t *start_fn, const char *helpername, void *userdata)
74 {
75 mowgli_eventloop_helper_proc_t *helper;
76 mowgli_helper_create_req_t child;
77 int io_fd[2];
78
79 return_val_if_fail(eventloop != NULL, NULL);
80 return_val_if_fail(start_fn != NULL, NULL);
81
82 child.start_fn = start_fn;
83 child.userdata = userdata;
84
85 helper = mowgli_alloc(sizeof *helper);
86 helper->type.type = MOWGLI_EVENTLOOP_TYPE_HELPER;
87 helper->eventloop = eventloop;
88
89 socketpair(AF_UNIX, SOCK_STREAM, 0, io_fd);
90
91 /* set up helper/child fd mapping */
92 helper->fd = io_fd[0];
93 child.fd = io_fd[1];
94
95 /* make pollables and make them non-blocking */
96 helper->pfd = mowgli_pollable_create(eventloop, helper->fd, helper);
97 mowgli_pollable_set_nonblocking(helper->pfd, true);
98
99 /* spawn helper process using mowgli_process_clone() */
100 helper->child = mowgli_process_clone((mowgli_process_start_fn_t) mowgli_helper_trampoline, helpername, &child);
101
102 if (helper->child == NULL)
103 {
104 mowgli_pollable_destroy(eventloop, helper->pfd);
105
106 close(io_fd[0]);
107 close(io_fd[1]);
108
109 mowgli_free(helper);
110 return NULL;
111 }
112
113 close(child.fd);
114
115 return helper;
116 }
117
118 mowgli_eventloop_helper_proc_t *
119 mowgli_helper_spawn(mowgli_eventloop_t *eventloop, const char *path, char *const argv[])
120 {
121 mowgli_eventloop_helper_proc_t *helper;
122 int io_fd[2];
123 char buf[64];
124
125 return_val_if_fail(eventloop != NULL, NULL);
126 return_val_if_fail(path != NULL, NULL);
127
128 helper = mowgli_alloc(sizeof *helper);
129 helper->type.type = MOWGLI_EVENTLOOP_TYPE_HELPER;
130 helper->eventloop = eventloop;
131
132 socketpair(AF_UNIX, SOCK_STREAM, 0, io_fd);
133
134 /* set up helper/child fd mapping */
135 helper->fd = io_fd[0];
136
137 /* make pollables and make them non-blocking */
138 helper->pfd = mowgli_pollable_create(eventloop, helper->fd, helper);
139
140 snprintf(buf, sizeof buf, "%d", io_fd[1]);
141 setenv("IO_FD", buf, 1);
142
143 /* Spawn helper process using mowgli_process_spawn(), helper will get
144 * IO_FD mapping from getenv(). Ugly hack, but it works...
145 * --nenolod
146 */
147 helper->child = mowgli_process_spawn(path, argv);
148
149 if (helper->child == NULL)
150 {
151 mowgli_pollable_destroy(eventloop, helper->pfd);
152
153 close(io_fd[0]);
154 close(io_fd[1]);
155
156 mowgli_free(helper);
157 return NULL;
158 }
159
160 close(io_fd[1]);
161
162 return helper;
163 }
164
165 /* Set up a helper connection to parent using getenv() */
166 mowgli_eventloop_helper_proc_t *
167 mowgli_helper_setup(mowgli_eventloop_t *eventloop)
168 {
169 mowgli_eventloop_helper_proc_t *helper;
170 const char *env_io_fd;
171
172 env_io_fd = getenv("IO_FD");
173
174 /* this shouldn't be a hard-fail because some idiot may run the helper from
175 * the cmdline. allow the helper to error out gracefully if not spawned as
176 * a helper.
177 */
178 if (env_io_fd == NULL)
179 return NULL;
180
181 helper = mowgli_alloc(sizeof *helper);
182 helper->type.type = MOWGLI_EVENTLOOP_TYPE_HELPER;
183 helper->eventloop = eventloop;
184 helper->fd = atoi(env_io_fd);
185 helper->pfd = mowgli_pollable_create(helper->eventloop, helper->fd, helper);
186
187 mowgli_pollable_set_nonblocking(helper->pfd, true);
188
189 return helper;
190 }
191
192 static void
193 mowgli_helper_io_trampoline(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata)
194 {
195 mowgli_eventloop_helper_proc_t *helper = userdata;
196
197 switch (dir)
198 {
199 case MOWGLI_EVENTLOOP_IO_READ:
200
201 if (helper->read_function != NULL)
202 {
203 helper->read_function(eventloop, helper, MOWGLI_EVENTLOOP_IO_READ, helper->userdata);
204 return;
205 }
206
207 default:
208 break;
209 }
210 }
211
212 void
213 mowgli_helper_set_read_cb(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_proc_t *helper, mowgli_eventloop_io_cb_t *read_fn)
214 {
215 return_if_fail(eventloop != NULL);
216 return_if_fail(helper != NULL);
217
218 if (read_fn == NULL)
219 mowgli_pollable_setselect(eventloop, helper->pfd, MOWGLI_EVENTLOOP_IO_READ, NULL);
220
221 helper->read_function = read_fn;
222 mowgli_pollable_setselect(eventloop, helper->pfd, MOWGLI_EVENTLOOP_IO_READ, mowgli_helper_io_trampoline);
223 }
224
225 void
226 mowgli_helper_destroy(mowgli_eventloop_t *eventloop, mowgli_eventloop_helper_proc_t *helper)
227 {
228 return_if_fail(eventloop != NULL);
229 return_if_fail(helper != NULL);
230
231 mowgli_process_kill(helper->child);
232 mowgli_pollable_destroy(eventloop, helper->pfd);
233 close(helper->fd);
234
235 mowgli_free(helper);
236 }