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