Blob


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