ATF_TC_BODY(reregister_reg, tc) { char buf[1024]; int localfd, etcfd; ssize_t n; int tfd; etcfd = open("/etc/passwd", O_RDONLY); ATF_REQUIRE(etcfd != -1); localfd = open("./testfile", O_RDWR | O_CREAT, 0666); ATF_REQUIRE(localfd != -1); ATF_REQUIRE_EQ(write(localfd, TESTSTR1, TESTSTR1SZ), TESTSTR1SZ); /* testfile now contains test string */ rump_init(); ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "/etc/passwd", RUMP_ETFS_REG), 0); tfd = rump_sys_open(TESTPATH1, O_RDONLY); ATF_REQUIRE(tfd != -1); ATF_REQUIRE(rump_sys_read(tfd, buf, sizeof(buf)) > 0); rump_sys_close(tfd); rump_pub_etfs_remove(TESTPATH1); ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./testfile", RUMP_ETFS_REG), 0); tfd = rump_sys_open(TESTPATH2, O_RDWR); ATF_REQUIRE(tfd != -1); memset(buf, 0, sizeof(buf)); ATF_REQUIRE((n = rump_sys_read(tfd, buf, sizeof(buf))) > 0); /* check that we have what we expected */ ATF_REQUIRE_STREQ(buf, TESTSTR1); /* ... while here, check that writing works too */ ATF_REQUIRE_EQ(rump_sys_lseek(tfd, 0, SEEK_SET), 0); ATF_REQUIRE(TESTSTR1SZ <= TESTSTR2SZ); ATF_REQUIRE_EQ(rump_sys_write(tfd, TESTSTR2, TESTSTR2SZ), TESTSTR2SZ); memset(buf, 0, sizeof(buf)); ATF_REQUIRE_EQ(lseek(localfd, 0, SEEK_SET), 0); ATF_REQUIRE(read(localfd, buf, sizeof(buf)) > 0); ATF_REQUIRE_STREQ(buf, TESTSTR2); close(etcfd); close(localfd); }
static void read_after_unlink(const atf_tc_t *tc, const char *mp) { char buf[TBSIZE], buf2[TBSIZE]; int fd; FSTEST_ENTER(); /* create file and put some content into it */ RL(fd = rump_sys_open("file", O_RDWR|O_CREAT, 0666)); memset(buf, 'D', TBSIZE); ATF_REQUIRE_EQ(rump_sys_write(fd, buf, TBSIZE), TBSIZE); rump_sys_close(fd); /* flush buffers from UBC to file system */ ATF_REQUIRE_ERRNO(EBUSY, rump_sys_unmount(mp, 0) == -1); RL(fd = rump_sys_open("file", O_RDWR)); RL(rump_sys_unlink("file")); ATF_REQUIRE_EQ(rump_sys_read(fd, buf2, TBSIZE), TBSIZE); ATF_REQUIRE_EQ(memcmp(buf, buf2, TBSIZE), 0); rump_sys_close(fd); FSTEST_EXIT(); }
ATF_TC_BODY(range_blk, tc) { char buf[32000]; char cmpbuf[32000]; ssize_t n; int rv, tfd; /* create a 64000 byte file with 16 1's at offset = 32000 */ rv = system("dd if=/dev/zero of=disk.img bs=1000 count=64"); ATF_REQUIRE_EQ(rv, 0); rv = system("yes | tr '\\ny' '\\1' " "| dd of=disk.img conv=notrunc bs=1 count=16 seek=32000"); ATF_REQUIRE_EQ(rv, 0); /* map the file at [16000,48000]. this puts our 1's at offset 16000 */ rump_init(); ATF_REQUIRE_EQ(rump_pub_etfs_register_withsize(TESTPATH1, "disk.img", RUMP_ETFS_BLK, 16000, 32000), 0); tfd = rump_sys_open(TESTPATH1, O_RDWR); ATF_REQUIRE(tfd != -1); n = rump_sys_read(tfd, buf, sizeof(buf)); ATF_REQUIRE_EQ(n, sizeof(buf)); ATF_REQUIRE_EQ(rump_sys_close(tfd), 0); ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH1), 0); /* check that we got what is expected */ memset(cmpbuf, 0, sizeof(cmpbuf)); memset(cmpbuf+16000, 1, 16); ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, sizeof(buf)), 0); }
int main() { struct sockaddr_in sin; char buf[65535]; struct hostent *hp; ssize_t nn; ssize_t off; int s, e; hp = gethostbyname(DESTHOST); if (!hp || hp->h_addrtype != AF_INET) errx(1, "failed to resolve \"%s\"", DESTHOST); rump_init(); #ifndef USE_SOCKIN if ((e = rump_pub_netconfig_ifcreate("virt0")) != 0) die(e, "create virt0"); if ((e = rump_pub_netconfig_dhcp_ipv4_oneshot("virt0")) != 0) die(e, "dhcp address"); #endif s = rump_sys_socket(PF_INET, SOCK_STREAM, 0); if (s == -1) die(errno, "socket"); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; #if 0 sin.sin_len = sizeof(sin); #endif sin.sin_port = htons(80); memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr)); if (rump_sys_connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) die(errno, "connect"); printf("connected\n"); #define WANTHTML "GET / HTTP/1.1\nHost: www.netbsd.org\n\n" nn = rump_sys_write(s, WANTHTML, sizeof(WANTHTML)-1); printf("write rv %zd\n", nn); for (;;) { nn = rump_sys_read(s, buf, sizeof(buf)-1); if (nn == -1) die(errno, "read failed"); if (nn == 0) break; buf[nn] = '\0'; printf("%s", buf); } rump_sys_close(s); die(0, NULL); }
ATF_TC_BODY(reregister_blk, tc) { char buf[512 * 128]; char cmpbuf[512 * 128]; int rv, tfd; /* first, create some image files */ rv = system("dd if=/dev/zero bs=512 count=64 " "| tr '\\0' '\\1' > disk1.img"); ATF_REQUIRE_EQ(rv, 0); rv = system("dd if=/dev/zero bs=512 count=128 " "| tr '\\0' '\\2' > disk2.img"); ATF_REQUIRE_EQ(rv, 0); rump_init(); ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH1, "./disk1.img", RUMP_ETFS_BLK), 0); tfd = rump_sys_open(TESTPATH1, O_RDONLY); ATF_REQUIRE(tfd != -1); ATF_REQUIRE_EQ(rump_sys_read(tfd, buf, sizeof(buf)), 64*512); memset(cmpbuf, 1, sizeof(cmpbuf)); ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, 64*512), 0); ATF_REQUIRE_EQ(rump_sys_close(tfd), 0); ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH1), 0); ATF_REQUIRE_EQ(rump_pub_etfs_register(TESTPATH2, "./disk2.img", RUMP_ETFS_BLK), 0); tfd = rump_sys_open(TESTPATH2, O_RDONLY); ATF_REQUIRE(tfd != -1); ATF_REQUIRE_EQ(rump_sys_read(tfd, buf, sizeof(buf)), 128*512); memset(cmpbuf, 2, sizeof(cmpbuf)); ATF_REQUIRE_EQ(memcmp(buf, cmpbuf, 128*512), 0); ATF_REQUIRE_EQ(rump_sys_close(tfd), 0); ATF_REQUIRE_EQ(rump_pub_etfs_remove(TESTPATH2), 0); }
int netconnect() { struct sockaddr_in sin; char *buf; ssize_t nn, off; int error; int s; s = rump_sys_socket(PF_INET, SOCK_STREAM, 0); if (s == -1) { printf("you're mean! no sucket for you!\n"); return -1; } printf("!!!\nTHE FD value: %d\n!!!\n", s); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(80); sin.sin_addr.s_addr = DESTADDR; if (rump_sys_connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) { printf("could not connect\n"); return -1; } nn = rump_sys_write(s, WANTHTML, sizeof(WANTHTML)-1); printf("wrote http request, rv %zd\n", nn); buf = calloc(1, BUFSIZE); off = 0; do { nn = rump_sys_read(s, buf+off, (BUFSIZE-off)-1); off += nn; if (off >= BUFSIZE) exit(-1); } while (nn > 0); if (nn == -1) { printf("read failed: %zd\n", nn); return -1; } printf("read %zd bytes\n", off); /* display last 500 bytes of delicious info */ buf[off] = '\0'; off -= 500; if (off < 0) off = 0; printf("that was an educational experience. " "we learned:\n"); printf("%s", &buf[off]); }
static void read_fault(const atf_tc_t *tc, const char *mp) { char ch = 123; int fd; FSTEST_ENTER(); RL(fd = rump_sys_open("file", O_CREAT | O_RDWR, 0777)); ATF_REQUIRE_EQ(rump_sys_write(fd, &ch, 1), 1); RL(rump_sys_close(fd)); RL(fd = rump_sys_open("file", O_RDONLY | O_SYNC | O_RSYNC)); ATF_REQUIRE_ERRNO(EFAULT, rump_sys_read(fd, NULL, 1) == -1); RL(rump_sys_close(fd)); FSTEST_EXIT(); }
int main(int argc, char *argv[]) { struct wscons_event *wev; int shift = 0; char buf[128]; int fd; rump_boot_sethowto(RUMP_AB_VERBOSE); rump_init(); fd = rump_sys_open("/dev/wskbd", 0); if (fd == -1) err(1, "open"); while (rump_sys_read(fd, buf, sizeof(buf)) > 0) { const char *typestr; /* XXX: timespec in 5.0 vs. -current */ wev = (void *)buf; switch (wev->type) { case WSCONS_EVENT_KEY_UP: typestr = "up"; if (wev->value == 0xe1 || wev->value == 0xe5) shift = 0; break; case WSCONS_EVENT_KEY_DOWN: typestr = "down"; if (wev->value == 0xe1 || wev->value == 0xe5) shift = SHIFT; break; default: typestr = "unknown"; break; } printf("event type: %d (%s)\n", wev->type, typestr); printf("value 0x%x", wev->value); /* * There's probably a value-to-readable tool somewhere * in the tree, but i'm not sure where or how to use it, * so I'll just punt with the supersimple version for now. */ if (wev->value >= 0x04 && wev->value <= 0x1d) printf(" (%c)", wev->value - 0x04 + 'a' + shift); printf("\n"); } }
int main(int argc, char *argv[]) { struct rump_ufs_args args; char imgpath[MAXPATHLEN+1]; union u u; char buf[8192]; int fd; /* * the driver doesn't support endian swapping, so pick image * with matching endianness. */ u.i = 0x12345678; if (u.c == 0x12) { snprintf(imgpath, sizeof(imgpath), "%s/%s", argv[1], "sysvbfs_be.img"); } else { snprintf(imgpath, sizeof(imgpath), "%s/%s", argv[1], "sysvbfs_le.img"); } rump_init(); #define MYFSDEV "/de-vice" rump_pub_etfs_register(MYFSDEV, imgpath, RUMP_ETFS_BLK); args.fspec = (void *)(uintptr_t)MYFSDEV; if (rump_sys_mkdir("/mnt", 0755) == -1) die("mkdir /mnt"); if (rump_sys_mount(RUMP_MOUNT_SYSVBFS, "/mnt", RUMP_MNT_RDONLY, &args, sizeof(args)) == -1) die("mount"); if ((fd = rump_sys_open("/mnt/README", 0)) == -1) die("open file"); memset(buf, 0, sizeof(buf)); if (rump_sys_read(fd, buf, sizeof(buf)) <= 0) die("read version"); if (strcmp(buf, EXPECTED) != 0) die("got unexpected result"); rump_sys_close(fd); if (rump_sys_unmount("/mnt", 0) == -1) die("unmount failed"); return 0; }
static void read_directory(const atf_tc_t *tc, const char *mp) { char buf[1024]; int fd, res; ssize_t size; FSTEST_ENTER(); fd = rump_sys_open(".", O_DIRECTORY | O_RDONLY, 0777); ATF_REQUIRE(fd != -1); size = rump_sys_pread(fd, buf, sizeof(buf), 0); ATF_CHECK(size != -1 || errno == EISDIR); size = rump_sys_read(fd, buf, sizeof(buf)); ATF_CHECK(size != -1 || errno == EISDIR); res = rump_sys_close(fd); ATF_REQUIRE(res != -1); FSTEST_EXIT(); }
int main() { char buf[8192]; int fd; rump_init(); if (rump_sys_mkdir("/kern", 0755) == -1) die("mkdir /kern"); if (rump_sys_mount("kernfs", "/kern", 0, NULL, 0) == -1) die("mount kernfs"); if ((fd = rump_sys_open("/kern/version", 0)) == -1) die("open /kern/version"); printf("\nReading version info from /kern:\n"); if (rump_sys_read(fd, buf, sizeof(buf)) <= 0) die("read version"); printf("\n%s", buf); rump_sys_reboot(0, NULL); return 0; }
static void * prober(void *arg) { int fd, error; char buf[4]; ssize_t n; fd = rump_sys_open(arg, O_RDONLY); for (;;) { n = rump_sys_read(fd, buf, sizeof(buf)); switch (n) { case 4: error = 0; goto out; case -1: if (errno == ENXIO) { usleep(1000); continue; } /* FALLTHROUGH */ default: error = EPIPE; goto out; } } out: error = rump_daemonize_done(error); REQUIRE(error, "rump_daemonize_done"); if (error) exit(1); return NULL; }
void rumpkern_demo(void) { char buf[256]; ssize_t nn; int fd, rv; printk(KERN_INFO "reading rump kernel version from kernfs/version:\n"); rv = rump_sys_mkdir("/mnt", 0777); BUG_ON(rv == -1); rv = rump_sys_mount("kernfs", "/mnt", 0, NULL, 0); BUG_ON(rv == -1); fd = rump_sys_open("/mnt/version", RUMP_O_RDONLY, 0); BUG_ON(rv == -1); nn = rump_sys_read(fd, buf, sizeof(buf)); BUG_ON(nn < 1); buf[nn] = '\0'; printk(KERN_INFO "%s", buf); }
int copy_file(FTSENT *entp, int dne) { static unsigned char buf[MAXBSIZE]; struct stat to_stat, *fs; int ch, checkch, rv, rcount, rval, tolnk, wcount, fdin, fdout; off_t off; fs = entp->fts_statp; tolnk = ((Rflag && !(Lflag || Hflag)) || Pflag); /* * If the file exists and we're interactive, verify with the user. * If the file DNE, set the mode to be the from file, minus setuid * bits, modified by the umask; arguably wrong, but it makes copying * executables work right and it's been that way forever. (The * other choice is 666 or'ed with the execute bits on the from file * modified by the umask.) */ if (!dne) { if (iflag) { (void)fprintf(stderr, "overwrite %s? ", to.p_path); checkch = ch = getchar(); while (ch != '\n' && ch != EOF) ch = getchar(); if (checkch != 'y' && checkch != 'Y') return (0); } rump_sys_unlink(to.p_path); } rv = rump_sys_open(to.p_path, fs->st_mode & ~(S_ISUID | S_ISGID)); if (rv == -1 && (fflag || tolnk)) { /* * attempt to remove existing destination file name and * create a new file */ rump_sys_unlink(to.p_path); rv = rump_sys_open(to.p_path, fs->st_mode & ~(S_ISUID | S_ISGID)); if (rv == -1) { warn("%s", to.p_path); return (1); } } fdout = rv; fdin = rump_sys_open(entp->fts_path, O_RDONLY); rval = 0; /* * There's no reason to do anything other than close the file * now if it's empty, so let's not bother. */ off = 0; if (fs->st_size > 0) { while ((rcount = rump_sys_read(fdin, buf, MAXBSIZE)) > 0) { wcount = rump_sys_write(fdout, buf, (size_t)rcount); if (rcount != wcount || wcount == -1) { warn("%s", to.p_path); rval = 1; break; } off += rcount; } if (rcount < 0) { warn("%s", entp->fts_path); rval = 1; } } if (rval == 1) return (1); if (pflag && setfile(fs, 0)) rval = 1; /* * If the source was setuid or setgid, lose the bits unless the * copy is owned by the same user and group. */ #define RETAINBITS \ (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) if (!pflag && dne && fs->st_mode & (S_ISUID | S_ISGID) && fs->st_uid == myuid) { if (rump_sys_stat(to.p_path, &to_stat)) { warn("%s", to.p_path); rval = 1; } else if (fs->st_gid == to_stat.st_gid && rump_sys_chmod(to.p_path, fs->st_mode & RETAINBITS & ~myumask)) { warn("%s", to.p_path); rval = 1; } } /* set the mod/access times now after close of the fd */ if (pflag && set_utimes(to.p_path, fs)) { rval = 1; } return (rval); }