]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib: (pidutils) add a routine to parse pids and err() on failure
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 13 Jan 2026 20:18:27 +0000 (15:18 -0500)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Fri, 23 Jan 2026 18:53:34 +0000 (13:53 -0500)
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
include/pidutils.h
lib/pidutils.c

index b2c575c190b60bd8a580f17066c00a1a3b28701b..979ec0590e8e65862f7c85da1d8c6713f987fddb 100644 (file)
@@ -10,5 +10,6 @@
 #include <sys/types.h>
 
 extern int ul_parse_pid_str(char *pidstr, pid_t *pid_num, uint64_t *pfd_ino);
+extern void ul_parse_pid_str_or_err(char *pidstr, pid_t *pid_num, uint64_t *pfd_ino);
 
 #endif /* UTIL_LINUX_PIDUTILS_H */
index 3998816d6b97afcf801d998cd6c3e649d8809228..245cbb452feec02934f173bb2db75128a3fbd9fd 100644 (file)
@@ -8,6 +8,7 @@
 #include <sys/stat.h>
 #include <errno.h>
 
+#include "nls.h"
 #include "strutils.h"
 #include "pidutils.h"
 
@@ -62,3 +63,22 @@ int ul_parse_pid_str(char *pidstr, pid_t *pid_num, uint64_t *pfd_ino)
        }
        return 0;
 }
+
+/*
+ * ul_parse_pid_str_or_err() - Parse a string and store the found pid
+ *                             and pidfd inode, or exit on error.
+ *
+ * @pidstr:  string in format `pid[:pidfd_inode]` that is to be parsed
+ * @pid_num: stores pid number
+ * @pfd_ino: stores pidfd inode number
+ *
+ * If @pfd_ino is not destined to be set, pass it as NULL.
+ *
+ * On failure, err() is called with an error message to indicate the issue.
+ */
+void ul_parse_pid_str_or_err(char *pidstr, pid_t *pid_num, uint64_t *pfd_ino)
+{
+       if (ul_parse_pid_str(pidstr, pid_num, pfd_ino) < 0) {
+               err(EXIT_FAILURE, N_("failed to parse PID argument '%s'"), pidstr);
+       }
+}