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 98d3e6c1 2024-02-18 op #include "ev.h"
34 87d297d1 2024-02-22 op #include "imsgev.h"
35 59ef79dd 2022-02-11 op #include "minibuffer.h"
36 59ef79dd 2022-02-11 op #include "telescope.h"
37 59ef79dd 2022-02-11 op #include "utils.h"
38 59ef79dd 2022-02-11 op #include "ui.h"
40 59ef79dd 2022-02-11 op #define CONTROL_BACKLOG 5
43 98d3e6c1 2024-02-18 op unsigned long timeout;
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 ee0aac2f 2022-05-25 op if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
66 59ef79dd 2022-02-11 op warn("%s: socket", __func__);
70 ee0aac2f 2022-05-25 op if (!mark_nonblock_cloexec(fd)) {
75 59ef79dd 2022-02-11 op memset(&sun, 0, sizeof(sun));
76 59ef79dd 2022-02-11 op sun.sun_family = AF_UNIX;
77 59ef79dd 2022-02-11 op strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
79 59ef79dd 2022-02-11 op if (unlink(path) == -1)
80 59ef79dd 2022-02-11 op if (errno != ENOENT) {
81 59ef79dd 2022-02-11 op warn("%s: unlink %s", __func__, path);
86 59ef79dd 2022-02-11 op old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
87 59ef79dd 2022-02-11 op if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
88 59ef79dd 2022-02-11 op warn("%s: bind: %s", __func__, path);
90 59ef79dd 2022-02-11 op umask(old_umask);
93 59ef79dd 2022-02-11 op umask(old_umask);
95 59ef79dd 2022-02-11 op if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
96 59ef79dd 2022-02-11 op warn("%s: chmod", __func__);
98 59ef79dd 2022-02-11 op (void)unlink(path);
106 59ef79dd 2022-02-11 op control_listen(int fd)
108 59ef79dd 2022-02-11 op control_state.fd = fd;
109 59ef79dd 2022-02-11 op if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
110 59ef79dd 2022-02-11 op warn("%s: listen", __func__);
114 98d3e6c1 2024-02-18 op ev_add(control_state.fd, EV_READ, control_accept, NULL);
119 98d3e6c1 2024-02-18 op control_accept(int listenfd, int 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 98d3e6c1 2024-02-18 op ev_add(control_state.fd, EV_READ, control_accept, NULL);
127 59ef79dd 2022-02-11 op if ((event & EV_TIMEOUT))
130 59ef79dd 2022-02-11 op len = sizeof(sun);
131 ee0aac2f 2022-05-25 op if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
133 59ef79dd 2022-02-11 op * Pause accept if we are out of file descriptors, or
134 98d3e6c1 2024-02-18 op * ev will haunt us here too.
136 59ef79dd 2022-02-11 op if (errno == ENFILE || errno == EMFILE) {
137 59ef79dd 2022-02-11 op struct timeval evtpause = { 1, 0 };
139 98d3e6c1 2024-02-18 op ev_del(control_state.fd);
140 98d3e6c1 2024-02-18 op control_state.timeout = ev_timer(&evtpause,
141 98d3e6c1 2024-02-18 op control_accept, NULL);
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 ee0aac2f 2022-05-25 op if (!mark_nonblock_cloexec(connfd)) {
149 ee0aac2f 2022-05-25 op message("%s: mark_nonblock_cloexec: %s", __func__,
150 ee0aac2f 2022-05-25 op strerror(errno));
151 ee0aac2f 2022-05-25 op close(connfd);
155 59ef79dd 2022-02-11 op if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
156 59ef79dd 2022-02-11 op message("%s: calloc: %s", __func__, strerror(errno));
157 59ef79dd 2022-02-11 op close(connfd);
161 59ef79dd 2022-02-11 op imsg_init(&c->iev.ibuf, connfd);
162 59ef79dd 2022-02-11 op c->iev.handler = control_dispatch_imsg;
163 59ef79dd 2022-02-11 op c->iev.events = EV_READ;
164 98d3e6c1 2024-02-18 op if (ev_add(connfd, c->iev.events, c->iev.handler, &c->iev) == -1) {
165 98d3e6c1 2024-02-18 op message("%s: ev_add: %s", __func__, strerror(errno));
166 98d3e6c1 2024-02-18 op close(connfd);
171 59ef79dd 2022-02-11 op TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
174 59ef79dd 2022-02-11 op struct ctl_conn *
175 59ef79dd 2022-02-11 op control_connbyfd(int fd)
177 59ef79dd 2022-02-11 op struct ctl_conn *c;
179 59ef79dd 2022-02-11 op TAILQ_FOREACH(c, &ctl_conns, entry) {
180 59ef79dd 2022-02-11 op if (c->iev.ibuf.fd == fd)
187 59ef79dd 2022-02-11 op struct ctl_conn *
188 59ef79dd 2022-02-11 op control_connbypid(pid_t pid)
190 59ef79dd 2022-02-11 op struct ctl_conn *c;
192 59ef79dd 2022-02-11 op TAILQ_FOREACH(c, &ctl_conns, entry) {
193 59ef79dd 2022-02-11 op if (c->iev.ibuf.pid == pid)
201 59ef79dd 2022-02-11 op control_close(int fd)
203 59ef79dd 2022-02-11 op struct ctl_conn *c;
205 59ef79dd 2022-02-11 op if ((c = control_connbyfd(fd)) == NULL) {
206 59ef79dd 2022-02-11 op message("%s: fd %d: not found", __func__, fd);
210 59ef79dd 2022-02-11 op msgbuf_clear(&c->iev.ibuf.w);
211 59ef79dd 2022-02-11 op TAILQ_REMOVE(&ctl_conns, c, entry);
213 98d3e6c1 2024-02-18 op ev_del(c->iev.ibuf.fd);
214 59ef79dd 2022-02-11 op close(c->iev.ibuf.fd);
216 59ef79dd 2022-02-11 op /* Some file descriptors are available again. */
217 98d3e6c1 2024-02-18 op if (ev_timer_pending(control_state.timeout)) {
218 98d3e6c1 2024-02-18 op ev_timer_cancel(control_state.timeout);
219 98d3e6c1 2024-02-18 op control_state.timeout = 0;
220 98d3e6c1 2024-02-18 op ev_add(control_state.fd, EV_READ, control_accept, NULL);
227 98d3e6c1 2024-02-18 op control_dispatch_imsg(int fd, int event, void *bula)
229 59ef79dd 2022-02-11 op struct ctl_conn *c;
230 59ef79dd 2022-02-11 op struct imsg imsg;
233 59ef79dd 2022-02-11 op if ((c = control_connbyfd(fd)) == NULL) {
234 59ef79dd 2022-02-11 op message("%s: fd %d: not found", __func__, fd);
238 59ef79dd 2022-02-11 op if (event & EV_READ) {
239 59ef79dd 2022-02-11 op if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
241 59ef79dd 2022-02-11 op control_close(fd);
245 59ef79dd 2022-02-11 op if (event & EV_WRITE) {
246 59ef79dd 2022-02-11 op if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
247 59ef79dd 2022-02-11 op control_close(fd);
253 59ef79dd 2022-02-11 op if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
254 59ef79dd 2022-02-11 op control_close(fd);
260 59ef79dd 2022-02-11 op switch (imsg.hdr.type) {
261 59ef79dd 2022-02-11 op case IMSG_CTL_OPEN_URL: {
262 7452ce7d 2022-03-01 op static char uri[GEMINI_URL_LEN];
264 7452ce7d 2022-03-01 op if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
266 7452ce7d 2022-03-01 op memset(uri, 0, sizeof(uri));
267 59ef79dd 2022-02-11 op memcpy(uri, imsg.data, sizeof(uri));
268 59ef79dd 2022-02-11 op if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
271 0a987358 2022-02-24 op ui_remotely_open_link(uri);
275 59ef79dd 2022-02-11 op message("%s: error handling imsg %d", __func__,
276 59ef79dd 2022-02-11 op imsg.hdr.type);
279 59ef79dd 2022-02-11 op imsg_free(&imsg);
282 59ef79dd 2022-02-11 op imsg_event_add(&c->iev);