Merge from TNG.
[tprouty/samba.git] / testsuite / nsswitch / getpwent_r.c
1 /*
2  * Use set/get/endpwent calls from two processes to iterate over the
3  * password database.  This checks the multithreaded stuff works.
4  */
5
6 #include <stdio.h>
7 #include <pwd.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <wait.h>
13
14 void dump_pwent(char *id)
15 {
16     struct passwd *pw;
17     char fname[255];
18     FILE *fptr;
19
20     /* Open results file */
21
22     sprintf(fname, "/tmp/getpwent_r-%s.out-%d", id, getpid());
23
24     if ((fptr = fopen(fname, "w")) < 0) {
25         fprintf(stderr, "ERROR: could not open file %s: %s\n", fname,
26                 sys_errlist[errno]);
27         return;
28     }
29
30     /* Dump passwd database */
31
32     setpwent();
33         
34     while((pw = getpwent()) != NULL) {
35        fprintf(fptr,"%s:%s:%s:%d:%d\n", pw->pw_name, pw->pw_passwd,
36                pw->pw_gecos, pw->pw_uid, pw->pw_gid);
37     }
38         
39     endpwent();
40
41     /* Close results file */
42
43     fclose(fptr);
44 }
45
46 #define NUM_FORKS 2
47
48 int main(int argc, char **argv)
49 {
50     pid_t pids[NUM_FORKS];
51     int i, status;
52
53     /* Check args */
54
55     if (argc != 2) {
56         printf("ERROR: must specify output file identifier\n");
57         return 1;
58     }
59
60     for(i = 0; i < NUM_FORKS; i++) {
61
62         /* Fork off lots */
63
64         if ((pids[i] = fork()) == -1) {
65             perror("fork");
66             return 1;
67         }
68
69         /* Child does tests */
70
71         if (pids[i] == 0) {
72             dump_pwent(argv[1]);
73             return 0;
74         }
75     }
76
77     /* Wait for everyone to finish */
78
79     for (i = 0; i < NUM_FORKS; i++) {
80         waitpid(pids[i], &status, 0);
81     }
82
83     printf("PASS: getpwent_r.c\n");
84     return 0;
85 }