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