Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / stream_test.c
blob90e1ed01330b4f259677c02ad1c395c96a5471c4
1 /* $NetBSD$ */
3 #include "sys_defs.h"
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
9 #include "iostuff.h"
10 #include "msg.h"
11 #include "msg_vstream.h"
12 #include "listen.h"
13 #include "connect.h"
15 #ifdef SUNOS5
16 #include <stropts.h>
18 #define FIFO "/tmp/test-fifo"
20 static const char *progname;
22 static void print_fstat(int fd)
24 struct stat st;
26 if (fstat(fd, &st) < 0)
27 msg_fatal("fstat: %m");
28 vstream_printf("fd %d\n", fd);
29 vstream_printf("dev %ld\n", (long) st.st_dev);
30 vstream_printf("ino %ld\n", (long) st.st_ino);
31 vstream_fflush(VSTREAM_OUT);
34 static NORETURN usage(void)
36 msg_fatal("usage: %s [-p] [-n count] [-v]", progname);
39 int main(int argc, char **argv)
41 int server_fd;
42 int client_fd;
43 int fd;
44 int print_fstats = 0;
45 int count = 1;
46 int ch;
47 int i;
49 progname = argv[0];
50 msg_vstream_init(argv[0], VSTREAM_ERR);
53 * Parse JCL.
55 while ((ch = GETOPT(argc, argv, "pn:v")) > 0) {
56 switch (ch) {
57 default:
58 usage();
59 case 'p':
60 print_fstats = 1;
61 break;
62 case 'n':
63 if ((count = atoi(optarg)) < 1)
64 usage();
65 break;
66 case 'v':
67 msg_verbose++;
68 break;
71 server_fd = stream_listen(FIFO, 0, 0);
72 if (readable(server_fd))
73 msg_fatal("server fd is readable after create");
76 * Connect in client.
78 for (i = 0; i < count; i++) {
79 msg_info("connect attempt %d", i);
80 if ((client_fd = stream_connect(FIFO, 0, 0)) < 0)
81 msg_fatal("open %s as client: %m", FIFO);
82 if (readable(server_fd))
83 msg_info("server fd is readable after client open");
84 if (close(client_fd) < 0)
85 msg_fatal("close client fd: %m");
89 * Accept in server.
91 for (i = 0; i < count; i++) {
92 msg_info("receive attempt %d", i);
93 if (!readable(server_fd)) {
94 msg_info("wait for server fd to become readable");
95 read_wait(server_fd, -1);
97 if ((fd = stream_accept(server_fd)) < 0)
98 msg_fatal("receive fd: %m");
99 if (print_fstats)
100 print_fstat(fd);
101 if (close(fd) < 0)
102 msg_fatal("close received fd: %m");
104 if (close(server_fd) < 0)
105 msg_fatal("close server fd");
106 return (0);
108 #else
109 int main(int argc, char **argv)
111 return (0);
113 #endif