2009-04-25 04:58:15 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 2009 , Nicolas François
|
2009-04-25 04:58:15 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2009-04-25 04:58:15 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#ident "$Id$"
|
|
|
|
|
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
2023-02-07 10:19:42 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2009-04-25 04:58:15 +05:30
|
|
|
|
|
|
|
int get_pid (const char *pidstr, pid_t *pid)
|
|
|
|
{
|
|
|
|
long long int val;
|
|
|
|
char *endptr;
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoll (pidstr, &endptr, 10);
|
|
|
|
if ( ('\0' == *pidstr)
|
|
|
|
|| ('\0' != *endptr)
|
|
|
|
|| (ERANGE == errno)
|
2023-05-12 17:29:47 +05:30
|
|
|
|| (val < 1)
|
2009-05-01 02:42:33 +05:30
|
|
|
|| (/*@+longintegral@*/val != (pid_t)val)/*@=longintegral@*/) {
|
2009-04-25 04:58:15 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-01 18:20:48 +05:30
|
|
|
*pid = val;
|
2009-04-25 04:58:15 +05:30
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-02-07 10:19:42 +05:30
|
|
|
/*
|
|
|
|
* If use passed in fd:4 as an argument, then return the
|
|
|
|
* value '4', the fd to use.
|
2023-02-25 01:22:32 +05:30
|
|
|
* On error, return -1.
|
2023-02-07 10:19:42 +05:30
|
|
|
*/
|
|
|
|
int get_pidfd_from_fd(const char *pidfdstr)
|
|
|
|
{
|
|
|
|
long long int val;
|
|
|
|
char *endptr;
|
2023-02-25 02:36:02 +05:30
|
|
|
struct stat st;
|
|
|
|
dev_t proc_st_dev, proc_st_rdev;
|
2023-02-07 10:19:42 +05:30
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
val = strtoll (pidfdstr, &endptr, 10);
|
|
|
|
if ( ('\0' == *pidfdstr)
|
|
|
|
|| ('\0' != *endptr)
|
|
|
|
|| (ERANGE == errno)
|
2023-05-12 17:29:47 +05:30
|
|
|
|| (val < 0)
|
|
|
|
|| (/*@+longintegral@*/val != (int)val)/*@=longintegral@*/) {
|
2023-02-25 01:22:32 +05:30
|
|
|
return -1;
|
2023-02-07 10:19:42 +05:30
|
|
|
}
|
|
|
|
|
2023-02-25 02:36:02 +05:30
|
|
|
if (stat("/proc/self/uid_map", &st) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_st_dev = st.st_dev;
|
|
|
|
proc_st_rdev = st.st_rdev;
|
|
|
|
|
|
|
|
if (fstat(val, &st) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (st.st_dev != proc_st_dev || st.st_rdev != proc_st_rdev) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-02-07 10:19:42 +05:30
|
|
|
return (int)val;
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_pidfd(const char *pidstr)
|
|
|
|
{
|
|
|
|
int proc_dir_fd;
|
|
|
|
int written;
|
|
|
|
char proc_dir_name[32];
|
|
|
|
pid_t target;
|
|
|
|
|
|
|
|
if (get_pid(pidstr, &target) == 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
/* max string length is 6 + 10 + 1 + 1 = 18, allocate 32 bytes */
|
|
|
|
written = snprintf(proc_dir_name, sizeof(proc_dir_name), "/proc/%u/",
|
|
|
|
target);
|
|
|
|
if ((written <= 0) || ((size_t)written >= sizeof(proc_dir_name))) {
|
|
|
|
fprintf(stderr, "snprintf of proc path failed for %u: %s\n",
|
|
|
|
target, strerror(errno));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
proc_dir_fd = open(proc_dir_name, O_DIRECTORY);
|
|
|
|
if (proc_dir_fd < 0) {
|
|
|
|
fprintf(stderr, _("Could not open proc directory for target %u: %s\n"),
|
|
|
|
target, strerror(errno));
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
return proc_dir_fd;
|
|
|
|
}
|