VM: simplify slab allocator
[minix.git] / test / common.c
blob8e78336f47cf8df9f31bf420dcc05de663da69a0
1 /* Utility routines for Minix tests.
2 * This is designed to be #includ'ed near the top of test programs. It is
3 * self-contained except for MAX_ERRORS.
4 */
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <sys/statvfs.h>
13 int common_test_nr = -1, errct = 0, subtest;
15 void cleanup(void);
16 int does_fs_truncate(void);
17 void e(int n);
18 int name_max(char *path);
19 void quit(void);
20 void rm_rf_dir(int test_nr);
21 void rm_rf_ppdir(int test_nr);
22 void start(int test_nr);
24 void start(test_nr)
25 int test_nr;
27 char buf[64];
28 int i;
30 common_test_nr = test_nr;
31 printf("Test %2d ", test_nr);
32 fflush(stdout); /* since stdout is probably line buffered */
33 sync();
34 rm_rf_dir(test_nr);
35 sprintf(buf, "mkdir DIR_%02d", test_nr);
36 if (system(buf) != 0) {
37 e(666);
38 quit();
40 sprintf(buf, "DIR_%02d", test_nr);
41 if (chdir(buf) != 0) {
42 e(6666);
43 quit();
46 for (i = 3; i < OPEN_MAX; ++i) {
47 /* Close all files except stdin, stdout, and stderr */
48 (void) close(i);
52 int does_fs_truncate(void)
54 struct statvfs stvfs;
55 int does_truncate = 0;
56 char cwd[PATH_MAX]; /* Storage for path to current working dir */
58 if (realpath(".", cwd) == NULL) e(7777); /* Get current working dir */
59 if (statvfs(cwd, &stvfs) != 0) e(7778); /* Get FS information */
60 /* Depending on how an FS handles too long file names, we have to adjust our
61 * error checking. If an FS does not truncate file names, it should generate
62 * an ENAMETOOLONG error when we provide too long a file name.
64 if (!(stvfs.f_flag & ST_NOTRUNC)) does_truncate = 1;
66 return(does_truncate);
69 int name_max(char *path)
71 struct statvfs stvfs;
73 if (statvfs(path, &stvfs) != 0) e(7779);
74 return(stvfs.f_namemax);
78 void rm_rf_dir(test_nr)
79 int test_nr;
81 char buf[128];
83 /* "rm -rf dir" will not work unless all the subdirectories have suitable
84 * permissions. Minix chmod is not recursive so it is not easy to change
85 * all the permissions. I had to fix opendir() to stop the bash shell
86 * from hanging when it opendir()s fifos.
88 sprintf(buf, "chmod 777 DIR_%02d DIR_%02d/* DIR_%02d/*/* >/dev/null 2>&1",
89 test_nr, test_nr, test_nr);
90 (void) system(buf); /* usually fails */
91 sprintf(buf, "rm -rf DIR_%02d >/dev/null 2>&1", test_nr);
92 if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
95 void rm_rf_ppdir(test_nr)
96 int test_nr;
98 /* Attempt to remove everything in the test directory (== the current dir). */
100 char buf[128];
102 sprintf(buf, "chmod 777 ../DIR_%02d/* ../DIR_%02d/*/* >/dev/null 2>&1",
103 test_nr, test_nr);
104 (void) system(buf);
105 sprintf(buf, "rm -rf ../DIR_%02d >/dev/null 2>&1", test_nr);
106 if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
109 void e(n)
110 int n;
112 int err_number;
113 err_number = errno; /* Store before printf can clobber it */
114 if (errct == 0) printf("\n"); /* finish header */
115 printf("Subtest %d, error %d, errno %d: %s\n",
116 subtest, n, errno, strerror(errno));
117 if (errct++ > MAX_ERROR) {
118 printf("Too many errors; test aborted\n");
119 cleanup();
120 exit(1);
122 errno = err_number;
125 void cleanup()
127 if (chdir("..") == 0 && common_test_nr != -1) rm_rf_dir(common_test_nr);
130 void quit()
132 cleanup();
133 if (errct == 0) {
134 printf("ok\n");
135 exit(0);
136 } else {
137 printf("%d errors\n", errct);
138 exit(1);