Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / test / syslink / test1.c
CommitLineData
8aea60b2
MD
1/*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * $DragonFly: src/test/syslink/test1.c,v 1.1 2007/05/28 05:28:12 dillon Exp $
35 */
36#include <sys/types.h>
37#include <sys/syslink.h>
38#include <sys/syslink_msg.h>
39#include <errno.h>
40#include <string.h>
41
42static void reader(int fd);
43static void writer(int fd);
44
45int
46main(int ac, char **av)
47{
48 union syslink_info_all info;
49 int fd1;
50 int fd2;
51
52 bzero(&info, sizeof(info));
53 if (syslink(SYSLINK_CMD_NEW, &info.head, sizeof(info)) < 0) {
54 perror("syslink");
55 exit(1);
56 }
57 printf("fds %d %d\n", info.cmd_new.fds[0], info.cmd_new.fds[1]);
58 if (fork() == 0) {
59 reader(info.cmd_new.fds[0]);
60 } else {
61 writer(info.cmd_new.fds[1]);
62 }
63 while (wait(NULL) > 0)
64 ;
65 return(0);
66}
67
68static
69void
70writer(int fd)
71{
72 union syslink_small_msg cmd;
73 union syslink_small_msg rep;
74 int n;
75 int wcount = 10;
76
77 bzero(&cmd, sizeof(cmd));
78 cmd.msg.sm_bytes = sizeof(struct syslink_msg);
79 cmd.msg.sm_proto = SMPROTO_BSDVFS;
80 cmd.msg.sm_head.se_cmd = 0;
81 cmd.msg.sm_head.se_bytes = sizeof(cmd.msg.sm_head);
82 for (;;) {
83 ++cmd.msg.sm_msgid;
84 n = write(fd, &cmd, cmd.msg.sm_bytes);
85 if (n < 0) {
86 printf("write error %s\n", strerror(errno));
87 } else if (wcount) {
88 printf("write n = %d %lld\n", n, cmd.msg.sm_msgid);
89 --wcount;
90 } else {
91 printf("write n = %d %lld\n", n, cmd.msg.sm_msgid);
92 n = read(fd, &rep, sizeof(rep));
93 printf("read-reply %d %lld\n", n, rep.msg.sm_msgid);
94 }
95 }
96}
97
98static
99void
100reader(int fd)
101{
102 union syslink_small_msg cmd;
103 union syslink_small_msg rep;
104 int n;
105
106 bzero(&rep, sizeof(rep));
107 rep.msg.sm_bytes = sizeof(struct syslink_msg);
108 rep.msg.sm_proto = SMPROTO_BSDVFS | SM_PROTO_REPLY;
109 rep.msg.sm_msgid = 1;
110 rep.msg.sm_head.se_cmd = 0;
111 rep.msg.sm_head.se_bytes = sizeof(rep.msg.sm_head);
112
113 for (;;) {
114 n = read(fd, &cmd, sizeof(cmd));
115 if (n < 0 && errno == ENOSPC) {
116 printf("no space\n");
117 exit(1);
118 }
119 if (n < 0) {
120 printf("read error %s\n", strerror(errno));
121 } else {
122 printf("read n = %d\n", n);
123 rep.msg.sm_msgid = cmd.msg.sm_msgid;
124 n = write(fd, &rep, rep.msg.sm_bytes);
125 if (n < 0) {
126 printf("reply error %s\n", strerror(errno));
127 } else {
128 printf("reply ok\n");
129 }
130 }
131 if (n < 0)
132 break;
133 /*sleep(1);*/
134 }
135}
136