summaryrefslogtreecommitdiffstats
path: root/lem/signal/core.c
blob: 695f9d6e8a6759665652699d1b7ab577c09fa7ed (plain) (blame)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
 * This file is part of LEM, a Lua Event Machine.
 * Copyright 2013 Asbjørn Sloth Tønnesen
 * Copyright 2013 Emil Renner Berthing
 *
 * LEM is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * LEM is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with LEM.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <lem.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

struct signal_mapping {
	const char *name;
	uint8_t	no;
};

/* Several signal numbers are architecture-dependent,
 * therefore we need a lookup table on the C-side */
#define ARRAYLEN(a) (sizeof(a)/sizeof((a)[0]))
#define _(sig) { #sig, SIG ## sig }
static struct signal_mapping sigmap[] = {
	_(HUP), _(INT), _(USR1), _(USR2),
	_(QUIT), _(ILL), _(TRAP), _(ABRT),
	_(BUS), _(FPE), _(SEGV), _(PIPE),
	_(ALRM), _(TERM), _(CONT), _(CHLD),
	_(TSTP), _(TTIN), _(TTOU), _(PROF),
	_(SYS), _(URG), _(VTALRM),
	_(XCPU), _(XFSZ), _(WINCH),
#ifdef SIGPWR
	_(PWR),
#endif
};
#undef _

#if EV_SIGNAL_ENABLE
struct sigwatcher {
	struct sigwatcher *next;
	struct ev_signal w;
};

static sigset_t signal_sigset;
static struct sigwatcher *signal_watchers;

static void
signal_os_handler(EV_P_ struct ev_signal *w, int revents)
{
	lua_State *S;

	(void)revents;

	S = lem_newthread();
	lua_pushlightuserdata(S, &sigmap);
	lua_rawget(S, LUA_REGISTRYINDEX);
	if (lua_type(S, 1) != LUA_TFUNCTION) {
		lem_forgetthread(S);
		return;
	}

	lua_pushinteger(S, w->signum);
	lem_queue(S, 1);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
static inline void
signal_watcher_init(struct sigwatcher *s, int sig)
{
	ev_signal_init(&s->w, signal_os_handler, sig);
}
#pragma GCC diagnostic pop

static int
signal_os_watch(lua_State *T, int sig)
{
	struct sigwatcher *s;

	if (sigismember(&signal_sigset, sig))
		goto out; /* already watched */

	s = lem_xmalloc(sizeof(struct sigwatcher));

	signal_watcher_init(s, sig);
	ev_set_priority(&s->w, EV_MAXPRI);
	ev_signal_start(LEM_ &s->w);
	ev_unref(LEM); /* watcher shouldn't keep loop alive */

	sigaddset(&signal_sigset, sig);
	pthread_sigmask(SIG_UNBLOCK, &signal_sigset, NULL);

	s->next = signal_watchers;
	signal_watchers = s;
out:
	lua_pushboolean(T, 1);
	return 1;
}

static int
signal_os_unwatch(lua_State *T, int sig)
{
	struct sigwatcher **prevp;
	struct sigwatcher *s;

	for (prevp = &signal_watchers, s = signal_watchers;
			s != NULL;
			prevp = &s->next, s = s->next) {
		if (s->w.signum == sig)
			break;
	}
	if (s != NULL) {
		ev_ref(LEM);
		ev_signal_stop(LEM_ &s->w);

		sigdelset(&signal_sigset, sig);

		*prevp = s->next;
		free(s);
	}
	lua_pushboolean(T, 1);
	return 1;
}
#else /* EV_SIGNAL_ENABLE */
static int
signal_os_unsupported(lua_State *T)
{
	lua_pushnil(T);
	lua_pushliteral(T, "Your libev is compiled without signal support.");
	return 2
}

static inline int
signal_os_watch(lua_State *T)
{
	return signal_os_unsupported(T);
}

static inline int
signal_os_unwatch(lua_State *T)
{
	return signal_os_unsupported(T);
}
#endif

#if EV_CHILD_ENABLE
static struct ev_child signal_child_watcher;

static void
signal_child_handler(EV_P_ struct ev_child *w, int revents)
{
	lua_State *S;
	int status;
	int pid = w->pid;
	int rpid = w->rpid;

	(void)revents;

	S = lem_newthread();
	lua_pushlightuserdata(S, &sigmap);
	lua_rawget(S, LUA_REGISTRYINDEX);
	if (lua_type(S, 1) != LUA_TFUNCTION) {
		lem_forgetthread(S);
		return;
	}
	lua_pushinteger(S, SIGCHLD);

	status = w->rstatus;
	lua_createtable(S, 0, 5);

	lua_pushinteger(S, pid);
	lua_setfield(S, -2, "pid");
	lua_pushinteger(S, rpid);
	lua_setfield(S, -2, "rpid");

	if (WIFEXITED(status)) {
		lua_pushinteger(S, WEXITSTATUS(status));
		lua_setfield(S, -2, "status");
		lua_pushstring(S, "exited");
	} else if (WIFSIGNALED(status)) {
		lua_pushinteger(S, WTERMSIG(status));
		lua_setfield(S, -2, "signal");
#ifdef WCOREDUMP
		lua_pushboolean(S, WCOREDUMP(status));
		lua_setfield(S, -2, "coredumped");
#endif
		lua_pushstring(S, "signaled");
	} else if (WIFSTOPPED(status)) {
		lua_pushinteger(S, WSTOPSIG(status));
		lua_setfield(S, -2, "signal");
		lua_pushstring(S, "stopped");
	} else if (WIFCONTINUED(status)) {
		lua_pushstring(S, "continued");
	} else {
		assert(0); /* XXX do something more graceful */
	}
	lua_setfield(S, -2, "type");

	lem_queue(S, 2);
}

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
static inline void
signal_child_watcher_init(void)
{
	ev_child_init(&signal_child_watcher, signal_child_handler, 0, 1);
}

static inline int
signal_child_active(void)
{
	return ev_is_active(&signal_child_watcher);
}
#pragma GCC diagnostic pop

static int
signal_child_watch(lua_State *T)
{
	if (!signal_child_active()) {
		ev_child_start(LEM_ &signal_child_watcher);
		ev_unref(LEM); /* watcher shouldn't keep loop alive */
	}
	lua_pushboolean(T, 1);
	return 1;
}

static int
signal_child_unwatch(lua_State *T)
{
	if (signal_child_active()) {
		ev_ref(LEM);
		ev_child_stop(LEM_ &signal_child_watcher);
	}
	lua_pushboolean(T, 1);
	return 1;
}
#else /* EV_CHILD_ENABLE */
static int
signal_child_unsupported(lua_State *T)
{
	lua_pushnil(T);
	lua_pushliteral(T, "Your libev is compiled without child support.");
	return 2
}

static inline int
signal_child_watch(lua_State *T)
{
	return signal_child_unsupported(T);
}

static inline int
signal_child_unwatch(lua_State *T)
{
	return signal_child_unsupported(T);
}
#endif

static int
signal_tonumber(lua_State *T)
{
	const char *needle = luaL_checkstring(T, 1);
	unsigned int i;

	for (i = 0; i < ARRAYLEN(sigmap); i++) {
		struct signal_mapping *sig = &sigmap[i];
		if (strcmp(sig->name, needle) == 0) {
			lua_pushinteger(T, sig->no);
			return 1;
		}
	}
	lua_pushnil(T);
	return 1;
}

static int
signal_tostring(lua_State *T)
{
	int needle = luaL_checkinteger(T, 1);
	unsigned int i;

	for (i = 0; i < ARRAYLEN(sigmap); i++) {
		struct signal_mapping *sig = &sigmap[i];
		if (sig->no == needle) {
			lua_pushstring(T, sig->name);
			return 1;
		}
	}
	lua_pushnil(T);
	return 1;
}

static int
signal_sethandler(lua_State *T)
{
	int type;

	if (lua_gettop(T) < 1)
		lua_pushnil(T);

	type = lua_type(T, 1);
	if (type != LUA_TNIL && type != LUA_TFUNCTION)
		return luaL_argerror(T, 1, "expected nil or a function");

	lua_settop(T, 1);
	lua_pushlightuserdata(T, &sigmap);
	lua_insert(T, 1);
	lua_rawset(T, LUA_REGISTRYINDEX);
	return 0;
}

static int
signal_watch(lua_State *T)
{
	int sig = luaL_checkinteger(T, 1);

	lua_settop(T, 1);
	lua_pushlightuserdata(T, &sigmap);
	lua_rawget(T, LUA_REGISTRYINDEX);
	if (lua_isnil(T, 2))
		return luaL_error(T, "You must set a signal handler first");

	if (sig == SIGCHLD)
		return signal_child_watch(T);

	return signal_os_watch(T, sig);
}

static int
signal_unwatch(lua_State *T)
{
	int sig = luaL_checkinteger(T, 1);

	if (sig == SIGCHLD)
		return signal_child_unwatch(T);

	return signal_os_unwatch(T, sig);
}

int
luaopen_lem_signal_core(lua_State *T)
{
#if EV_CHILD_ENABLE
	signal_child_watcher_init();
#endif

#if EV_SIGNAL_ENABLE
	sigemptyset(&signal_sigset);
	/* signal_watchers = NULL; globals are zero initalized */
#endif

	/* create module table */
	lua_newtable(T);

	/* set tonumber function */
	lua_pushcfunction(T, signal_tonumber);
	lua_setfield(T, -2, "tonumber");
	/* set tostring function */
	lua_pushcfunction(T, signal_tostring);
	lua_setfield(T, -2, "tostring");
	/* set sethandler function */
	lua_pushcfunction(T, signal_sethandler);
	lua_setfield(T, -2, "sethandler");
	/* set watch function */
	lua_pushcfunction(T, signal_watch);
	lua_setfield(T, -2, "watch");
	/* set unwatch function */
	lua_pushcfunction(T, signal_unwatch);
	lua_setfield(T, -2, "unwatch");

	return 1;
}