Import Upstream version 1.8.5
[hcoop/debian/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 <afsconfig.h>
12 #include <afs/param.h>
13
14
15 #include <unistd.h>
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <errno.h>
20 #include <sys/select.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <string.h>
24 #include <sys/time.h>
25 #include <netinet/in.h>
26 #include <netdb.h>
27 #include <signal.h>
28 #include <sys/ioctl.h>
29 #include <assert.h>
30 #include <sys/stat.h>
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 = calloc(1, sizeof(fd_set));
42 return tmp;
43 }
44
45 void
46 IOMGR_FreeFDSet(fd_set * fds)
47 {
48 free(fds);
49 }
50 #endif
51
52 /* The TCP spec calls for writing at least one byte of OOB data which is
53 * read by the receiver using recv with the MSG_OOB flag set.
54 */
55 void
56 sendOOB(int fd)
57 {
58 char c = (char)1;
59
60 Log("Sending OOB.\n");
61 if (send(fd, &c, 1, MSG_OOB) < 0) {
62 Die(1, "sendOOB");
63 }
64 }
65
66 void
67 recvOOB(int fd)
68 {
69 char c;
70
71 Log("Received OOB\n");
72 if (recv(fd, &c, 1, MSG_OOB) < 0) {
73 Die(1, "recvOOB");
74 }
75 Log("Handled OOB\n");
76 }
77
78 void
79 assertNullFDSet(int fd, fd_set * fds)
80 {
81 int i;
82 int n = sizeof(*fds) / sizeof(int);
83 int *j = (int *)fds;
84
85 if (fd >= 0)
86 FD_CLR(fd, fds);
87
88 for (i = 0; i < n; i++)
89 assert(j[i] == 0);
90 }
91
92 /* OpenFDs
93 *
94 * Open file descriptors until file descriptor n or higher is returned.
95 */
96 #include <sys/stat.h>
97 void
98 OpenFDs(n)
99 int n;
100 {
101 int i;
102 struct stat sbuf;
103 int fd, lfd;
104
105 lfd = -1;
106 for (i = 0; i < n; i++) {
107 if (fstat(i, &sbuf) == 0)
108 continue;
109 if ((fd = open("/dev/null", 0, 0)) < 0) {
110 if (lfd >= 0) {
111 close(lfd);
112 return;
113 }
114 } else {
115 if (fd >= n) {
116 close(fd);
117 return;
118 } else {
119 lfd = fd;
120 }
121 }
122 }
123 }
124
125 /* If flag is set, abort. */
126 void
127 Die(int flag, char *msg)
128 {
129 char tmp[1024];
130 extern char *program;
131
132 (void)sprintf(tmp, "%s: %s: ", program ? program : "", msg);
133 perror(tmp);
134 fflush(stderr);
135 if (flag)
136 abort();
137 else
138 exit(1);
139 }
140
141
142
143 void
144 Log(char *fmt, ...)
145 {
146 va_list args;
147 struct timeval now;
148 struct tm *ltime;
149 time_t tt;
150 int code;
151 PROCESS pid;
152 extern char *program;
153
154 code = gettimeofday(&now, NULL);
155 assert(code == 0);
156
157 tt = now.tv_sec;
158 ltime = localtime(&tt);
159
160 LWP_CurrentProcess(&pid);
161 fprintf(stderr, "%s 0x%x %02d:%02d:%02d.%d: ", program ? program : "",
162 pid, ltime->tm_hour, ltime->tm_min, ltime->tm_sec, now.tv_usec);
163
164 va_start(args, fmt);
165
166 vfprintf(stderr, fmt, args);
167 fflush(stdout);
168 va_end(args);
169 }