Skip to content

Commit 43faed1

Browse files
committed
fixup! Add signal functions (sigmodify, pending)
1 parent 832f94e commit 43faed1

File tree

1 file changed

+49
-47
lines changed

1 file changed

+49
-47
lines changed

lib/lualinux.c

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ static int lualinux_schedule(lua_State *L)
123123
* @function sigmask
124124
* @tparam integer sig Signal number
125125
* @tparam[opt] integer cmd 0=BLOCK (default), 1=UNBLOCK
126-
* @treturn boolean `true` on success
127126
* @raise error string on failure (EINVAL, EPERM, etc.)
128127
* @within linux
129128
* @usage
@@ -141,8 +140,7 @@ static int lualinux_sigmask(lua_State *L)
141140
sigaddset(&newmask, signum);
142141

143142
lunatik_try(L, sigprocmask, cmd, &newmask, NULL);
144-
lua_pushboolean(L, true);
145-
return 1;
143+
return 0;
146144
}
147145

148146
/***
@@ -151,62 +149,26 @@ static int lualinux_sigmask(lua_State *L)
151149
* @function sigpending
152150
* @treturn boolean
153151
* @within linux
152+
* @usage
153+
* linux.sigpending()
154154
*/
155155
static int lualinux_sigpending(lua_State *L)
156156
{
157157
lua_pushboolean(L, signal_pending(current));
158158
return 1;
159159
}
160160

161-
/***
162-
* Kills a process by sending a signal.
163-
* By default, sends SIGKILL.
164-
* An optional second argument can specify a different signal (either by number or by using the constants from `linux.signal`).
165-
*
166-
* @function kill
167-
* @tparam integer pid Process ID to kill.
168-
* @tparam[opt] integer sig Signal number to send (default: `linux.signal.KILL`).
169-
* @treturn boolean `true` if the signal was sent successfully.
170-
* @treturn[error] boolean `false` followed by an error number if the operation fails.
171-
* @raise Errors:
172-
* - (3): The specified PID doesn't exist
173-
* - other errno values depending on the failure cause (e.g., `EPERM`, `EINVAL`, etc.)
174-
* @usage
175-
* linux.kill(1234) -- Kill process 1234 with SIGKILL (default)
176-
* linux.kill(1234, linux.signal.TERM) -- Kill process 1234 with SIGTERM
177-
*/
178-
static int lualinux_kill(lua_State *L)
179-
{
180-
pid_t nr = (pid_t)luaL_checkinteger(L, 1);
181-
int sig = luaL_optinteger(L, 2, SIGKILL);
182-
struct pid *pid = find_get_pid(nr);
183-
184-
int ret = ESRCH;
185-
if (pid == NULL)
186-
goto err;
187-
188-
ret = kill_pid(pid, sig, 1);
189-
put_pid(pid);
190-
191-
if (ret)
192-
goto err;
193-
194-
lua_pushboolean(L, true);
195-
return 1;
196-
err:
197-
lua_pushboolean(L, false);
198-
lua_pushinteger(L, ret);
199-
return 2;
200-
}
201-
202161
/***
203162
* Checks signal state for current task.
204163
*
205164
* @function sigstate
206165
* @tparam integer sig Signal number
207-
* @tparam[opt] string state One of: sigstate_opts
166+
* @tparam[opt] string state One of: "blocked", "pending", "allowed"
208167
* @treturn boolean
209168
* @within linux
169+
* @usage
170+
* linux.sigstate(15) -- check if SIGTERM is blocked (default)
171+
* linux.sigstate(linux.signal.TERM, "pending")
210172
*/
211173
static int lualinux_sigstate(lua_State *L)
212174
{
@@ -216,11 +178,10 @@ static int lualinux_sigstate(lua_State *L)
216178
SIGSTATE_ALLOWED,
217179
};
218180

219-
static const char *const sigstate_opts[] = {
181+
const char *const sigstate_opts[] = {
220182
[SIGSTATE_BLOCKED] = "blocked",
221183
[SIGSTATE_PENDING] = "pending",
222184
[SIGSTATE_ALLOWED] = "allowed",
223-
NULL
224185
};
225186

226187
int signum = luaL_checkinteger(L, 1);
@@ -243,6 +204,47 @@ static int lualinux_sigstate(lua_State *L)
243204
return 1;
244205
}
245206

207+
/***
208+
* Kills a process by sending a signal.
209+
* By default, sends SIGKILL.
210+
* An optional second argument can specify a different signal (either by number or by using the constants from `linux.signal`).
211+
*
212+
* @function kill
213+
* @tparam integer pid Process ID to kill.
214+
* @tparam[opt] integer sig Signal number to send (default: `linux.signal.KILL`).
215+
* @treturn boolean `true` if the signal was sent successfully.
216+
* @treturn[error] boolean `false` followed by an error number if the operation fails.
217+
* @raise Errors:
218+
* - (3): The specified PID doesn't exist
219+
* - other errno values depending on the failure cause (e.g., `EPERM`, `EINVAL`, etc.)
220+
* @usage
221+
* linux.kill(1234) -- Kill process 1234 with SIGKILL (default)
222+
* linux.kill(1234, linux.signal.TERM) -- Kill process 1234 with SIGTERM
223+
*/
224+
static int lualinux_kill(lua_State *L)
225+
{
226+
pid_t nr = (pid_t)luaL_checkinteger(L, 1);
227+
int sig = luaL_optinteger(L, 2, SIGKILL);
228+
struct pid *pid = find_get_pid(nr);
229+
230+
int ret = ESRCH;
231+
if (pid == NULL)
232+
goto err;
233+
234+
ret = kill_pid(pid, sig, 1);
235+
put_pid(pid);
236+
237+
if (ret)
238+
goto err;
239+
240+
lua_pushboolean(L, true);
241+
return 1;
242+
err:
243+
lua_pushboolean(L, false);
244+
lua_pushinteger(L, ret);
245+
return 2;
246+
}
247+
246248
/***
247249
* Controls kernel tracing.
248250
* Turns kernel tracing on or off via `tracing_on()` and `tracing_off()`.

0 commit comments

Comments
 (0)