2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and 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.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/socket.h>
31 #include "minibuffer.h"
33 #include "telescope.h"
36 static struct option longopts[] = {
37 {"colors", no_argument, NULL, 'c'},
38 {"help", no_argument, NULL, 'h'},
39 {"version", no_argument, NULL, 'v'},
43 static const char *opts = "Cc:hnT:v";
45 static struct imsgev *iev_fs, *iev_net;
47 struct tabshead tabshead = TAILQ_HEAD_INITIALIZER(tabshead);
48 struct proxylist proxies = TAILQ_HEAD_INITIALIZER(proxies);
50 enum telescope_process {
56 #define CANNOT_FETCH 0
57 #define TOO_MUCH_REDIRECTS 1
58 #define MALFORMED_RESPONSE 2
59 #define UNKNOWN_TYPE_OR_CSET 3
60 #define UNKNOWN_PROTOCOL 4
62 /* XXX: keep in sync with telescope.c:/^normalize_code/ */
63 const char *err_pages[70] = {
64 [CANNOT_FETCH] = "# Couldn't load the page\n",
65 [TOO_MUCH_REDIRECTS] = "# Too much redirects\n",
66 [MALFORMED_RESPONSE] = "# Got a malformed response\n",
67 [UNKNOWN_TYPE_OR_CSET] = "# Unsupported type or charset\n",
68 [UNKNOWN_PROTOCOL] = "# Unknown protocol\n",
70 [10] = "# Input required\n",
71 [11] = "# Input required\n",
72 [40] = "# Temporary failure\n",
73 [41] = "# Server unavailable\n",
74 [42] = "# CGI error\n",
75 [43] = "# Proxy error\n",
76 [44] = "# Slow down\n",
77 [50] = "# Permanent failure\n",
78 [51] = "# Not found\n",
80 [53] = "# Proxy request refused\n",
81 [59] = "# Bad request\n",
82 [60] = "# Client certificate required\n",
83 [61] = "# Certificate not authorised\n",
84 [62] = "# Certificate not valid\n"
87 static void die(void) __attribute__((__noreturn__));
88 static struct tab *tab_by_id(uint32_t);
89 static void handle_imsg_err(struct imsg*, size_t);
90 static void handle_imsg_check_cert(struct imsg*, size_t);
91 static void handle_check_cert_user_choice(int, struct tab *);
92 static void handle_maybe_save_new_cert(int, struct tab *);
93 static void handle_imsg_got_code(struct imsg*, size_t);
94 static void handle_imsg_got_meta(struct imsg*, size_t);
95 static void handle_maybe_save_page(int, struct tab *);
96 static void handle_save_page_path(const char *, struct tab *);
97 static void handle_imsg_file_opened(struct imsg*, size_t);
98 static void handle_imsg_buf(struct imsg*, size_t);
99 static void handle_imsg_eof(struct imsg*, size_t);
100 static void handle_imsg_bookmark_ok(struct imsg*, size_t);
101 static void handle_imsg_save_cert_ok(struct imsg*, size_t);
102 static void handle_imsg_update_cert_ok(struct imsg *, size_t);
103 static void handle_dispatch_imsg(int, short, void*);
104 static void load_page_from_str(struct tab*, const char*);
105 static void load_about_url(struct tab*, const char*);
106 static void load_finger_url(struct tab *, const char *);
107 static void load_gemini_url(struct tab*, const char*);
108 static void load_via_proxy(struct tab *, const char *,
110 static void make_request(struct tab *, struct get_req *, int,
112 static int do_load_url(struct tab*, const char *, const char *);
113 static void parse_session_line(char *, const char **, uint32_t *);
114 static void load_last_session(void);
115 static pid_t start_child(enum telescope_process, const char *, int);
116 static int ui_send_net(int, uint32_t, const void *, uint16_t);
117 static int ui_send_fs(int, uint32_t, const void *, uint16_t);
119 static struct proto {
121 void (*loadfn)(struct tab*, const char*);
123 {"about", load_about_url},
124 {"finger", load_finger_url},
125 {"gemini", load_gemini_url},
129 static imsg_handlerfn *handlers[] = {
130 [IMSG_ERR] = handle_imsg_err,
131 [IMSG_CHECK_CERT] = handle_imsg_check_cert,
132 [IMSG_GOT_CODE] = handle_imsg_got_code,
133 [IMSG_GOT_META] = handle_imsg_got_meta,
134 [IMSG_BUF] = handle_imsg_buf,
135 [IMSG_EOF] = handle_imsg_eof,
136 [IMSG_BOOKMARK_OK] = handle_imsg_bookmark_ok,
137 [IMSG_SAVE_CERT_OK] = handle_imsg_save_cert_ok,
138 [IMSG_UPDATE_CERT_OK] = handle_imsg_update_cert_ok,
139 [IMSG_FILE_OPENED] = handle_imsg_file_opened,
142 static struct ohash certs;
144 static void __attribute__((__noreturn__))
151 tab_by_id(uint32_t id)
155 TAILQ_FOREACH(t, &tabshead, tabs) {
164 handle_imsg_err(struct imsg *imsg, size_t datalen)
169 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
173 page[datalen-1] = '\0';
175 if (asprintf(&page, "# Error loading %s\n\n> %s\n",
176 tab->hist_cur->h, page) == -1)
178 load_page_from_str(tab, page);
183 handle_imsg_check_cert(struct imsg *imsg, size_t datalen)
185 const char *hash, *host, *port;
187 struct tofu_entry *e;
191 if (hash[datalen-1] != '\0')
194 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
197 if (tab->proxy != NULL) {
198 host = tab->proxy->host;
199 port = tab->proxy->port;
201 host = tab->uri.host;
202 port = tab->uri.port;
205 if ((e = tofu_lookup(&certs, host, port)) == NULL) {
206 /* TODO: an update in libressl/libretls changed
207 * significantly. Find a better approach at storing
209 if (datalen > sizeof(e->hash))
212 tofu_res = 1; /* trust on first use */
213 if ((e = calloc(1, sizeof(*e))) == NULL)
215 strlcpy(e->domain, host, sizeof(e->domain));
216 if (*port != '\0' && strcmp(port, "1965")) {
217 strlcat(e->domain, ":", sizeof(e->domain));
218 strlcat(e->domain, port, sizeof(e->domain));
220 strlcpy(e->hash, hash, sizeof(e->hash));
222 ui_send_fs(IMSG_SAVE_CERT, tab->id, e, sizeof(*e));
224 tofu_res = !strcmp(hash, e->hash);
227 if (e->verified == -1)
228 tab->trust = TS_TEMP_TRUSTED;
229 else if (e->verified == 1)
230 tab->trust = TS_VERIFIED;
232 tab->trust = TS_TRUSTED;
234 ui_send_net(IMSG_CERT_STATUS, imsg->hdr.peerid,
235 &tofu_res, sizeof(tofu_res));
237 tab->trust = TS_UNTRUSTED;
238 load_page_from_str(tab, "# Certificate mismatch\n");
239 if ((tab->cert = strdup(hash)) == NULL)
241 ui_yornp("Certificate mismatch. Proceed?",
242 handle_check_cert_user_choice, tab);
247 handle_check_cert_user_choice(int accept, struct tab *tab)
249 ui_send_net(IMSG_CERT_STATUS, tab->id, &accept,
254 * trust the certificate for this session only. If
255 * the page results in a redirect while we're asking
256 * the user to save, we'll end up with an invalid
257 * tabid (one request == one tab id) and crash. It
258 * also makes sense to save it for the current session
259 * if the user accepted it.
261 tofu_temp_trust(&certs, tab->uri.host, tab->uri.port, tab->cert);
263 ui_yornp("Save the new certificate?",
264 handle_maybe_save_new_cert, tab);
272 handle_maybe_save_new_cert(int accept, struct tab *tab)
274 struct tofu_entry *e;
275 const char *host, *port;
277 if (tab->proxy != NULL) {
278 host = tab->proxy->host;
279 port = tab->proxy->port;
281 host = tab->uri.host;
282 port = tab->uri.port;
288 if ((e = calloc(1, sizeof(*e))) == NULL)
291 strlcpy(e->domain, host, sizeof(e->domain));
292 if (*port != '\0' && strcmp(port, "1965")) {
293 strlcat(e->domain, ":", sizeof(e->domain));
294 strlcat(e->domain, port, sizeof(e->domain));
296 strlcpy(e->hash, tab->cert, sizeof(e->hash));
297 ui_send_fs(IMSG_UPDATE_CERT, 0, e, sizeof(*e));
299 tofu_update(&certs, e);
301 tab->trust = TS_TRUSTED;
309 normalize_code(int n)
312 if (n == 10 || n == 11)
318 if (n == 30 || n == 31)
326 if (n <= 53 || n == 59)
334 return MALFORMED_RESPONSE;
338 handle_imsg_got_code(struct imsg *imsg, size_t datalen)
342 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
345 if (sizeof(tab->code) != datalen)
348 memcpy(&tab->code, imsg->data, sizeof(tab->code));
349 tab->code = normalize_code(tab->code);
350 if (tab->code != 30 && tab->code != 31)
351 tab->redirect_count = 0;
355 handle_imsg_got_meta(struct imsg *imsg, size_t datalen)
359 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
362 if (sizeof(tab->meta) <= datalen)
365 memcpy(tab->meta, imsg->data, datalen);
367 if (tab->code < 10) { /* internal errors */
368 load_page_from_str(tab, err_pages[tab->code]);
369 } else if (tab->code < 20) { /* 1x */
370 load_page_from_str(tab, err_pages[tab->code]);
371 ui_require_input(tab, tab->code == 11);
372 } else if (tab->code == 20) {
373 if (setup_parser_for(tab)) {
374 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
376 load_page_from_str(tab, err_pages[UNKNOWN_TYPE_OR_CSET]);
377 ui_yornp("Can't display page, wanna save?",
378 handle_maybe_save_page, tab);
380 } else if (tab->code < 40) { /* 3x */
381 tab->redirect_count++;
383 /* TODO: make customizable? */
384 if (tab->redirect_count > 5) {
385 load_page_from_str(tab,
386 err_pages[TOO_MUCH_REDIRECTS]);
388 do_load_url(tab, tab->meta, NULL);
389 } else { /* 4x, 5x & 6x */
390 load_page_from_str(tab, err_pages[tab->code]);
395 handle_maybe_save_page(int dosave, struct tab *tab)
398 ui_read("Save to path", handle_save_page_path, tab);
404 handle_save_page_path(const char *path, struct tab *tab)
411 tab->path = strdup(path);
413 ui_send_fs(IMSG_FILE_OPEN, tab->id, path, strlen(path)+1);
417 handle_imsg_file_opened(struct imsg *imsg, size_t datalen)
424 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL) {
430 if (imsg->fd == -1) {
434 if (e[datalen-1] != '\0')
436 l = asprintf(&page, "# Can't open file\n\n> %s: %s\n",
440 load_page_from_str(tab, page);
444 ui_send_net(IMSG_PROCEED, tab->id, NULL, 0);
449 handle_imsg_buf(struct imsg *imsg, size_t datalen)
453 char *page, buf[FMT_SCALED_STRSIZE] = {0};
455 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
458 tab->bytes += datalen;
460 if (!tab->buffer.page.parse(&tab->buffer.page,
461 imsg->data, datalen))
464 write(tab->fd, imsg->data, datalen);
465 fmt_scaled(tab->bytes, buf);
466 l = asprintf(&page, "Saving to \"%s\"... (%s)\n",
471 load_page_from_str(tab, page);
475 ui_on_tab_refresh(tab);
479 handle_imsg_eof(struct imsg *imsg, size_t datalen)
483 char *page, buf[FMT_SCALED_STRSIZE] = {0};
485 if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL)
489 if (!tab->buffer.page.free(&tab->buffer.page))
492 fmt_scaled(tab->bytes, buf);
493 l = asprintf(&page, "Saved to \"%s\" (%s)\n",
498 load_page_from_str(tab, page);
507 ui_on_tab_refresh(tab);
508 ui_on_tab_loaded(tab);
512 handle_imsg_bookmark_ok(struct imsg *imsg, size_t datalen)
516 if (datalen != sizeof(res))
519 memcpy(&res, imsg->data, sizeof(res));
521 message("Added to bookmarks!");
523 message("Failed to add to bookmarks: %s",
528 handle_imsg_save_cert_ok(struct imsg *imsg, size_t datalen)
532 if (datalen != sizeof(res))
534 memcpy(&res, imsg->data, datalen);
536 message("Failed to save the cert for: %s",
541 handle_imsg_update_cert_ok(struct imsg *imsg, size_t datalen)
545 if (datalen != sizeof(res))
547 memcpy(&res, imsg->data, datalen);
549 message("Failed to update the certificate");
553 handle_dispatch_imsg(int fd, short ev, void *d)
555 struct imsgev *iev = d;
557 if (dispatch_imsg(iev, ev, handlers, sizeof(handlers)) == -1)
558 err(1, "connection closed");
562 load_page_from_str(struct tab *tab, const char *page)
564 erase_buffer(&tab->buffer);
565 gemtext_initparser(&tab->buffer.page);
566 if (!tab->buffer.page.parse(&tab->buffer.page, page, strlen(page)))
568 if (!tab->buffer.page.free(&tab->buffer.page))
570 ui_on_tab_refresh(tab);
571 ui_on_tab_loaded(tab);
575 load_about_url(struct tab *tab, const char *url)
577 tab->trust = TS_VERIFIED;
579 gemtext_initparser(&tab->buffer.page);
581 ui_send_fs(IMSG_GET, tab->id,
582 tab->hist_cur->h, strlen(tab->hist_cur->h)+1);
586 load_finger_url(struct tab *tab, const char *url)
592 memset(&req, 0, sizeof(req));
593 if (*tab->uri.port != '\0')
594 strlcpy(req.port, tab->uri.port, sizeof(req.port));
596 strlcpy(req.port, "79", sizeof(req.port));
599 * Sometimes the finger url have the user as path component
600 * (e.g. finger://thelambdalab.xyz/plugd), sometimes as
601 * userinfo (e.g. finger://cobradile@finger.farm).
603 if ((at = strchr(tab->uri.host, '@')) != NULL) {
604 len = at - tab->uri.host;
605 memcpy(req.req, tab->uri.host, len);
607 if (len >= sizeof(req.req))
611 strlcpy(req.host, at+1, sizeof(req.host));
613 strlcpy(req.host, tab->uri.host, sizeof(req.host));
615 /* +1 to skip the initial `/' */
616 strlcpy(req.req, tab->uri.path+1, sizeof(req.req));
618 strlcat(req.req, "\r\n", sizeof(req.req));
620 textplain_initparser(&tab->buffer.page);
621 make_request(tab, &req, PROTO_FINGER, NULL);
625 load_gemini_url(struct tab *tab, const char *url)
629 memset(&req, 0, sizeof(req));
630 strlcpy(req.host, tab->uri.host, sizeof(req.host));
631 strlcpy(req.port, tab->uri.port, sizeof(req.host));
633 make_request(tab, &req, PROTO_GEMINI, tab->hist_cur->h);
637 load_via_proxy(struct tab *tab, const char *url, struct proxy *p)
641 memset(&req, 0, sizeof(req));
642 strlcpy(req.host, p->host, sizeof(req.host));
643 strlcpy(req.port, p->port, sizeof(req.host));
647 make_request(tab, &req, p->proto, tab->hist_cur->h);
651 make_request(struct tab *tab, struct get_req *req, int proto, const char *r)
654 tab->id = tab_new_id();
658 strlcpy(req->req, r, sizeof(req->req));
659 strlcat(req->req, "\r\n", sizeof(req->req));
662 ui_send_net(IMSG_GET_RAW, tab->id, req, sizeof(*req));
666 * Effectively load the given url in the given tab. Return 1 when
667 * loading the page asynchronously, and thus when an erase_buffer can
668 * be done right after this function return, or 0 when loading the
669 * page synchronously. In this last case, erase_buffer *MUST* be
670 * called by the handling function (such as load_page_from_str).
673 do_load_url(struct tab *tab, const char *url, const char *base)
689 tab->trust = TS_UNKNOWN;
692 memcpy(&uri, &tab->uri, sizeof(tab->uri));
694 phos_parse_absolute_uri(base, &uri);
696 if (!phos_resolve_uri_from_str(&uri, url, &tab->uri)) {
697 if (asprintf(&t, "#error loading %s\n>%s\n",
698 url, "Can't parse the URI") == -1)
700 strlcpy(tab->hist_cur->h, url, sizeof(tab->hist_cur->h));
701 load_page_from_str(tab, t);
706 phos_serialize_uri(&tab->uri, tab->hist_cur->h,
707 sizeof(tab->hist_cur->h));
709 for (p = protos; p->schema != NULL; ++p) {
710 if (!strcmp(tab->uri.scheme, p->schema)) {
716 TAILQ_FOREACH(proxy, &proxies, proxies) {
717 if (!strcmp(tab->uri.scheme, proxy->match_proto)) {
718 load_via_proxy(tab, url, proxy);
723 load_page_from_str(tab, err_pages[UNKNOWN_PROTOCOL]);
728 * Load url in tab. If tab is marked as lazy, only prepare the url
729 * but don't load it. If tab is lazy and a url was already prepared,
733 load_url(struct tab *tab, const char *url, const char *base)
737 lazy = tab->flags & TAB_LAZY;
739 if (lazy && tab->hist_cur != NULL) {
741 tab->flags &= ~TAB_LAZY;
744 if (!lazy || tab->hist_cur == NULL) {
745 if (tab->hist_cur != NULL)
746 hist_clear_forward(&tab->hist,
747 TAILQ_NEXT(tab->hist_cur, entries));
749 if ((tab->hist_cur = calloc(1, sizeof(*tab->hist_cur))) == NULL) {
754 strlcpy(tab->buffer.page.title, url,
755 sizeof(tab->buffer.page.title));
756 hist_push(&tab->hist, tab->hist_cur);
759 strlcpy(tab->hist_cur->h, url,
760 sizeof(tab->hist_cur->h));
763 if (!lazy && do_load_url(tab, url, base))
764 erase_buffer(&tab->buffer);
768 load_previous_page(struct tab *tab)
772 if ((h = TAILQ_PREV(tab->hist_cur, mhisthead, entries)) == NULL)
775 do_load_url(tab, h->h, NULL);
780 load_next_page(struct tab *tab)
784 if ((h = TAILQ_NEXT(tab->hist_cur, entries)) == NULL)
787 do_load_url(tab, h->h, NULL);
792 * Free every resource linked to the tab, including the tab itself.
793 * Removes the tab from the tablist, but doesn't update the
794 * current_tab though.
797 free_tab(struct tab *tab)
801 if (evtimer_pending(&tab->loadingev, NULL))
802 evtimer_del(&tab->loadingev);
804 TAILQ_REMOVE(&tabshead, tab, tabs);
809 stop_tab(struct tab *tab)
811 ui_send_net(IMSG_STOP, tab->id, NULL, 0);
818 load_page_from_str(tab, "Stopped.\n");
823 add_to_bookmarks(const char *str)
825 ui_send_fs(IMSG_BOOKMARK_PAGE, 0,
836 ui_send_fs(IMSG_SESSION_START, 0, NULL, 0);
838 TAILQ_FOREACH(tab, &tabshead, tabs) {
840 if (tab == current_tab)
841 flags |= TAB_CURRENT;
843 t = tab->hist_cur->h;
844 ui_send_fs(IMSG_SESSION_TAB, flags, t, strlen(t)+1);
846 t = tab->buffer.page.title;
847 ui_send_fs(IMSG_SESSION_TAB_TITLE, 0, t, strlen(t)+1);
850 ui_send_fs(IMSG_SESSION_END, 0, NULL, 0);
854 * Parse a line of the session file. The format is:
856 * URL [flags,...] [title]\n
859 parse_session_line(char *line, const char **title, uint32_t *flags)
865 if ((s = strchr(line, ' ')) == NULL)
870 if ((t = strchr(s, ' ')) != NULL) {
875 while ((ap = strsep(&s, ",")) != NULL) {
878 else if (!strcmp(ap, "current"))
879 *flags |= TAB_CURRENT;
881 message("unknown tab flag: %s", ap);
886 load_last_session(void)
889 char *nl, *line = NULL;
894 struct tab *tab, *curr = NULL;
896 if ((session = fopen(session_file, "r")) == NULL) {
898 new_tab("about:new", NULL);
899 switch_to_tab(new_tab("about:help", NULL));
903 while ((linelen = getline(&line, &linesize, session)) != -1) {
904 if ((nl = strchr(line, '\n')) != NULL)
906 parse_session_line(line, &title, &flags);
907 if ((tab = new_tab(line, NULL)) == NULL)
909 strlcpy(tab->buffer.page.title, title,
910 sizeof(tab->buffer.page.title));
911 if (flags & TAB_CURRENT)
916 message("error reading %s: %s",
917 session_file, strerror(errno));
924 if (last_time_crashed())
925 switch_to_tab(new_tab("about:crash", NULL));
931 start_child(enum telescope_process p, const char *argv0, int fd)
937 switch (pid = fork()) {
947 if (dup2(fd, 3) == -1)
948 err(1, "cannot setup imsg fd");
950 argv[argc++] = argv0;
953 errx(1, "Can't start ui process");
955 argv[argc++] = "-Tf";
958 argv[argc++] = "-Tn";
963 execvp(argv0, (char *const *)argv);
964 err(1, "execvp(%s)", argv0);
968 ui_send_net(int type, uint32_t peerid, const void *data,
971 return imsg_compose_event(iev_net, type, peerid, 0, -1, data,
976 ui_send_fs(int type, uint32_t peerid, const void *data, uint16_t datalen)
978 return imsg_compose_event(iev_fs, type, peerid, 0, -1, data,
982 static void __attribute__((noreturn))
985 fprintf(stderr, "USAGE: %s [-hnv] [-c config] [url]\n",
987 fprintf(stderr, "version: " PACKAGE " " VERSION "\n");
992 main(int argc, char * const *argv)
994 struct imsgev net_ibuf, fs_ibuf;
995 int pipe2net[2], pipe2fs[2];
996 int ch, configtest = 0, fail = 0;
1000 char path[PATH_MAX];
1001 const char *url = NEW_TAB_URL;
1006 signal(SIGCHLD, SIG_IGN);
1007 signal(SIGPIPE, SIG_IGN);
1009 if (getenv("NO_COLOR") != NULL)
1012 strlcpy(path, getenv("HOME"), sizeof(path));
1013 strlcat(path, "/.telescope/config", sizeof(path));
1015 while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) {
1018 exit(ui_print_colors());
1021 strlcpy(path, optarg, sizeof(path));
1037 errx(1, "invalid process spec %c",
1042 printf("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION);
1056 else if (proc == PROC_FS)
1058 else if (proc == PROC_NET)
1069 /* setup keys before reading the config */
1070 TAILQ_INIT(&global_map.m);
1071 global_map.unhandled_input = global_key_unbound;
1072 TAILQ_INIT(&minibuffer_map.m);
1075 parseconfig(path, fail);
1082 if ((sessionfd = lock_session()) == -1)
1083 errx(1, "can't lock session, is another instance of "
1084 "telescope already running?");
1086 /* Start children. */
1087 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2fs) == -1)
1088 err(1, "socketpair");
1089 start_child(PROC_FS, argv0, pipe2fs[1]);
1090 imsg_init(&fs_ibuf.ibuf, pipe2fs[0]);
1092 iev_fs->handler = handle_dispatch_imsg;
1094 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, pipe2net) == -1)
1095 err(1, "socketpair");
1096 start_child(PROC_NET, argv0, pipe2net[1]);
1097 imsg_init(&net_ibuf.ibuf, pipe2net[0]);
1098 iev_net = &net_ibuf;
1099 iev_net->handler = handle_dispatch_imsg;
1103 /* initialize tofu & load certificates */
1104 tofu_init(&certs, 5, offsetof(struct tofu_entry, domain));
1109 /* Setup event handlers for pipes to fs/net */
1110 iev_fs->events = EV_READ;
1111 event_set(&iev_fs->ev, iev_fs->ibuf.fd, iev_fs->events,
1112 iev_fs->handler, iev_fs);
1113 event_add(&iev_fs->ev, NULL);
1115 iev_net->events = EV_READ;
1116 event_set(&iev_net->ev, iev_net->ibuf.fd, iev_net->events,
1117 iev_net->handler, iev_net);
1118 event_add(&iev_net->ev, NULL);
1121 load_last_session();
1122 if (has_url || TAILQ_EMPTY(&tabshead))
1125 sandbox_ui_process();
1130 ui_send_fs(IMSG_QUIT, 0, NULL, 0);
1131 ui_send_net(IMSG_QUIT, 0, NULL, 0);
1132 imsg_flush(&iev_fs->ibuf);
1133 imsg_flush(&iev_net->ibuf);