chiark / gitweb /
basic: introduce *_to_string_with_check() functions
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 29 Dec 2017 08:03:54 +0000 (17:03 +0900)
committerSven Eden <yamakuzure@gmx.net>
Wed, 30 May 2018 05:49:49 +0000 (07:49 +0200)
They are used in later commits.

src/basic/errno-list.h
src/basic/parse-util.c
src/basic/process-util.h
src/basic/signal-util.h

index acd70950d8bf91e824fbb152c35f614bf1e75a46..0f87161605e409317bd39473f54e141a38e570fb 100644 (file)
@@ -20,6 +20,7 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+//#include <stdbool.h>
 /*
  * MAX_ERRNO is defined as 4095 in linux/err.h
  * We use the same value here.
@@ -30,3 +31,6 @@ const char *errno_to_name(int id);
 int errno_from_name(const char *name);
 #if 0 /// UNNEEDED by elogind
 #endif // 0
+static inline bool errno_is_valid(int n) {
+        return n > 0 && n <= ERRNO_MAX;
+}
index 37f6a08c36eff8267e29a6dc71ba167b7ba03955..ea902ed96e929939fa58424ac6b5f8c0fbb4e0c6 100644 (file)
@@ -288,7 +288,8 @@ int parse_errno(const char *t) {
         if (r < 0)
                 return r;
 
-        if (e < 0 || e > ERRNO_MAX)
+        /* 0 is also allowed here */
+        if (!errno_is_valid(e) && e != 0)
                 return -ERANGE;
 
         return e;
index 9128d0b2c5a1d448c4120c6d64753e9bff6c8520..efefbee4ad91738a6fd75c961e98de4bea576208 100644 (file)
@@ -149,6 +149,13 @@ static inline bool pid_is_valid(pid_t p) {
         return p > 0;
 }
 
+static inline int sched_policy_to_string_alloc_with_check(int n, char **s) {
+        if (!sched_policy_is_valid(n))
+                return -EINVAL;
+
+        return sched_policy_to_string_alloc(n, s);
+}
+
 #if 0 /// UNNEEDED by elogind
 int ioprio_parse_priority(const char *s, int *ret);
 #endif // 0
index 2343ca80f88cf8d3d16fb6e516f5d37832fb72e0..a66699179cf9340adc3b4ec02bd8dd50e747fe9a 100644 (file)
@@ -59,3 +59,10 @@ static inline void block_signals_reset(sigset_t *ss) {
 static inline bool SIGNAL_VALID(int signo) {
         return signo > 0 && signo < _NSIG;
 }
+
+static inline const char* signal_to_string_with_check(int n) {
+        if (!SIGNAL_VALID(n))
+                return NULL;
+
+        return signal_to_string(n);
+}