ATF_TC_BODY(rfork, tc)
{
	struct stat sb;
	struct lwp *l, *l2;
	int fd;

	RZ(rump_init());

	ATF_REQUIRE_EQ(rump_pub_lwproc_rfork(RUMP_RFFDG|RUMP_RFCFDG), EINVAL);

	RZ(rump_pub_lwproc_rfork(0));
	l = rump_pub_lwproc_curlwp();

	RL(fd = rump_sys_open("/file", O_RDWR | O_CREAT, 0777));

	/* ok, first check rfork(RUMP_RFCFDG) does *not* preserve fd's */
	RZ(rump_pub_lwproc_rfork(RUMP_RFCFDG));
	ATF_REQUIRE_ERRNO(EBADF, rump_sys_write(fd, &fd, sizeof(fd)) == -1);

	/* then check that rfork(0) does */
	rump_pub_lwproc_switch(l);
	RZ(rump_pub_lwproc_rfork(0));
	ATF_REQUIRE_EQ(rump_sys_write(fd, &fd, sizeof(fd)), sizeof(fd));
	RL(rump_sys_fstat(fd, &sb));
	l2 = rump_pub_lwproc_curlwp();

	/*
	 * check that the shared fd table is really shared by
	 * closing fd in parent
	 */
	rump_pub_lwproc_switch(l);
	RL(rump_sys_close(fd));
	rump_pub_lwproc_switch(l2);
	ATF_REQUIRE_ERRNO(EBADF, rump_sys_fstat(fd, &sb) == -1);

	/* redo, this time copying the fd table instead of sharing it */
	rump_pub_lwproc_releaselwp();
	rump_pub_lwproc_switch(l);
	RL(fd = rump_sys_open("/file", O_RDWR, 0777));
	RZ(rump_pub_lwproc_rfork(RUMP_RFFDG));
	ATF_REQUIRE_EQ(rump_sys_write(fd, &fd, sizeof(fd)), sizeof(fd));
	RL(rump_sys_fstat(fd, &sb));
	l2 = rump_pub_lwproc_curlwp();

	/* check that the fd table is copied */
	rump_pub_lwproc_switch(l);
	RL(rump_sys_close(fd));
	rump_pub_lwproc_switch(l2);
	RL(rump_sys_fstat(fd, &sb));
	ATF_REQUIRE_EQ(sb.st_size, sizeof(fd));
}
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();
}
Esempio n. 3
0
ATF_TC_BODY(devnull2, tc)
{
	struct union_args unionargs;
	int fd, res;

	rump_init();

	if (rump_sys_mkdir("/mp", 0777) == -1)
		atf_tc_fail_errno("mkdir mp");

	unionargs.target = __UNCONST("/dev");
	unionargs.mntflags = UNMNT_BELOW;

	if (rump_sys_mount(MOUNT_UNION, "/mp", 0,
	    &unionargs, sizeof(unionargs)) == -1)
		atf_tc_fail_errno("union mount");

	fd = rump_sys_open("/mp/null", O_WRONLY | O_CREAT | O_APPEND);
	if (fd == -1)
		atf_tc_fail_errno("open");

	res = rump_sys_write(fd, &fd, sizeof(fd));
	if (res != sizeof(fd))
		atf_tc_fail("write");
}
Esempio n. 4
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);
}
static void
overwritebody(const atf_tc_t *tc, off_t count, bool dotrunc)
{
	char *buf;
	int fd;

	REQUIRE_LIBC(buf = malloc(count), NULL);
	FSTEST_ENTER();
	RL(fd = rump_sys_open("testi", O_CREAT | O_RDWR, 0666));
	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, count), count);
	RL(rump_sys_close(fd));

	RL(fd = rump_sys_open("testi", O_RDWR));
	if (dotrunc)
		RL(rump_sys_ftruncate(fd, 0));
	ATF_REQUIRE_EQ(rump_sys_write(fd, buf, count), count);
	RL(rump_sys_close(fd));
	FSTEST_EXIT();
}
Esempio n. 6
0
/*
 * Prepare rump, configure interface and route to cause fragmentation
 */
static void
setup(void)
{
	char ifname[IFNAMSIZ];
	struct {
		struct rt_msghdr m_rtm;
		struct sockaddr_in m_sin;
	} m_rtmsg;
#define rtm m_rtmsg.m_rtm
#define rsin m_rtmsg.m_sin
	struct ifreq ifr;
	int s;

	rump_init();

	/* first, config lo0 & route */
	strcpy(ifname, "lo0");
	netcfg_rump_if(ifname, "127.0.0.1", "255.0.0.0");
	netcfg_rump_route("127.0.0.1", "255.0.0.0", "127.0.0.1");

	if ((s = rump_sys_socket(PF_ROUTE, SOCK_RAW, 0)) == -1)
		atf_tc_fail_errno("routing socket");

	/*
	 * set MTU for interface so that route MTU doesn't
	 * get overridden by it.
	 */
	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, "lo0");
	ifr.ifr_mtu = 1300;
	if (rump_sys_ioctl(s, SIOCSIFMTU, &ifr) == -1)
		atf_tc_fail_errno("set mtu");

	/* change route MTU to 100 */
	memset(&m_rtmsg, 0, sizeof(m_rtmsg));
	rtm.rtm_type = RTM_CHANGE;
	rtm.rtm_flags = RTF_STATIC;
	rtm.rtm_version = RTM_VERSION;
	rtm.rtm_seq = 3;
	rtm.rtm_inits = RTV_MTU;
	rtm.rtm_addrs = RTA_DST;
	rtm.rtm_rmx.rmx_mtu = 100;
	rtm.rtm_msglen = sizeof(m_rtmsg);

	memset(&rsin, 0, sizeof(rsin));
	rsin.sin_family = AF_INET;
	rsin.sin_len = sizeof(rsin);
	rsin.sin_addr.s_addr = inet_addr("127.0.0.1");

	if (rump_sys_write(s, &m_rtmsg, sizeof(m_rtmsg)) != sizeof(m_rtmsg))
		atf_tc_fail_errno("set route mtu");
	rump_sys_close(s);
}
Esempio n. 7
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();
}
Esempio n. 9
0
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);
}
Esempio n. 10
0
int
main(int argc, char *argv[])
{
    char buf[8192];
    ssize_t n;
    int probeonly = 0;
    int fd_src, fd_dst;

    if (argc != 2)
        errx(1, "need 2 args");

    if (strcmp(argv[1], "probe") == 0)
        probeonly = 1;

    if (probeonly)
        rump_boot_sethowto(RUMP_AB_VERBOSE);
    rump_init();
    if (probeonly)
        exit(0);

    fd_dst = rump_sys_open("/dev/ulpt0", O_RDWR);
    if (fd_dst == -1)
        err(1, "printer open");

    fd_src = open(argv[1], O_RDONLY);
    if (fd_src == -1)
        err(1, "open source");

    for (;;) {
        n = read(fd_src, buf, sizeof(buf));
        if (n == 0)
            break;
        if (n == -1)
            err(1, "read");

        if (rump_sys_write(fd_dst, buf, n) != n)
            err(1, "write to printer");
    }
    rump_sys_close(fd_dst);

    printf("done\n");
}
Esempio n. 11
0
ATF_TC_BODY(bpfwriteleak, tc)
{
	char buf[28]; /* sizeof(garbage) > etherhdrlen */
	struct ifreq ifr;
	int ifnum, bpfd;

	RZ(rump_init());
	RZ(rump_pub_shmif_create(NULL, &ifnum));
	sprintf(ifr.ifr_name, "shmif%d", ifnum);

	RL(bpfd = rump_sys_open("/dev/bpf", O_RDWR));
	RL(rump_sys_ioctl(bpfd, BIOCSETIF, &ifr));
	RL(rump_sys_ioctl(bpfd, BIOCSFEEDBACK, &ifr));

	if (getmtdata() != 0)
		atf_tc_fail("test precondition failed: MT_DATA mbufs != 0");

	ATF_REQUIRE_ERRNO(ENETDOWN, rump_sys_write(bpfd, buf, sizeof(buf))==-1);

	ATF_REQUIRE_EQ(getmtdata(), 0);
}
static void
extendbody(const atf_tc_t *tc, off_t seekcnt)
{
	char buf[TESTSZ+1];
	struct stat sb;
	int fd;

	FSTEST_ENTER();
	RL(fd = rump_sys_open("testfile",
	    O_CREAT | O_RDWR | (seekcnt ? O_APPEND : 0)));
	RL(rump_sys_ftruncate(fd, seekcnt));
	RL(rump_sys_fstat(fd, &sb));
	ATF_REQUIRE_EQ(sb.st_size, seekcnt);

	ATF_REQUIRE_EQ(rump_sys_write(fd, TESTSTR, TESTSZ), TESTSZ);
	ATF_REQUIRE_EQ(rump_sys_pread(fd, buf, TESTSZ, seekcnt), TESTSZ);
	ATF_REQUIRE_STREQ(buf, TESTSTR);

	RL(rump_sys_fstat(fd, &sb));
	ATF_REQUIRE_EQ(sb.st_size, (off_t)TESTSZ + seekcnt);
	RL(rump_sys_close(fd));
	FSTEST_EXIT();
}
Esempio n. 13
0
File: oss.c Progetto: Larhard/hurd
/* write to trivfs */
kern_return_t
trivfs_S_io_write (trivfs_protid_t cred,
		mach_port_t reply, mach_msg_type_name_t reply_type,
		char * data, mach_msg_type_number_t data_len,
		loff_t offs, mach_msg_type_number_t *amount)
{
	if (!cred) {
		return EOPNOTSUPP;
	} else if (! (cred->po->openmodes & O_WRITE)) {
		return EBADF;
	}

	debug("write %d bytes", data_len);
	int sent = rump_sys_write(audio_fd, (char *) data, data_len);

	if (sent < 0) {
		err("rump_sys_write: %s", rump_strerror(errno));
		return EIO;
	}

	*amount = sent;
	return 0;
}
Esempio n. 14
0
static ssize_t
writestr(int fd, const char *str)
{
	return rump_sys_write(fd, str, bmk_strlen(str));
}
Esempio n. 15
0
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);
}