1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/* $OpenBSD$ */
/*
* Copyright (c) 2015 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/utsname.h>
#include <errno.h>
#include <event.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tmux.h"
struct tmuxproc {
const char *name;
int exit;
void (*signalcb)(int);
};
struct tmuxpeer {
struct tmuxproc *parent;
struct imsgbuf ibuf;
struct event event;
int flags;
#define PEER_BAD 0x1
void (*dispatchcb)(struct imsg *, void *);
void *arg;
};
static int peer_check_version(struct tmuxpeer *, struct imsg *);
static void proc_update_event(struct tmuxpeer *);
static void
proc_event_cb(__unused int fd, short events, void *arg)
{
struct tmuxpeer *peer = arg;
ssize_t n;
struct imsg imsg;
if (!(peer->flags & PEER_BAD) && (events & EV_READ)) {
if (((n = imsg_read(&peer->ibuf)) == -1 && errno != EAGAIN) ||
n == 0) {
peer->dispatchcb(NULL, peer->arg);
return;
}
for (;;) {
if ((n = imsg_get(&peer->ibuf, &imsg)) == -1) {
peer->dispatchcb(NULL, peer->arg);
return;
}
if (n == 0)
break;
log_debug("peer %p message %d", peer, imsg.hdr.type);
if (peer_check_version(peer, &imsg) != 0) {
if (imsg.fd != -1)
close(imsg.fd);
imsg_free(&imsg);
break;
}
peer->dispatchcb(&imsg, peer->arg);
imsg_free(&imsg);
}
}
if (events & EV_WRITE) {
if (msgbuf_write(&peer->ibuf.w) <= 0 && errno != EAGAIN) {
peer->dispatchcb(NULL, peer->arg);
return;
}
}
if ((peer->flags & PEER_BAD) && peer->ibuf.w.queued == 0) {
peer->dispatchcb(NULL, peer->arg);
return;
}
proc_update_event(peer);
}
static void
proc_signal_cb(int signo, __unused short events, void *arg)
{
struct tmuxproc *tp = arg;
tp->signalcb(signo);
}
static int
peer_check_version(struct tmuxpeer *peer, struct imsg *imsg)
{
int version;
version = imsg->hdr.peerid & 0xff;
if (imsg->hdr.type != MSG_VERSION && version != PROTOCOL_VERSION) {
log_debug("peer %p bad version %d", peer, version);
proc_send(peer, MSG_VERSION, -1, NULL, 0);
peer->flags |= PEER_BAD;
return (-1);
}
return (0);
}
static void
proc_update_event(struct tmuxpeer *peer)
{
short events;
event_del(&peer->event);
events = EV_READ;
if (peer->ibuf.w.queued > 0)
events |= EV_WRITE;
event_set(&peer->event, peer->ibuf.fd, events, proc_event_cb, peer);
event_add(&peer->event, NULL);
}
int
proc_send(struct tmuxpeer *peer, enum msgtype type, int fd, const void *buf,
size_t len)
{
struct imsgbuf *ibuf = &peer->ibuf;
void *vp = (void *)buf;
int retval;
if (peer->flags & PEER_BAD)
return (-1);
log_debug("sending message %d to peer %p (%zu bytes)", type, peer, len);
retval = imsg_compose(ibuf, type, PROTOCOL_VERSION, -1, fd, vp, len);
if (retval != 1)
return (-1);
proc_update_event(peer);
return (0);
}
int
proc_send_s(struct tmuxpeer *peer, enum msgtype type, const char *s)
{
return (proc_send(peer, type, -1, s, strlen(s) + 1));
}
struct tmuxproc *
proc_start(const char *name, struct event_base *base, int forkflag,
void (*signalcb)(int))
{
struct tmuxproc *tp;
struct utsname u;
if (forkflag) {
switch (fork()) {
case -1:
fatal("fork failed");
case 0:
break;
default:
return (NULL);
}
if (daemon(1, 0) != 0)
fatal("daemon failed");
clear_signals(0);
if (event_reinit(base) != 0)
fatalx("event_reinit failed");
}
log_open(name);
setproctitle("%s (%s)", name, socket_path);
if (uname(&u) < 0)
memset(&u, 0, sizeof u);
log_debug("%s started (%ld): socket %s, protocol %d", name,
(long)getpid(), socket_path, PROTOCOL_VERSION);
log_debug("on %s %s %s; libevent %s (%s)", u.sysname, u.release,
u.version, event_get_version(), event_get_method());
tp = xcalloc(1, sizeof *tp);
tp->name = xstrdup(name);
tp->signalcb = signalcb;
set_signals(proc_signal_cb, tp);
return (tp);
}
void
proc_loop(struct tmuxproc *tp, int (*loopcb)(void))
{
log_debug("%s loop enter", tp->name);
do
event_loop(EVLOOP_ONCE);
while (!tp->exit && (loopcb == NULL || !loopcb ()));
log_debug("%s loop exit", tp->name);
}
void
proc_exit(struct tmuxproc *tp)
{
tp->exit = 1;
}
struct tmuxpeer *
proc_add_peer(struct tmuxproc *tp, int fd,
void (*dispatchcb)(struct imsg *, void *), void *arg)
{
struct tmuxpeer *peer;
peer = xcalloc(1, sizeof *peer);
peer->parent = tp;
peer->dispatchcb = dispatchcb;
peer->arg = arg;
imsg_init(&peer->ibuf, fd);
event_set(&peer->event, fd, EV_READ, proc_event_cb, peer);
log_debug("add peer %p: %d (%p)", peer, fd, arg);
proc_update_event(peer);
return (peer);
}
void
proc_remove_peer(struct tmuxpeer *peer)
{
log_debug("remove peer %p", peer);
event_del(&peer->event);
imsg_clear(&peer->ibuf);
close(peer->ibuf.fd);
free(peer);
}
void
proc_kill_peer(struct tmuxpeer *peer)
{
peer->flags |= PEER_BAD;
}
|