xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/stream_test.c (revision 41fbaed053f8fbfdf9d2a4ee0a7386a3c83f8505)
1 /*	$NetBSD: stream_test.c,v 1.1.1.1 2009/06/23 10:09:01 tron Exp $	*/
2 
3 #include "sys_defs.h"
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <fcntl.h>
8 
9 #include "iostuff.h"
10 #include "msg.h"
11 #include "msg_vstream.h"
12 #include "listen.h"
13 #include "connect.h"
14 
15 #ifdef SUNOS5
16 #include <stropts.h>
17 
18 #define FIFO	"/tmp/test-fifo"
19 
20 static const char *progname;
21 
print_fstat(int fd)22 static void print_fstat(int fd)
23 {
24     struct stat st;
25 
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);
32 }
33 
usage(void)34 static NORETURN usage(void)
35 {
36     msg_fatal("usage: %s [-p] [-n count] [-v]", progname);
37 }
38 
main(int argc,char ** argv)39 int     main(int argc, char **argv)
40 {
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;
48 
49     progname = argv[0];
50     msg_vstream_init(argv[0], VSTREAM_ERR);
51 
52     /*
53      * Parse JCL.
54      */
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;
69 	}
70     }
71     server_fd = stream_listen(FIFO, 0, 0);
72     if (readable(server_fd))
73 	msg_fatal("server fd is readable after create");
74 
75     /*
76      * Connect in client.
77      */
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");
86     }
87 
88     /*
89      * Accept in server.
90      */
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);
96 	}
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");
103     }
104     if (close(server_fd) < 0)
105 	msg_fatal("close server fd");
106     return (0);
107 }
108 #else
main(int argc,char ** argv)109 int     main(int argc, char **argv)
110 {
111     return (0);
112 }
113 #endif
114