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