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 "ev.h"
34 #include "imsgev.h"
35 #include "minibuffer.h"
36 #include "telescope.h"
37 #include "utils.h"
38 #include "ui.h"
40 #define CONTROL_BACKLOG 5
42 struct {
43 unsigned long timeout;
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, 0)) == -1) {
66 warn("%s: socket", __func__);
67 return (-1);
68 }
70 if (!mark_nonblock_cloexec(fd)) {
71 close(fd);
72 return (-1);
73 }
75 memset(&sun, 0, sizeof(sun));
76 sun.sun_family = AF_UNIX;
77 strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
79 if (unlink(path) == -1)
80 if (errno != ENOENT) {
81 warn("%s: unlink %s", __func__, path);
82 close(fd);
83 return (-1);
84 }
86 old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
87 if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
88 warn("%s: bind: %s", __func__, path);
89 close(fd);
90 umask(old_umask);
91 return (-1);
92 }
93 umask(old_umask);
95 if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
96 warn("%s: chmod", __func__);
97 close(fd);
98 (void)unlink(path);
99 return (-1);
102 return (fd);
105 int
106 control_listen(int fd)
108 control_state.fd = fd;
109 if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
110 warn("%s: listen", __func__);
111 return (-1);
114 ev_add(control_state.fd, EV_READ, control_accept, NULL);
115 return (0);
118 void
119 control_accept(int listenfd, int event, void *bula)
121 int connfd;
122 socklen_t len;
123 struct sockaddr_un sun;
124 struct ctl_conn *c;
126 ev_add(control_state.fd, EV_READ, control_accept, NULL);
127 if ((event & EV_TIMEOUT))
128 return;
130 len = sizeof(sun);
131 if ((connfd = accept(listenfd, (struct sockaddr *)&sun, &len)) == -1) {
132 /*
133 * Pause accept if we are out of file descriptors, or
134 * ev will haunt us here too.
135 */
136 if (errno == ENFILE || errno == EMFILE) {
137 struct timeval evtpause = { 1, 0 };
139 ev_del(control_state.fd);
140 control_state.timeout = ev_timer(&evtpause,
141 control_accept, NULL);
142 } else if (errno != EWOULDBLOCK && errno != EINTR &&
143 errno != ECONNABORTED)
144 message("%s: accept4: %s", __func__, strerror(errno));
145 return;
148 if (!mark_nonblock_cloexec(connfd)) {
149 message("%s: mark_nonblock_cloexec: %s", __func__,
150 strerror(errno));
151 close(connfd);
152 return;
155 if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
156 message("%s: calloc: %s", __func__, strerror(errno));
157 close(connfd);
158 return;
161 imsg_init(&c->iev.ibuf, connfd);
162 c->iev.handler = control_dispatch_imsg;
163 c->iev.events = EV_READ;
164 if (ev_add(connfd, c->iev.events, c->iev.handler, &c->iev) == -1) {
165 message("%s: ev_add: %s", __func__, strerror(errno));
166 close(connfd);
167 free(c);
168 return;
171 TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
174 struct ctl_conn *
175 control_connbyfd(int fd)
177 struct ctl_conn *c;
179 TAILQ_FOREACH(c, &ctl_conns, entry) {
180 if (c->iev.ibuf.fd == fd)
181 break;
184 return (c);
187 struct ctl_conn *
188 control_connbypid(pid_t pid)
190 struct ctl_conn *c;
192 TAILQ_FOREACH(c, &ctl_conns, entry) {
193 if (c->iev.ibuf.pid == pid)
194 break;
197 return (c);
200 void
201 control_close(int fd)
203 struct ctl_conn *c;
205 if ((c = control_connbyfd(fd)) == NULL) {
206 message("%s: fd %d: not found", __func__, fd);
207 return;
210 msgbuf_clear(&c->iev.ibuf.w);
211 TAILQ_REMOVE(&ctl_conns, c, entry);
213 ev_del(c->iev.ibuf.fd);
214 close(c->iev.ibuf.fd);
216 /* Some file descriptors are available again. */
217 if (ev_timer_pending(control_state.timeout)) {
218 ev_timer_cancel(control_state.timeout);
219 control_state.timeout = 0;
220 ev_add(control_state.fd, EV_READ, control_accept, NULL);
223 free(c);
226 void
227 control_dispatch_imsg(int fd, int event, void *bula)
229 struct ctl_conn *c;
230 struct imsg imsg;
231 ssize_t n;
233 if ((c = control_connbyfd(fd)) == NULL) {
234 message("%s: fd %d: not found", __func__, fd);
235 return;
238 if (event & EV_READ) {
239 if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
240 n == 0) {
241 control_close(fd);
242 return;
245 if (event & EV_WRITE) {
246 if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
247 control_close(fd);
248 return;
252 for (;;) {
253 if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
254 control_close(fd);
255 return;
257 if (n == 0)
258 break;
260 switch (imsg.hdr.type) {
261 case IMSG_CTL_OPEN_URL: {
262 static char uri[GEMINI_URL_LEN];
264 if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
265 break;
266 memset(uri, 0, sizeof(uri));
267 memcpy(uri, imsg.data, sizeof(uri));
268 if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
269 break;
271 ui_remotely_open_link(uri);
272 break;
274 default:
275 message("%s: error handling imsg %d", __func__,
276 imsg.hdr.type);
277 break;
279 imsg_free(&imsg);
282 imsg_event_add(&c->iev);