fix crash in process.signum, use pgrep over pidof in process.pidof

pidof is, as far as I know, only available on Linux. pgrep is much
more portable (even though it is not part of a standard), and can
be found in the base system on most BSDs and included in most
Linux distributions as part of the procps-ng package.
This commit is contained in:
Jeremy Baxter 2023-12-27 14:18:07 +13:00
parent 7b8c7632e9
commit 099a9c33e6

View file

@ -9,9 +9,6 @@
#include <signal.h> #include <signal.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#ifdef __OpenBSD__
# include <string.h>
#endif
#include <lua.h> #include <lua.h>
#include <lauxlib.h> #include <lauxlib.h>
@ -22,9 +19,61 @@
#define PID_MAX 8 /* rounded to the nearest even number */ #define PID_MAX 8 /* rounded to the nearest even number */
#define PROCESS_MAX 256 #define PROCESS_MAX 256
/* signals and signal count */ /* signals */
#define SIGC 36 static const int signals[] = {
static const char *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", [SIGHUP] = "SIGHUP",
[SIGINT] = "SIGINT", [SIGINT] = "SIGINT",
[SIGQUIT] = "SIGQUIT", [SIGQUIT] = "SIGQUIT",
@ -91,9 +140,13 @@ process_pid(lua_State *L)
} }
/*** /***
* Returns the PID (process ID) of the given process. * Returns the PID (process ID) of the given process,
* or nil if the process could not be found.
* *
* Returns nil if the process was not 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 * @function pidof
* @usage process.pidof("init") * @usage process.pidof("init")
@ -102,61 +155,89 @@ process_pid(lua_State *L)
static int static int
process_pidof(lua_State *L) process_pidof(lua_State *L)
{ {
const char *process; /* parameter 1 (string) */ const char *process; /* parameter 1 (string) */
char *command; /* pidof command buffer */ char command[PROCESS_MAX]; /* pgrep command buffer */
char *buffer; /* pidof reading buffer */ char *buffer; /* pgrep reading buffer */
int pexit; /* pidof exit code */ int pexit; /* pgrep exit code */
long pid; /* pid to return to Lua */ long pid; /* pid to return to Lua */
size_t pidmax; /* length passed to getline */ size_t pidmax; /* length passed to getline */
ssize_t ret; /* getline return value */ ssize_t ret; /* getline return value */
FILE *p; /* pidof stream */ FILE *p; /* pgrep reading stream */
process = luaL_checkstring(L, 1); process = luaL_checkstring(L, 1);
command = calloc(1, PROCESS_MAX * sizeof(char *));
/* construct pidof command */ /* construct pgrep command */
strlcat(command, "pidof -s '", (size_t)PROCESS_MAX); memset(command, 0, PROCESS_MAX * sizeof(char));
strlcat(command, process, (size_t)PROCESS_MAX); strlcat(command, "pgrep '", PROCESS_MAX);
strlcat(command, "'", (size_t)PROCESS_MAX); strlcat(command, process, PROCESS_MAX);
strlcat(command, "' | sed 1q", PROCESS_MAX);
p = popen(command, "r"); p = popen(command, "r");
buffer = malloc(PID_MAX * sizeof(char *)); buffer = malloc(PID_MAX * sizeof(char *));
pidmax = (size_t)PID_MAX; pidmax = PID_MAX;
/* read line from pidof */ /* read line from pgrep */
ret = getline(&buffer, &pidmax, p); ret = getline(&buffer, &pidmax, p);
pexit = pclose(p); pexit = pclose(p);
if (ret == -1 || pexit != 0) { /* did getline or pidof fail? */ if (ret == -1 || pexit != 0) { /* did getline or pgrep fail? */
luaL_pushfail(L); lua_pushnil(L);
return 1; return 1;
} }
/* convert it to an integer and push */ /* convert it to an integer and push */
pid = strtol(buffer, NULL, 10); pid = strtol(buffer, NULL, 10);
lua_pushinteger(L, pid); lua_pushinteger(L, pid);
free(command);
free(buffer); free(buffer);
return 1; return 1;
} }
static int #define REG_SIGC "callisto!process:sigc"
strtosig(const char *sig)
{
int i;
for (i = 1; i <= SIGC; i++) { static int
if (strcmp(signals[i], sig) == 0) initsignals(lua_State *L)
return i; /* valid signal found */ {
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 */ return -1; /* invalid signal */
} }
/*** /***
* Returns the given signal as an integer. * Returns the given signal as an integer.
* This function may return different values *
* across different operating systems, as * This signal value is a platform-dependent value;
* signal constants vary across different Unixes. * do not attempt to use it portably across different platforms.
* *
* @function signum * @function signum
* @usage local sigkill = process.signum("SIGKILL") * @usage local sigkill = process.signum("SIGKILL")
@ -165,26 +246,43 @@ strtosig(const char *sig)
static int static int
process_signum(lua_State *L) process_signum(lua_State *L)
{ {
int sig; char *sigstr;
int sig, sigc;
if ((sig = strtosig(luaL_checkstring(L, 1))) != -1) /* valid signal? */ 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 */ lua_pushinteger(L, sig); /* return signal */
else return 1;
return lfailm(L, "no such signal"); }
return 1; return luaL_error(L, "no such signal");
} }
static int static int
sigsend(lua_State *L, pid_t pid, const char *sigstr) sigsend(lua_State *L, pid_t pid, const char *sigstr)
{ {
int ret, sig; int ret, sig, sigc;
sig = strtosig(sigstr); 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? */ if (sig != -1) /* valid signal? */
ret = kill(pid, sig); ret = kill(pid, sig);
else else
return lfailm(L, "no such signal"); return luaL_error(L, "no such signal");
if (ret == 0) { /* check for success */ if (ret == 0) { /* check for success */
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
@ -194,13 +292,13 @@ sigsend(lua_State *L, pid_t pid, const char *sigstr)
return lfail(L); return lfail(L);
} }
#undef REG_SIGC
/*** /***
* Sends the given signal to the * Sends the given signal to the process with the given PID.
* process with the given PID.
* *
* The *signal* parameter is a string * The *signal* parameter is a string containing the name
* containing the name of the desired * of the desired signal to send (e.g. SIGKILL).
* signal to send..
* *
* @function send * @function send
* @usage * @usage