aboutsummaryrefslogtreecommitdiff
path: root/telescope.c
blob: 8bc89bfa10d7876b108f85b77bf468c796837c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#include "telescope.h"

#include <sys/socket.h>

#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct event		 netev, fsev;
struct tabshead		 tabshead;
struct proxylist	 proxies;

/* the first is also the fallback one */
static struct proto protos[] = {
	{ "gemini",	load_gemini_url },
	{ "about",	load_about_url },
	{ NULL, NULL },
};

static struct imsgbuf	*netibuf, *fsibuf;

static void		 die(void) __attribute__((__noreturn__));
static struct tab	*tab_by_id(uint32_t);
static void		 handle_imsg_err(struct imsg*, size_t);
static void		 handle_imsg_check_cert(struct imsg*, size_t);
static void		 handle_check_cert_user_choice(int, unsigned int);
static void		 handle_maybe_save_new_cert(int, unsigned int);
static void		 handle_imsg_got_code(struct imsg*, size_t);
static void		 handle_imsg_got_meta(struct imsg*, size_t);
static void		 handle_maybe_save_page(int, unsigned int);
static void		 handle_save_page_path(const char *, unsigned int);
static void		 handle_imsg_file_opened(struct imsg*, size_t);
static void		 handle_imsg_buf(struct imsg*, size_t);
static void		 handle_imsg_eof(struct imsg*, size_t);
static void		 handle_imsg_bookmark_ok(struct imsg*, size_t);
static void		 handle_imsg_save_cert_ok(struct imsg*, size_t);
static void		 handle_imsg_update_cert_ok(struct imsg *, size_t);
static void		 handle_dispatch_imsg(int, short, void*);
static void		 load_page_from_str(struct tab*, const char*);
static void		 do_load_url(struct tab*, const char*);

static imsg_handlerfn *handlers[] = {
	[IMSG_ERR] = handle_imsg_err,
	[IMSG_CHECK_CERT] = handle_imsg_check_cert,
	[IMSG_GOT_CODE] = handle_imsg_got_code,
	[IMSG_GOT_META] = handle_imsg_got_meta,
	[IMSG_BUF] = handle_imsg_buf,
	[IMSG_EOF] = handle_imsg_eof,
	[IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
	[IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
	[IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
	[IMSG_FILE_OPENED] = handle_imsg_file_opened,
};

static struct ohash	certs;

static void __attribute__((__noreturn__))
die(void)
{
	abort(); 		/* TODO */
}

static struct tab *
tab_by_id(uint32_t id)
{
	struct tab *t;

	TAILQ_FOREACH(t, &tabshead, tabs) {
		if (t->id == id)
			return t;
	}

	die();
}

static void
handle_imsg_err(struct imsg *imsg, size_t datalen)
{
	struct tab	*tab;
	char		*page;

	tab = tab_by_id(imsg->hdr.peerid);

	page = imsg->data;
	page[datalen-1] = '\0';

	if (asprintf(&page, "# Error loading %s\n\n> %s\n",
	    tab->hist_cur->h, page) == -1)
		die();
	load_page_from_str(tab, page);
	free(page);
}

static void
handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
{
	const char		*hash, *host, *port;
	int			 tofu_res;
	struct tofu_entry	*e;
	struct tab		*tab;

	hash = imsg->data;
	if (hash[datalen-1] != '\0')
		abort();

	tab = tab_by_id(imsg->hdr.peerid);

	if (tab->proxy != NULL) {
		host = tab->proxy->host;
		port = tab->proxy->port;
	} else {
		host = tab->uri.host;
		port = tab->uri.port;
	}

	if ((e = tofu_lookup(&certs, host, port)) == NULL) {
		/* TODO: an update in libressl/libretls changed
		 * significantly.  Find a better approach at storing
		 * the certs! */
		if (datalen > sizeof(e->hash))
			abort();

		tofu_res = 1;	/* trust on first use */
		if ((e = calloc(1, sizeof(*e))) == NULL)
			abort();
		strlcpy(e->domain, host, sizeof(e->domain));
		if (*port != '\0' && strcmp(port, "1965")) {
			strlcat(e->domain, ":", sizeof(e->domain));
			strlcat(e->domain, port, sizeof(e->domain));
		}
		strlcpy(e->hash, hash, sizeof(e->hash));
		tofu_add(&certs, e);
		imsg_compose(fsibuf, IMSG_SAVE_CERT, tab->id, 0, -1,
		    e, sizeof(*e));
		imsg_flush(fsibuf);
	} else
		tofu_res = !strcmp(hash, e->hash);

	if (tofu_res) {
		tab->trust = e->verified ? TS_VERIFIED : TS_TRUSTED;
		imsg_compose(netibuf, IMSG_CERT_STATUS, imsg->hdr.peerid, 0, -1,
		    &tofu_res, sizeof(tofu_res));
		imsg_flush(netibuf);
	} else {
		tab->trust = TS_UNTRUSTED;
		load_page_from_str(tab, "# Certificate mismatch\n");
		if ((tab->cert = strdup(hash)) == NULL)
			die();
		ui_yornp("Certificate mismatch.  Proceed?",
		    handle_check_cert_user_choice, tab->id);
	}
}

static void
handle_check_cert_user_choice(int accept, unsigned int tabid)
{
	struct tab *tab;

	tab = tab_by_id(tabid);

	imsg_compose(netibuf, IMSG_CERT_STATUS, tabid, 0, -1,
	    &accept, sizeof(accept));
	imsg_flush(netibuf);

	if (accept)
		ui_yornp("Save the new certificate?",
		    handle_maybe_save_new_cert, tabid);
	else {
		free(tab->cert);
		tab->cert = NULL;
	}
}

static void
handle_maybe_save_new_cert(int accept, unsigned int tabid)
{
	struct tab *tab;
	struct tofu_entry *e;
	const char *host, *port;

	tab = tab_by_id(tabid);

	if (tab->proxy != NULL) {
		host = tab->proxy->host;
		port = tab->proxy->port;
	} else {
		host = tab->uri.host;
		port = tab->uri.port;
	}

	if (!accept)
		goto end;

	if ((e = calloc(1, sizeof(*e))) == NULL)
		die();

	strlcpy(e->domain, host, sizeof(e->domain));
	if (*port != '\0' && strcmp(port, "1965")) {
		strlcat(e->domain, ":", sizeof(e->domain));
		strlcat(e->domain, port, sizeof(e->domain));
	}
	strlcpy(e->hash, tab->cert, sizeof(e->hash));
	imsg_compose(fsibuf, IMSG_UPDATE_CERT, 0, 0, -1, e, sizeof(*e));
	imsg_flush(fsibuf);

	tofu_update(&certs, e);

	tab->trust = TS_TRUSTED;

end:
	free(tab->cert);
	tab->cert = NULL;
}

static inline int
normalize_code(int n)
{
	if (n < 20) {
		if (n == 10 || n == 11)
			return n;
		return 10;
	} else if (n < 30) {
		return 20;
	} else if (n < 40) {
		if (n == 30 || n == 31)
			return n;
		return 30;
	} else if (n < 50) {
		if (n <= 44)
			return n;
		return 40;
	} else if (n < 60) {
		if (n <= 53 || n == 59)
			return n;
		return 50;
	} else if (n < 70) {
		if (n <= 62)
			return n;
		return 60;
	} else
		return MALFORMED_RESPONSE;
}

static void
handle_imsg_got_code(struct imsg *imsg, size_t datalen)
{
	struct tab	*tab;

	tab = tab_by_id(imsg->hdr.peerid);

	if (sizeof(tab->code) != datalen)
		die();

	memcpy(&tab->code, imsg->data, sizeof(tab->code));
	tab->code = normalize_code(tab->code);
	if (tab->code != 30 && tab->code != 31)
		tab->redirect_count = 0;
}

static void
handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
{
	struct tab	*tab;

	tab = tab_by_id(imsg->hdr.peerid);

	if (sizeof(tab->meta) <= datalen)
		die();

	memcpy(tab->meta, imsg->data, datalen);

	if (tab->code < 10) {	/* internal errors */
		load_page_from_str(tab, err_pages[tab->code]);
	} else if (tab->code < 20) {	/* 1x */
		load_page_from_str(tab, err_pages[tab->code]);
		ui_require_input(tab, tab->code == 11);
	} else if (tab->code == 20) {
		if (setup_parser_for(tab)) {
			imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
			imsg_flush(netibuf);
		} else {
			load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
			ui_yornp("Can't display page, wanna save?",
			    handle_maybe_save_page, tab->id);
		}
	} else if (tab->code < 40) { /* 3x */
		tab->redirect_count++;

		/* TODO: make customizable? */
		if (tab->redirect_count > 5) {
			load_page_from_str(tab,
			    err_pages[TOO_MUCH_REDIRECTS]);
		} else
			do_load_url(tab, tab->meta);
	} else { /* 4x, 5x & 6x */
		load_page_from_str(tab, err_pages[tab->code]);
	}
}

static void
handle_maybe_save_page(int dosave, unsigned int tabid)
{
	if (dosave)
		ui_read("Save to path", handle_save_page_path, tabid);
	else
		stop_tab(tab_by_id(tabid));
}

static void
handle_save_page_path(const char *path, unsigned int tabid)
{
	struct tab *tab;

	if (path == NULL) {
		stop_tab(tab_by_id(tabid));
		return;
	}

	tab = tab_by_id(tabid);
	tab->path = strdup(path);

	imsg_compose(fsibuf, IMSG_FILE_OPEN, tabid, 0, -1, path, strlen(path)+1);
	imsg_flush(fsibuf);
}

static void
handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
{
	struct tab	*tab;
	char		*page;
	const char	*e;
	int		 l;

	tab = tab_by_id(imsg->hdr.peerid);

	if (imsg->fd == -1) {
		stop_tab(tab);

		e = imsg->data;
		if (e[datalen-1] != '\0')
			die();
		l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
		    tab->path, e);
		if (l == -1)
			die();
		load_page_from_str(tab, page);
		free(page);
	} else {
		tab->fd = imsg->fd;
		imsg_compose(netibuf, IMSG_PROCEED, tab->id, 0, -1, NULL, 0);
		imsg_flush(netibuf);
	}
}

static void
handle_imsg_buf(struct imsg *imsg, size_t datalen)
{
	struct tab	*tab;
	int		 l;
	char		*page, buf[FMT_SCALED_STRSIZE] = {0};

	tab = tab_by_id(imsg->hdr.peerid);

	tab->bytes += datalen;
	if (tab->fd == -1) {
		if (!tab->buffer.page.parse(&tab->buffer.page,
		    imsg->data, datalen))
			die();
	} else {
		write(tab->fd, imsg->data, datalen);
		fmt_scaled(tab->bytes, buf);
		l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
		    tab->path,
		    buf);
		if (l == -1)
			die();
		load_page_from_str(tab, page);
		free(page);
	}

	ui_on_tab_refresh(tab);
}

static void
handle_imsg_eof(struct imsg *imsg, size_t datalen)
{
	struct tab	*tab;
	int		 l;
	char		*page, buf[FMT_SCALED_STRSIZE] = {0};

	tab = tab_by_id(imsg->hdr.peerid);

	if (tab->fd == -1) {
		if (!tab->buffer.page.free(&tab->buffer.page))
			die();
	} else {
		fmt_scaled(tab->bytes, buf);
		l = asprintf(&page, "Saved to \"%s\" (%s)\n",
		    tab->path,
		    buf);
		if (l == -1)
			die();
		load_page_from_str(tab, page);
		free(page);

		close(tab->fd);
		tab->fd = -1;
		free(tab->path);
		tab->path = NULL;
	}

	ui_on_tab_refresh(tab);
	ui_on_tab_loaded(tab);
}

static void
handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
{
	int res;

	if (datalen != sizeof(res))
		die();

	memcpy(&res, imsg->data, sizeof(res));
	if (res == 0)
		message("Added to bookmarks!");
	else
		message("Failed to add to bookmarks: %s",
		    strerror(res));
}

static void
handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
{
	int res;

	if (datalen != sizeof(res))
		die();
	memcpy(&res, imsg->data, datalen);
	if (res != 0)
		message("Failed to save the cert for: %s",
		    strerror(res));
}

static void
handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
{
	int res;

	if (datalen != sizeof(res))
		die();
	memcpy(&res, imsg->data, datalen);
	if (!res)
		message("Failed to update the certificate");
}

static void
handle_dispatch_imsg(int fd, short ev, void *d)
{
	struct imsgbuf	*ibuf = d;
	dispatch_imsg(ibuf, handlers, sizeof(handlers));
}

static void
load_page_from_str(struct tab *tab, const char *page)
{
	gemtext_initparser(&tab->buffer.page);
	if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
		die();
	if (!tab->buffer.page.free(&tab->buffer.page))
		die();
	ui_on_tab_refresh(tab);
	ui_on_tab_loaded(tab);
}

void
load_about_url(struct tab *tab, const char *url)
{
	tab->trust = TS_VERIFIED;

	gemtext_initparser(&tab->buffer.page);

	imsg_compose(fsibuf, IMSG_GET, tab->id, 0, -1,
	    tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
	imsg_flush(fsibuf);
}

void
load_gemini_url(struct tab *tab, const char *url)
{
	struct get_req	 req;

	stop_tab(tab);
	tab->id = tab_new_id();

	memset(&req, 0, sizeof(req));
	strlcpy(req.host, tab->uri.host, sizeof(req.host));
	strlcpy(req.port, tab->uri.port, sizeof(req.host));

	strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
	strlcat(req.req, "\r\n", sizeof(req.req));

	req.proto = PROTO_GEMINI;

	imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
	    &req, sizeof(req));
	imsg_flush(netibuf);
}

void
load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
{
	struct get_req req;

	stop_tab(tab);
	tab->id = tab_new_id();
	tab->proxy = p;

	memset(&req, 0, sizeof(req));
	strlcpy(req.host, p->host, sizeof(req.host));
	strlcpy(req.port, p->port, sizeof(req.host));

	strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
	strlcat(req.req, "\r\n", sizeof(req.req));

	req.proto = p->proto;

	imsg_compose(netibuf, IMSG_GET_RAW, tab->id, 0, -1,
	    &req, sizeof(req));
	imsg_flush(netibuf);
}

static void
do_load_url(struct tab *tab, const char *url)
{
	struct phos_uri	 uri;
	struct proto	*p;
	struct proxy	*proxy;
	char		*t;

	if (tab->fd != -1) {
		close(tab->fd);
		tab->fd = -1;
		free(tab->path);
		tab->path = NULL;
	}

	tab->trust = TS_UNKNOWN;

	memcpy(&uri, &tab->uri, sizeof(tab->uri));
	if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
                if (asprintf(&t, "#error loading %s\n>%s\n",
		    url, "Can't parse the URI") == -1)
			die();
		strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
		load_page_from_str(tab, t);
		free(t);
		return;
	}

	phos_serialize_uri(&tab->uri, tab->hist_cur->h,
	    sizeof(tab->hist_cur->h));

	for (p = protos; p->schema != NULL; ++p) {
		if (!strcmp(tab->uri.scheme, p->schema)) {
			p->loadfn(tab, url);
                        return;
		}
	}

	TAILQ_FOREACH(proxy, &proxies, proxies) {
		if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
			load_via_proxy(tab, url, proxy);
			return;
		}
	}

	protos[0].loadfn(tab, url);
}

void
load_url(struct tab *tab, const char *url)
{
	if (tab->hist_cur != NULL)
		hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));

	if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
		event_loopbreak();
		return;
	}

	tab->proxy = NULL;

	hist_push(&tab->hist, tab->hist_cur);
	do_load_url(tab, url);
	empty_vlist(&tab->buffer);
	empty_linelist(&tab->buffer);
}

int
load_previous_page(struct tab *tab)
{
	struct hist	*h;

	if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
		return 0;
	tab->hist_cur = h;
	do_load_url(tab, h->h);
	return 1;
}

int
load_next_page(struct tab *tab)
{
	struct hist	*h;

	if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
		return 0;
	tab->hist_cur = h;
	do_load_url(tab, h->h);
	return 1;
}

void
stop_tab(struct tab *tab)
{
	imsg_compose(netibuf, IMSG_STOP, tab->id, 0, -1, NULL, 0);
	imsg_flush(netibuf);

	if (tab->fd != -1) {
		close(tab->fd);
		tab->fd = -1;
		free(tab->path);
		tab->path = NULL;
		load_page_from_str(tab, "Stopped.\n");
	}
}

void
add_to_bookmarks(const char *str)
{
	imsg_compose(fsibuf, IMSG_BOOKMARK_PAGE, 0, 0, -1, str, strlen(str)+1);
	imsg_flush(fsibuf);
}

void
save_session(void)
{
	struct tab *tab;

	imsg_compose(fsibuf, IMSG_SESSION_START, 0, 0, -1, NULL, 0);
	imsg_flush(fsibuf);

	TAILQ_FOREACH(tab, &tabshead, tabs) {
		imsg_compose(fsibuf, IMSG_SESSION_TAB, 0, 0, -1,
		    tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
		imsg_flush(fsibuf);
	}

	imsg_compose(fsibuf, IMSG_SESSION_END, 0, 0, -1, NULL, 0);
	imsg_flush(fsibuf);
}

int
main(int argc, char * const *argv)
{
	struct imsgbuf	network_ibuf, fs_ibuf;
	int		net_fds[2], fs_fds[2];

	signal(SIGCHLD, SIG_IGN);
	signal(SIGPIPE, SIG_IGN);

	/* initialize part of the fs layer.  Before starting the UI
	 * and dropping the priviledges we need to read some stuff. */
	fs_init();

	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fs_fds) == -1)
		err(1, "socketpair");

	switch (fork()) {
	case -1:
		err(1, "fork");
	case 0:
		/* child */
		setproctitle("fs");
		close(fs_fds[0]);
		imsg_init(&fs_ibuf, fs_fds[1]);
		exit(fs_main(&fs_ibuf));
	default:
		close(fs_fds[1]);
		imsg_init(&fs_ibuf, fs_fds[0]);
		fsibuf = &fs_ibuf;
	}

	if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, net_fds) == -1)
		err(1, "socketpair");

	switch (fork()) {
	case -1:
		err(1, "fork");
	case 0:
		/* child */
		setproctitle("client");
		close(net_fds[0]);
		close(fs_fds[0]);
		imsg_init(&network_ibuf, net_fds[1]);
		exit(client_main(&network_ibuf));
	default:
		close(net_fds[1]);
		imsg_init(&network_ibuf, net_fds[0]);
		netibuf = &network_ibuf;
	}

	setproctitle("ui");

	tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
	load_certs(&certs);

	TAILQ_INIT(&tabshead);
	TAILQ_INIT(&proxies);

	event_init();

	event_set(&netev, netibuf->fd, EV_READ | EV_PERSIST,
	    handle_dispatch_imsg, netibuf);
	event_add(&netev, NULL);

	event_set(&fsev, fsibuf->fd, EV_READ | EV_PERSIST,
	    handle_dispatch_imsg, fsibuf);
	event_add(&fsev, NULL);

	if (ui_init(argc, argv)) {
		sandbox_ui_process();
		event_dispatch();
		ui_end();
	}

	imsg_compose(netibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
	imsg_flush(netibuf);

	imsg_compose(fsibuf, IMSG_QUIT, 0, 0, -1, NULL, 0);
	imsg_flush(fsibuf);

	return 0;
}