Adjust the caps client/server test code to match new CAPS features. The
[dragonfly.git] / test / caps / client.c
1 /*
2  * $DragonFly: src/test/caps/client.c,v 1.2 2004/03/06 22:15:00 dillon Exp $
3  */
4 #include <sys/types.h>
5 #include <sys/time.h>
6 #include <sys/caps.h>
7 #include <sys/errno.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <unistd.h>
12
13 int
14 main(int ac, char **av)
15 {
16     int cid;
17     int n;
18     int count = 0;
19     int lostit = 0;
20     int didfork = 0;
21     int which = 0;
22     char buf[256];
23     struct caps_msgid msgid;
24     off_t msgcid;
25     caps_gen_t gen = 0;
26     caps_gen_t ngen;
27
28     cid = caps_sys_client("test", getuid(), getgid(), 0, CAPF_ANYCLIENT);
29     for (;;) {
30         errno = 0;
31         if (cid >= 0) {
32             msgcid = caps_sys_put(cid, "xyz", 3);
33             ngen = caps_sys_getgen(cid);
34         }
35         if (cid < 0 || (msgcid < 0 && errno == ENOTCONN)) {
36             if (lostit == 0) {
37                 if (didfork) {
38                     didfork = 0;
39                     printf("%d client forked, reconnecting to service\n", which);
40                 } else {
41                     printf("%d client lost connection, reconnecting\n", which);
42                 }
43             }
44             lostit = 1;
45             caps_sys_close(cid);
46             cid = caps_sys_client("test", getuid(), getgid(), 0, 
47                         CAPF_ANYCLIENT | CAPF_WAITSVC);
48             continue;
49         }
50         if (lostit) {
51             printf("%d client resume on reconnect after lost connection\n", which);
52             lostit = 0;
53         }
54         if (ngen != gen) {
55             printf("%d client: note generation change %lld\n", which, ngen);
56             gen = ngen;
57         }
58 #ifdef DEBUG
59         printf("msgcid = %016llx %d\n", msgcid, errno);
60 #endif
61         n = caps_sys_wait(cid, buf, sizeof(buf), &msgid, NULL);
62 #ifdef DEBUG
63         printf("n = %d msgid=%016llx state=%d errno=%d\n", n, msgid.c_id, msgid.c_state, errno);
64         if (n > 0)
65             printf("REPLY: %*.*s\n", n, n, buf);
66 #endif
67         ++count;
68         if ((count & 65535) == 0)
69             printf("%d %d\n", which, count);
70         if (count == 1000 && which == 0) {
71             if (fork() == 0) {
72                 usleep(100000);
73                 didfork = 1;
74                 which = 1;
75             } else {
76                 printf("forked pid %d\n", (int)getpid());
77             }
78         }
79     }
80     return(0);
81 }
82