Postfix3.3.1
stream_test.c
[詳解]
1 #include "sys_defs.h"
2 #include <sys/stat.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <fcntl.h>
6 
7 #include "iostuff.h"
8 #include "msg.h"
9 #include "msg_vstream.h"
10 #include "listen.h"
11 #include "connect.h"
12 
13 #ifdef SUNOS5
14 #include <stropts.h>
15 
16 #define FIFO "/tmp/test-fifo"
17 
18 static const char *progname;
19 
20 static void print_fstat(int fd)
21 {
22  struct stat st;
23 
24  if (fstat(fd, &st) < 0)
25  msg_fatal("fstat: %m");
26  vstream_printf("fd %d\n", fd);
27  vstream_printf("dev %ld\n", (long) st.st_dev);
28  vstream_printf("ino %ld\n", (long) st.st_ino);
30 }
31 
32 static NORETURN usage(void)
33 {
34  msg_fatal("usage: %s [-p] [-n count] [-v]", progname);
35 }
36 
37 int main(int argc, char **argv)
38 {
39  int server_fd;
40  int client_fd;
41  int fd;
42  int print_fstats = 0;
43  int count = 1;
44  int ch;
45  int i;
46 
47  progname = argv[0];
48  msg_vstream_init(argv[0], VSTREAM_ERR);
49 
50  /*
51  * Parse JCL.
52  */
53  while ((ch = GETOPT(argc, argv, "pn:v")) > 0) {
54  switch (ch) {
55  default:
56  usage();
57  case 'p':
58  print_fstats = 1;
59  break;
60  case 'n':
61  if ((count = atoi(optarg)) < 1)
62  usage();
63  break;
64  case 'v':
65  msg_verbose++;
66  break;
67  }
68  }
69  server_fd = stream_listen(FIFO, 0, 0);
70  if (readable(server_fd))
71  msg_fatal("server fd is readable after create");
72 
73  /*
74  * Connect in client.
75  */
76  for (i = 0; i < count; i++) {
77  msg_info("connect attempt %d", i);
78  if ((client_fd = stream_connect(FIFO, 0, 0)) < 0)
79  msg_fatal("open %s as client: %m", FIFO);
80  if (readable(server_fd))
81  msg_info("server fd is readable after client open");
82  if (close(client_fd) < 0)
83  msg_fatal("close client fd: %m");
84  }
85 
86  /*
87  * Accept in server.
88  */
89  for (i = 0; i < count; i++) {
90  msg_info("receive attempt %d", i);
91  if (!readable(server_fd)) {
92  msg_info("wait for server fd to become readable");
93  read_wait(server_fd, -1);
94  }
95  if ((fd = stream_accept(server_fd)) < 0)
96  msg_fatal("receive fd: %m");
97  if (print_fstats)
98  print_fstat(fd);
99  if (close(fd) < 0)
100  msg_fatal("close received fd: %m");
101  }
102  if (close(server_fd) < 0)
103  msg_fatal("close server fd");
104  return (0);
105 }
106 #else
107 int main(int argc, char **argv)
108 {
109  return (0);
110 }
111 #endif
int msg_verbose
Definition: msg.c:177
int stream_listen(const char *, int, int)
Definition: stream_listen.c:71
#define NORETURN
Definition: sys_defs.h:1583
#define VSTREAM_OUT
Definition: vstream.h:67
#define stat(p, s)
Definition: warn_stat.h:18
#define readable(fd)
Definition: iostuff.h:36
VSTREAM * vstream_printf(const char *fmt,...)
Definition: vstream.c:1335
#define read_wait(fd, timeout)
Definition: iostuff.h:39
NORETURN msg_fatal(const char *fmt,...)
Definition: msg.c:249
int stream_connect(const char *, int, int)
int vstream_fflush(VSTREAM *stream)
Definition: vstream.c:1257
#define GETOPT(argc, argv, str)
Definition: sys_defs.h:1313
void msg_vstream_init(const char *name, VSTREAM *vp)
Definition: msg_vstream.c:77
int main(int argc, char **argv)
Definition: stream_test.c:107
#define VSTREAM_ERR
Definition: vstream.h:68
int stream_accept(int)
Definition: stream_listen.c:87
#define fstat(f, s)
Definition: warn_stat.h:20
void msg_info(const char *fmt,...)
Definition: msg.c:199