-
Notifications
You must be signed in to change notification settings - Fork 1
/
lprocess.c
381 lines (332 loc) · 7.32 KB
/
lprocess.c
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
/*
* Callisto - standalone scripting platform for Lua 5.4
* Copyright (c) 2023-2024 Jeremy Baxter.
*/
/***
* Processes, signals, and signal handlers.
*
* @module process
*/
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <lua/lauxlib.h>
#include <lua/lua.h>
#include "util.h"
#define PID_MAX 8 /* rounded to the nearest even number */
#define PROCESS_MAX 256
/* clang-format off */
static const int signals[] = {
SIGHUP,
SIGINT,
SIGQUIT,
SIGILL,
SIGTRAP,
SIGABRT,
SIGIOT,
SIGFPE,
SIGKILL,
SIGBUS,
SIGSEGV,
SIGSYS,
SIGPIPE,
SIGALRM,
SIGTERM,
SIGURG,
SIGSTOP,
SIGTSTP,
SIGCONT,
SIGCHLD,
SIGTTIN,
SIGTTOU,
#ifdef SIGSTKFL
SIGSTKFL,
#endif
SIGIO,
SIGXCPU,
SIGXFSZ,
#ifdef SIGVTALR
SIGVTALR,
#elif defined(SIGVTALRM)
SIGVTALRM,
#endif
SIGPROF,
#ifdef SIGWINC
SIGWINC,
#elif defined(SIGWINCH)
SIGWINCH,
#endif
#ifdef SIGINFO
SIGINFO,
#endif
#ifdef SIGPOLL
SIGPOLL,
#endif
#ifdef SIGPWR
SIGPWR,
#endif
SIGUSR1,
SIGUSR2,
-1 /* end */
};
static const char *sigstrs[] = {
[SIGHUP] = "SIGHUP",
[SIGINT] = "SIGINT",
[SIGQUIT] = "SIGQUIT",
[SIGILL] = "SIGILL",
[SIGTRAP] = "SIGTRAP",
[SIGABRT] = "SIGABRT",
[SIGIOT] = "SIGIOT",
[SIGFPE] = "SIGFPE",
[SIGKILL] = "SIGKILL",
[SIGBUS] = "SIGBUS",
[SIGSEGV] = "SIGSEGV",
[SIGSYS] = "SIGSYS",
[SIGPIPE] = "SIGPIPE",
[SIGALRM] = "SIGALRM",
[SIGTERM] = "SIGTERM",
[SIGURG] = "SIGURG",
[SIGSTOP] = "SIGSTOP",
[SIGTSTP] = "SIGTSTP",
[SIGCONT] = "SIGCONT",
[SIGCHLD] = "SIGCHLD",
[SIGTTIN] = "SIGTTIN",
[SIGTTOU] = "SIGTTOU",
#ifdef SIGSTKFL
[SIGSTKFL] = "SIGSTKFL",
#endif
[SIGIO] = "SIGIO",
[SIGXCPU] = "SIGXCPU",
[SIGXFSZ] = "SIGXFSZ",
#ifdef SIGVTALR
[SIGVTALR] = "SIGVTALR",
#elif defined(SIGVTALRM)
[SIGVTALRM] = "SIGVTALRM",
#endif
[SIGPROF] = "SIGPROF",
#ifdef SIGWINC
[SIGWINC] = "SIGWINC",
#elif defined(SIGWINCH)
[SIGWINCH] = "SIGWINCH",
#endif
#ifdef SIGINFO
[SIGINFO] = "SIGINFO",
#endif
#ifdef SIGPOLL
[SIGPOLL] = "SIGPOLL",
#endif
#ifdef SIGPWR
[SIGPWR] = "SIGPWR",
#endif
[SIGUSR1] = "SIGUSR1",
[SIGUSR2] = "SIGUSR2"
};
/* clang-format on */
/***
* Returns the PID of the current process.
*
* @function pid
* @usage local pid = process.pid()
*/
static int
process_pid(lua_State *L)
{
lua_pushinteger(L, getpid());
return 1;
}
/***
* Returns the PID (process ID) of the given process,
* or nil if the process could not be found.
*
* Depends on the nonportable userspace utility `pgrep`.
* Can be found in the procps-ng package on Linux usually
* included in most distributions, or as part of the base
* system on OpenBSD, NetBSD and FreeBSD.
*
* @function pidof
* @usage process.pidof("init")
* @tparam string process The name of the process to look up.
*/
static int
process_pidof(lua_State *L)
{
const char *process; /* parameter 1 (string) */
char command[PROCESS_MAX]; /* pgrep command buffer */
char *buffer; /* pgrep reading buffer */
int pexit; /* pgrep exit code */
long pid; /* pid to return to Lua */
size_t pidmax; /* length passed to getline */
ssize_t ret; /* getline return value */
FILE *p; /* pgrep reading stream */
process = luaL_checkstring(L, 1);
/* construct pgrep command */
memset(command, 0, PROCESS_MAX * sizeof(char));
strbcat(command, "pgrep '", PROCESS_MAX);
strbcat(command, process, PROCESS_MAX);
strbcat(command, "' | sed 1q", PROCESS_MAX);
p = popen(command, "r");
buffer = malloc(PID_MAX * sizeof(char *));
pidmax = PID_MAX;
/* read line from pgrep */
ret = getline(&buffer, &pidmax, p);
pexit = pclose(p);
if (ret == -1 || pexit != 0) { /* did getline or pgrep fail? */
lua_pushnil(L);
return 1;
}
/* convert it to an integer and push */
pid = strtol(buffer, NULL, 10);
lua_pushinteger(L, pid);
free(buffer);
return 1;
}
#define REG_SIGC "callisto!process:sigc"
static int
initsignals(lua_State *L)
{
int sigc;
/* get the registry entry containing the sig count */
lua_getfield(L, LUA_REGISTRYINDEX, REG_SIGC);
/* if the registry entry isn't a number greater than 0... */
if (!lua_isnoneornil(L, -1)) {
if (lua_type(L, -1) != LUA_TNUMBER || lua_tointeger(L, -1) <= 0) {
luaL_error(L, "registry index for signal count is invalid");
return 0;
}
}
sigc = 0;
while (signals[sigc] != -1)
sigc++;
lua_pushinteger(L, sigc + 1);
lua_setfield(L, LUA_REGISTRYINDEX, REG_SIGC);
return 1;
}
static int
strtosig(const char *sig, int sigc)
{
int i, j;
j = 0;
for (i = signals[j]; i < sigc; j++) {
i = signals[j];
if (strcasecmp(sigstrs[i], sig) == 0)
return i; /* signal found */
}
return -1; /* invalid signal */
}
/***
* Returns the given signal as an integer.
*
* This signal value is a platform-dependent value;
* do not attempt to use it portably across different platforms.
*
* @function signum
* @usage local sigkill = process.signum("SIGKILL")
* @tparam string signal The signal to look up.
*/
static int
process_signum(lua_State *L)
{
char *sigstr;
int sig, sigc;
sigstr = strdup(luaL_checkstring(L, 1));
if (!initsignals(L))
return 0;
lua_getfield(L, LUA_REGISTRYINDEX, REG_SIGC);
sigc = lua_tointeger(L, -1);
sig = strtosig(sigstr, sigc);
free(sigstr);
if (sig != -1) { /* valid signal? */
lua_pushinteger(L, sig); /* return signal */
return 1;
}
return luaL_error(L, "no such signal");
}
static int
sigsend(lua_State *L, pid_t pid, const char *sigstr)
{
int ret, sig, sigc;
if (!initsignals(L))
return 0;
lua_getfield(L, LUA_REGISTRYINDEX, REG_SIGC);
sigc = lua_tointeger(L, -1);
sig = strtosig(sigstr, sigc);
if (sig != -1) /* valid signal? */
ret = kill(pid, sig);
else
return luaL_error(L, "no such signal");
if (ret == 0) { /* check for success */
lua_pushboolean(L, 1);
return 1;
}
return lfail(L);
}
#undef REG_SIGC
/***
* Sends the given signal to the process with the given PID.
*
* The *signal* parameter is a string containing the name
* of the desired signal to send (e.g. SIGKILL).
*
* @function send
* @usage
local pid = process.pid("sh")
process.send(pid, "SIGTERM")
* @tparam integer pid The PID of the process.
* @tparam string signal The signal to send.
*/
static int
process_send(lua_State *L)
{
pid_t pid; /* parameter 1 (integer) */
const char *sig; /* parameter 2 (string) */
pid = luaL_checkinteger(L, 1);
sig = luaL_checkstring(L, 2);
return sigsend(L, pid, sig);
}
/***
* Kills the process with the given PID.
*
* Equivalent to `process.send(pid, "SIGKILL")`.
*
* @function kill
* @tparam integer pid The PID of the process.
*/
static int
process_kill(lua_State *L)
{
pid_t pid; /* parameter 1 (integer) */
pid = luaL_checkinteger(L, 1);
return sigsend(L, pid, "SIGKILL");
}
/***
* Terminates the process with the given PID.
*
* Equivalent to `process.send(pid, "SIGTERM")`.
*
* @function terminate
* @tparam integer pid The PID of the process.
*/
static int
process_terminate(lua_State *L)
{
pid_t pid; /* parameter 1 (integer) */
pid = luaL_checkinteger(L, 1);
return sigsend(L, pid, "SIGTERM");
}
/* clang-format off */
static const luaL_Reg proclib[] = {
{"kill", process_kill},
{"pid", process_pid},
{"pidof", process_pidof},
{"send", process_send},
{"signum", process_signum},
{"terminate", process_terminate},
{NULL, NULL}
};
int
luaopen_process(lua_State *L)
{
luaL_newlib(L, proclib);
return 0;
}