tree-wide: add new SIGNAL_VALID() macro-like function that validates signal numbers

And port all code over to use it.
This commit is contained in:
Lennart Poettering 2016-04-08 11:27:28 +02:00
parent 4c310c073a
commit 6eb7c172b5
9 changed files with 20 additions and 13 deletions

View File

@ -255,7 +255,7 @@ int signal_from_string(const char *s) {
}
if (safe_atou(s, &u) >= 0) {
signo = (int) u + offset;
if (signo > 0 && signo < _NSIG)
if (SIGNAL_VALID(signo))
return signo;
}
return -EINVAL;

View File

@ -50,3 +50,7 @@ static inline void block_signals_reset(sigset_t *ss) {
assert_se(sigprocmask_many(SIG_BLOCK, &t, __VA_ARGS__, -1) >= 0); \
t; \
})
static inline bool SIGNAL_VALID(int signo) {
return signo > 0 && signo < _NSIG;
}

View File

@ -58,7 +58,7 @@ int bus_kill_context_set_transient_property(
k = kill_mode_from_string(m);
if (k < 0)
return -EINVAL;
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Kill mode '%s' not known.", m);
if (mode != UNIT_CHECK) {
c->kill_mode = k;
@ -75,7 +75,7 @@ int bus_kill_context_set_transient_property(
if (r < 0)
return r;
if (sig <= 0 || sig >= _NSIG)
if (!SIGNAL_VALID(sig))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal %i out of range", sig);
if (mode != UNIT_CHECK) {

View File

@ -27,6 +27,7 @@
#include "locale-util.h"
#include "log.h"
#include "selinux-access.h"
#include "signal-util.h"
#include "special.h"
#include "string-util.h"
#include "strv.h"
@ -547,7 +548,7 @@ int bus_unit_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid who argument %s", swho);
}
if (signo <= 0 || signo >= _NSIG)
if (!SIGNAL_VALID(signo))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Signal number out of range.");
r = bus_verify_manage_units_async_full(
@ -1002,7 +1003,6 @@ int bus_unit_queue_job(
type = JOB_TRY_RELOAD;
}
if (type == JOB_STOP &&
(u->load_state == UNIT_NOT_FOUND || u->load_state == UNIT_ERROR) &&
unit_active_state(u) == UNIT_INACTIVE)
@ -1259,6 +1259,7 @@ int bus_unit_set_properties(
}
int bus_unit_check_load_state(Unit *u, sd_bus_error *error) {
assert(u);
if (u->load_state == UNIT_LOADED)
return 0;

View File

@ -47,6 +47,7 @@
#include "path-util.h"
#include "process-util.h"
#include "set.h"
#include "signal-util.h"
#include "special.h"
#include "stat-util.h"
#include "stdio-util.h"
@ -3062,8 +3063,7 @@ bool unit_active_or_pending(Unit *u) {
int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error) {
assert(u);
assert(w >= 0 && w < _KILL_WHO_MAX);
assert(signo > 0);
assert(signo < _NSIG);
assert(SIGNAL_VALID(signo));
if (!UNIT_VTABLE(u)->kill)
return -EOPNOTSUPP;

View File

@ -1145,8 +1145,7 @@ _public_ int sd_event_add_signal(
int r;
assert_return(e, -EINVAL);
assert_return(sig > 0, -EINVAL);
assert_return(sig < _NSIG, -EINVAL);
assert_return(SIGNAL_VALID(sig), -EINVAL);
assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
assert_return(!event_pid_changed(e), -ECHILD);
@ -2200,7 +2199,7 @@ static int process_signal(sd_event *e, struct signal_data *d, uint32_t events) {
if (_unlikely_(n != sizeof(si)))
return -EIO;
assert(si.ssi_signo < _NSIG);
assert(SIGNAL_VALID(si.ssi_signo));
read_one = true;

View File

@ -28,6 +28,7 @@
#include "logind-session-device.h"
#include "logind-session.h"
#include "logind.h"
#include "signal-util.h"
#include "strv.h"
#include "util.h"
@ -300,7 +301,7 @@ int bus_session_method_kill(sd_bus_message *message, void *userdata, sd_bus_erro
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
}
if (signo <= 0 || signo >= _NSIG)
if (!SIGNAL_VALID(signo))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
r = bus_verify_polkit_async(

View File

@ -25,6 +25,7 @@
#include "formats-util.h"
#include "logind-user.h"
#include "logind.h"
#include "signal-util.h"
#include "strv.h"
#include "user-util.h"
@ -222,7 +223,7 @@ int bus_user_method_kill(sd_bus_message *message, void *userdata, sd_bus_error *
if (r < 0)
return r;
if (signo <= 0 || signo >= _NSIG)
if (!SIGNAL_VALID(signo))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
r = user_kill(u, signo);

View File

@ -46,6 +46,7 @@
#include "mkdir.h"
#include "path-util.h"
#include "process-util.h"
#include "signal-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "user-util.h"
@ -166,7 +167,7 @@ int bus_machine_method_kill(sd_bus_message *message, void *userdata, sd_bus_erro
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid kill parameter '%s'", swho);
}
if (signo <= 0 || signo >= _NSIG)
if (!SIGNAL_VALID(signo))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid signal %i", signo);
r = bus_verify_polkit_async(