Blob


1 #include <sys/socket.h>
3 #include <errno.h>
4 #include <getopt.h>
5 #include <limits.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
12 #include "defaults.h"
13 #include "pages.h"
14 #include "parser.h"
15 #include "telescope.h"
16 #include "ui.h"
18 static struct option longopts[] = {
19 {"colors", no_argument, NULL, 'c'},
20 {"help", no_argument, NULL, 'h'},
21 {"version", no_argument, NULL, 'v'},
22 {NULL, 0, NULL, 0},
23 };
25 static const char *opts = "Cc:hnT:v";
27 static struct imsgev *iev_fs, *iev_net;
29 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
30 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
32 enum telescope_process {
33 PROC_UI,
34 PROC_FS,
35 PROC_NET,
36 };
38 /* the first is also the fallback one */
39 static struct proto protos[] = {
40 { "gemini", load_gemini_url },
41 { "about", load_about_url },
42 { NULL, NULL },
43 };
45 static void die(void) __attribute__((__noreturn__));
46 static struct tab *tab_by_id(uint32_t);
47 static void handle_imsg_err(struct imsg*, size_t);
48 static void handle_imsg_check_cert(struct imsg*, size_t);
49 static void handle_check_cert_user_choice(int, struct tab *);
50 static void handle_maybe_save_new_cert(int, struct tab *);
51 static void handle_imsg_got_code(struct imsg*, size_t);
52 static void handle_imsg_got_meta(struct imsg*, size_t);
53 static void handle_maybe_save_page(int, struct tab *);
54 static void handle_save_page_path(const char *, struct tab *);
55 static void handle_imsg_file_opened(struct imsg*, size_t);
56 static void handle_imsg_buf(struct imsg*, size_t);
57 static void handle_imsg_eof(struct imsg*, size_t);
58 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
59 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
60 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
61 static void handle_dispatch_imsg(int, short, void*);
62 static void load_page_from_str(struct tab*, const char*);
63 static int do_load_url(struct tab*, const char*);
64 static pid_t start_child(enum telescope_process, const char *, int);
65 static int ui_send_net(int, uint32_t, const void *, uint16_t);
66 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
68 static imsg_handlerfn *handlers[] = {
69 [IMSG_ERR] = handle_imsg_err,
70 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
71 [IMSG_GOT_CODE] = handle_imsg_got_code,
72 [IMSG_GOT_META] = handle_imsg_got_meta,
73 [IMSG_BUF] = handle_imsg_buf,
74 [IMSG_EOF] = handle_imsg_eof,
75 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
76 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
77 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
78 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
79 };
81 static struct ohash certs;
83 static void __attribute__((__noreturn__))
84 die(void)
85 {
86 abort(); /* TODO */
87 }
89 static struct tab *
90 tab_by_id(uint32_t id)
91 {
92 struct tab *t;
94 TAILQ_FOREACH(t, &tabshead, tabs) {
95 if (t->id == id)
96 return t;
97 }
99 die();
102 static void
103 handle_imsg_err(struct imsg *imsg, size_t datalen)
105 struct tab *tab;
106 char *page;
108 tab = tab_by_id(imsg->hdr.peerid);
110 page = imsg->data;
111 page[datalen-1] = '\0';
113 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
114 tab->hist_cur->h, page) == -1)
115 die();
116 load_page_from_str(tab, page);
117 free(page);
120 static void
121 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
123 const char *hash, *host, *port;
124 int tofu_res;
125 struct tofu_entry *e;
126 struct tab *tab;
128 hash = imsg->data;
129 if (hash[datalen-1] != '\0')
130 abort();
132 tab = tab_by_id(imsg->hdr.peerid);
134 if (tab->proxy != NULL) {
135 host = tab->proxy->host;
136 port = tab->proxy->port;
137 } else {
138 host = tab->uri.host;
139 port = tab->uri.port;
142 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
143 /* TODO: an update in libressl/libretls changed
144 * significantly. Find a better approach at storing
145 * the certs! */
146 if (datalen > sizeof(e->hash))
147 abort();
149 tofu_res = 1; /* trust on first use */
150 if ((e = calloc(1, sizeof(*e))) == NULL)
151 abort();
152 strlcpy(e->domain, host, sizeof(e->domain));
153 if (*port != '\0' && strcmp(port, "1965")) {
154 strlcat(e->domain, ":", sizeof(e->domain));
155 strlcat(e->domain, port, sizeof(e->domain));
157 strlcpy(e->hash, hash, sizeof(e->hash));
158 tofu_add(&certs, e);
159 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
160 } else
161 tofu_res = !strcmp(hash, e->hash);
163 if (tofu_res) {
164 if (e->verified == -1)
165 tab->trust = TS_TEMP_TRUSTED;
166 else if (e->verified == 1)
167 tab->trust = TS_VERIFIED;
168 else
169 tab->trust = TS_TRUSTED;
171 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
172 &tofu_res, sizeof(tofu_res));
173 } else {
174 tab->trust = TS_UNTRUSTED;
175 load_page_from_str(tab, "# Certificate mismatch\n");
176 if ((tab->cert = strdup(hash)) == NULL)
177 die();
178 ui_yornp("Certificate mismatch. Proceed?",
179 handle_check_cert_user_choice, tab);
183 static void
184 handle_check_cert_user_choice(int accept, struct tab *tab)
186 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
187 sizeof(accept));
189 if (accept) {
190 /*
191 * trust the certificate for this session only. If
192 * the page results in a redirect while we're asking
193 * the user to save, we'll end up with an invalid
194 * tabid (one request == one tab id) and crash. It
195 * also makes sense to save it for the current session
196 * if the user accepted it.
197 */
198 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
200 ui_yornp("Save the new certificate?",
201 handle_maybe_save_new_cert, tab);
202 } else {
203 free(tab->cert);
204 tab->cert = NULL;
208 static void
209 handle_maybe_save_new_cert(int accept, struct tab *tab)
211 struct tofu_entry *e;
212 const char *host, *port;
214 if (tab->proxy != NULL) {
215 host = tab->proxy->host;
216 port = tab->proxy->port;
217 } else {
218 host = tab->uri.host;
219 port = tab->uri.port;
222 if (!accept)
223 goto end;
225 if ((e = calloc(1, sizeof(*e))) == NULL)
226 die();
228 strlcpy(e->domain, host, sizeof(e->domain));
229 if (*port != '\0' && strcmp(port, "1965")) {
230 strlcat(e->domain, ":", sizeof(e->domain));
231 strlcat(e->domain, port, sizeof(e->domain));
233 strlcpy(e->hash, tab->cert, sizeof(e->hash));
234 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
236 tofu_update(&certs, e);
238 tab->trust = TS_TRUSTED;
240 end:
241 free(tab->cert);
242 tab->cert = NULL;
245 static inline int
246 normalize_code(int n)
248 if (n < 20) {
249 if (n == 10 || n == 11)
250 return n;
251 return 10;
252 } else if (n < 30) {
253 return 20;
254 } else if (n < 40) {
255 if (n == 30 || n == 31)
256 return n;
257 return 30;
258 } else if (n < 50) {
259 if (n <= 44)
260 return n;
261 return 40;
262 } else if (n < 60) {
263 if (n <= 53 || n == 59)
264 return n;
265 return 50;
266 } else if (n < 70) {
267 if (n <= 62)
268 return n;
269 return 60;
270 } else
271 return MALFORMED_RESPONSE;
274 static void
275 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
277 struct tab *tab;
279 tab = tab_by_id(imsg->hdr.peerid);
281 if (sizeof(tab->code) != datalen)
282 die();
284 memcpy(&tab->code, imsg->data, sizeof(tab->code));
285 tab->code = normalize_code(tab->code);
286 if (tab->code != 30 && tab->code != 31)
287 tab->redirect_count = 0;
290 static void
291 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
293 struct tab *tab;
295 tab = tab_by_id(imsg->hdr.peerid);
297 if (sizeof(tab->meta) <= datalen)
298 die();
300 memcpy(tab->meta, imsg->data, datalen);
302 if (tab->code < 10) { /* internal errors */
303 load_page_from_str(tab, err_pages[tab->code]);
304 } else if (tab->code < 20) { /* 1x */
305 load_page_from_str(tab, err_pages[tab->code]);
306 ui_require_input(tab, tab->code == 11);
307 } else if (tab->code == 20) {
308 if (setup_parser_for(tab)) {
309 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
310 } else {
311 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
312 ui_yornp("Can't display page, wanna save?",
313 handle_maybe_save_page, tab);
315 } else if (tab->code < 40) { /* 3x */
316 tab->redirect_count++;
318 /* TODO: make customizable? */
319 if (tab->redirect_count > 5) {
320 load_page_from_str(tab,
321 err_pages[TOO_MUCH_REDIRECTS]);
322 } else
323 do_load_url(tab, tab->meta);
324 } else { /* 4x, 5x & 6x */
325 load_page_from_str(tab, err_pages[tab->code]);
329 static void
330 handle_maybe_save_page(int dosave, struct tab *tab)
332 if (dosave)
333 ui_read("Save to path", handle_save_page_path, tab);
334 else
335 stop_tab(tab);
338 static void
339 handle_save_page_path(const char *path, struct tab *tab)
341 if (path == NULL) {
342 stop_tab(tab);
343 return;
346 tab->path = strdup(path);
348 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
351 static void
352 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
354 struct tab *tab;
355 char *page;
356 const char *e;
357 int l;
359 tab = tab_by_id(imsg->hdr.peerid);
361 if (imsg->fd == -1) {
362 stop_tab(tab);
364 e = imsg->data;
365 if (e[datalen-1] != '\0')
366 die();
367 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
368 tab->path, e);
369 if (l == -1)
370 die();
371 load_page_from_str(tab, page);
372 free(page);
373 } else {
374 tab->fd = imsg->fd;
375 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
379 static void
380 handle_imsg_buf(struct imsg *imsg, size_t datalen)
382 struct tab *tab;
383 int l;
384 char *page, buf[FMT_SCALED_STRSIZE] = {0};
386 tab = tab_by_id(imsg->hdr.peerid);
388 tab->bytes += datalen;
389 if (tab->fd == -1) {
390 if (!tab->buffer.page.parse(&tab->buffer.page,
391 imsg->data, datalen))
392 die();
393 } else {
394 write(tab->fd, imsg->data, datalen);
395 fmt_scaled(tab->bytes, buf);
396 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
397 tab->path,
398 buf);
399 if (l == -1)
400 die();
401 load_page_from_str(tab, page);
402 free(page);
405 ui_on_tab_refresh(tab);
408 static void
409 handle_imsg_eof(struct imsg *imsg, size_t datalen)
411 struct tab *tab;
412 int l;
413 char *page, buf[FMT_SCALED_STRSIZE] = {0};
415 tab = tab_by_id(imsg->hdr.peerid);
417 if (tab->fd == -1) {
418 if (!tab->buffer.page.free(&tab->buffer.page))
419 die();
420 } else {
421 fmt_scaled(tab->bytes, buf);
422 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
423 tab->path,
424 buf);
425 if (l == -1)
426 die();
427 load_page_from_str(tab, page);
428 free(page);
430 close(tab->fd);
431 tab->fd = -1;
432 free(tab->path);
433 tab->path = NULL;
436 ui_on_tab_refresh(tab);
437 ui_on_tab_loaded(tab);
440 static void
441 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
443 int res;
445 if (datalen != sizeof(res))
446 die();
448 memcpy(&res, imsg->data, sizeof(res));
449 if (res == 0)
450 message("Added to bookmarks!");
451 else
452 message("Failed to add to bookmarks: %s",
453 strerror(res));
456 static void
457 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
459 int res;
461 if (datalen != sizeof(res))
462 die();
463 memcpy(&res, imsg->data, datalen);
464 if (res != 0)
465 message("Failed to save the cert for: %s",
466 strerror(res));
469 static void
470 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
472 int res;
474 if (datalen != sizeof(res))
475 die();
476 memcpy(&res, imsg->data, datalen);
477 if (!res)
478 message("Failed to update the certificate");
481 static void
482 handle_dispatch_imsg(int fd, short ev, void *d)
484 struct imsgev *iev = d;
485 dispatch_imsg(iev, ev, handlers, sizeof(handlers));
488 static void
489 load_page_from_str(struct tab *tab, const char *page)
491 erase_buffer(&tab->buffer);
492 gemtext_initparser(&tab->buffer.page);
493 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
494 die();
495 if (!tab->buffer.page.free(&tab->buffer.page))
496 die();
497 ui_on_tab_refresh(tab);
498 ui_on_tab_loaded(tab);
501 void
502 load_about_url(struct tab *tab, const char *url)
504 tab->trust = TS_VERIFIED;
506 gemtext_initparser(&tab->buffer.page);
508 ui_send_fs(IMSG_GET, tab->id,
509 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
512 void
513 load_gemini_url(struct tab *tab, const char *url)
515 struct get_req req;
517 stop_tab(tab);
518 tab->id = tab_new_id();
520 memset(&req, 0, sizeof(req));
521 strlcpy(req.host, tab->uri.host, sizeof(req.host));
522 strlcpy(req.port, tab->uri.port, sizeof(req.host));
524 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
525 strlcat(req.req, "\r\n", sizeof(req.req));
527 req.proto = PROTO_GEMINI;
529 ui_send_net(IMSG_GET_RAW, tab->id,
530 &req, sizeof(req));
533 void
534 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
536 struct get_req req;
538 stop_tab(tab);
539 tab->id = tab_new_id();
540 tab->proxy = p;
542 memset(&req, 0, sizeof(req));
543 strlcpy(req.host, p->host, sizeof(req.host));
544 strlcpy(req.port, p->port, sizeof(req.host));
546 strlcpy(req.req, tab->hist_cur->h, sizeof(req.req));
547 strlcat(req.req, "\r\n", sizeof(req.req));
549 req.proto = p->proto;
551 ui_send_net(IMSG_GET_RAW, tab->id,
552 &req, sizeof(req));
555 /*
556 * Effectively load the given url in the given tab. Return 1 when
557 * loading the page asynchronously, and thus when an erase_buffer can
558 * be done right after this function return, or 0 when loading the
559 * page synchronously. In this last case, erase_buffer *MUST* be
560 * called by the handling function (such as load_page_from_str).
561 */
562 static int
563 do_load_url(struct tab *tab, const char *url)
565 struct phos_uri uri;
566 struct proto *p;
567 struct proxy *proxy;
568 char *t;
570 tab->proxy = NULL;
572 if (tab->fd != -1) {
573 close(tab->fd);
574 tab->fd = -1;
575 free(tab->path);
576 tab->path = NULL;
579 tab->trust = TS_UNKNOWN;
581 memcpy(&uri, &tab->uri, sizeof(tab->uri));
582 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
583 if (asprintf(&t, "#error loading %s\n>%s\n",
584 url, "Can't parse the URI") == -1)
585 die();
586 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
587 load_page_from_str(tab, t);
588 free(t);
589 return 0;
592 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
593 sizeof(tab->hist_cur->h));
595 for (p = protos; p->schema != NULL; ++p) {
596 if (!strcmp(tab->uri.scheme, p->schema)) {
597 p->loadfn(tab, url);
598 return 1;
602 TAILQ_FOREACH(proxy, &proxies, proxies) {
603 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
604 load_via_proxy(tab, url, proxy);
605 return 1;
609 load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
610 return 0;
613 void
614 load_url(struct tab *tab, const char *url)
616 if (tab->hist_cur != NULL)
617 hist_clear_forward(&tab->hist, TAILQ_NEXT(tab->hist_cur, entries));
619 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
620 event_loopbreak();
621 return;
624 hist_push(&tab->hist, tab->hist_cur);
625 if (do_load_url(tab, url))
626 erase_buffer(&tab->buffer);
629 int
630 load_previous_page(struct tab *tab)
632 struct hist *h;
634 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
635 return 0;
636 tab->hist_cur = h;
637 do_load_url(tab, h->h);
638 return 1;
641 int
642 load_next_page(struct tab *tab)
644 struct hist *h;
646 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
647 return 0;
648 tab->hist_cur = h;
649 do_load_url(tab, h->h);
650 return 1;
653 void
654 stop_tab(struct tab *tab)
656 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
658 if (tab->fd != -1) {
659 close(tab->fd);
660 tab->fd = -1;
661 free(tab->path);
662 tab->path = NULL;
663 load_page_from_str(tab, "Stopped.\n");
667 void
668 add_to_bookmarks(const char *str)
670 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
671 str, strlen(str)+1);
674 void
675 save_session(void)
677 struct tab *tab;
679 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
681 TAILQ_FOREACH(tab, &tabshead, tabs) {
682 ui_send_fs(IMSG_SESSION_TAB, 0,
683 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
686 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
689 static void
690 session_new_tab_cb(const char *url)
692 new_tab(url);
695 static pid_t
696 start_child(enum telescope_process p, const char *argv0, int fd)
698 const char *argv[4];
699 int argc = 0;
700 pid_t pid;
702 switch (pid = fork()) {
703 case -1:
704 die();
705 case 0:
706 break;
707 default:
708 close(fd);
709 return pid;
712 if (dup2(fd, 3) == -1)
713 err(1, "cannot setup imsg fd");
715 argv[argc++] = argv0;
716 switch (p) {
717 case PROC_UI:
718 errx(1, "Can't start ui process");
719 case PROC_FS:
720 argv[argc++] = "-Tf";
721 break;
722 case PROC_NET:
723 argv[argc++] = "-Tn";
724 break;
727 argv[argc++] = NULL;
728 execvp(argv0, (char *const *)argv);
729 err(1, "execvp(%s)", argv0);
732 static int
733 ui_send_net(int type, uint32_t peerid, const void *data,
734 uint16_t datalen)
736 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
737 datalen);
740 static int
741 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
743 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
744 datalen);
747 static void __attribute__((noreturn))
748 usage(int r)
750 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
751 getprogname());
752 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
753 exit(r);
756 int
757 main(int argc, char * const *argv)
759 struct imsgev net_ibuf, fs_ibuf;
760 int pipe2net[2], pipe2fs[2];
761 int ch, configtest = 0, fail = 0;
762 int has_url = 0;
763 int proc = -1;
764 int sessionfd;
765 char path[PATH_MAX];
766 const char *url = NEW_TAB_URL;
767 const char *argv0;
769 argv0 = argv[0];
771 signal(SIGCHLD, SIG_IGN);
772 signal(SIGPIPE, SIG_IGN);
774 if (getenv("NO_COLOR") != NULL)
775 enable_colors = 0;
777 strlcpy(path, getenv("HOME"), sizeof(path));
778 strlcat(path, "/.telescope/config", sizeof(path));
780 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
781 switch (ch) {
782 case 'C':
783 exit(ui_print_colors());
784 case 'c':
785 fail = 1;
786 strlcpy(path, optarg, sizeof(path));
787 break;
788 case 'n':
789 configtest = 1;
790 break;
791 case 'h':
792 usage(0);
793 case 'T':
794 switch (*optarg) {
795 case 'f':
796 proc = PROC_FS;
797 break;
798 case 'n':
799 proc = PROC_NET;
800 break;
801 default:
802 errx(1, "invalid process spec %c",
803 *optarg);
805 break;
806 case 'v':
807 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
808 exit(0);
809 break;
810 default:
811 usage(1);
815 argc -= optind;
816 argv += optind;
818 if (proc != -1) {
819 if (argc > 0)
820 usage(1);
821 else if (proc == PROC_FS)
822 return fs_main();
823 else if (proc == PROC_NET)
824 return client_main();
825 else
826 usage(1);
829 if (argc != 0) {
830 has_url = 1;
831 url = argv[0];
834 /* setup keys before reading the config */
835 TAILQ_INIT(&global_map.m);
836 global_map.unhandled_input = global_key_unbound;
837 TAILQ_INIT(&minibuffer_map.m);
839 config_init();
840 parseconfig(path, fail);
841 if (configtest){
842 puts("config OK");
843 exit(0);
846 fs_init();
847 if ((sessionfd = lock_session()) == -1)
848 errx(1, "can't lock session, is another instance of "
849 "telescope already running?");
851 /* Start children. */
852 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
853 err(1, "socketpair");
854 start_child(PROC_FS, argv0, pipe2fs[1]);
855 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
856 iev_fs = &fs_ibuf;
857 iev_fs->handler = handle_dispatch_imsg;
859 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
860 err(1, "socketpair");
861 start_child(PROC_NET, argv0, pipe2net[1]);
862 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
863 iev_net = &net_ibuf;
864 iev_net->handler = handle_dispatch_imsg;
866 setproctitle("ui");
868 /* initialize tofu & load certificates */
869 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
870 load_certs(&certs);
872 event_init();
874 /* Setup event handlers for pipes to fs/net */
875 iev_fs->events = EV_READ;
876 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
877 iev_fs->handler, iev_fs);
878 event_add(&iev_fs->ev, NULL);
880 iev_net->events = EV_READ;
881 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
882 iev_net->handler, iev_net);
883 event_add(&iev_net->ev, NULL);
885 if (ui_init()) {
886 load_last_session(session_new_tab_cb);
887 if (has_url || TAILQ_EMPTY(&tabshead))
888 new_tab(url);
890 sandbox_ui_process();
891 event_dispatch();
892 ui_end();
895 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
896 ui_send_net(IMSG_QUIT, 0, NULL, 0);
897 imsg_flush(&iev_fs->ibuf);
898 imsg_flush(&iev_net->ibuf);
900 close(sessionfd);
902 return 0;