blob: 0e42085f08b190156a96c3faab9c3de637989207 [file] [log] [blame]
robbiewf0888c02002-12-04 19:42:27 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewf0888c02002-12-04 19:42:27 +000018 */
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 Gao354ebb42012-12-07 10:10:04 +080023 /*inode02.c */
robbiewf0888c02002-12-04 19:42:27 +000024/*======================================================================
25 =================== TESTPLAN SEGMENT ===================
26CALLS: 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_modak4bb656a2009-02-26 12:02:09 +000039#define PATH_STRING_LENGTH 1024
robbiewf0888c02002-12-04 19:42:27 +000040#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_modak4bb656a2009-02-26 12:02:09 +000045#define MKDIR_STRING_LENGTH (MAX_PATH_STRING_LENGTH + 7)
robbiewf0888c02002-12-04 19:42:27 +000046
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>
robbiewa70576c2003-03-04 18:33:41 +000055#include <errno.h>
robbiewf0888c02002-12-04 19:42:27 +000056#include <sys/types.h>
57#include <sys/stat.h>
58#include <signal.h>
59#include <fcntl.h>
60#include <errno.h>
Steven Jackson249f4052016-12-13 16:16:00 +000061#include <sys/wait.h>
robbiewf0888c02002-12-04 19:42:27 +000062
63#ifdef LINUX
64#include <stdlib.h>
65#include <unistd.h>
66#include <string.h>
67#endif
68
69#define MAXCHILD 25
Wanlong Gao354ebb42012-12-07 10:10:04 +080070int allchild[MAXCHILD + 1];
robbiewf0888c02002-12-04 19:42:27 +000071
72char name[NAME_LENGTH + 1];
73char path_string[PATH_STRING_LENGTH + 1];
74char read_string[PATH_STRING_LENGTH + 1];
75char write_string[PATH_STRING_LENGTH + 1];
robbiewa010b032003-04-08 15:07:49 +000076char remove_string[PATH_STRING_LENGTH + 10];
Wanlong Gao354ebb42012-12-07 10:10:04 +080077int parent_pid;
78int nchild;
robbiewf0888c02002-12-04 19:42:27 +000079
Wanlong Gao354ebb42012-12-07 10:10:04 +080080FILE *list_stream = NULL;
81int list_id;
82int file_id;
robbiewf0888c02002-12-04 19:42:27 +000083
84int increment_name(), get_next_name(), mode(), escrivez(), massmurder();
85int max_depth, max_breadth, file_length;
Wanlong Gao354ebb42012-12-07 10:10:04 +080086int bd_arg(char *);
robbiewf0888c02002-12-04 19:42:27 +000087
88#ifdef LINUX
Wanlong Gao354ebb42012-12-07 10:10:04 +080089void (*sigset(int, void (*)(int))) (int);
robbiewf0888c02002-12-04 19:42:27 +000090#endif
91
92/** LTP Port **/
93#include "test.h"
robbiewf0888c02002-12-04 19:42:27 +000094
95void setup(void);
96void fail_exit(void);
97void anyfail(void);
98void ok_exit(void);
99void forkfail(void);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100void terror(char *);
robbiewf0888c02002-12-04 19:42:27 +0000101int instress(void);
102
103#define FAILED 0
104#define PASSED 1
105
106int local_flag = PASSED;
robbiewf0888c02002-12-04 19:42:27 +0000107FILE *temp;
108
Wanlong Gao354ebb42012-12-07 10:10:04 +0800109char *TCID = "inode02"; /* Test program identifier. */
110int TST_TOTAL = 1; /* Total number of test cases. */
robbiewf0888c02002-12-04 19:42:27 +0000111/**************/
112
robbiewf0888c02002-12-04 19:42:27 +0000113int main(argc, argv)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800114int argc;
115char *argv[];
robbiewf0888c02002-12-04 19:42:27 +0000116{
117 int pid, tree(), p, status;
118 int count, child;
119 register int i;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800120 int term();
robbiewf0888c02002-12-04 19:42:27 +0000121
122 setup();
123
124 parent_pid = getpid();
125
126 if (sigset(SIGTERM, (void (*)())term) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 tst_resm(TBROK, "\tSIGTERM sigset set failed, errno=%d\n",
128 errno);
robbiewf0888c02002-12-04 19:42:27 +0000129 exit(1);
130 }
131
132 /************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 /* */
134 /* Input the parameters for the directory--- */
135 /* file trees which are to be generated */
136 /* */
robbiewf0888c02002-12-04 19:42:27 +0000137 /************************************************/
138
139 if (argc < 2) {
140 max_depth = 6;
141 max_breadth = 5;
142 file_length = 8;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800143 nchild = 5;
robbiewf0888c02002-12-04 19:42:27 +0000144 } else if (argc < 5) {
145 tst_resm(TCONF, "Bad argument count.\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800146 printf
147 ("\tinode02 max_depth max_breadth file_length #children\n\tdefault: inode02 6 5 8 5\n");
robbiewf0888c02002-12-04 19:42:27 +0000148 exit(1);
149 } else {
150 i = 1;
151 if (sscanf(argv[i++], "%d", &max_depth) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 bd_arg(argv[i - 1]);
robbiewf0888c02002-12-04 19:42:27 +0000153 if (sscanf(argv[i++], "%d", &max_breadth) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 bd_arg(argv[i - 1]);
robbiewf0888c02002-12-04 19:42:27 +0000155 if (sscanf(argv[i++], "%d", &file_length) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800156 bd_arg(argv[i - 1]);
robbiewf0888c02002-12-04 19:42:27 +0000157 if (sscanf(argv[i++], "%d", &nchild) != 1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 bd_arg(argv[i - 1]);
robbiewf0888c02002-12-04 19:42:27 +0000159 if (nchild > MAXCHILD) {
160 fprintf(temp, "too many children - max is %d\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +0800161 MAXCHILD);
robbiewf0888c02002-12-04 19:42:27 +0000162 exit(1);
163 }
164 }
165
166 /************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800167 /* */
168 /* Generate and check nchild trees */
169 /* */
robbiewf0888c02002-12-04 19:42:27 +0000170 /************************************************/
171
Wanlong Gao354ebb42012-12-07 10:10:04 +0800172 for (p = 0; p < nchild; p++) {
robbiewf0888c02002-12-04 19:42:27 +0000173 pid = fork();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800174 if (pid == 0) {
robbiewf0888c02002-12-04 19:42:27 +0000175 tree();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800176 } else {
177 if (pid < 1) {
178 terror
179 ("Fork failed (may be OK if under stress)");
robbiewf0888c02002-12-04 19:42:27 +0000180 massmurder();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 if (instress()) {
robbiewf0888c02002-12-04 19:42:27 +0000182 ok_exit();
183 }
184 forkfail();
185 }
186 }
187 }
188
189 count = 0;
190 while ((child = wait(&status)) > 0) {
191#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800192 tst_resm(TINFO, "Test %d exited status = %d\n", child, status);
robbiewf0888c02002-12-04 19:42:27 +0000193#endif
194 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800195 fprintf(temp, "Test %d failed - expected 0 exit.\n",
196 child);
robbiewf0888c02002-12-04 19:42:27 +0000197 local_flag = FAILED;
198 }
199 count++;
200 }
201
202 if (count != nchild) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 tst_resm(TFAIL, "Wrong number of children waited on!\n");
204 tst_resm(TFAIL, "Saw %d, expected %d\n", count, nchild);
robbiewf0888c02002-12-04 19:42:27 +0000205 local_flag = FAILED;
206 }
207
robbiewf0888c02002-12-04 19:42:27 +0000208 /************************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800209 /* */
210 /* And report the results.......... */
211 /* */
robbiewf0888c02002-12-04 19:42:27 +0000212 /************************************************/
213
robbiewf0888c02002-12-04 19:42:27 +0000214 anyfail();
215 /** NOT REACHED **/
Garrett Cooper2c282152010-12-16 00:55:50 -0800216 tst_exit();
robbiewf0888c02002-12-04 19:42:27 +0000217}
218
robbiewf0888c02002-12-04 19:42:27 +0000219int bd_arg(str)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800220char *str;
robbiewf0888c02002-12-04 19:42:27 +0000221{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 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);
robbiewf0888c02002-12-04 19:42:27 +0000225 exit(1);
226}
227
228int 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();
robbiewa010b032003-04-08 15:07:49 +0000247 char path_list_string[PATH_STRING_LENGTH + 10];
robbiewf0888c02002-12-04 19:42:27 +0000248 int len;
249 int status;
Jan Stancek6d427a92013-03-20 10:44:32 +0100250 int snp_ret;
robbiewf0888c02002-12-04 19:42:27 +0000251
robbiewf0888c02002-12-04 19:42:27 +0000252 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800253 /* */
robbiewf0888c02002-12-04 19:42:27 +0000254 /* make the root directory for */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800255 /* the tree */
256 /* */
robbiewf0888c02002-12-04 19:42:27 +0000257 /********************************/
258
259 sprintf(path_string, "inode02.%d", getpid());
260
261 ret_val = mkdir(path_string, DIRECTORY_MODE);
262
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800263 if (ret_val == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800264 tst_resm(TBROK,
265 "Reason: Impossible to create directory %s, errno=%d\n",
266 path_string, errno);
robbiewf0888c02002-12-04 19:42:27 +0000267 exit(-5);
268 }
269
270 strcpy(remove_string, "rm -rf ");
271 strcat(remove_string, path_string);
272
273#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800274 tst_resm(TINFO, "\n%s\n", path_string);
robbiewf0888c02002-12-04 19:42:27 +0000275#endif
276
277 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800278 /* */
279 /* create the "path_list" file, in */
robbiewf0888c02002-12-04 19:42:27 +0000280 /* which the list of generated paths */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800281 /* will be stored so that they later */
282 /* may be checked */
283 /* */
robbiewf0888c02002-12-04 19:42:27 +0000284 /****************************************/
285
Jan Stancek6d427a92013-03-20 10:44:32 +0100286 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 }
robbiewf0888c02002-12-04 19:42:27 +0000293 list_id = creat(path_list_string, FILE_MODE);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800294 if (list_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800295 fprintf(temp,
296 "\nThe path_list file '%s' cannot be created, errno=%d\n",
297 path_list_string, errno);
robbiewf0888c02002-12-04 19:42:27 +0000298 exit(-7);
299 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000300
robbiewf0888c02002-12-04 19:42:27 +0000301 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800302 /* */
303 /* and store its name in path_list */
304 /* */
robbiewf0888c02002-12-04 19:42:27 +0000305 /****************************************/
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 Gao354ebb42012-12-07 10:10:04 +0800314 /* */
315 /* generate the directory-file tree */
316 /* */
robbiewf0888c02002-12-04 19:42:27 +0000317 /****************************************/
318
319 level = 0;
320
321#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800322 tst_resm(TINFO, "\n\t%s\n\n", "GENERATING:");
robbiewf0888c02002-12-04 19:42:27 +0000323#endif
324
325 gen_ret_val = generate(path_string, level);
326 close(list_id);
327 list_id = open(path_list_string, READ);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800328 if (list_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800329 fprintf(temp,
330 "\nThe path_list file cannot be opened for reading, errno=%d\n",
331 errno);
robbiewf0888c02002-12-04 19:42:27 +0000332 exit(-8);
333 }
334 list_stream = fdopen(list_id, "r");
335
336 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800337 /* */
338 /* check the directory-file tree */
339 /* for correctness */
340 /* */
robbiewf0888c02002-12-04 19:42:27 +0000341 /****************************************/
342
343#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800344 tst_resm(TINFO, "\n\t%s\n\n", "CHECKING:");
robbiewf0888c02002-12-04 19:42:27 +0000345#endif
346
347 ch_ret_val = check();
348
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800349 if (gen_ret_val > ch_ret_val)
robbiewf0888c02002-12-04 19:42:27 +0000350 exit_val = ch_ret_val;
351 else
352 exit_val = gen_ret_val;
353
Wanlong Gao354ebb42012-12-07 10:10:04 +0800354 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 }
robbiew5c367e42005-12-23 18:16:06 +0000361
robbiewf0888c02002-12-04 19:42:27 +0000362 /*
363 * Remove file.
364 */
365
366 status = system(remove_string);
367 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800368 fprintf(temp, "Caution - `%s' failed.\n", remove_string);
369 fprintf(temp, "Status returned %d.\n", status);
robbiewf0888c02002-12-04 19:42:27 +0000370 }
371
372 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800373 /* */
374 /* .....and exit main */
375 /* */
robbiewf0888c02002-12-04 19:42:27 +0000376 /****************************************/
377
378 exit(exit_val);
379}
380
381int 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/****************************************/
robbiewf0888c02002-12-04 19:42:27 +0000394 /***************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800395 /* */
396char string[]; /* the directory path */
robbiewf0888c02002-12-04 19:42:27 +0000397 /* string below which a */
398 /* tree is generated */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800399 /* */
robbiewf0888c02002-12-04 19:42:27 +0000400 /***************************/
401
402 /***************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800403 /* */
404int level; /* the tree depth variable */
405 /* */
robbiewf0888c02002-12-04 19:42:27 +0000406 /***************************/
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 Gao354ebb42012-12-07 10:10:04 +0800413 int i, j; /* iteration counters */
Jan Stancek6d427a92013-03-20 10:44:32 +0100414 int snp_ret;
robbiewf0888c02002-12-04 19:42:27 +0000415
416 switch_flag = level & TRUE;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800417 if (strlen(string) >= MAX_PATH_STRING_LENGTH) {
robbiewf0888c02002-12-04 19:42:27 +0000418
419 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800420 /* */
421 /* Maximum path name length */
422 /* reached */
423 /* */
robbiewf0888c02002-12-04 19:42:27 +0000424 /********************************/
425
Wanlong Gao354ebb42012-12-07 10:10:04 +0800426 fprintf(temp, "\nMaximum path_name length reached\n");
427 return (-1);
428 } else if (level < max_depth) {
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800429 for (i = 0; i <= max_breadth; i++) {
robbiewf0888c02002-12-04 19:42:27 +0000430 get_next_name();
Jan Stancek6d427a92013-03-20 10:44:32 +0100431 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 }
robbiewf0888c02002-12-04 19:42:27 +0000438
439 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800440 /* */
441 /* switch between creating files */
442 /* and making directories */
443 /* */
robbiewf0888c02002-12-04 19:42:27 +0000444 /****************************************/
445
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800446 if (switch_flag) {
robbiewf0888c02002-12-04 19:42:27 +0000447 switch_flag = FALSE;
448
449 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800450 /* */
451 /* create a new file */
452 /* */
robbiewf0888c02002-12-04 19:42:27 +0000453 /****************************************/
454
455 file_id = creat(new_string, FILE_MODE);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800456 if (file_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800457 fprintf(temp,
458 "\nImpossible to create file %s, errno=%d\n",
Garrett Cooper2c282152010-12-16 00:55:50 -0800459 new_string, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800460 return (-2);
robbiewf0888c02002-12-04 19:42:27 +0000461 }
robbiewf0888c02002-12-04 19:42:27 +0000462#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800463 tst_resm(TINFO, "%d %s F\n", level,
464 new_string);
robbiewf0888c02002-12-04 19:42:27 +0000465#endif
466
robbiewf0888c02002-12-04 19:42:27 +0000467 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800468 /* */
469 /* write to it */
470 /* */
robbiewf0888c02002-12-04 19:42:27 +0000471 /****************************************/
472
473 len = strlen(new_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800474 for (j = 1; j <= file_length; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800475 ret_len =
476 write(file_id, new_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800477 if (ret_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800478 fprintf(temp,
479 "\nUnsuccessful write to file %s, errno=%d\n",
480 new_string, errno);
481 return (-3);
Garrett Cooper2c282152010-12-16 00:55:50 -0800482 }
robbiewf0888c02002-12-04 19:42:27 +0000483 }
484 close(file_id);
485
486 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800487 /* */
488 /* and store its name in path_list */
489 /* */
robbiewf0888c02002-12-04 19:42:27 +0000490 /****************************************/
491
492 strcpy(write_string, new_string);
493 len = strlen(write_string);
494 write_string[len++] = 'F';
495 write_string[len] = '\0';
subrata_modakbdbaec52009-02-26 12:14:51 +0000496 escrivez(write_string);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800497 } else {
robbiewf0888c02002-12-04 19:42:27 +0000498 switch_flag = TRUE;
499
500 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800501 /* */
502 /* or make a directory */
503 /* */
504 /* (mknod can only be called when in */
505 /* super user mode) */
506 /* */
robbiewf0888c02002-12-04 19:42:27 +0000507 /****************************************/
508
robbiewf0888c02002-12-04 19:42:27 +0000509 ret_val = mkdir(new_string, DIRECTORY_MODE);
510
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800511 if (ret_val != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800512 fprintf(temp,
513 "\nImpossible to create directory %s, errno=%d\n",
514 new_string, errno);
515 return (-5);
robbiewf0888c02002-12-04 19:42:27 +0000516 }
robbiewf0888c02002-12-04 19:42:27 +0000517#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800518 tst_resm(TINFO, "%d %s D\n", level,
519 new_string);
robbiewf0888c02002-12-04 19:42:27 +0000520#endif
521
522 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800523 /* */
524 /* store its name in path_list */
525 /* */
robbiewf0888c02002-12-04 19:42:27 +0000526 /****************************************/
527
528 strcpy(write_string, new_string);
529 len = strlen(write_string);
530 write_string[len++] = 'D';
531 write_string[len] = '\0';
subrata_modakbdbaec52009-02-26 12:14:51 +0000532 escrivez(write_string);
robbiewf0888c02002-12-04 19:42:27 +0000533
534 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800535 /* */
536 /* and generate a new level */
537 /* */
robbiewf0888c02002-12-04 19:42:27 +0000538 /****************************************/
539
540 new_level = level + 1;
541 new_ret_val = generate(new_string, new_level);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800542 if (new_ret_val < ret_val)
subrata_modakbdbaec52009-02-26 12:14:51 +0000543 ret_val = new_ret_val;
robbiewf0888c02002-12-04 19:42:27 +0000544 }
545 }
546
547 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800548 /* */
549 /* Maximum breadth reached */
550 /* */
robbiewf0888c02002-12-04 19:42:27 +0000551 /********************************/
552
Wanlong Gao354ebb42012-12-07 10:10:04 +0800553 return (ret_val);
554 } else
555 /********************************/
556 /* */
557 /* Maximum depth reached */
558 /* */
559 /********************************/
subrata_modak43337a32009-02-26 11:43:51 +0000560 return 0;
subrata_modak4bb656a2009-02-26 12:02:09 +0000561}
Wanlong Gao354ebb42012-12-07 10:10:04 +0800562
robbiewf0888c02002-12-04 19:42:27 +0000563int 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 Cooperdf3eb162010-11-28 22:44:32 -0800578 for (;;) {
robbiewf0888c02002-12-04 19:42:27 +0000579
580 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800581 /* */
582 /* read a path string from path_list */
583 /* */
robbiewf0888c02002-12-04 19:42:27 +0000584 /****************************************/
585
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800586 if (fscanf(list_stream, "%s", path_string) == EOF) {
robbiewf0888c02002-12-04 19:42:27 +0000587
588#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800589 tst_resm(TINFO, "\nEnd of path_list file reached \n");
robbiewf0888c02002-12-04 19:42:27 +0000590#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800591
subrata_modak43337a32009-02-26 11:43:51 +0000592 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000593 }
robbiewf0888c02002-12-04 19:42:27 +0000594#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800595 tst_resm(TINFO, "%s\n", path_string);
robbiewf0888c02002-12-04 19:42:27 +0000596#endif
597
598 len = strlen(path_string);
599 len--;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800600 if (path_string[len] == 'F') {
robbiewf0888c02002-12-04 19:42:27 +0000601
Wanlong Gao354ebb42012-12-07 10:10:04 +0800602 /********************************/
603 /* */
604 /* this should be a file */
605 /* */
robbiewf0888c02002-12-04 19:42:27 +0000606 /********************************/
607
608 path_string[len] = '\0';
609 file_id = open(path_string, READ);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800610 if (file_id <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800611 fprintf(temp,
612 "\nImpossible to open file %s, errno=%d\n",
robbiewf0888c02002-12-04 19:42:27 +0000613 path_string, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800614 return (-1);
robbiewf0888c02002-12-04 19:42:27 +0000615 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800616
robbiewf0888c02002-12-04 19:42:27 +0000617 else {
618 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800619 /* */
620 /* check its contents */
621 /* */
robbiewf0888c02002-12-04 19:42:27 +0000622 /********************************/
Garrett Cooper2c282152010-12-16 00:55:50 -0800623
Wanlong Gao354ebb42012-12-07 10:10:04 +0800624 ret_len = 0;
robbiewf0888c02002-12-04 19:42:27 +0000625 len = strlen(path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800626 for (j = 1; j <= file_length; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800627 ret_len =
628 read(file_id, read_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800629 if (len != ret_len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800630 fprintf(temp,
631 "\nFile read error for file %s, errno=%d\n",
632 path_string, errno);
633 return (-3);
robbiewf0888c02002-12-04 19:42:27 +0000634 }
635 read_string[len] = '\0';
636 val = strcmp(read_string, path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800637 if (val != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800638 fprintf(temp,
639 "\nContents of file %s are different than expected: %s\n",
640 path_string,
641 read_string);
642 return (-4);
robbiewf0888c02002-12-04 19:42:27 +0000643 }
644 }
645 close(file_id);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800646 } /* 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 {
robbiewf0888c02002-12-04 19:42:27 +0000654
Wanlong Gao354ebb42012-12-07 10:10:04 +0800655 /********************************/
656 /* */
657 /* otherwise.......... */
658 /* it should be a directory */
659 /* */
robbiewf0888c02002-12-04 19:42:27 +0000660 /********************************/
661
662 path_string[len] = '\0';
663 path_mode = mode(path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800664 if (path_mode == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800665 fprintf(temp,
666 "\nPreviously created directory path %s was not open\n",
667 path_string);
668 return (-4);
robbiewf0888c02002-12-04 19:42:27 +0000669 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800670 if ((040000 & path_mode) != 040000) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800671 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);
robbiewf0888c02002-12-04 19:42:27 +0000676 }
677 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800678 } /* while */
robbiewf0888c02002-12-04 19:42:27 +0000679}
680
681int get_next_name()
682
683/****************************************/
684/* */
685/* get the next---in a dictionary */
686/* sense---file or directory name */
687/* */
688/****************************************/
robbiewf0888c02002-12-04 19:42:27 +0000689{
690 static int k;
691 int i;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800692 int last_position;
robbiewf0888c02002-12-04 19:42:27 +0000693
694 last_position = NAME_LENGTH - 1;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800695 if (k == 0) {
robbiewf0888c02002-12-04 19:42:27 +0000696
697 /************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800698 /* */
699 /* initialize name */
700 /* */
robbiewf0888c02002-12-04 19:42:27 +0000701 /************************/
702
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800703 for (i = 0; i < NAME_LENGTH; i++)
robbiewf0888c02002-12-04 19:42:27 +0000704 name[i] = 'a';
705 name[NAME_LENGTH] = '\0';
706 k++;
707 }
708 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800709 /* */
710 else
711 increment_name(last_position); /* i.e., beginning at the last */
712 /* position */
713 /* */
robbiewf0888c02002-12-04 19:42:27 +0000714 /********************************/
subrata_modak43337a32009-02-26 11:43:51 +0000715 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000716}
717
718int 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 Gao354ebb42012-12-07 10:10:04 +0800727 int position;
robbiewf0888c02002-12-04 19:42:27 +0000728{
729 int next_position;
730
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800731 if (name[position] == 'z')
732 if (position == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800733 fprintf(temp,
734 "ERROR: There are no more available names\n");
robbiewf0888c02002-12-04 19:42:27 +0000735 exit(-1);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800736 } else {
737 name[position] = 'a'; /**********************/
738 next_position = --position; /* */
739 increment_name(next_position); /* increment the */
740 /* previous letter */
741 /* */
subrata_modakbdbaec52009-02-26 12:14:51 +0000742 /**********************/
robbiewf0888c02002-12-04 19:42:27 +0000743 }
744 /*********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800745 /* */
746 else
747 name[position]++; /* otherwise, increment this one */
748 return 0; /* */
robbiewf0888c02002-12-04 19:42:27 +0000749 /*********************************/
750}
751
752int mode(path_string)
753
754/****************************************/
755/* */
756/* determine and return the mode of */
757/* the file named by path_string */
758/* */
759/****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800760 char path_string[];
robbiewf0888c02002-12-04 19:42:27 +0000761{
762 struct stat buf;
763 int ret_val, mod;
764
765 ret_val = stat(path_string, &buf);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800766 if (ret_val == -1)
767 return (-1);
robbiewf0888c02002-12-04 19:42:27 +0000768 else {
769 mod = buf.st_mode;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800770 return (mod);
robbiewf0888c02002-12-04 19:42:27 +0000771 }
772}
773
774int escrivez(string)
775
776char 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 Gao354ebb42012-12-07 10:10:04 +0800784 len++;
robbiewf0888c02002-12-04 19:42:27 +0000785 ret_len = write(list_id, write_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800786 if (len != ret_len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800787 fprintf(temp,
788 "A string of deviant length %d written to path_list, errno=%d\n",
789 ret_len, errno);
robbiewf0888c02002-12-04 19:42:27 +0000790 exit(-2);
791 }
subrata_modak43337a32009-02-26 11:43:51 +0000792 return 0;
subrata_modak4bb656a2009-02-26 12:02:09 +0000793}
robbiewf0888c02002-12-04 19:42:27 +0000794
795int term()
796{
797 int status;
798
799 fflush(temp);
800 if (parent_pid == getpid()) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800801 massmurder(); /* kill kids */
robbiewf0888c02002-12-04 19:42:27 +0000802 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 Gao354ebb42012-12-07 10:10:04 +0800812 fprintf(temp, "Caution - ``%s'' returned status %d\n",
813 remove_string, status);
robbiewf0888c02002-12-04 19:42:27 +0000814 }
815 exit(0);
816 }
subrata_modak43337a32009-02-26 11:43:51 +0000817 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000818}
Wanlong Gao354ebb42012-12-07 10:10:04 +0800819
robbiewf0888c02002-12-04 19:42:27 +0000820int massmurder()
821{
822 int i;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800823 for (i = 0; i < MAXCHILD; i++) {
824 if (allchild[i]) {
robbiewf0888c02002-12-04 19:42:27 +0000825 kill(allchild[i], SIGTERM);
826 }
827 }
subrata_modak43337a32009-02-26 11:43:51 +0000828 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000829}
830
831/** LTP Port **/
832/*
833 * setup
834 *
835 * Do set up - here its a dummy function
836 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800837void setup()
robbiewf0888c02002-12-04 19:42:27 +0000838{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800839 tst_tmpdir();
840 temp = stderr;
robbiewf0888c02002-12-04 19:42:27 +0000841}
842
robbiewf0888c02002-12-04 19:42:27 +0000843/*
844 * fail_exit()
845 *
846 * Exit on failure
847 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800848void fail_exit()
robbiewf0888c02002-12-04 19:42:27 +0000849{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100850 tst_brkm(TFAIL, tst_rmdir, "Test failed\n");
robbiewf0888c02002-12-04 19:42:27 +0000851}
852
853/*
854 *
855 * Function: anyfail()
856 *
857 * Description: Exit a test.
858 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800859void anyfail()
robbiewf0888c02002-12-04 19:42:27 +0000860{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800861 (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
862 : tst_resm(TPASS, "Test passed");
863 tst_rmdir();
864 tst_exit();
robbiewf0888c02002-12-04 19:42:27 +0000865}
866
867/*
868 * ok_exit
869 *
870 * Calling block passed the test
871 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800872void ok_exit()
robbiewf0888c02002-12-04 19:42:27 +0000873{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800874 local_flag = PASSED;
875 return;
robbiewf0888c02002-12-04 19:42:27 +0000876}
877
878/*
879 * forkfail()
880 *
881 * exit on failure
882 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800883void forkfail()
robbiewf0888c02002-12-04 19:42:27 +0000884{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100885 tst_brkm(TBROK, tst_rmdir, "Reason: %s\n", strerror(errno));
robbiewf0888c02002-12-04 19:42:27 +0000886}
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 Gao354ebb42012-12-07 10:10:04 +0800895void terror(char *message)
robbiewf0888c02002-12-04 19:42:27 +0000896{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800897 tst_resm(TBROK, "Reason: %s:%s\n", message, strerror(errno));
898 return;
robbiewf0888c02002-12-04 19:42:27 +0000899}
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 Gao354ebb42012-12-07 10:10:04 +0800907int instress()
robbiewf0888c02002-12-04 19:42:27 +0000908{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800909 tst_resm(TINFO, "System resource may be too low, fork() malloc()"
910 " etc are likely to fail.\n");
911 return 1;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700912}