summaryrefslogtreecommitdiffstats
path: root/src/util/stream_test.c
blob: 5c8f82fa4bf60fe9faf9acd150de1599665d7de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "sys_defs.h"
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

#include "iostuff.h"
#include "msg.h"
#include "msg_vstream.h"
#include "listen.h"
#include "connect.h"

#ifdef SUNOS5
#include <stropts.h>

#define FIFO	"/tmp/test-fifo"

static const char *progname;

static void print_fstat(int fd)
{
    struct stat st;

    if (fstat(fd, &st) < 0)
	msg_fatal("fstat: %m");
    vstream_printf("fd	%d\n", fd);
    vstream_printf("dev	%ld\n", (long) st.st_dev);
    vstream_printf("ino	%ld\n", (long) st.st_ino);
    vstream_fflush(VSTREAM_OUT);
}

static NORETURN usage(void)
{
    msg_fatal("usage: %s [-p] [-n count] [-v]", progname);
}

int     main(int argc, char **argv)
{
    int     server_fd;
    int     client_fd;
    int     fd;
    int     print_fstats = 0;
    int     count = 1;
    int     ch;
    int     i;

    progname = argv[0];
    msg_vstream_init(argv[0], VSTREAM_ERR);

    /*
     * Parse JCL.
     */
    while ((ch = GETOPT(argc, argv, "pn:v")) > 0) {
	switch (ch) {
	default:
	    usage();
	case 'p':
	    print_fstats = 1;
	    break;
	case 'n':
	    if ((count = atoi(optarg)) < 1)
		usage();
	    break;
	case 'v':
	    msg_verbose++;
	    break;
	}
    }
    server_fd = stream_listen(FIFO, 0, 0);
    if (readable(server_fd))
	msg_fatal("server fd is readable after create");

    /*
     * Connect in client.
     */
    for (i = 0; i < count; i++) {
	msg_info("connect attempt %d", i);
	if ((client_fd = stream_connect(FIFO, 0, 0)) < 0)
	    msg_fatal("open %s as client: %m", FIFO);
	if (readable(server_fd))
	    msg_info("server fd is readable after client open");
	if (close(client_fd) < 0)
	    msg_fatal("close client fd: %m");
    }

    /*
     * Accept in server.
     */
    for (i = 0; i < count; i++) {
	msg_info("receive attempt %d", i);
	if (!readable(server_fd)) {
	    msg_info("wait for server fd to become readable");
	    read_wait(server_fd, -1);
	}
	if ((fd = stream_accept(server_fd)) < 0)
	    msg_fatal("receive fd: %m");
	if (print_fstats)
	    print_fstat(fd);
	if (close(fd) < 0)
	    msg_fatal("close received fd: %m");
    }
    if (close(server_fd) < 0)
	msg_fatal("close server fd");
    return (0);
}
#else
int     main(int argc, char **argv)
{
    return (0);
}
#endif