plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * proc01.c - Tests Linux /proc file reading. |
| 3 | * |
| 4 | * Copyright (C) 2001 Stephane Fillod <f4cfe@free.fr> |
subrata_modak | cf473b7 | 2009-02-05 11:15:57 +0000 | [diff] [blame] | 5 | * Copyright (c) 2008, 2009 Red Hat, Inc. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 6 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of version 2 of the GNU General Public License as |
| 9 | * published by the Free Software Foundation. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 10 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 11 | * This program is distributed in the hope that it would be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 14 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 15 | * Further, this software is distributed without any warranty that it is |
| 16 | * free of the rightful claim of any third person regarding infringement |
| 17 | * or the like. Any license provided herein, whether implied or |
| 18 | * otherwise, applies only to this software file. Patent licenses, if |
| 19 | * any, provided herein do not apply to combinations of this program with |
| 20 | * other software, or any other product whatsoever. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 21 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License along |
| 23 | * with this program; if not, write the Free Software Foundation, Inc., 59 |
| 24 | * Temple Place - Suite 330, Boston MA 02111-1307, USA. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 25 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
subrata_modak | afe343d | 2009-02-05 11:20:49 +0000 | [diff] [blame] | 28 | #include "config.h" |
| 29 | |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <limits.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <dirent.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
subrata_modak | ca317e6 | 2007-11-13 09:25:05 +0000 | [diff] [blame] | 40 | #include <fnmatch.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 41 | |
subrata_modak | 190c7ad | 2009-03-16 08:27:20 +0000 | [diff] [blame] | 42 | #ifdef HAVE_LIBSELINUX_DEVEL |
subrata_modak | afe343d | 2009-02-05 11:20:49 +0000 | [diff] [blame] | 43 | #include <selinux/selinux.h> |
| 44 | #endif |
| 45 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | #include "test.h" |
| 47 | #include "usctest.h" |
| 48 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | #define MAX_BUFF_SIZE 65536 |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 50 | #define MAX_FUNC_NAME 256 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 52 | char *TCID = "proc01"; |
| 53 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | extern int Tst_count; |
| 55 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 56 | static int opt_verbose = 0; |
| 57 | static int opt_procpath = 0; |
| 58 | static char *opt_procpathstr; |
| 59 | static int opt_buffsize = 0; |
| 60 | static int opt_readirq = 0; |
| 61 | static char *opt_buffsizestr; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 63 | static char *procpath = "/proc"; |
| 64 | static char selfpath[] = "/proc/self"; |
| 65 | size_t buffsize = 1024; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 67 | unsigned long long total_read = 0; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 68 | unsigned int total_obj = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 70 | struct mapping { |
| 71 | char func[MAX_FUNC_NAME]; |
| 72 | char file[PATH_MAX]; |
| 73 | int err; |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 74 | }; |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 75 | |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 76 | typedef struct mapping Mapping; |
| 77 | |
| 78 | /* Those are known failures for 2.6.18 baremetal kernel and Xen dom0 |
| 79 | kernel on i686, x86_64, ia64, ppc64 and s390x. In addition, It looks |
| 80 | like if SELinux is disabled, the test may still fail on some other |
| 81 | entries. */ |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 82 | const Mapping known_issues[] = { |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 83 | {"open", "/proc/acpi/event", EBUSY}, |
| 84 | {"open", "/proc/sal/cpe/data", EBUSY}, |
| 85 | {"open", "/proc/sal/cmc/data", EBUSY}, |
| 86 | {"open", "/proc/sal/init/data", EBUSY}, |
| 87 | {"open", "/proc/sal/mca/data", EBUSY}, |
| 88 | {"read", "/proc/kmsg", EAGAIN}, |
| 89 | {"read", "/proc/sal/cpe/event", EAGAIN}, |
| 90 | {"read", "/proc/sal/cmc/event", EAGAIN}, |
| 91 | {"read", "/proc/sal/init/event", EAGAIN}, |
| 92 | {"read", "/proc/sal/mca/event", EAGAIN}, |
| 93 | {"read", "/proc/xen/privcmd", EINVAL}, |
| 94 | {"read", "/proc/self/mem", EIO}, |
| 95 | {"read", "/proc/self/task/[0-9]*/mem", EIO}, |
subrata_modak | cf473b7 | 2009-02-05 11:15:57 +0000 | [diff] [blame] | 96 | {"read", "/proc/self/attr/*", EINVAL}, |
| 97 | {"read", "/proc/self/task/[0-9]*/attr/*", EINVAL}, |
| 98 | {"read", "/proc/ppc64/rtas/error_log", EINVAL}, |
| 99 | {"read", "/proc/fs/nfsd/unlock_filesystem", EINVAL}, |
| 100 | {"read", "/proc/fs/nfsd/unlock_ip", EINVAL}, |
| 101 | {"read", "/proc/fs/nfsd/filehandle", EINVAL}, |
| 102 | {"read", "/proc/fs/nfsd/.getfs", EINVAL}, |
| 103 | {"read", "/proc/fs/nfsd/.getfd", EINVAL}, |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 104 | {"", "", 0} |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 105 | }; |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 106 | |
subrata_modak | 190c7ad | 2009-03-16 08:27:20 +0000 | [diff] [blame] | 107 | /* |
| 108 | * If a particular LSM is enabled, it is expected that some entries can |
| 109 | * be read successfully. Otherwise, those entries will retrun some |
| 110 | * failures listed above. Here to add any LSM specific entries. |
| 111 | */ |
| 112 | |
| 113 | /* |
| 114 | * Test macro to indicate that SELinux libraries and headers are |
| 115 | * installed. |
| 116 | */ |
| 117 | #ifdef HAVE_LIBSELINUX_DEVEL |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 118 | const char lsm_should_work[][PATH_MAX] = { |
subrata_modak | afe343d | 2009-02-05 11:20:49 +0000 | [diff] [blame] | 119 | "/proc/self/attr/*", |
| 120 | "/proc/self/task/[0-9]*/attr/*", |
| 121 | "" |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 122 | }; |
subrata_modak | 190c7ad | 2009-03-16 08:27:20 +0000 | [diff] [blame] | 123 | /* Place holder for none of LSM is detected. */ |
subrata_modak | 13a2142 | 2009-02-16 05:44:04 +0000 | [diff] [blame] | 124 | #else |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 125 | const char lsm_should_work[][PATH_MAX] = { |
subrata_modak | 13a2142 | 2009-02-16 05:44:04 +0000 | [diff] [blame] | 126 | "" |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 127 | }; |
subrata_modak | 13a2142 | 2009-02-16 05:44:04 +0000 | [diff] [blame] | 128 | #endif |
subrata_modak | afe343d | 2009-02-05 11:20:49 +0000 | [diff] [blame] | 129 | |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 130 | /* Known files that does not honor O_NONBLOCK, so they will hang |
subrata_modak | afe343d | 2009-02-05 11:20:49 +0000 | [diff] [blame] | 131 | the test while being read. */ |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 132 | const char error_nonblock[][PATH_MAX] = { |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 133 | "/proc/xen/xenbus", |
| 134 | "" |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 135 | }; |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 136 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 137 | /* |
| 138 | * Verify expected failures, and then let the test to continue. |
| 139 | * |
| 140 | * Return 0 when a problem errno is found. |
| 141 | * Return 1 when a known issue is found. |
| 142 | * |
subrata_modak | 190c7ad | 2009-03-16 08:27:20 +0000 | [diff] [blame] | 143 | */ |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 144 | int found_errno(const char *syscall, const char *obj, int tmperr) |
| 145 | { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 146 | int i; |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 147 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 148 | /* Should not see any error for certain entries if a LSM is enabled. */ |
| 149 | #ifdef HAVE_LIBSELINUX_DEVEL |
| 150 | if (is_selinux_enabled()) { |
| 151 | for (i = 0; lsm_should_work[i][0] != '\0'; i++) { |
| 152 | if (!strcmp(obj, lsm_should_work[i]) || |
| 153 | !fnmatch(lsm_should_work[i], obj, FNM_PATHNAME)) { |
| 154 | return 0; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | #endif |
| 159 | for (i = 0; known_issues[i].err != 0; i++) { |
| 160 | if (tmperr == known_issues[i].err && |
| 161 | (!strcmp(obj, known_issues[i].file) || |
| 162 | !fnmatch(known_issues[i].file, obj, FNM_PATHNAME)) && |
| 163 | !strcmp(syscall, known_issues[i].func)) { |
| 164 | /* Using strcmp / fnmatch could have messed up the |
| 165 | * errno value. */ |
| 166 | errno = tmperr; |
| 167 | tst_resm(TINFO | TERRNO, "%s: known issue", obj); |
| 168 | return 1; |
| 169 | } |
| 170 | } |
| 171 | return 0; |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 172 | } |
| 173 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | void cleanup() |
| 175 | { |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 176 | /* |
| 177 | * remove the tmp directory and exit |
| 178 | */ |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 179 | TEST_CLEANUP; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 180 | tst_rmdir(); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 181 | tst_exit(); |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 182 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void setup() |
| 186 | { |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 187 | /* |
| 188 | * setup a default signal hander and a |
| 189 | * temporary working directory. |
| 190 | */ |
| 191 | tst_sig(FORK, DEF_HANDLER, cleanup); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 192 | TEST_PAUSE; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 193 | tst_tmpdir(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void help() |
| 197 | { |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 198 | printf(" -b x read byte count\n"); |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 199 | printf(" -q read .../irq/... entries\n"); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 200 | printf(" -r x proc pathname\n"); |
| 201 | printf(" -v verbose mode\n"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /* |
| 205 | * add the -m option whose parameter is the |
| 206 | * pages that should be mapped. |
| 207 | */ |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 208 | option_t options[] = { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 209 | { "b:", &opt_buffsize, &opt_buffsizestr}, |
| 210 | { "q", &opt_readirq, NULL }, |
| 211 | { "r:", &opt_procpath, &opt_procpathstr}, |
| 212 | { "v", &opt_verbose, NULL }, |
| 213 | { NULL, NULL, NULL } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 214 | }; |
| 215 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | /* |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 217 | * NB: this function is recursive |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 218 | * returns 0 if no error encountered, otherwise number of errors (objs) |
| 219 | * |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 220 | * REM: Funny enough, while developing this function (actually replacing |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 221 | * streamed fopen by standard open), I hit a real /proc bug. |
| 222 | * On a 2.2.13-SuSE kernel, "cat /proc/tty/driver/serial" would fail |
| 223 | * with EFAULT, while "cat /proc/tty/driver/serial > somefile" wouldn't. |
| 224 | * Okay, this might be due to a slight serial misconfiguration, but still. |
| 225 | * Analysis with strace showed up the difference was on the count size |
| 226 | * of read (1024 bytes vs 4096 bytes). So I tested further.. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 227 | * read count of 512 bytes adds /proc/tty/drivers to the list |
| 228 | * of broken proc files, while 64 bytes reads removes |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 229 | * /proc/tty/driver/serial from the list. Interesting, isn't it? |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | * Now, there's a -b option to this test, so you can try your luck. --SF |
| 231 | * |
| 232 | * It's more fun to run this test it as root, as all the files will be accessible! |
| 233 | * (however, be careful, there might be some bufferoverflow holes..) |
| 234 | * reading proc files might be also a good kernel latency killer. |
| 235 | */ |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 236 | long readproc(const char *obj) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 238 | DIR *dir = NULL; /* pointer to a directory */ |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 239 | struct dirent *dir_ent; /* pointer to directory entries */ |
| 240 | char dirobj[PATH_MAX]; /* object inside directory to modify */ |
| 241 | struct stat statbuf; /* used to hold stat information */ |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 242 | int fd, tmperr, i; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 243 | ssize_t nread; |
| 244 | static char buf[MAX_BUFF_SIZE]; /* static kills reentrancy, but we don't care about the contents */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 245 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 246 | /* Determine the file type */ |
| 247 | if (lstat(obj, &statbuf) < 0) { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 248 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 249 | /* permission denied is not considered as error */ |
| 250 | if (errno != EACCES) { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 251 | tst_resm(TFAIL | TERRNO, "%s: lstat", obj); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 252 | return 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 253 | } |
| 254 | return 0; |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 255 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 256 | } |
| 257 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 258 | /* Prevent loops, but read /proc/self. */ |
subrata_modak | ca317e6 | 2007-11-13 09:25:05 +0000 | [diff] [blame] | 259 | if (S_ISLNK(statbuf.st_mode) && strcmp(obj, selfpath)) |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 260 | return 0; |
| 261 | |
| 262 | total_obj++; |
| 263 | |
| 264 | /* Take appropriate action, depending on the file type */ |
subrata_modak | ca317e6 | 2007-11-13 09:25:05 +0000 | [diff] [blame] | 265 | if (S_ISDIR(statbuf.st_mode) || !strcmp(obj, selfpath)) { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 266 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 267 | /* object is a directory */ |
| 268 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 269 | /* |
| 270 | * Skip over the /proc/irq directory, unless the user |
| 271 | * requested that we read the directory because it could |
| 272 | * map to a broken driver which effectively `hangs' the |
| 273 | * test. |
| 274 | */ |
| 275 | if (!opt_readirq && !strcmp(obj, "irq")) { |
| 276 | return 0; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 277 | /* Open the directory to get access to what is in it */ |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 278 | } else if ((dir = opendir(obj)) == NULL) { |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 279 | if (errno != EACCES) { |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 280 | tst_resm(TFAIL | TERRNO, "%s: opendir", |
| 281 | obj); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 282 | return 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 283 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 284 | return 0; |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 285 | } else { |
| 286 | |
| 287 | long ret_val = 0; |
| 288 | |
| 289 | /* Loop through the entries in the directory */ |
| 290 | for (dir_ent = (struct dirent *)readdir(dir); |
| 291 | dir_ent != NULL; |
| 292 | dir_ent = (struct dirent *)readdir(dir)) { |
| 293 | |
| 294 | /* Ignore ".", "..", "kcore", and |
| 295 | * "/proc/<pid>" (unless this is our |
| 296 | * starting point as directed by the |
| 297 | * user). |
| 298 | */ |
| 299 | if ( strcmp(dir_ent->d_name, ".") && |
| 300 | strcmp(dir_ent->d_name, "..") && |
| 301 | strcmp(dir_ent->d_name, "kcore") && |
| 302 | (fnmatch("[0-9]*", dir_ent->d_name, |
| 303 | FNM_PATHNAME) || |
| 304 | strcmp(obj, procpath))) { |
| 305 | |
| 306 | if (opt_verbose) { |
| 307 | fprintf(stderr, "%s\n", |
| 308 | dir_ent->d_name); |
| 309 | } |
| 310 | |
| 311 | /* Recursively call this routine to test the |
| 312 | * current entry */ |
| 313 | snprintf(dirobj, PATH_MAX, |
| 314 | "%s/%s", obj, |
| 315 | dir_ent->d_name); |
| 316 | ret_val += readproc(dirobj); |
| 317 | |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | |
| 322 | /* Close the directory */ |
| 323 | if (dir) |
| 324 | (void) closedir(dir); |
| 325 | |
| 326 | return ret_val; |
| 327 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 328 | } |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 329 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 330 | } else { /* if it's not a dir, read it! */ |
| 331 | |
| 332 | if (!S_ISREG(statbuf.st_mode)) |
| 333 | return 0; |
| 334 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 335 | #ifdef DEBUG |
| 336 | fprintf(stderr, "%s", obj); |
| 337 | #endif |
subrata_modak | 8a9ecb7 | 2008-05-26 06:09:19 +0000 | [diff] [blame] | 338 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 339 | /* is O_NONBLOCK enough to escape from FIFO's ? */ |
| 340 | if ((fd = open(obj, O_RDONLY | O_NONBLOCK)) < 0) { |
| 341 | |
| 342 | tmperr = errno; |
| 343 | |
| 344 | if (!found_errno("open", obj, tmperr)) { |
| 345 | |
| 346 | errno = tmperr; |
| 347 | |
| 348 | if (errno != EACCES) { |
| 349 | tst_resm(TFAIL | TERRNO, |
| 350 | "%s: open failed", obj); |
| 351 | return 1; |
| 352 | } |
| 353 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 354 | } |
| 355 | return 0; |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 356 | |
| 357 | } |
| 358 | |
| 359 | /* Skip write-only files. */ |
| 360 | if ((statbuf.st_mode & S_IRUSR) == 0 && |
| 361 | (statbuf.st_mode & S_IWUSR) != 0) { |
| 362 | tst_resm(TINFO, "%s: is write-only.", obj); |
| 363 | return 0; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 364 | } |
| 365 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 366 | /* Skip files does not honor O_NONBLOCK. */ |
| 367 | for (i = 0; error_nonblock[i][0] != '\0'; i++) { |
| 368 | if (!strcmp(obj, error_nonblock[i])) { |
| 369 | tst_resm(TWARN, "%s: does not honor " |
| 370 | "O_NONBLOCK", obj); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 371 | return 0; |
| 372 | } |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 373 | } |
| 374 | |
| 375 | do { |
| 376 | |
| 377 | nread = read(fd, buf, buffsize); |
| 378 | |
| 379 | if (nread < 0) { |
| 380 | |
| 381 | tmperr = errno; |
| 382 | (void) close(fd); |
| 383 | |
| 384 | /* ignore no perm (not root) and no |
| 385 | * process (terminated) errors */ |
| 386 | if (!found_errno("read", obj, |
| 387 | tmperr)) { |
| 388 | |
| 389 | errno = tmperr; |
| 390 | |
| 391 | if (errno != EACCES && |
| 392 | errno != ESRCH) { |
| 393 | tst_resm(TFAIL | TERRNO, |
| 394 | "read failed: " |
| 395 | "%s", obj); |
| 396 | return 1; |
| 397 | } |
| 398 | return 0; |
| 399 | |
| 400 | } |
| 401 | |
| 402 | } |
| 403 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 404 | if (opt_verbose) { |
| 405 | #ifdef DEBUG |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 406 | fprintf(stderr, "%ld", nread); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 407 | #endif |
subrata_modak | 2f4de1d | 2008-05-26 06:10:16 +0000 | [diff] [blame] | 408 | fprintf(stderr, "."); |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | total_read += nread; |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 412 | |
| 413 | } while (0 < nread); |
| 414 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 415 | if (opt_verbose) |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 416 | fprintf(stderr, "\n"); |
| 417 | |
| 418 | if (0 <= fd) |
| 419 | (void) close(fd); |
| 420 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 421 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 422 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 423 | /* It's better to assume success by default rather than failure. */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 424 | return 0; |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 425 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 426 | } |
| 427 | |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 428 | int main(int argc, char *argv[]) |
| 429 | { |
| 430 | char *msg; |
| 431 | int lc; |
| 432 | |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 433 | if ((msg = parse_opts(argc, argv, options, help)) != NULL) |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 434 | tst_brkm(TBROK, cleanup, "OPTION PARSING ERROR - %s", msg); |
| 435 | |
| 436 | if (opt_buffsize) { |
| 437 | size_t bs; |
| 438 | bs = atoi(opt_buffsizestr); |
| 439 | if (bs <= MAX_BUFF_SIZE) |
| 440 | buffsize = bs; |
| 441 | else |
| 442 | tst_brkm(TBROK, cleanup, |
| 443 | "Invalid arg for -b (max: %u): %s", |
| 444 | MAX_BUFF_SIZE, opt_buffsizestr); |
| 445 | } |
| 446 | |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 447 | if (opt_procpath) |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 448 | procpath = opt_procpathstr; |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 449 | |
| 450 | setup(); |
| 451 | |
| 452 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 453 | Tst_count = 0; |
| 454 | |
| 455 | TEST(readproc(procpath)); |
| 456 | |
| 457 | if (TEST_RETURN != 0) { |
subrata_modak | da124b9 | 2009-10-13 14:00:45 +0000 | [diff] [blame] | 458 | tst_resm(TFAIL, "readproc() failed with %ld errors.", |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 459 | TEST_RETURN); |
| 460 | } else { |
| 461 | tst_resm(TPASS, "readproc() completed successfully, " |
yaberauneya | da96356 | 2009-11-15 08:21:45 +0000 | [diff] [blame^] | 462 | "total read: %llu bytes, %u objs", total_read, |
vapier | 10fe5d8 | 2006-08-21 07:16:54 +0000 | [diff] [blame] | 463 | total_obj); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | cleanup(); |
| 468 | return 0; |
| 469 | } |