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