Blame


1 5e11c00c 2021-03-02 op #include "telescope.h"
2 5e11c00c 2021-03-02 op
3 5e11c00c 2021-03-02 op #include <sys/socket.h>
4 5e11c00c 2021-03-02 op
5 5e11c00c 2021-03-02 op #include <err.h>
6 5e11c00c 2021-03-02 op #include <errno.h>
7 5e11c00c 2021-03-02 op #include <signal.h>
8 5e11c00c 2021-03-02 op #include <stdio.h>
9 5e11c00c 2021-03-02 op #include <stdlib.h>
10 5e11c00c 2021-03-02 op #include <string.h>
11 5e11c00c 2021-03-02 op #include <unistd.h>
12 5e11c00c 2021-03-02 op
13 35e1f40a 2021-03-14 op struct event netev, fsev;
14 5e11c00c 2021-03-02 op struct tabshead tabshead;
15 5e11c00c 2021-03-02 op
16 c07ac996 2021-03-12 op /* the first is also the fallback one */
17 c07ac996 2021-03-12 op static struct proto protos[] = {
18 4d3785b1 2021-03-09 op { "gemini:", load_gemini_url },
19 4d3785b1 2021-03-09 op { "about:", load_about_url },
20 4d3785b1 2021-03-09 op { NULL, NULL },
21 4d3785b1 2021-03-09 op };
22 4d3785b1 2021-03-09 op
23 35e1f40a 2021-03-14 op static struct imsgbuf *netibuf, *fsibuf;
24 5e11c00c 2021-03-02 op
25 2051e653 2021-03-13 op static void die(void) __attribute__((__noreturn__));
26 2051e653 2021-03-13 op static struct tab *tab_by_id(uint32_t);
27 2051e653 2021-03-13 op static void handle_imsg_err(struct imsg*, size_t);
28 2051e653 2021-03-13 op static void handle_imsg_check_cert(struct imsg*, size_t);
29 2051e653 2021-03-13 op static void handle_imsg_got_code(struct imsg*, size_t);
30 2051e653 2021-03-13 op static void handle_imsg_got_meta(struct imsg*, size_t);
31 2051e653 2021-03-13 op static void handle_imsg_buf(struct imsg*, size_t);
32 2051e653 2021-03-13 op static void handle_imsg_eof(struct imsg*, size_t);
33 2051e653 2021-03-13 op static void dispatch_imsg(int, short, void*);
34 2051e653 2021-03-13 op static void load_page_from_str(struct tab*, const char*);
35 2051e653 2021-03-13 op static void do_load_url(struct tab*, const char*);
36 5e11c00c 2021-03-02 op
37 5e11c00c 2021-03-02 op static imsg_handlerfn *handlers[] = {
38 5e11c00c 2021-03-02 op [IMSG_ERR] = handle_imsg_err,
39 5e11c00c 2021-03-02 op [IMSG_CHECK_CERT] = handle_imsg_check_cert,
40 5e11c00c 2021-03-02 op [IMSG_GOT_CODE] = handle_imsg_got_code,
41 5e11c00c 2021-03-02 op [IMSG_GOT_META] = handle_imsg_got_meta,
42 5e11c00c 2021-03-02 op [IMSG_BUF] = handle_imsg_buf,
43 5e11c00c 2021-03-02 op [IMSG_EOF] = handle_imsg_eof,
44 5e11c00c 2021-03-02 op };
45 5e11c00c 2021-03-02 op
46 5e11c00c 2021-03-02 op static void __attribute__((__noreturn__))
47 5e11c00c 2021-03-02 op die(void)
48 5e11c00c 2021-03-02 op {
49 5e11c00c 2021-03-02 op abort(); /* TODO */
50 5e11c00c 2021-03-02 op }
51 5e11c00c 2021-03-02 op
52 5e11c00c 2021-03-02 op static struct tab *
53 5e11c00c 2021-03-02 op tab_by_id(uint32_t id)
54 5e11c00c 2021-03-02 op {
55 5e11c00c 2021-03-02 op struct tab *t;
56 5e11c00c 2021-03-02 op
57 5e11c00c 2021-03-02 op TAILQ_FOREACH(t, &tabshead, tabs) {
58 5e11c00c 2021-03-02 op if (t->id == id)
59 5e11c00c 2021-03-02 op return t;
60 5e11c00c 2021-03-02 op }
61 5e11c00c 2021-03-02 op
62 5e11c00c 2021-03-02 op die();
63 5e11c00c 2021-03-02 op }
64 5e11c00c 2021-03-02 op
65 5e11c00c 2021-03-02 op static void
66 5e11c00c 2021-03-02 op handle_imsg_err(struct imsg *imsg, size_t datalen)
67 5e11c00c 2021-03-02 op {
68 3a9b9365 2021-03-09 op struct tab *tab;
69 3a9b9365 2021-03-09 op char *page;
70 3a9b9365 2021-03-09 op
71 3a9b9365 2021-03-09 op tab = tab_by_id(imsg->hdr.peerid);
72 3a9b9365 2021-03-09 op
73 3a9b9365 2021-03-09 op page = imsg->data;
74 3a9b9365 2021-03-09 op page[datalen-1] = '\0';
75 3a9b9365 2021-03-09 op
76 3a9b9365 2021-03-09 op if (asprintf(&page, "# Error loading %s\n\n> %s\n",
77 2051e653 2021-03-13 op tab->hist_cur->h, page) == -1)
78 3a9b9365 2021-03-09 op die();
79 3a9b9365 2021-03-09 op load_page_from_str(tab, page);
80 3a9b9365 2021-03-09 op free(page);
81 5e11c00c 2021-03-02 op }
82 5e11c00c 2021-03-02 op
83 5e11c00c 2021-03-02 op static void
84 5e11c00c 2021-03-02 op handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
85 5e11c00c 2021-03-02 op {
86 5e11c00c 2021-03-02 op int tofu_res = 1;
87 5e11c00c 2021-03-02 op
88 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1, &tofu_res, sizeof(tofu_res));
89 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
90 5e11c00c 2021-03-02 op }
91 5e11c00c 2021-03-02 op
92 5cd2ebb1 2021-03-11 op static inline int
93 5cd2ebb1 2021-03-11 op normalize_code(int n)
94 5cd2ebb1 2021-03-11 op {
95 5cd2ebb1 2021-03-11 op if (n < 20) {
96 5cd2ebb1 2021-03-11 op if (n == 10 || n == 11)
97 5cd2ebb1 2021-03-11 op return n;
98 5cd2ebb1 2021-03-11 op return 10;
99 5cd2ebb1 2021-03-11 op } else if (n < 30) {
100 5cd2ebb1 2021-03-11 op return 20;
101 5cd2ebb1 2021-03-11 op } else if (n < 40) {
102 5cd2ebb1 2021-03-11 op if (n == 30 || n == 31)
103 5cd2ebb1 2021-03-11 op return n;
104 5cd2ebb1 2021-03-11 op return 30;
105 5cd2ebb1 2021-03-11 op } else if (n < 50) {
106 5cd2ebb1 2021-03-11 op if (n <= 44)
107 5cd2ebb1 2021-03-11 op return n;
108 5cd2ebb1 2021-03-11 op return 40;
109 5cd2ebb1 2021-03-11 op } else if (n < 60) {
110 5cd2ebb1 2021-03-11 op if (n <= 53 || n == 59)
111 5cd2ebb1 2021-03-11 op return n;
112 5cd2ebb1 2021-03-11 op return 50;
113 5cd2ebb1 2021-03-11 op } else if (n < 70) {
114 5cd2ebb1 2021-03-11 op if (n <= 62)
115 5cd2ebb1 2021-03-11 op return n;
116 5cd2ebb1 2021-03-11 op return 60;
117 5cd2ebb1 2021-03-11 op } else
118 5cd2ebb1 2021-03-11 op return MALFORMED_RESPONSE;
119 5cd2ebb1 2021-03-11 op }
120 5cd2ebb1 2021-03-11 op
121 5e11c00c 2021-03-02 op static void
122 5e11c00c 2021-03-02 op handle_imsg_got_code(struct imsg *imsg, size_t datalen)
123 5e11c00c 2021-03-02 op {
124 0972d8b2 2021-03-02 op struct tab *tab;
125 5e11c00c 2021-03-02 op
126 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
127 0972d8b2 2021-03-02 op
128 0972d8b2 2021-03-02 op if (sizeof(tab->code) != datalen)
129 5e11c00c 2021-03-02 op die();
130 5e11c00c 2021-03-02 op
131 5cd2ebb1 2021-03-11 op memcpy(&tab->code, imsg->data, sizeof(tab->code));
132 5cd2ebb1 2021-03-11 op tab->code = normalize_code(tab->code);
133 5cd2ebb1 2021-03-11 op if (tab->code != 30 && tab->code != 31)
134 0972d8b2 2021-03-02 op tab->redirect_count = 0;
135 5e11c00c 2021-03-02 op }
136 5e11c00c 2021-03-02 op
137 5e11c00c 2021-03-02 op static void
138 5e11c00c 2021-03-02 op handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
139 5e11c00c 2021-03-02 op {
140 0972d8b2 2021-03-02 op struct tab *tab;
141 0972d8b2 2021-03-02 op
142 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
143 0972d8b2 2021-03-02 op
144 0972d8b2 2021-03-02 op if (sizeof(tab->meta) <= datalen)
145 0972d8b2 2021-03-02 op die();
146 5cd2ebb1 2021-03-11 op
147 0972d8b2 2021-03-02 op memcpy(tab->meta, imsg->data, datalen);
148 0972d8b2 2021-03-02 op
149 5cd2ebb1 2021-03-11 op if (tab->code < 10) { /* internal errors */
150 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
151 5cd2ebb1 2021-03-11 op } else if (tab->code < 20) { /* 1x */
152 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
153 5cd2ebb1 2021-03-11 op ui_require_input(tab, tab->code == 11);
154 5cd2ebb1 2021-03-11 op } else if (tab->code == 20) {
155 c07ac996 2021-03-12 op if (setup_parser_for(tab)) {
156 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
157 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
158 c07ac996 2021-03-12 op } else {
159 c07ac996 2021-03-12 op load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
160 c07ac996 2021-03-12 op }
161 5cd2ebb1 2021-03-11 op } else if (tab->code < 40) { /* 3x */
162 3a9b9365 2021-03-09 op tab->redirect_count++;
163 0972d8b2 2021-03-02 op
164 3a9b9365 2021-03-09 op /* TODO: make customizable? */
165 3a9b9365 2021-03-09 op if (tab->redirect_count > 5) {
166 3a9b9365 2021-03-09 op load_page_from_str(tab,
167 3a9b9365 2021-03-09 op err_pages[TOO_MUCH_REDIRECTS]);
168 5cd2ebb1 2021-03-11 op } else
169 2051e653 2021-03-13 op do_load_url(tab, tab->meta);
170 5cd2ebb1 2021-03-11 op } else { /* 4x, 5x & 6x */
171 5cd2ebb1 2021-03-11 op load_page_from_str(tab, err_pages[tab->code]);
172 3a9b9365 2021-03-09 op }
173 5e11c00c 2021-03-02 op }
174 5e11c00c 2021-03-02 op
175 5e11c00c 2021-03-02 op static void
176 5e11c00c 2021-03-02 op handle_imsg_buf(struct imsg *imsg, size_t datalen)
177 5e11c00c 2021-03-02 op {
178 0972d8b2 2021-03-02 op struct tab *tab;
179 5e11c00c 2021-03-02 op
180 0972d8b2 2021-03-02 op tab = tab_by_id(imsg->hdr.peerid);
181 5e11c00c 2021-03-02 op
182 0972d8b2 2021-03-02 op if (!tab->page.parse(&tab->page, imsg->data, datalen))
183 5e11c00c 2021-03-02 op die();
184 5e11c00c 2021-03-02 op
185 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
186 5e11c00c 2021-03-02 op }
187 5e11c00c 2021-03-02 op
188 5e11c00c 2021-03-02 op static void
189 5e11c00c 2021-03-02 op handle_imsg_eof(struct imsg *imsg, size_t datalen)
190 5e11c00c 2021-03-02 op {
191 a5c3e03d 2021-03-02 op struct tab *t;
192 a5c3e03d 2021-03-02 op
193 a5c3e03d 2021-03-02 op t = tab_by_id(imsg->hdr.peerid);
194 a5c3e03d 2021-03-02 op if (!t->page.free(&t->page))
195 a5c3e03d 2021-03-02 op die();
196 a5c3e03d 2021-03-02 op
197 a5c3e03d 2021-03-02 op ui_on_tab_refresh(t);
198 8af5e5ed 2021-03-08 op ui_on_tab_loaded(t);
199 5e11c00c 2021-03-02 op }
200 5e11c00c 2021-03-02 op
201 5e11c00c 2021-03-02 op static void
202 5e11c00c 2021-03-02 op dispatch_imsg(int fd, short ev, void *d)
203 5e11c00c 2021-03-02 op {
204 35e1f40a 2021-03-14 op struct imsgbuf *ibuf = d;
205 35e1f40a 2021-03-14 op struct imsg imsg;
206 35e1f40a 2021-03-14 op size_t datalen;
207 35e1f40a 2021-03-14 op ssize_t n;
208 5e11c00c 2021-03-02 op
209 35e1f40a 2021-03-14 op if ((n = imsg_read(ibuf)) == -1) {
210 5e11c00c 2021-03-02 op if (errno == EAGAIN || errno == EWOULDBLOCK)
211 5e11c00c 2021-03-02 op return;
212 5e11c00c 2021-03-02 op die();
213 5e11c00c 2021-03-02 op }
214 5e11c00c 2021-03-02 op
215 35e1f40a 2021-03-14 op if (n == 0)
216 35e1f40a 2021-03-14 op _exit(1);
217 5e11c00c 2021-03-02 op
218 5e11c00c 2021-03-02 op for (;;) {
219 35e1f40a 2021-03-14 op if ((n = imsg_get(ibuf, &imsg)) == -1)
220 5e11c00c 2021-03-02 op die();
221 5e11c00c 2021-03-02 op if (n == 0)
222 5e11c00c 2021-03-02 op return;
223 5e11c00c 2021-03-02 op datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
224 5e11c00c 2021-03-02 op handlers[imsg.hdr.type](&imsg, datalen);
225 5e11c00c 2021-03-02 op imsg_free(&imsg);
226 5e11c00c 2021-03-02 op }
227 5e11c00c 2021-03-02 op }
228 5e11c00c 2021-03-02 op
229 0972d8b2 2021-03-02 op static void
230 0972d8b2 2021-03-02 op load_page_from_str(struct tab *tab, const char *page)
231 0972d8b2 2021-03-02 op {
232 0972d8b2 2021-03-02 op gemtext_initparser(&tab->page);
233 3a9b9365 2021-03-09 op if (!tab->page.parse(&tab->page, page, strlen(page)))
234 0972d8b2 2021-03-02 op die();
235 0972d8b2 2021-03-02 op if (!tab->page.free(&tab->page))
236 0972d8b2 2021-03-02 op die();
237 0972d8b2 2021-03-02 op ui_on_tab_refresh(tab);
238 8af5e5ed 2021-03-08 op ui_on_tab_loaded(tab);
239 0972d8b2 2021-03-02 op }
240 0972d8b2 2021-03-02 op
241 bcb0b073 2021-03-07 op void
242 4d3785b1 2021-03-09 op load_about_url(struct tab *tab, const char *url)
243 0972d8b2 2021-03-02 op {
244 2051e653 2021-03-13 op char *m;
245 2051e653 2021-03-13 op size_t len;
246 8af5e5ed 2021-03-08 op
247 2051e653 2021-03-13 op
248 4d3785b1 2021-03-09 op memset(&tab->url, 0, sizeof(tab->url));
249 4d3785b1 2021-03-09 op
250 4d3785b1 2021-03-09 op m = strchr(url, ':');
251 4d3785b1 2021-03-09 op strlcpy(tab->url.scheme, "about", sizeof(tab->url.scheme));
252 4d3785b1 2021-03-09 op strlcpy(tab->url.path, m+1, sizeof(tab->url.path));
253 097ff997 2021-03-09 op
254 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
255 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
256 4d3785b1 2021-03-09 op
257 35e1f40a 2021-03-14 op gemtext_initparser(&tab->page);
258 35e1f40a 2021-03-14 op
259 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
260 35e1f40a 2021-03-14 op tab->hist_cur->h, len+1);
261 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
262 4d3785b1 2021-03-09 op }
263 0972d8b2 2021-03-02 op
264 4d3785b1 2021-03-09 op void
265 4d3785b1 2021-03-09 op load_gemini_url(struct tab *tab, const char *url)
266 4d3785b1 2021-03-09 op {
267 4d3785b1 2021-03-09 op const char *err;
268 4d3785b1 2021-03-09 op char *p;
269 2051e653 2021-03-13 op size_t len;
270 3a9b9365 2021-03-09 op
271 4d3785b1 2021-03-09 op if (has_prefix(url, "gemini:")) {
272 4d3785b1 2021-03-09 op if (!url_parse(url, &tab->url, &err))
273 3a9b9365 2021-03-09 op goto err;
274 4d3785b1 2021-03-09 op } else {
275 4d3785b1 2021-03-09 op if (!url_resolve_from(&tab->url, url, &err))
276 4d3785b1 2021-03-09 op goto err;
277 3a9b9365 2021-03-09 op }
278 3a9b9365 2021-03-09 op
279 2051e653 2021-03-13 op len = sizeof(tab->hist_cur->h)-1;
280 2051e653 2021-03-13 op url_unparse(&tab->url, tab->hist_cur->h, len);
281 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_GET, tab->id, 0, -1,
282 2051e653 2021-03-13 op tab->hist_cur->h, len+1);
283 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
284 3a9b9365 2021-03-09 op return;
285 3a9b9365 2021-03-09 op
286 3a9b9365 2021-03-09 op err:
287 4d3785b1 2021-03-09 op if (asprintf(&p, "#error loading %s\n>%s\n",
288 4d3785b1 2021-03-09 op url, err) == -1)
289 3a9b9365 2021-03-09 op die();
290 2051e653 2021-03-13 op strlcpy(tab->hist_cur->h, url, len);
291 4d3785b1 2021-03-09 op load_page_from_str(tab, p);
292 4d3785b1 2021-03-09 op free(p);
293 0972d8b2 2021-03-02 op }
294 0972d8b2 2021-03-02 op
295 2051e653 2021-03-13 op static void
296 2051e653 2021-03-13 op do_load_url(struct tab *tab, const char *url)
297 4d3785b1 2021-03-09 op {
298 4d3785b1 2021-03-09 op struct proto *p;
299 4d3785b1 2021-03-09 op
300 4d3785b1 2021-03-09 op for (p = protos; p->schema != NULL; ++p) {
301 4d3785b1 2021-03-09 op if (has_prefix(url, p->schema)) {
302 4d3785b1 2021-03-09 op p->loadfn(tab, url);
303 4d3785b1 2021-03-09 op return;
304 4d3785b1 2021-03-09 op }
305 4d3785b1 2021-03-09 op }
306 4d3785b1 2021-03-09 op
307 4d3785b1 2021-03-09 op protos[0].loadfn(tab, url);
308 4d3785b1 2021-03-09 op }
309 4d3785b1 2021-03-09 op
310 9ad4627d 2021-03-10 op void
311 2051e653 2021-03-13 op load_url(struct tab *tab, const char *url)
312 2051e653 2021-03-13 op {
313 2051e653 2021-03-13 op if (tab->hist_cur != NULL)
314 2051e653 2021-03-13 op hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
315 2051e653 2021-03-13 op
316 2051e653 2021-03-13 op if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
317 2051e653 2021-03-13 op event_loopbreak();
318 2051e653 2021-03-13 op return;
319 2051e653 2021-03-13 op }
320 2051e653 2021-03-13 op hist_push(&tab->hist, tab->hist_cur);
321 2051e653 2021-03-13 op do_load_url(tab, url);
322 2051e653 2021-03-13 op }
323 2051e653 2021-03-13 op
324 2051e653 2021-03-13 op int
325 2051e653 2021-03-13 op load_previous_page(struct tab *tab)
326 2051e653 2021-03-13 op {
327 2051e653 2021-03-13 op struct hist *h;
328 2051e653 2021-03-13 op
329 2051e653 2021-03-13 op if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
330 2051e653 2021-03-13 op return 0;
331 2051e653 2021-03-13 op tab->hist_cur = h;
332 2051e653 2021-03-13 op do_load_url(tab, h->h);
333 2051e653 2021-03-13 op return 1;
334 2051e653 2021-03-13 op }
335 2051e653 2021-03-13 op
336 2051e653 2021-03-13 op int
337 2051e653 2021-03-13 op load_next_page(struct tab *tab)
338 2051e653 2021-03-13 op {
339 2051e653 2021-03-13 op struct hist *h;
340 2051e653 2021-03-13 op
341 2051e653 2021-03-13 op if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
342 2051e653 2021-03-13 op return 0;
343 2051e653 2021-03-13 op tab->hist_cur = h;
344 2051e653 2021-03-13 op do_load_url(tab, h->h);
345 2051e653 2021-03-13 op return 1;
346 2051e653 2021-03-13 op }
347 2051e653 2021-03-13 op
348 2051e653 2021-03-13 op void
349 9ad4627d 2021-03-10 op stop_tab(struct tab *tab)
350 9ad4627d 2021-03-10 op {
351 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
352 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
353 9ad4627d 2021-03-10 op }
354 9ad4627d 2021-03-10 op
355 5e11c00c 2021-03-02 op int
356 5e11c00c 2021-03-02 op main(void)
357 5e11c00c 2021-03-02 op {
358 35e1f40a 2021-03-14 op struct imsgbuf network_ibuf, fs_ibuf;
359 35e1f40a 2021-03-14 op int net_fds[2], fs_fds[2];
360 35e1f40a 2021-03-14 op pid_t pid;
361 5e11c00c 2021-03-02 op
362 35e1f40a 2021-03-14 op pid = getpid();
363 35e1f40a 2021-03-14 op
364 5e11c00c 2021-03-02 op signal(SIGCHLD, SIG_IGN);
365 5e11c00c 2021-03-02 op
366 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
367 5e11c00c 2021-03-02 op err(1, "socketpair");
368 5e11c00c 2021-03-02 op
369 5e11c00c 2021-03-02 op switch (fork()) {
370 5e11c00c 2021-03-02 op case -1:
371 5e11c00c 2021-03-02 op err(1, "fork");
372 5e11c00c 2021-03-02 op case 0:
373 5e11c00c 2021-03-02 op /* child */
374 35e1f40a 2021-03-14 op setproctitle("(%d) fs", pid);
375 35e1f40a 2021-03-14 op close(fs_fds[0]);
376 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[1]);
377 35e1f40a 2021-03-14 op exit(fs_main(&fs_ibuf));
378 35e1f40a 2021-03-14 op default:
379 35e1f40a 2021-03-14 op close(fs_fds[1]);
380 35e1f40a 2021-03-14 op imsg_init(&fs_ibuf, fs_fds[0]);
381 35e1f40a 2021-03-14 op fsibuf = &fs_ibuf;
382 5e11c00c 2021-03-02 op }
383 b1d4d01b 2021-03-14 op
384 35e1f40a 2021-03-14 op if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
385 35e1f40a 2021-03-14 op err(1, "socketpair");
386 5e11c00c 2021-03-02 op
387 35e1f40a 2021-03-14 op switch (fork()) {
388 35e1f40a 2021-03-14 op case -1:
389 35e1f40a 2021-03-14 op err(1, "fork");
390 35e1f40a 2021-03-14 op case 0:
391 35e1f40a 2021-03-14 op /* child */
392 35e1f40a 2021-03-14 op setproctitle("(%d) client", pid);
393 35e1f40a 2021-03-14 op close(net_fds[0]);
394 35e1f40a 2021-03-14 op close(fs_fds[0]);
395 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[1]);
396 35e1f40a 2021-03-14 op exit(client_main(&network_ibuf));
397 35e1f40a 2021-03-14 op default:
398 35e1f40a 2021-03-14 op close(net_fds[1]);
399 35e1f40a 2021-03-14 op imsg_init(&network_ibuf, net_fds[0]);
400 35e1f40a 2021-03-14 op netibuf = &network_ibuf;
401 35e1f40a 2021-03-14 op }
402 5e11c00c 2021-03-02 op
403 35e1f40a 2021-03-14 op setproctitle("(%d) ui", pid);
404 35e1f40a 2021-03-14 op
405 5e11c00c 2021-03-02 op TAILQ_INIT(&tabshead);
406 5e11c00c 2021-03-02 op
407 5e11c00c 2021-03-02 op event_init();
408 5e11c00c 2021-03-02 op
409 35e1f40a 2021-03-14 op event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, netibuf);
410 35e1f40a 2021-03-14 op event_add(&netev, NULL);
411 5e11c00c 2021-03-02 op
412 35e1f40a 2021-03-14 op event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST, dispatch_imsg, fsibuf);
413 35e1f40a 2021-03-14 op event_add(&fsev, NULL);
414 35e1f40a 2021-03-14 op
415 5e11c00c 2021-03-02 op ui_init();
416 5e11c00c 2021-03-02 op
417 b1d4d01b 2021-03-14 op sandbox_ui_process();
418 b1d4d01b 2021-03-14 op
419 5e11c00c 2021-03-02 op event_dispatch();
420 5e11c00c 2021-03-02 op
421 1b8a4bbf 2021-03-12 op imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
422 1b8a4bbf 2021-03-12 op imsg_flush(netibuf);
423 5e11c00c 2021-03-02 op
424 35e1f40a 2021-03-14 op imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
425 35e1f40a 2021-03-14 op imsg_flush(fsibuf);
426 35e1f40a 2021-03-14 op
427 5e11c00c 2021-03-02 op ui_end();
428 5e11c00c 2021-03-02 op
429 5e11c00c 2021-03-02 op return 0;
430 5e11c00c 2021-03-02 op }