t_fifos.c revision 1.1
1/*	$NetBSD: t_fifos.c,v 1.1 2010/03/29 13:26:32 pooka Exp $	*/
2
3#include <sys/types.h>
4#include <sys/mount.h>
5
6#include <atf-c.h>
7#include <errno.h>
8#include <fcntl.h>
9#include <pthread.h>
10#include <stdio.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <string.h>
14
15#include <rump/rump.h>
16#include <rump/rump_syscalls.h>
17
18#include <ufs/ufs/ufsmount.h>
19
20/* for debugging */
21#define USE_ATF
22
23#ifdef USE_ATF
24#include "../../h_macros.h"
25
26ATF_TC_WITH_CLEANUP(fifos);
27ATF_TC_HEAD(fifos, tc)
28{
29	atf_tc_set_md_var(tc, "descr", "test fifo support in ffs");
30	atf_tc_set_md_var(tc, "timeout", "5");
31}
32#else
33#include <err.h>
34#define atf_tc_fail_errno(a) err(1, a)
35#define atf_tc_fail(a, ...) err(1, a)
36#endif
37
38#define teststr1 "raving & drooling"
39#define teststr2 "haha, charade"
40
41static void *
42w1(void *arg)
43{
44	int fd;
45
46	fd = rump_sys_open("sheep", O_WRONLY);
47	if (fd == -1)
48		atf_tc_fail_errno("w1 open");
49	if (rump_sys_write(fd, teststr1, sizeof(teststr1)) != sizeof(teststr1))
50		atf_tc_fail_errno("w1 write");
51	rump_sys_close(fd);
52
53	return NULL;
54}
55
56static void *
57w2(void *arg)
58{
59	int fd;
60
61	fd = rump_sys_open("pigs", O_WRONLY);
62	if (fd == -1)
63		atf_tc_fail_errno("w2 open");
64	if (rump_sys_write(fd, teststr2, sizeof(teststr2)) != sizeof(teststr2))
65		atf_tc_fail_errno("w2 write");
66	rump_sys_close(fd);
67
68	return NULL;
69}
70
71static void *
72r1(void *arg)
73{
74	char buf[32];
75	int fd;
76
77	fd = rump_sys_open("sheep", O_RDONLY);
78	if (fd == -1)
79		atf_tc_fail_errno("r1 open");
80	if (rump_sys_read(fd, buf, sizeof(buf)) != sizeof(teststr1))
81		atf_tc_fail_errno("r1 read");
82	rump_sys_close(fd);
83
84	if (strcmp(teststr1, buf) != 0)
85		atf_tc_fail("got invalid str, %s vs. %s", buf, teststr1);
86
87	return NULL;
88}
89
90static void *
91r2(void *arg)
92{
93	char buf[32];
94	int fd;
95
96	fd = rump_sys_open("pigs", O_RDONLY);
97	if (fd == -1)
98		atf_tc_fail_errno("r2 open");
99	if (rump_sys_read(fd, buf, sizeof(buf)) != sizeof(teststr2))
100		atf_tc_fail_errno("r2 read");
101	rump_sys_close(fd);
102
103	if (strcmp(teststr2, buf) != 0)
104		atf_tc_fail("got invalid str, %s vs. %s", buf, teststr2);
105
106	return NULL;
107}
108
109#define IMGNAME "atf.img"
110
111const char *newfs = "newfs -F -s 10000 " IMGNAME;
112#define FAKEBLK "/dev/sp00ka"
113
114#ifdef USE_ATF
115ATF_TC_BODY(fifos, tc)
116#else
117int main(int argc, char *argv[])
118#endif
119{
120	struct ufs_args args;
121	pthread_t ptw1, ptw2, ptr1, ptr2;
122
123	if (system(newfs) == -1)
124		atf_tc_fail_errno("newfs failed");
125
126	memset(&args, 0, sizeof(args));
127	args.fspec = __UNCONST(FAKEBLK);
128
129	rump_init();
130	if (rump_sys_mkdir("/animals", 0777) == -1)
131		atf_tc_fail_errno("cannot create mountpoint");
132	rump_pub_etfs_register(FAKEBLK, IMGNAME, RUMP_ETFS_BLK);
133	if (rump_sys_mount(MOUNT_FFS, "/animals", 0, &args, sizeof(args))==-1)
134		atf_tc_fail_errno("rump_sys_mount failed");
135
136	/* create fifos */
137	if (rump_sys_chdir("/animals") == 1)
138		atf_tc_fail_errno("chdir");
139	if (rump_sys_mkfifo("pigs", S_IFIFO | 0777) == -1)
140		atf_tc_fail_errno("mknod1");
141	if (rump_sys_mkfifo("sheep", S_IFIFO | 0777) == -1)
142		atf_tc_fail_errno("mknod2");
143
144	pthread_create(&ptw1, NULL, w1, NULL);
145	pthread_create(&ptw2, NULL, w2, NULL);
146	pthread_create(&ptr1, NULL, r1, NULL);
147	pthread_create(&ptr2, NULL, r2, NULL);
148
149	pthread_join(ptw1, NULL);
150	pthread_join(ptw2, NULL);
151	pthread_join(ptr1, NULL);
152	pthread_join(ptr2, NULL);
153
154	if (rump_sys_chdir("/") == 1)
155		atf_tc_fail_errno("chdir");
156
157	if (rump_sys_unmount("/animals", 0) == -1)
158		atf_tc_fail_errno("unmount failed");
159}
160
161#ifdef USE_ATF
162ATF_TC_CLEANUP(fifos, tc)
163{
164
165	unlink(IMGNAME);
166}
167
168ATF_TP_ADD_TCS(tp)
169{
170	ATF_TP_ADD_TC(tp, fifos);
171	return 0;
172}
173#endif
174