process: fix segmentation fault in strtosig
Refactor some of the magic behind strtosig; store the signal count in a static variable, avoiding the complexity of accessing the Lua registry.
This commit is contained in:
parent
be1d3a1352
commit
41829c3170
1 changed files with 11 additions and 46 deletions
57
lprocess.c
57
lprocess.c
|
@ -22,6 +22,8 @@
|
||||||
#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
|
||||||
|
|
||||||
|
static int sigc = -1;
|
||||||
|
|
||||||
/* clang-format off */
|
/* clang-format off */
|
||||||
|
|
||||||
static const int signals[] = {
|
static const int signals[] = {
|
||||||
|
@ -199,37 +201,15 @@ process_pidof(lua_State *L)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
#define REG_SIGC "callisto!process:sigc"
|
|
||||||
|
|
||||||
static int
|
static int
|
||||||
initsignals(lua_State *L)
|
strtosig(const char *sig)
|
||||||
{
|
{
|
||||||
int sigc;
|
int i, j;
|
||||||
|
|
||||||
/* 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;
|
sigc = 0;
|
||||||
while (signals[sigc] != -1)
|
while (signals[sigc] != -1)
|
||||||
sigc++;
|
sigc++;
|
||||||
|
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;
|
j = 0;
|
||||||
for (i = signals[j]; i < sigc; j++) {
|
for (i = signals[j]; i < sigc; j++) {
|
||||||
|
@ -253,18 +233,11 @@ strtosig(const char *sig, int sigc)
|
||||||
static int
|
static int
|
||||||
process_signum(lua_State *L)
|
process_signum(lua_State *L)
|
||||||
{
|
{
|
||||||
char *sigstr;
|
const char *sigstr;
|
||||||
int sig, sigc;
|
int sig;
|
||||||
|
|
||||||
sigstr = strdup(luaL_checkstring(L, 1));
|
sigstr = luaL_checkstring(L, 1);
|
||||||
|
sig = strtosig(sigstr);
|
||||||
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? */
|
if (sig != -1) { /* valid signal? */
|
||||||
lua_pushinteger(L, sig); /* return signal */
|
lua_pushinteger(L, sig); /* return signal */
|
||||||
|
@ -277,15 +250,9 @@ process_signum(lua_State *L)
|
||||||
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, sigc;
|
int ret, sig;
|
||||||
|
|
||||||
if (!initsignals(L))
|
sig = strtosig(sigstr);
|
||||||
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
|
||||||
|
@ -299,8 +266,6 @@ 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 process with the given PID.
|
* Sends the given signal to the process with the given PID.
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue