1 59ef79dd 2022-02-11 op /* $OpenBSD: control.c,v 1.4 2021/08/01 09:07:03 florian Exp $ */
4 59ef79dd 2022-02-11 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
6 59ef79dd 2022-02-11 op * Permission to use, copy, modify, and distribute this software for any
7 59ef79dd 2022-02-11 op * purpose with or without fee is hereby granted, provided that the above
8 59ef79dd 2022-02-11 op * copyright notice and this permission notice appear in all copies.
10 59ef79dd 2022-02-11 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 59ef79dd 2022-02-11 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 59ef79dd 2022-02-11 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 59ef79dd 2022-02-11 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 59ef79dd 2022-02-11 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 59ef79dd 2022-02-11 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 59ef79dd 2022-02-11 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 59ef79dd 2022-02-11 op #include "compat.h"
21 59ef79dd 2022-02-11 op #include <sys/types.h>
22 59ef79dd 2022-02-11 op #include <sys/stat.h>
23 59ef79dd 2022-02-11 op #include <sys/socket.h>
24 59ef79dd 2022-02-11 op #include <sys/uio.h>
25 59ef79dd 2022-02-11 op #include <sys/un.h>
27 59ef79dd 2022-02-11 op #include <errno.h>
28 59ef79dd 2022-02-11 op #include <stdlib.h>
29 59ef79dd 2022-02-11 op #include <string.h>
30 59ef79dd 2022-02-11 op #include <unistd.h>
32 59ef79dd 2022-02-11 op #include "control.h"
33 59ef79dd 2022-02-11 op #include "minibuffer.h"
34 59ef79dd 2022-02-11 op #include "session.h"
35 59ef79dd 2022-02-11 op #include "telescope.h"
36 59ef79dd 2022-02-11 op #include "utils.h"
37 59ef79dd 2022-02-11 op #include "ui.h"
39 59ef79dd 2022-02-11 op #define CONTROL_BACKLOG 5
42 59ef79dd 2022-02-11 op struct event ev;
43 59ef79dd 2022-02-11 op struct event evt;
45 59ef79dd 2022-02-11 op } control_state = {.fd = -1};
47 59ef79dd 2022-02-11 op struct ctl_conn {
48 59ef79dd 2022-02-11 op TAILQ_ENTRY(ctl_conn) entry;
49 59ef79dd 2022-02-11 op struct imsgev iev;
52 59ef79dd 2022-02-11 op struct ctl_conn *control_connbyfd(int);
53 59ef79dd 2022-02-11 op struct ctl_conn *control_connbypid(pid_t);
54 59ef79dd 2022-02-11 op void control_close(int);
56 59ef79dd 2022-02-11 op TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
59 59ef79dd 2022-02-11 op control_init(char *path)
61 59ef79dd 2022-02-11 op struct sockaddr_un sun;
63 59ef79dd 2022-02-11 op mode_t old_umask;
65 59ef79dd 2022-02-11 op if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
67 59ef79dd 2022-02-11 op warn("%s: socket", __func__);
71 59ef79dd 2022-02-11 op memset(&sun, 0, sizeof(sun));
72 59ef79dd 2022-02-11 op sun.sun_family = AF_UNIX;
73 59ef79dd 2022-02-11 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
75 59ef79dd 2022-02-11 op if (unlink(path) == -1)
76 59ef79dd 2022-02-11 op if (errno != ENOENT) {
77 59ef79dd 2022-02-11 op warn("%s: unlink %s", __func__, path);
82 59ef79dd 2022-02-11 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
83 59ef79dd 2022-02-11 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
84 59ef79dd 2022-02-11 op warn("%s: bind: %s", __func__, path);
86 59ef79dd 2022-02-11 op umask(old_umask);
89 59ef79dd 2022-02-11 op umask(old_umask);
91 59ef79dd 2022-02-11 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
92 59ef79dd 2022-02-11 op warn("%s: chmod", __func__);
94 59ef79dd 2022-02-11 op (void)unlink(path);
102 59ef79dd 2022-02-11 op control_listen(int fd)
104 59ef79dd 2022-02-11 op control_state.fd = fd;
105 59ef79dd 2022-02-11 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
106 59ef79dd 2022-02-11 op warn("%s: listen", __func__);
110 59ef79dd 2022-02-11 op event_set(&control_state.ev, control_state.fd, EV_READ,
111 59ef79dd 2022-02-11 op control_accept, NULL);
112 59ef79dd 2022-02-11 op event_add(&control_state.ev, NULL);
113 59ef79dd 2022-02-11 op evtimer_set(&control_state.evt, control_accept, NULL);
119 59ef79dd 2022-02-11 op control_accept(int listenfd, short event, void *bula)
122 59ef79dd 2022-02-11 op socklen_t len;
123 59ef79dd 2022-02-11 op struct sockaddr_un sun;
124 59ef79dd 2022-02-11 op struct ctl_conn *c;
126 59ef79dd 2022-02-11 op event_add(&control_state.ev, NULL);
127 59ef79dd 2022-02-11 op if ((event & EV_TIMEOUT))
130 59ef79dd 2022-02-11 op len = sizeof(sun);
131 59ef79dd 2022-02-11 op if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
132 59ef79dd 2022-02-11 op SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
134 59ef79dd 2022-02-11 op * Pause accept if we are out of file descriptors, or
135 59ef79dd 2022-02-11 op * libevent will haunt us here too.
137 59ef79dd 2022-02-11 op if (errno == ENFILE || errno == EMFILE) {
138 59ef79dd 2022-02-11 op struct timeval evtpause = { 1, 0 };
140 59ef79dd 2022-02-11 op event_del(&control_state.ev);
141 59ef79dd 2022-02-11 op evtimer_add(&control_state.evt, &evtpause);
142 59ef79dd 2022-02-11 op } else if (errno != EWOULDBLOCK && errno != EINTR &&
143 59ef79dd 2022-02-11 op errno != ECONNABORTED)
144 59ef79dd 2022-02-11 op message("%s: accept4: %s", __func__, strerror(errno));
148 59ef79dd 2022-02-11 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
149 59ef79dd 2022-02-11 op message("%s: calloc: %s", __func__, strerror(errno));
150 59ef79dd 2022-02-11 op close(connfd);
154 59ef79dd 2022-02-11 op imsg_init(&c->iev.ibuf, connfd);
155 59ef79dd 2022-02-11 op c->iev.handler = control_dispatch_imsg;
156 59ef79dd 2022-02-11 op c->iev.events = EV_READ;
157 59ef79dd 2022-02-11 op event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
158 59ef79dd 2022-02-11 op c->iev.handler, &c->iev);
159 59ef79dd 2022-02-11 op event_add(&c->iev.ev, NULL);
161 59ef79dd 2022-02-11 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
164 59ef79dd 2022-02-11 op struct ctl_conn *
165 59ef79dd 2022-02-11 op control_connbyfd(int fd)
167 59ef79dd 2022-02-11 op struct ctl_conn *c;
169 59ef79dd 2022-02-11 op TAILQ_FOREACH(c, &ctl_conns, entry) {
170 59ef79dd 2022-02-11 op if (c->iev.ibuf.fd == fd)
177 59ef79dd 2022-02-11 op struct ctl_conn *
178 59ef79dd 2022-02-11 op control_connbypid(pid_t pid)
180 59ef79dd 2022-02-11 op struct ctl_conn *c;
182 59ef79dd 2022-02-11 op TAILQ_FOREACH(c, &ctl_conns, entry) {
183 59ef79dd 2022-02-11 op if (c->iev.ibuf.pid == pid)
191 59ef79dd 2022-02-11 op control_close(int fd)
193 59ef79dd 2022-02-11 op struct ctl_conn *c;
195 59ef79dd 2022-02-11 op if ((c = control_connbyfd(fd)) == NULL) {
196 59ef79dd 2022-02-11 op message("%s: fd %d: not found", __func__, fd);
200 59ef79dd 2022-02-11 op msgbuf_clear(&c->iev.ibuf.w);
201 59ef79dd 2022-02-11 op TAILQ_REMOVE(&ctl_conns, c, entry);
203 59ef79dd 2022-02-11 op event_del(&c->iev.ev);
204 59ef79dd 2022-02-11 op close(c->iev.ibuf.fd);
206 59ef79dd 2022-02-11 op /* Some file descriptors are available again. */
207 59ef79dd 2022-02-11 op if (evtimer_pending(&control_state.evt, NULL)) {
208 59ef79dd 2022-02-11 op evtimer_del(&control_state.evt);
209 59ef79dd 2022-02-11 op event_add(&control_state.ev, NULL);
216 59ef79dd 2022-02-11 op control_dispatch_imsg(int fd, short event, void *bula)
218 59ef79dd 2022-02-11 op struct ctl_conn *c;
219 59ef79dd 2022-02-11 op struct imsg imsg;
222 59ef79dd 2022-02-11 op if ((c = control_connbyfd(fd)) == NULL) {
223 59ef79dd 2022-02-11 op message("%s: fd %d: not found", __func__, fd);
227 59ef79dd 2022-02-11 op if (event & EV_READ) {
228 59ef79dd 2022-02-11 op if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
230 59ef79dd 2022-02-11 op control_close(fd);
234 59ef79dd 2022-02-11 op if (event & EV_WRITE) {
235 59ef79dd 2022-02-11 op if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
236 59ef79dd 2022-02-11 op control_close(fd);
242 59ef79dd 2022-02-11 op if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
243 59ef79dd 2022-02-11 op control_close(fd);
249 59ef79dd 2022-02-11 op switch (imsg.hdr.type) {
250 59ef79dd 2022-02-11 op case IMSG_CTL_OPEN_URL: {
251 59ef79dd 2022-02-11 op char uri[GEMINI_URL_LEN] = { 0 };
253 59ef79dd 2022-02-11 op if (IMSG_DATA_SIZE(imsg) > sizeof(uri)-1)
255 59ef79dd 2022-02-11 op memcpy(uri, imsg.data, sizeof(uri));
256 59ef79dd 2022-02-11 op if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
259 59ef79dd 2022-02-11 op new_tab(uri, NULL, NULL);
260 59ef79dd 2022-02-11 op ui_on_tab_refresh(current_tab);
264 59ef79dd 2022-02-11 op message("%s: error handling imsg %d", __func__,
265 59ef79dd 2022-02-11 op imsg.hdr.type);
268 59ef79dd 2022-02-11 op imsg_free(&imsg);
271 59ef79dd 2022-02-11 op imsg_event_add(&c->iev);
275 59ef79dd 2022-02-11 op control_imsg_relay(struct imsg *imsg)
277 59ef79dd 2022-02-11 op struct ctl_conn *c;
279 59ef79dd 2022-02-11 op if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
282 59ef79dd 2022-02-11 op return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
283 59ef79dd 2022-02-11 op -1, imsg->data, IMSG_DATA_SIZE(*imsg)));