Build courier-authlib 0.59.3-1hcoop1.
[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 static const char rcsid[]="$Id: authdaemontest.c,v 1.2 2004/10/21 00:10:49 mrsam Exp $";
22
23 static int runtest(int count, char **argv)
24 {
25 int i;
26 pid_t p;
27 int waitstat;
28 int x;
29
30 for (i=0; i<count; i++)
31 {
32 p=fork();
33 if (p == -1)
34 {
35 perror("fork");
36 return (1);
37 }
38 if (p == 0)
39 {
40 execv(argv[0], argv);
41 perror("exec");
42 exit(1);
43 }
44
45 while (wait(&waitstat) != p)
46 ;
47 if (WIFEXITED(waitstat))
48 x=WEXITSTATUS(waitstat);
49 else
50 x=1;
51 if (x)
52 return (1);
53 }
54 return (0);
55 }
56
57 static int cleanup()
58 {
59 int waitstat;
60 int rc=0;
61 int x;
62
63 while (wait(&waitstat) >= 0 || errno != ECHILD)
64 {
65 x=1;
66 if (WIFEXITED(waitstat))
67 x=WEXITSTATUS(waitstat);
68 if (x)
69 rc=1;
70 }
71 return (rc);
72 }
73
74 static int dotest(int nchildren, int count, char **argv)
75 {
76 pid_t p;
77 int i;
78
79 signal(SIGCHLD, SIG_DFL);
80
81 for (i=0; i<nchildren; i++)
82 {
83 p=fork();
84 if (p == -1)
85 {
86 perror("fork");
87 cleanup();
88 return (1);
89 }
90 if (p == 0)
91 {
92 close(1);
93 if (open("/dev/null", O_WRONLY) != 1)
94 {
95 perror("open");
96 exit(1);
97 }
98 exit(runtest(count, argv));
99 }
100 }
101
102 return (cleanup());
103 }
104
105 int main(int argc, char **argv)
106 {
107 if (argc >= 4)
108 {
109 int nchildren=atoi(argv[1]);
110 int count=atoi(argv[2]);
111
112 if (nchildren > 0 && count > 0)
113 exit(dotest(nchildren, count, argv+3));
114 }
115
116 fprintf(stderr, "Usage: authdaemontest [nchildren] [count] ./authtest [userid] [password]\n");
117 exit(1);
118 return (1);
119 }