1 /* test 10 */ 2 3 #include <sys/types.h> 4 #include <sys/wait.h> 5 #include <errno.h> 6 #include <fcntl.h> 7 #include <stdlib.h> 8 #include <unistd.h> 9 #include <stdio.h> 10 11 char *name[] = {"t10a", "t10b", "t10c", "t10d", "t10e", "t10f", "t10g", 12 "t10h", "t10i", "t10j"}; 13 14 #define PROGBUF_LONGS 3000 15 long prog[PROGBUF_LONGS]; 16 int psize; 17 18 int max_error = 2; 19 #include "common.h" 20 21 22 int main(void); 23 void spawn(int n); 24 void mkfiles(void); 25 void cr_file(char *name, int size); 26 void rmfiles(void); 27 void quit(void); 28 29 int main() 30 { 31 int i, n, pid, r; 32 33 start(10); 34 system("cp ../t10a ."); 35 pid = getpid(); 36 37 /* Create files t10b ... t10h */ 38 mkfiles(); 39 40 if (getpid() == pid) 41 if (fork() == 0) { 42 execl("t10a", "t10a", (char *) 0); 43 exit(0); 44 } 45 if (getpid() == pid) 46 if (fork() == 0) { 47 execl("t10b", "t10b", (char *) 0); 48 exit(0); 49 } 50 if (getpid() == pid) 51 if (fork() == 0) { 52 execl("t10c", "t10c", (char *) 0); 53 exit(0); 54 } 55 if (getpid() == pid) 56 if (fork() == 0) { 57 execl("t10d", "t10d", (char *) 0); 58 exit(0); 59 } 60 61 srand(100); 62 for (i = 0; i < 60; i++) { 63 r = rand() & 07; 64 spawn(r); 65 } 66 67 for (i = 0; i < 4; i++) wait(&n); 68 rmfiles(); 69 quit(); 70 return(-1); /* impossible */ 71 } 72 73 void spawn(n) 74 int n; 75 { 76 int pid; 77 78 if ((pid = fork()) != 0) { 79 wait(&n); /* wait for some child (any one) */ 80 } else { 81 /* a successful exec or a successful detection of a broken executable 82 * is ok 83 */ 84 if(execl(name[n], name[n], (char *) 0) < 0 && errno == ENOEXEC) 85 exit(0); 86 errct++; 87 printf("Child execl didn't take. file=%s errno=%d\n", name[n], errno); 88 rmfiles(); 89 exit(1); 90 printf("Worse yet, EXIT didn't exit\n"); 91 } 92 } 93 94 void mkfiles() 95 { 96 int fd; 97 fd = open("t10a", 0); 98 if (fd < 0) { 99 printf("Can't open t10a\n"); 100 exit(1); 101 } 102 psize = read(fd, (char *) prog, PROGBUF_LONGS * 4); 103 cr_file("t10b", 1600); 104 cr_file("t10c", 1400); 105 cr_file("t10d", 2300); 106 cr_file("t10e", 3100); 107 cr_file("t10f", 2400); 108 cr_file("t10g", 1700); 109 cr_file("t10h", 1500); 110 cr_file("t10i", 4000); 111 cr_file("t10j", 2250); 112 close(fd); 113 } 114 115 void cr_file(name, size) 116 char *name; 117 int size; 118 119 { 120 int fd; 121 122 size += 3000; 123 fd = creat(name, 0755); 124 write(fd, (char *) prog, psize); 125 close(fd); 126 } 127 128 void rmfiles() 129 { 130 unlink("t10b"); 131 unlink("t10c"); 132 unlink("t10d"); 133 unlink("t10e"); 134 unlink("t10f"); 135 unlink("t10g"); 136 unlink("t10h"); 137 unlink("t10i"); 138 unlink("t10j"); 139 } 140 141