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