~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/pidfd/pidfd_open_test.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 
  3 #define _GNU_SOURCE
  4 #include <errno.h>
  5 #include <fcntl.h>
  6 #include <inttypes.h>
  7 #include <limits.h>
  8 #include <linux/types.h>
  9 #include <sched.h>
 10 #include <signal.h>
 11 #include <stdbool.h>
 12 #include <stdio.h>
 13 #include <stdlib.h>
 14 #include <string.h>
 15 #include <syscall.h>
 16 #include <sys/mount.h>
 17 #include <sys/prctl.h>
 18 #include <sys/wait.h>
 19 #include <unistd.h>
 20 
 21 #include "pidfd.h"
 22 #include "../kselftest.h"
 23 
 24 static int safe_int(const char *numstr, int *converted)
 25 {
 26         char *err = NULL;
 27         long sli;
 28 
 29         errno = 0;
 30         sli = strtol(numstr, &err, 0);
 31         if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
 32                 return -ERANGE;
 33 
 34         if (errno != 0 && sli == 0)
 35                 return -EINVAL;
 36 
 37         if (err == numstr || *err != '\0')
 38                 return -EINVAL;
 39 
 40         if (sli > INT_MAX || sli < INT_MIN)
 41                 return -ERANGE;
 42 
 43         *converted = (int)sli;
 44         return 0;
 45 }
 46 
 47 static int char_left_gc(const char *buffer, size_t len)
 48 {
 49         size_t i;
 50 
 51         for (i = 0; i < len; i++) {
 52                 if (buffer[i] == ' ' ||
 53                     buffer[i] == '\t')
 54                         continue;
 55 
 56                 return i;
 57         }
 58 
 59         return 0;
 60 }
 61 
 62 static int char_right_gc(const char *buffer, size_t len)
 63 {
 64         int i;
 65 
 66         for (i = len - 1; i >= 0; i--) {
 67                 if (buffer[i] == ' '  ||
 68                     buffer[i] == '\t' ||
 69                     buffer[i] == '\n' ||
 70                     buffer[i] == '\0')
 71                         continue;
 72 
 73                 return i + 1;
 74         }
 75 
 76         return 0;
 77 }
 78 
 79 static char *trim_whitespace_in_place(char *buffer)
 80 {
 81         buffer += char_left_gc(buffer, strlen(buffer));
 82         buffer[char_right_gc(buffer, strlen(buffer))] = '\0';
 83         return buffer;
 84 }
 85 
 86 static pid_t get_pid_from_fdinfo_file(int pidfd, const char *key, size_t keylen)
 87 {
 88         int ret;
 89         char path[512];
 90         FILE *f;
 91         size_t n = 0;
 92         pid_t result = -1;
 93         char *line = NULL;
 94 
 95         snprintf(path, sizeof(path), "/proc/self/fdinfo/%d", pidfd);
 96 
 97         f = fopen(path, "re");
 98         if (!f)
 99                 return -1;
100 
101         while (getline(&line, &n, f) != -1) {
102                 char *numstr;
103 
104                 if (strncmp(line, key, keylen))
105                         continue;
106 
107                 numstr = trim_whitespace_in_place(line + 4);
108                 ret = safe_int(numstr, &result);
109                 if (ret < 0)
110                         goto out;
111 
112                 break;
113         }
114 
115 out:
116         free(line);
117         fclose(f);
118         return result;
119 }
120 
121 int main(int argc, char **argv)
122 {
123         int pidfd = -1, ret = 1;
124         pid_t pid;
125 
126         ksft_set_plan(3);
127 
128         pidfd = sys_pidfd_open(-1, 0);
129         if (pidfd >= 0) {
130                 ksft_print_msg(
131                         "%s - succeeded to open pidfd for invalid pid -1\n",
132                         strerror(errno));
133                 goto on_error;
134         }
135         ksft_test_result_pass("do not allow invalid pid test: passed\n");
136 
137         pidfd = sys_pidfd_open(getpid(), 1);
138         if (pidfd >= 0) {
139                 ksft_print_msg(
140                         "%s - succeeded to open pidfd with invalid flag value specified\n",
141                         strerror(errno));
142                 goto on_error;
143         }
144         ksft_test_result_pass("do not allow invalid flag test: passed\n");
145 
146         pidfd = sys_pidfd_open(getpid(), 0);
147         if (pidfd < 0) {
148                 ksft_print_msg("%s - failed to open pidfd\n", strerror(errno));
149                 goto on_error;
150         }
151         ksft_test_result_pass("open a new pidfd test: passed\n");
152 
153         pid = get_pid_from_fdinfo_file(pidfd, "Pid:", sizeof("Pid:") - 1);
154         ksft_print_msg("pidfd %d refers to process with pid %d\n", pidfd, pid);
155 
156         ret = 0;
157 
158 on_error:
159         if (pidfd >= 0)
160                 close(pidfd);
161 
162         if (ret)
163                 ksft_exit_fail();
164         ksft_exit_pass();
165 }
166 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php