robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2002 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* 11/01/2002 Port to LTP robbiew@us.ibm.com */ |
| 21 | /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */ |
| 22 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 23 | /*inode02.c */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 24 | /*====================================================================== |
| 25 | =================== TESTPLAN SEGMENT =================== |
| 26 | CALLS: mkdir, stat, open |
| 27 | |
| 28 | Run with TERM mode. |
| 29 | |
| 30 | >KEYS: < file system and I/O management, system resource constraints. |
| 31 | >WHAT: < Can the system handle a heavy load on the file system I/O |
| 32 | < functions? |
| 33 | >HOW: < Create several identical process that call inode02.c. This |
| 34 | < will simulate the multi-user environment, and hopefully uncover |
| 35 | < conflicts that might occur in "real life" use. |
| 36 | >BUGS: < |
| 37 | ======================================================================*/ |
| 38 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 39 | #define PATH_STRING_LENGTH 1024 |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 40 | #define NAME_LENGTH 8 |
| 41 | #define MAX_PATH_STRING_LENGTH (PATH_STRING_LENGTH - NAME_LENGTH - 40) |
| 42 | #define DIRECTORY_MODE 00777 |
| 43 | #define FILE_MODE 00777 |
| 44 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 45 | #define MKDIR_STRING_LENGTH (MAX_PATH_STRING_LENGTH + 7) |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 46 | |
| 47 | /* #define DEBUG you can watch the generation with this flag */ |
| 48 | |
| 49 | #define TRUE 1 |
| 50 | #define FALSE 0 |
| 51 | #define READ 0 |
| 52 | #define WRITE 1 |
| 53 | |
| 54 | #include <stdio.h> |
robbiew | a70576c | 2003-03-04 18:33:41 +0000 | [diff] [blame] | 55 | #include <errno.h> |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 56 | #include <sys/types.h> |
| 57 | #include <sys/stat.h> |
| 58 | #include <signal.h> |
| 59 | #include <fcntl.h> |
| 60 | #include <errno.h> |
| 61 | #include <wait.h> |
| 62 | |
| 63 | #ifdef LINUX |
| 64 | #include <stdlib.h> |
| 65 | #include <unistd.h> |
| 66 | #include <string.h> |
| 67 | #endif |
| 68 | |
| 69 | #define MAXCHILD 25 |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 70 | int allchild[MAXCHILD + 1]; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 71 | |
| 72 | char name[NAME_LENGTH + 1]; |
| 73 | char path_string[PATH_STRING_LENGTH + 1]; |
| 74 | char read_string[PATH_STRING_LENGTH + 1]; |
| 75 | char write_string[PATH_STRING_LENGTH + 1]; |
robbiew | a010b03 | 2003-04-08 15:07:49 +0000 | [diff] [blame] | 76 | char remove_string[PATH_STRING_LENGTH + 10]; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | int parent_pid; |
| 78 | int nchild; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 79 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | FILE *list_stream = NULL; |
| 81 | int list_id; |
| 82 | int file_id; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 83 | |
| 84 | int increment_name(), get_next_name(), mode(), escrivez(), massmurder(); |
| 85 | int max_depth, max_breadth, file_length; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 86 | int bd_arg(char *); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 87 | |
| 88 | #ifdef LINUX |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 89 | void (*sigset(int, void (*)(int))) (int); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
| 92 | /** LTP Port **/ |
| 93 | #include "test.h" |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 94 | |
| 95 | void setup(void); |
| 96 | void fail_exit(void); |
| 97 | void anyfail(void); |
| 98 | void ok_exit(void); |
| 99 | void forkfail(void); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | void terror(char *); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 101 | int instress(void); |
| 102 | |
| 103 | #define FAILED 0 |
| 104 | #define PASSED 1 |
| 105 | |
| 106 | int local_flag = PASSED; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 107 | FILE *temp; |
| 108 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | char *TCID = "inode02"; /* Test program identifier. */ |
| 110 | int TST_TOTAL = 1; /* Total number of test cases. */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 111 | /**************/ |
| 112 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 113 | int main(argc, argv) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 114 | int argc; |
| 115 | char *argv[]; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 116 | { |
| 117 | int pid, tree(), p, status; |
| 118 | int count, child; |
| 119 | register int i; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 120 | int term(); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 121 | |
| 122 | setup(); |
| 123 | |
| 124 | parent_pid = getpid(); |
| 125 | |
| 126 | if (sigset(SIGTERM, (void (*)())term) == SIG_ERR) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 127 | tst_resm(TBROK, "\tSIGTERM sigset set failed, errno=%d\n", |
| 128 | errno); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 129 | exit(1); |
| 130 | } |
| 131 | |
| 132 | /************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | /* */ |
| 134 | /* Input the parameters for the directory--- */ |
| 135 | /* file trees which are to be generated */ |
| 136 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 137 | /************************************************/ |
| 138 | |
| 139 | if (argc < 2) { |
| 140 | max_depth = 6; |
| 141 | max_breadth = 5; |
| 142 | file_length = 8; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 143 | nchild = 5; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 144 | } else if (argc < 5) { |
| 145 | tst_resm(TCONF, "Bad argument count.\n"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 146 | printf |
| 147 | ("\tinode02 max_depth max_breadth file_length #children\n\tdefault: inode02 6 5 8 5\n"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 148 | exit(1); |
| 149 | } else { |
| 150 | i = 1; |
| 151 | if (sscanf(argv[i++], "%d", &max_depth) != 1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 152 | bd_arg(argv[i - 1]); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 153 | if (sscanf(argv[i++], "%d", &max_breadth) != 1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 154 | bd_arg(argv[i - 1]); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 155 | if (sscanf(argv[i++], "%d", &file_length) != 1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 156 | bd_arg(argv[i - 1]); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 157 | if (sscanf(argv[i++], "%d", &nchild) != 1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 158 | bd_arg(argv[i - 1]); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 159 | if (nchild > MAXCHILD) { |
| 160 | fprintf(temp, "too many children - max is %d\n", |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 161 | MAXCHILD); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 162 | exit(1); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 167 | /* */ |
| 168 | /* Generate and check nchild trees */ |
| 169 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 170 | /************************************************/ |
| 171 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 172 | for (p = 0; p < nchild; p++) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 173 | pid = fork(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 174 | if (pid == 0) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 175 | tree(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 176 | } else { |
| 177 | if (pid < 1) { |
| 178 | terror |
| 179 | ("Fork failed (may be OK if under stress)"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 180 | massmurder(); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 181 | if (instress()) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 182 | ok_exit(); |
| 183 | } |
| 184 | forkfail(); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | count = 0; |
| 190 | while ((child = wait(&status)) > 0) { |
| 191 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 192 | tst_resm(TINFO, "Test %d exited status = %d\n", child, status); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 193 | #endif |
| 194 | if (status) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 195 | fprintf(temp, "Test %d failed - expected 0 exit.\n", |
| 196 | child); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 197 | local_flag = FAILED; |
| 198 | } |
| 199 | count++; |
| 200 | } |
| 201 | |
| 202 | if (count != nchild) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 203 | tst_resm(TFAIL, "Wrong number of children waited on!\n"); |
| 204 | tst_resm(TFAIL, "Saw %d, expected %d\n", count, nchild); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 205 | local_flag = FAILED; |
| 206 | } |
| 207 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 208 | /************************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 209 | /* */ |
| 210 | /* And report the results.......... */ |
| 211 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 212 | /************************************************/ |
| 213 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 214 | anyfail(); |
| 215 | /** NOT REACHED **/ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 216 | tst_exit(); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 217 | } |
| 218 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 219 | int bd_arg(str) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 220 | char *str; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 221 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 222 | fprintf(temp, |
| 223 | "Bad argument - %s - could not parse as number.\n\tinode02 [max_depth] [max_breadth] [file_length] [#children]\n\tdefault: inode02 6 5 8 5\n", |
| 224 | str); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 225 | exit(1); |
| 226 | } |
| 227 | |
| 228 | int tree() |
| 229 | |
| 230 | /************************************************/ |
| 231 | /* */ |
| 232 | /* TREE */ |
| 233 | /* */ |
| 234 | /* generate a tree of directories and files */ |
| 235 | /* and save the path names in the path_list */ |
| 236 | /* file */ |
| 237 | /* */ |
| 238 | /* then, read the path names and attempt to */ |
| 239 | /* access the corresponding directories and */ |
| 240 | /* files */ |
| 241 | /* */ |
| 242 | /************************************************/ |
| 243 | { |
| 244 | int gen_ret_val, ch_ret_val, exit_val, level; |
| 245 | int ret_val; |
| 246 | int generate(), check(); |
robbiew | a010b03 | 2003-04-08 15:07:49 +0000 | [diff] [blame] | 247 | char path_list_string[PATH_STRING_LENGTH + 10]; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 248 | int len; |
| 249 | int status; |
Jan Stancek | 6d427a9 | 2013-03-20 10:44:32 +0100 | [diff] [blame] | 250 | int snp_ret; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 251 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 252 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 253 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 254 | /* make the root directory for */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 255 | /* the tree */ |
| 256 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 257 | /********************************/ |
| 258 | |
| 259 | sprintf(path_string, "inode02.%d", getpid()); |
| 260 | |
| 261 | ret_val = mkdir(path_string, DIRECTORY_MODE); |
| 262 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 263 | if (ret_val == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 264 | tst_resm(TBROK, |
| 265 | "Reason: Impossible to create directory %s, errno=%d\n", |
| 266 | path_string, errno); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 267 | exit(-5); |
| 268 | } |
| 269 | |
| 270 | strcpy(remove_string, "rm -rf "); |
| 271 | strcat(remove_string, path_string); |
| 272 | |
| 273 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 274 | tst_resm(TINFO, "\n%s\n", path_string); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 275 | #endif |
| 276 | |
| 277 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 278 | /* */ |
| 279 | /* create the "path_list" file, in */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 280 | /* which the list of generated paths */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 281 | /* will be stored so that they later */ |
| 282 | /* may be checked */ |
| 283 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 284 | /****************************************/ |
| 285 | |
Jan Stancek | 6d427a9 | 2013-03-20 10:44:32 +0100 | [diff] [blame] | 286 | snp_ret = snprintf(path_list_string, sizeof(path_list_string), |
| 287 | "%s/path_list", path_string); |
| 288 | if (snp_ret < 0 || snp_ret >= sizeof(path_list_string)) { |
| 289 | tst_resm(TBROK, "snprintf(path_list_string,..) returned %d", |
| 290 | snp_ret); |
| 291 | exit(-1); |
| 292 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 293 | list_id = creat(path_list_string, FILE_MODE); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 294 | if (list_id == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 295 | fprintf(temp, |
| 296 | "\nThe path_list file '%s' cannot be created, errno=%d\n", |
| 297 | path_list_string, errno); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 298 | exit(-7); |
| 299 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 300 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 301 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 302 | /* */ |
| 303 | /* and store its name in path_list */ |
| 304 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 305 | /****************************************/ |
| 306 | |
| 307 | strcpy(write_string, path_string); |
| 308 | len = strlen(write_string); |
| 309 | write_string[len++] = 'D'; |
| 310 | write_string[len] = '\0'; |
| 311 | escrivez(write_string); |
| 312 | |
| 313 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 314 | /* */ |
| 315 | /* generate the directory-file tree */ |
| 316 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 317 | /****************************************/ |
| 318 | |
| 319 | level = 0; |
| 320 | |
| 321 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 322 | tst_resm(TINFO, "\n\t%s\n\n", "GENERATING:"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 323 | #endif |
| 324 | |
| 325 | gen_ret_val = generate(path_string, level); |
| 326 | close(list_id); |
| 327 | list_id = open(path_list_string, READ); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 328 | if (list_id == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 329 | fprintf(temp, |
| 330 | "\nThe path_list file cannot be opened for reading, errno=%d\n", |
| 331 | errno); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 332 | exit(-8); |
| 333 | } |
| 334 | list_stream = fdopen(list_id, "r"); |
| 335 | |
| 336 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 337 | /* */ |
| 338 | /* check the directory-file tree */ |
| 339 | /* for correctness */ |
| 340 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 341 | /****************************************/ |
| 342 | |
| 343 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 344 | tst_resm(TINFO, "\n\t%s\n\n", "CHECKING:"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 345 | #endif |
| 346 | |
| 347 | ch_ret_val = check(); |
| 348 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 349 | if (gen_ret_val > ch_ret_val) |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 350 | exit_val = ch_ret_val; |
| 351 | else |
| 352 | exit_val = gen_ret_val; |
| 353 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 354 | status = fclose(list_stream); |
| 355 | if (status != 0) { |
| 356 | fprintf(temp, |
| 357 | "Failed to close list_stream: ret=%d errno=%d (%s)\n", |
| 358 | status, errno, strerror(errno)); |
| 359 | exit(-8); |
| 360 | } |
robbiew | 5c367e4 | 2005-12-23 18:16:06 +0000 | [diff] [blame] | 361 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 362 | /* |
| 363 | * Remove file. |
| 364 | */ |
| 365 | |
| 366 | status = system(remove_string); |
| 367 | if (status) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 368 | fprintf(temp, "Caution - `%s' failed.\n", remove_string); |
| 369 | fprintf(temp, "Status returned %d.\n", status); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 373 | /* */ |
| 374 | /* .....and exit main */ |
| 375 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 376 | /****************************************/ |
| 377 | |
| 378 | exit(exit_val); |
| 379 | } |
| 380 | |
| 381 | int generate(string, level) |
| 382 | |
| 383 | /****************************************/ |
| 384 | /* */ |
| 385 | /* generate recursively a tree of */ |
| 386 | /* directories and files: within */ |
| 387 | /* created directory, an alternating */ |
| 388 | /* series of files and directories */ |
| 389 | /* are constructed---until tree */ |
| 390 | /* breadth and depth limits are */ |
| 391 | /* reached or an error occurs */ |
| 392 | /* */ |
| 393 | /****************************************/ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 394 | /***************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 395 | /* */ |
| 396 | char string[]; /* the directory path */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 397 | /* string below which a */ |
| 398 | /* tree is generated */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 399 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 400 | /***************************/ |
| 401 | |
| 402 | /***************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 403 | /* */ |
| 404 | int level; /* the tree depth variable */ |
| 405 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 406 | /***************************/ |
| 407 | { |
| 408 | int switch_flag; |
| 409 | int ret_val = 0; |
| 410 | int new_ret_val, len, ret_len; |
| 411 | char new_string[PATH_STRING_LENGTH + 1]; |
| 412 | int new_level; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 413 | int i, j; /* iteration counters */ |
Jan Stancek | 6d427a9 | 2013-03-20 10:44:32 +0100 | [diff] [blame] | 414 | int snp_ret; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 415 | |
| 416 | switch_flag = level & TRUE; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 417 | if (strlen(string) >= MAX_PATH_STRING_LENGTH) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 418 | |
| 419 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 420 | /* */ |
| 421 | /* Maximum path name length */ |
| 422 | /* reached */ |
| 423 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 424 | /********************************/ |
| 425 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 426 | fprintf(temp, "\nMaximum path_name length reached\n"); |
| 427 | return (-1); |
| 428 | } else if (level < max_depth) { |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 429 | for (i = 0; i <= max_breadth; i++) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 430 | get_next_name(); |
Jan Stancek | 6d427a9 | 2013-03-20 10:44:32 +0100 | [diff] [blame] | 431 | snp_ret = snprintf(new_string, sizeof(new_string), |
| 432 | "%s/%s", string, name); |
| 433 | if (snp_ret < 0 || snp_ret >= sizeof(new_string)) { |
| 434 | tst_resm(TBROK, "snprintf(new_string,..) " |
| 435 | "returned %d", snp_ret); |
| 436 | exit(-1); |
| 437 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 438 | |
| 439 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 440 | /* */ |
| 441 | /* switch between creating files */ |
| 442 | /* and making directories */ |
| 443 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 444 | /****************************************/ |
| 445 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 446 | if (switch_flag) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 447 | switch_flag = FALSE; |
| 448 | |
| 449 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 450 | /* */ |
| 451 | /* create a new file */ |
| 452 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 453 | /****************************************/ |
| 454 | |
| 455 | file_id = creat(new_string, FILE_MODE); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 456 | if (file_id == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 457 | fprintf(temp, |
| 458 | "\nImpossible to create file %s, errno=%d\n", |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 459 | new_string, errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 460 | return (-2); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 461 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 462 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 463 | tst_resm(TINFO, "%d %s F\n", level, |
| 464 | new_string); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 465 | #endif |
| 466 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 467 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 468 | /* */ |
| 469 | /* write to it */ |
| 470 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 471 | /****************************************/ |
| 472 | |
| 473 | len = strlen(new_string); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 474 | for (j = 1; j <= file_length; j++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 475 | ret_len = |
| 476 | write(file_id, new_string, len); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 477 | if (ret_len != len) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 478 | fprintf(temp, |
| 479 | "\nUnsuccessful write to file %s, errno=%d\n", |
| 480 | new_string, errno); |
| 481 | return (-3); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 482 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 483 | } |
| 484 | close(file_id); |
| 485 | |
| 486 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 487 | /* */ |
| 488 | /* and store its name in path_list */ |
| 489 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 490 | /****************************************/ |
| 491 | |
| 492 | strcpy(write_string, new_string); |
| 493 | len = strlen(write_string); |
| 494 | write_string[len++] = 'F'; |
| 495 | write_string[len] = '\0'; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 496 | escrivez(write_string); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 497 | } else { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 498 | switch_flag = TRUE; |
| 499 | |
| 500 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 501 | /* */ |
| 502 | /* or make a directory */ |
| 503 | /* */ |
| 504 | /* (mknod can only be called when in */ |
| 505 | /* super user mode) */ |
| 506 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 507 | /****************************************/ |
| 508 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 509 | ret_val = mkdir(new_string, DIRECTORY_MODE); |
| 510 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 511 | if (ret_val != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 512 | fprintf(temp, |
| 513 | "\nImpossible to create directory %s, errno=%d\n", |
| 514 | new_string, errno); |
| 515 | return (-5); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 516 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 517 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 518 | tst_resm(TINFO, "%d %s D\n", level, |
| 519 | new_string); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 520 | #endif |
| 521 | |
| 522 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 523 | /* */ |
| 524 | /* store its name in path_list */ |
| 525 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 526 | /****************************************/ |
| 527 | |
| 528 | strcpy(write_string, new_string); |
| 529 | len = strlen(write_string); |
| 530 | write_string[len++] = 'D'; |
| 531 | write_string[len] = '\0'; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 532 | escrivez(write_string); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 533 | |
| 534 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 535 | /* */ |
| 536 | /* and generate a new level */ |
| 537 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 538 | /****************************************/ |
| 539 | |
| 540 | new_level = level + 1; |
| 541 | new_ret_val = generate(new_string, new_level); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 542 | if (new_ret_val < ret_val) |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 543 | ret_val = new_ret_val; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | |
| 547 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 548 | /* */ |
| 549 | /* Maximum breadth reached */ |
| 550 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 551 | /********************************/ |
| 552 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 553 | return (ret_val); |
| 554 | } else |
| 555 | /********************************/ |
| 556 | /* */ |
| 557 | /* Maximum depth reached */ |
| 558 | /* */ |
| 559 | /********************************/ |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 560 | return 0; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 561 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 562 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 563 | int check() |
| 564 | |
| 565 | /****************************************/ |
| 566 | /* */ |
| 567 | /* check for file and directory */ |
| 568 | /* correctness by reading records */ |
| 569 | /* from the path_list and attempting */ |
| 570 | /* to determine if the corresponding */ |
| 571 | /* files or directories are as */ |
| 572 | /* created */ |
| 573 | /* */ |
| 574 | /****************************************/ |
| 575 | { |
| 576 | int len, path_mode, val, ret_len, j; |
| 577 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 578 | for (;;) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 579 | |
| 580 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 581 | /* */ |
| 582 | /* read a path string from path_list */ |
| 583 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 584 | /****************************************/ |
| 585 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 586 | if (fscanf(list_stream, "%s", path_string) == EOF) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 587 | |
| 588 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 589 | tst_resm(TINFO, "\nEnd of path_list file reached \n"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 590 | #endif |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 591 | |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 592 | return 0; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 593 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 594 | #ifdef DEBUG |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 595 | tst_resm(TINFO, "%s\n", path_string); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 596 | #endif |
| 597 | |
| 598 | len = strlen(path_string); |
| 599 | len--; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 600 | if (path_string[len] == 'F') { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 601 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 602 | /********************************/ |
| 603 | /* */ |
| 604 | /* this should be a file */ |
| 605 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 606 | /********************************/ |
| 607 | |
| 608 | path_string[len] = '\0'; |
| 609 | file_id = open(path_string, READ); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 610 | if (file_id <= 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 611 | fprintf(temp, |
| 612 | "\nImpossible to open file %s, errno=%d\n", |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 613 | path_string, errno); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 614 | return (-1); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 615 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 616 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 617 | else { |
| 618 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 619 | /* */ |
| 620 | /* check its contents */ |
| 621 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 622 | /********************************/ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 623 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 624 | ret_len = 0; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 625 | len = strlen(path_string); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 626 | for (j = 1; j <= file_length; j++) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 627 | ret_len = |
| 628 | read(file_id, read_string, len); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 629 | if (len != ret_len) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 630 | fprintf(temp, |
| 631 | "\nFile read error for file %s, errno=%d\n", |
| 632 | path_string, errno); |
| 633 | return (-3); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 634 | } |
| 635 | read_string[len] = '\0'; |
| 636 | val = strcmp(read_string, path_string); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 637 | if (val != 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 638 | fprintf(temp, |
| 639 | "\nContents of file %s are different than expected: %s\n", |
| 640 | path_string, |
| 641 | read_string); |
| 642 | return (-4); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | close(file_id); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 646 | } /* else for */ |
| 647 | if (ret_len <= 0) { |
| 648 | fprintf(temp, |
| 649 | "\nImpossible to read file %s, errno=%d\n", |
| 650 | path_string, errno); |
| 651 | return (-2); |
| 652 | } |
| 653 | } else { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 654 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 655 | /********************************/ |
| 656 | /* */ |
| 657 | /* otherwise.......... */ |
| 658 | /* it should be a directory */ |
| 659 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 660 | /********************************/ |
| 661 | |
| 662 | path_string[len] = '\0'; |
| 663 | path_mode = mode(path_string); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 664 | if (path_mode == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 665 | fprintf(temp, |
| 666 | "\nPreviously created directory path %s was not open\n", |
| 667 | path_string); |
| 668 | return (-4); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 669 | } |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 670 | if ((040000 & path_mode) != 040000) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 671 | fprintf(temp, |
| 672 | "\nPath %s was not recognized to be a directory\n", |
| 673 | path_string); |
| 674 | fprintf(temp, "Its mode is %o\n", path_mode); |
| 675 | return (-5); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 678 | } /* while */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | int get_next_name() |
| 682 | |
| 683 | /****************************************/ |
| 684 | /* */ |
| 685 | /* get the next---in a dictionary */ |
| 686 | /* sense---file or directory name */ |
| 687 | /* */ |
| 688 | /****************************************/ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 689 | { |
| 690 | static int k; |
| 691 | int i; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 692 | int last_position; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 693 | |
| 694 | last_position = NAME_LENGTH - 1; |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 695 | if (k == 0) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 696 | |
| 697 | /************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 698 | /* */ |
| 699 | /* initialize name */ |
| 700 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 701 | /************************/ |
| 702 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 703 | for (i = 0; i < NAME_LENGTH; i++) |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 704 | name[i] = 'a'; |
| 705 | name[NAME_LENGTH] = '\0'; |
| 706 | k++; |
| 707 | } |
| 708 | /********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 709 | /* */ |
| 710 | else |
| 711 | increment_name(last_position); /* i.e., beginning at the last */ |
| 712 | /* position */ |
| 713 | /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 714 | /********************************/ |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 715 | return 0; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 716 | } |
| 717 | |
| 718 | int increment_name(position) |
| 719 | |
| 720 | /****************************************/ |
| 721 | /* */ |
| 722 | /* recursively revise the letters in */ |
| 723 | /* a name to get the lexiographically */ |
| 724 | /* next name */ |
| 725 | /* */ |
| 726 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 727 | int position; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 728 | { |
| 729 | int next_position; |
| 730 | |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 731 | if (name[position] == 'z') |
| 732 | if (position == 0) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 733 | fprintf(temp, |
| 734 | "ERROR: There are no more available names\n"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 735 | exit(-1); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 736 | } else { |
| 737 | name[position] = 'a'; /**********************/ |
| 738 | next_position = --position; /* */ |
| 739 | increment_name(next_position); /* increment the */ |
| 740 | /* previous letter */ |
| 741 | /* */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 742 | /**********************/ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 743 | } |
| 744 | /*********************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 745 | /* */ |
| 746 | else |
| 747 | name[position]++; /* otherwise, increment this one */ |
| 748 | return 0; /* */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 749 | /*********************************/ |
| 750 | } |
| 751 | |
| 752 | int mode(path_string) |
| 753 | |
| 754 | /****************************************/ |
| 755 | /* */ |
| 756 | /* determine and return the mode of */ |
| 757 | /* the file named by path_string */ |
| 758 | /* */ |
| 759 | /****************************************/ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 760 | char path_string[]; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 761 | { |
| 762 | struct stat buf; |
| 763 | int ret_val, mod; |
| 764 | |
| 765 | ret_val = stat(path_string, &buf); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 766 | if (ret_val == -1) |
| 767 | return (-1); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 768 | else { |
| 769 | mod = buf.st_mode; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 770 | return (mod); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
| 774 | int escrivez(string) |
| 775 | |
| 776 | char string[]; |
| 777 | { |
| 778 | char write_string[PATH_STRING_LENGTH + 1]; |
| 779 | int len, ret_len; |
| 780 | |
| 781 | strcpy(write_string, string); |
| 782 | len = strlen(write_string); |
| 783 | write_string[len] = '\n'; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 784 | len++; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 785 | ret_len = write(list_id, write_string, len); |
Garrett Cooper | df3eb16 | 2010-11-28 22:44:32 -0800 | [diff] [blame] | 786 | if (len != ret_len) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 787 | fprintf(temp, |
| 788 | "A string of deviant length %d written to path_list, errno=%d\n", |
| 789 | ret_len, errno); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 790 | exit(-2); |
| 791 | } |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 792 | return 0; |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 793 | } |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 794 | |
| 795 | int term() |
| 796 | { |
| 797 | int status; |
| 798 | |
| 799 | fflush(temp); |
| 800 | if (parent_pid == getpid()) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 801 | massmurder(); /* kill kids */ |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 802 | fprintf(temp, "\term1 - SIGTERM received by parent.\n"); |
| 803 | fflush(temp); |
| 804 | } else { |
| 805 | fprintf(temp, "\tchild - got SIGTERM signal.\n"); |
| 806 | if (list_stream != NULL) |
| 807 | fclose(list_stream); |
| 808 | close(list_id); |
| 809 | close(file_id); |
| 810 | status = system(remove_string); |
| 811 | if (status) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 812 | fprintf(temp, "Caution - ``%s'' returned status %d\n", |
| 813 | remove_string, status); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 814 | } |
| 815 | exit(0); |
| 816 | } |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 817 | return 0; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 818 | } |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 819 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 820 | int massmurder() |
| 821 | { |
| 822 | int i; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 823 | for (i = 0; i < MAXCHILD; i++) { |
| 824 | if (allchild[i]) { |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 825 | kill(allchild[i], SIGTERM); |
| 826 | } |
| 827 | } |
subrata_modak | 43337a3 | 2009-02-26 11:43:51 +0000 | [diff] [blame] | 828 | return 0; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /** LTP Port **/ |
| 832 | /* |
| 833 | * setup |
| 834 | * |
| 835 | * Do set up - here its a dummy function |
| 836 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 837 | void setup() |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 838 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 839 | tst_tmpdir(); |
| 840 | temp = stderr; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 841 | } |
| 842 | |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 843 | /* |
| 844 | * fail_exit() |
| 845 | * |
| 846 | * Exit on failure |
| 847 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 848 | void fail_exit() |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 849 | { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame] | 850 | tst_brkm(TFAIL, tst_rmdir, "Test failed\n"); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | /* |
| 854 | * |
| 855 | * Function: anyfail() |
| 856 | * |
| 857 | * Description: Exit a test. |
| 858 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 859 | void anyfail() |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 860 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 861 | (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed") |
| 862 | : tst_resm(TPASS, "Test passed"); |
| 863 | tst_rmdir(); |
| 864 | tst_exit(); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* |
| 868 | * ok_exit |
| 869 | * |
| 870 | * Calling block passed the test |
| 871 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 872 | void ok_exit() |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 873 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 874 | local_flag = PASSED; |
| 875 | return; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /* |
| 879 | * forkfail() |
| 880 | * |
| 881 | * exit on failure |
| 882 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 883 | void forkfail() |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 884 | { |
Cyril Hrubis | 9fa8ad0 | 2014-12-16 13:20:49 +0100 | [diff] [blame] | 885 | tst_brkm(TBROK, tst_rmdir, "Reason: %s\n", strerror(errno)); |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | /* |
| 889 | * Function: terror |
| 890 | * |
| 891 | * Description: prints error message this may not be because some part of the |
| 892 | * test case failed, for example fork() failed. We will log this |
| 893 | * failure as TBROK instead of TFAIL. |
| 894 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 895 | void terror(char *message) |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 896 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 897 | tst_resm(TBROK, "Reason: %s:%s\n", message, strerror(errno)); |
| 898 | return; |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /* |
| 902 | * instress |
| 903 | * |
| 904 | * Assume that we are always running under stress, so this function will |
| 905 | * return > 0 value always. |
| 906 | */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 907 | int instress() |
robbiew | f0888c0 | 2002-12-04 19:42:27 +0000 | [diff] [blame] | 908 | { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 909 | tst_resm(TINFO, "System resource may be too low, fork() malloc()" |
| 910 | " etc are likely to fail.\n"); |
| 911 | return 1; |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 912 | } |