Import Debian changes 0.66.4-9
[hcoop/debian/courier-authlib.git] / authdaemontest.c
1 /*
2 ** Copyright 2000 Double Precision, Inc. See COPYING for
3 ** distribution information.
4 */
5
6 #include "courier_auth_config.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #if HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include "authwait.h"
20
21
22 static int runtest(int count, char **argv)
23 {
24 int i;
25 pid_t p;
26 int waitstat;
27 int x;
28
29 for (i=0; i<count; i++)
30 {
31 p=fork();
32 if (p == -1)
33 {
34 perror("fork");
35 return (1);
36 }
37 if (p == 0)
38 {
39 execv(argv[0], argv);
40 perror("exec");
41 exit(1);
42 }
43
44 while (wait(&waitstat) != p)
45 ;
46 if (WIFEXITED(waitstat))
47 x=WEXITSTATUS(waitstat);
48 else
49 x=1;
50 if (x)
51 return (1);
52 }
53 return (0);
54 }
55
56 static int cleanup()
57 {
58 int waitstat;
59 int rc=0;
60 int x;
61
62 while (wait(&waitstat) >= 0 || errno != ECHILD)
63 {
64 x=1;
65 if (WIFEXITED(waitstat))
66 x=WEXITSTATUS(waitstat);
67 if (x)
68 rc=1;
69 }
70 return (rc);
71 }
72
73 static int dotest(int nchildren, int count, char **argv)
74 {
75 pid_t p;
76 int i;
77
78 signal(SIGCHLD, SIG_DFL);
79
80 for (i=0; i<nchildren; i++)
81 {
82 p=fork();
83 if (p == -1)
84 {
85 perror("fork");
86 cleanup();
87 return (1);
88 }
89 if (p == 0)
90 {
91 close(1);
92 if (open("/dev/null", O_WRONLY) != 1)
93 {
94 perror("open");
95 exit(1);
96 }
97 exit(runtest(count, argv));
98 }
99 }
100
101 return (cleanup());
102 }
103
104 int main(int argc, char **argv)
105 {
106 if (argc >= 4)
107 {
108 int nchildren=atoi(argv[1]);
109 int count=atoi(argv[2]);
110
111 if (nchildren > 0 && count > 0)
112 exit(dotest(nchildren, count, argv+3));
113 }
114
115 fprintf(stderr, "Usage: authdaemontest [nchildren] [count] ./authtest [userid] [password]\n");
116 exit(1);
117 return (1);
118 }