f20dea47d01541a958e60c62e3391241b89f4f09
[openafs.git] / src / lwp / test / selsubs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* selsubs.c - common code for client and server. */
11 #include <unistd.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <sys/select.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <string.h>
20 #include <sys/time.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <signal.h>
24 #include <sys/ioctl.h>
25 #include <assert.h>
26 #include <sys/stat.h>
27
28 #include <afsconfig.h>
29 #include <afs/param.h>
30
31
32
33 #include "lwp.h"
34 #include "seltest.h"
35
36 #ifdef NEEDS_ALLOCFDSET
37 /* Include these if testing against 32 bit fd_set IOMGR. */
38 fd_set *
39 IOMGR_AllocFDSet(void)
40 {
41     fd_set *tmp = (fd_set *) malloc(sizeof(fd_set));
42     memset(tmp, 0, sizeof(fd_set));
43     return tmp;
44 }
45
46 void
47 IOMGR_FreeFDSet(fd_set * fds)
48 {
49     free((char *)fds);
50 }
51 #endif
52
53 /* The TCP spec calls for writing at least one byte of OOB data which is
54  * read by the receiver using recv with the MSG_OOB flag set.
55  */
56 void
57 sendOOB(int fd)
58 {
59     char c = (char)1;
60
61     Log("Sending OOB.\n");
62     if (send(fd, &c, 1, MSG_OOB) < 0) {
63         Die(1, "sendOOB");
64     }
65 }
66
67 void
68 recvOOB(int fd)
69 {
70     char c;
71
72     Log("Received OOB\n");
73     if (recv(fd, &c, 1, MSG_OOB) < 0) {
74         Die(1, "recvOOB");
75     }
76     Log("Handled OOB\n");
77 }
78
79 void
80 assertNullFDSet(int fd, fd_set * fds)
81 {
82     int i;
83     int n = sizeof(*fds) / sizeof(int);
84     int *j = (int *)fds;
85
86     if (fd >= 0)
87         FD_CLR(fd, fds);
88
89     for (i = 0; i < n; i++)
90         assert(j[i] == 0);
91 }
92
93 /* OpenFDs
94  *
95  * Open file descriptors until file descriptor n or higher is returned.
96  */
97 #include <sys/stat.h>
98 void
99 OpenFDs(n)
100      int n;
101 {
102     int i;
103     struct stat sbuf;
104     int fd, lfd;
105
106     lfd = -1;
107     for (i = 0; i < n; i++) {
108         if (fstat(i, &sbuf) == 0)
109             continue;
110         if ((fd = open("/dev/null", 0, 0)) < 0) {
111             if (lfd >= 0) {
112                 close(lfd);
113                 return;
114             }
115         } else {
116             if (fd >= n) {
117                 close(fd);
118                 return;
119             } else {
120                 lfd = fd;
121             }
122         }
123     }
124 }
125
126 /* If flag is set, abort. */
127 void
128 Die(int flag, char *msg)
129 {
130     char tmp[1024];
131     extern char *program;
132
133     (void)sprintf(tmp, "%s: %s: ", program ? program : "", msg);
134     perror(tmp);
135     fflush(stderr);
136     if (flag)
137         abort();
138     else
139         exit(1);
140 }
141
142
143
144 void
145 Log(char *fmt, ...)
146 {
147     va_list args;
148     struct timeval now;
149     struct tm *ltime;
150     time_t tt;
151     int code;
152     PROCESS pid;
153     extern char *program;
154
155     code = gettimeofday(&now, NULL);
156     assert(code == 0);
157
158     tt = now.tv_sec;
159     ltime = localtime(&tt);
160
161     LWP_CurrentProcess(&pid);
162     fprintf(stderr, "%s 0x%x %02d:%02d:%02d.%d: ", program ? program : "",
163             pid, ltime->tm_hour, ltime->tm_min, ltime->tm_sec, now.tv_usec);
164
165     va_start(args, fmt);
166
167     vfprintf(stderr, fmt, args);
168     fflush(stdout);
169     va_end(args);
170 }