001
2022-02-11
op
/* $OpenBSD: control.c,v 1.4 2021/08/01 09:07:03 florian Exp $ */
004
2022-02-11
op
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
006
2022-02-11
op
* Permission to use, copy, modify, and distribute this software for any
007
2022-02-11
op
* purpose with or without fee is hereby granted, provided that the above
008
2022-02-11
op
* copyright notice and this permission notice appear in all copies.
010
2022-02-11
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
011
2022-02-11
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
012
2022-02-11
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
013
2022-02-11
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
014
2022-02-11
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
015
2022-02-11
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
016
2022-02-11
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
019
2022-02-11
op
#include "compat.h"
021
2022-02-11
op
#include <sys/types.h>
022
2022-02-11
op
#include <sys/stat.h>
023
2022-02-11
op
#include <sys/socket.h>
024
2022-02-11
op
#include <sys/uio.h>
025
2022-02-11
op
#include <sys/un.h>
027
2022-02-11
op
#include <errno.h>
028
2022-02-11
op
#include <stdlib.h>
029
2022-02-11
op
#include <string.h>
030
2022-02-11
op
#include <unistd.h>
032
2022-02-11
op
#include "control.h"
033
2022-02-11
op
#include "minibuffer.h"
034
2022-02-11
op
#include "telescope.h"
035
2022-02-11
op
#include "utils.h"
036
2022-02-11
op
#include "ui.h"
038
2022-02-11
op
#define CONTROL_BACKLOG 5
041
2022-02-11
op
struct event ev;
042
2022-02-11
op
struct event evt;
044
2022-02-11
op
} control_state = {.fd = -1};
046
2022-02-11
op
struct ctl_conn {
047
2022-02-11
op
TAILQ_ENTRY(ctl_conn) entry;
048
2022-02-11
op
struct imsgev iev;
051
2022-02-11
op
struct ctl_conn *control_connbyfd(int);
052
2022-02-11
op
struct ctl_conn *control_connbypid(pid_t);
053
2022-02-11
op
void control_close(int);
055
2022-02-11
op
TAILQ_HEAD(ctl_conns, ctl_conn) ctl_conns = TAILQ_HEAD_INITIALIZER(ctl_conns);
058
2022-02-11
op
control_init(char *path)
060
2022-02-11
op
struct sockaddr_un sun;
062
2022-02-11
op
mode_t old_umask;
064
2022-02-11
op
if ((fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK,
065
2022-02-11
op
0)) == -1) {
066
2022-02-11
op
warn("%s: socket", __func__);
067
2022-02-11
op
return (-1);
070
2022-02-11
op
memset(&sun, 0, sizeof(sun));
071
2022-02-11
op
sun.sun_family = AF_UNIX;
072
2022-02-11
op
strlcpy(sun.sun_path, path, sizeof(sun.sun_path));
074
2022-02-11
op
if (unlink(path) == -1)
075
2022-02-11
op
if (errno != ENOENT) {
076
2022-02-11
op
warn("%s: unlink %s", __func__, path);
077
2022-02-11
op
close(fd);
078
2022-02-11
op
return (-1);
081
2022-02-11
op
old_umask = umask(S_IXUSR|S_IXGRP|S_IWOTH|S_IROTH|S_IXOTH);
082
2022-02-11
op
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
083
2022-02-11
op
warn("%s: bind: %s", __func__, path);
084
2022-02-11
op
close(fd);
085
2022-02-11
op
umask(old_umask);
086
2022-02-11
op
return (-1);
088
2022-02-11
op
umask(old_umask);
090
2022-02-11
op
if (chmod(path, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP) == -1) {
091
2022-02-11
op
warn("%s: chmod", __func__);
092
2022-02-11
op
close(fd);
093
2022-02-11
op
(void)unlink(path);
094
2022-02-11
op
return (-1);
097
2022-02-11
op
return (fd);
101
2022-02-11
op
control_listen(int fd)
103
2022-02-11
op
control_state.fd = fd;
104
2022-02-11
op
if (listen(control_state.fd, CONTROL_BACKLOG) == -1) {
105
2022-02-11
op
warn("%s: listen", __func__);
106
2022-02-11
op
return (-1);
109
2022-02-11
op
event_set(&control_state.ev, control_state.fd, EV_READ,
110
2022-02-11
op
control_accept, NULL);
111
2022-02-11
op
event_add(&control_state.ev, NULL);
112
2022-02-11
op
evtimer_set(&control_state.evt, control_accept, NULL);
114
2022-02-11
op
return (0);
118
2022-02-11
op
control_accept(int listenfd, short event, void *bula)
120
2022-02-11
op
int connfd;
121
2022-02-11
op
socklen_t len;
122
2022-02-11
op
struct sockaddr_un sun;
123
2022-02-11
op
struct ctl_conn *c;
125
2022-02-11
op
event_add(&control_state.ev, NULL);
126
2022-02-11
op
if ((event & EV_TIMEOUT))
129
2022-02-11
op
len = sizeof(sun);
130
2022-02-11
op
if ((connfd = accept4(listenfd, (struct sockaddr *)&sun, &len,
131
2022-02-11
op
SOCK_CLOEXEC | SOCK_NONBLOCK)) == -1) {
133
2022-02-11
op
* Pause accept if we are out of file descriptors, or
134
2022-02-11
op
* libevent will haunt us here too.
136
2022-02-11
op
if (errno == ENFILE || errno == EMFILE) {
137
2022-02-11
op
struct timeval evtpause = { 1, 0 };
139
2022-02-11
op
event_del(&control_state.ev);
140
2022-02-11
op
evtimer_add(&control_state.evt, &evtpause);
141
2022-02-11
op
} else if (errno != EWOULDBLOCK && errno != EINTR &&
142
2022-02-11
op
errno != ECONNABORTED)
143
2022-02-11
op
message("%s: accept4: %s", __func__, strerror(errno));
147
2022-02-11
op
if ((c = calloc(1, sizeof(struct ctl_conn))) == NULL) {
148
2022-02-11
op
message("%s: calloc: %s", __func__, strerror(errno));
149
2022-02-11
op
close(connfd);
153
2022-02-11
op
imsg_init(&c->iev.ibuf, connfd);
154
2022-02-11
op
c->iev.handler = control_dispatch_imsg;
155
2022-02-11
op
c->iev.events = EV_READ;
156
2022-02-11
op
event_set(&c->iev.ev, c->iev.ibuf.fd, c->iev.events,
157
2022-02-11
op
c->iev.handler, &c->iev);
158
2022-02-11
op
event_add(&c->iev.ev, NULL);
160
2022-02-11
op
TAILQ_INSERT_TAIL(&ctl_conns, c, entry);
163
2022-02-11
op
struct ctl_conn *
164
2022-02-11
op
control_connbyfd(int fd)
166
2022-02-11
op
struct ctl_conn *c;
168
2022-02-11
op
TAILQ_FOREACH(c, &ctl_conns, entry) {
169
2022-02-11
op
if (c->iev.ibuf.fd == fd)
173
2022-02-11
op
return (c);
176
2022-02-11
op
struct ctl_conn *
177
2022-02-11
op
control_connbypid(pid_t pid)
179
2022-02-11
op
struct ctl_conn *c;
181
2022-02-11
op
TAILQ_FOREACH(c, &ctl_conns, entry) {
182
2022-02-11
op
if (c->iev.ibuf.pid == pid)
186
2022-02-11
op
return (c);
190
2022-02-11
op
control_close(int fd)
192
2022-02-11
op
struct ctl_conn *c;
194
2022-02-11
op
if ((c = control_connbyfd(fd)) == NULL) {
195
2022-02-11
op
message("%s: fd %d: not found", __func__, fd);
199
2022-02-11
op
msgbuf_clear(&c->iev.ibuf.w);
200
2022-02-11
op
TAILQ_REMOVE(&ctl_conns, c, entry);
202
2022-02-11
op
event_del(&c->iev.ev);
203
2022-02-11
op
close(c->iev.ibuf.fd);
205
2022-02-11
op
/* Some file descriptors are available again. */
206
2022-02-11
op
if (evtimer_pending(&control_state.evt, NULL)) {
207
2022-02-11
op
evtimer_del(&control_state.evt);
208
2022-02-11
op
event_add(&control_state.ev, NULL);
215
2022-02-11
op
control_dispatch_imsg(int fd, short event, void *bula)
217
2022-02-11
op
struct ctl_conn *c;
218
2022-02-11
op
struct imsg imsg;
219
2022-02-11
op
ssize_t n;
221
2022-02-11
op
if ((c = control_connbyfd(fd)) == NULL) {
222
2022-02-11
op
message("%s: fd %d: not found", __func__, fd);
226
2022-02-11
op
if (event & EV_READ) {
227
2022-02-11
op
if (((n = imsg_read(&c->iev.ibuf)) == -1 && errno != EAGAIN) ||
228
2022-02-11
op
n == 0) {
229
2022-02-11
op
control_close(fd);
233
2022-02-11
op
if (event & EV_WRITE) {
234
2022-02-11
op
if (msgbuf_write(&c->iev.ibuf.w) <= 0 && errno != EAGAIN) {
235
2022-02-11
op
control_close(fd);
240
2022-02-11
op
for (;;) {
241
2022-02-11
op
if ((n = imsg_get(&c->iev.ibuf, &imsg)) == -1) {
242
2022-02-11
op
control_close(fd);
245
2022-02-11
op
if (n == 0)
248
2022-02-11
op
switch (imsg.hdr.type) {
249
2022-02-11
op
case IMSG_CTL_OPEN_URL: {
250
2022-03-01
op
static char uri[GEMINI_URL_LEN];
252
2022-03-01
op
if (IMSG_DATA_SIZE(imsg) >= sizeof(uri))
254
2022-03-01
op
memset(uri, 0, sizeof(uri));
255
2022-02-11
op
memcpy(uri, imsg.data, sizeof(uri));
256
2022-02-11
op
if (uri[IMSG_DATA_SIZE(imsg)-1] != '\0')
259
2022-02-24
op
ui_remotely_open_link(uri);
263
2022-02-11
op
message("%s: error handling imsg %d", __func__,
264
2022-02-11
op
imsg.hdr.type);
267
2022-02-11
op
imsg_free(&imsg);
270
2022-02-11
op
imsg_event_add(&c->iev);
274
2022-02-11
op
control_imsg_relay(struct imsg *imsg)
276
2022-02-11
op
struct ctl_conn *c;
278
2022-02-11
op
if ((c = control_connbypid(imsg->hdr.pid)) == NULL)
279
2022-02-11
op
return (0);
281
2022-02-11
op
return (imsg_compose_event(&c->iev, imsg->hdr.type, 0, imsg->hdr.pid,
282
2022-02-11
op
-1, imsg->data, IMSG_DATA_SIZE(*imsg)));