示例#1
0
文件: userns03.c 项目: xingdl2007/ltp
int main(int argc, char *argv[])
{
	pid_t cpid2;
	char path[BUFSIZ];
	int lc;
	int fd;

	tst_parse_opts(argc, argv, NULL, NULL);
	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		tst_count = 0;

		parentuid = geteuid();
		parentgid = getegid();

		cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn1, NULL);
		if (cpid1 < 0)
			tst_brkm(TBROK | TERRNO, cleanup,
				"cpid1 clone failed");

		cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn2, NULL);
		if (cpid2 < 0)
			tst_brkm(TBROK | TERRNO, cleanup,
				"cpid2 clone failed");

		if (setgroupstag == false) {
			sprintf(path, "/proc/%d/setgroups", cpid1);
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
			SAFE_CLOSE(cleanup, fd);

			sprintf(path, "/proc/%d/setgroups", cpid2);
			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
			SAFE_CLOSE(cleanup, fd);
		}

		updatemap(cpid1, UID_MAP, CHILD1UID, parentuid, cleanup);
		updatemap(cpid2, UID_MAP, CHILD2UID, parentuid, cleanup);

		updatemap(cpid1, GID_MAP, CHILD1GID, parentuid, cleanup);
		updatemap(cpid2, GID_MAP, CHILD2GID, parentuid, cleanup);

		TST_SAFE_CHECKPOINT_WAKE_AND_WAIT(cleanup, 1);

		tst_record_childstatus(cleanup, cpid1);
		tst_record_childstatus(cleanup, cpid2);
	}
	cleanup();
	tst_exit();
}
示例#2
0
文件: preadv01.c 项目: kraj/ltp
void setup(void)
{
	char buf[CHUNK];

	fd = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);

	memset(buf, 'a', sizeof(buf));
	SAFE_WRITE(1, fd, buf, sizeof(buf));

	memset(buf, 'b', sizeof(buf));
	SAFE_WRITE(1, fd, buf, sizeof(buf));
}
示例#3
0
文件: userns02.c 项目: HackLinux/ltp
int main(int argc, char *argv[])
{
	int status;
	int lc;
	int childpid;
	int parentuid;
	int parentgid;
	char path[BUFSIZ];
	char content[BUFSIZ];
	int fd;

	tst_parse_opts(argc, argv, NULL, NULL);
	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		tst_count = 0;
		childpid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
			(void *)child_fn1, NULL);

		if (childpid < 0)
			tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");

		parentuid = geteuid();
		parentgid = getegid();
		sprintf(path, "/proc/%d/uid_map", childpid);
		sprintf(content, "100 %d 1", parentuid);
		fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
		SAFE_WRITE(cleanup, 1, fd, content, strlen(content));
		SAFE_CLOSE(cleanup, fd);
		sprintf(path, "/proc/%d/gid_map", childpid);
		sprintf(content, "100 %d 1", parentgid);
		fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
		SAFE_WRITE(cleanup, 1, fd, content, strlen(content));
		SAFE_CLOSE(cleanup, fd);

		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);

		if (waitpid(childpid, &status, 0) < 0)
			tst_brkm(TBROK | TERRNO, cleanup, "waitpid failed");

		if (WIFSIGNALED(status)) {
			tst_resm(TFAIL, "child was killed with signal = %d",
				WTERMSIG(status));
		} else if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
			tst_resm(TFAIL, "child exited abnormally");
		else
			tst_resm(TPASS, "the uid and the gid are right inside "
				"the container");
	}
	cleanup();
	tst_exit();
}
示例#4
0
static void write_utf8(buffer_t buffer, VALUE string) {
    if (!is_legal_utf8_string(RSTRING_PTR(string), RSTRING_LEN(string))) {
        buffer_free(buffer);
        rb_raise(InvalidStringEncoding, "String not valid UTF-8");
    }
    SAFE_WRITE(buffer, RSTRING_PTR(string), RSTRING_LEN(string));
}
示例#5
0
文件: splice01.c 项目: Nudiv/ltp
static void setup(void)
{
	int i;

	if ((tst_kvercmp(2, 6, 17)) < 0) {
		tst_brkm(TCONF, NULL,
		         "The splice is supported 2.6.17 and newer");
	}

	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	if (tst_fs_type(cleanup, ".") == TST_NFS_MAGIC) {
		if  (tst_kvercmp(2, 6, 32) < 0)
			tst_brkm(TCONF, cleanup, "Cannot do splice on a file"
				" on NFS filesystem before 2.6.32");
	}

	for (i = 0; i < TEST_BLOCK_SIZE; i++)
		buffer[i] = i & 0xff;

	fd_in = SAFE_OPEN(cleanup, TESTFILE1, O_WRONLY | O_CREAT | O_TRUNC, 0777);
	SAFE_WRITE(cleanup, 1, fd_in, buffer, TEST_BLOCK_SIZE);
	SAFE_CLOSE(cleanup, fd_in);
	fd_in = 0;
}
示例#6
0
文件: madvise02.c 项目: 1587/ltp
static void setup(void)
{
	int i, fd;

	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);

	pagesize = getpagesize();

	/* Writing 16 pages of random data into this file */
	for (i = 0; i < (pagesize / 2); i++)
		SAFE_WRITE(1, fd, STR, sizeof(STR) - 1);

	SAFE_FSTAT(fd, &st);

	file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize);

	nonalign = file1 + 100;

	ptr_addr = SAFE_MALLOC(st.st_size);
	tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize);

	SAFE_CLOSE(fd);

	tcases_filter();
}
示例#7
0
文件: madvise01.c 项目: sathnaga/ltp
static void setup(void)
{
	unsigned int i;
	int fd;

	SAFE_MKDIR(TMP_DIR, 0664);
	SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL);

	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);

	/* Writing 40 KB of random data into this file [32 * 1280 = 40960] */
	for (i = 0; i < 1280; i++)
		SAFE_WRITE(1, fd, STR, strlen(STR));

	SAFE_FSTAT(fd, &st);

	/* Map the input file into shared memory */
	sfile = SAFE_MMAP(NULL, st.st_size,
			PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

	/* Map the input file into private memory. MADV_HUGEPAGE only works
	 * with private anonymous pages */
	amem = SAFE_MMAP(NULL, st.st_size,
			PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

	SAFE_CLOSE(fd);
}
示例#8
0
static void child_alloc(int *bufsz_arr)
{
	char **foo;
	int i, j;
	char buf[BUFSIZ];
	long count;

	foo = SAFE_MALLOC(tst_exit, nr_iovecs * sizeof(char *));

	count = 0;
	for (i = 0; i < nr_iovecs; i++) {
		foo[i] = SAFE_MALLOC(tst_exit, bufsz_arr[i]);
		for (j = 0; j < bufsz_arr[i]; j++) {
			foo[i][j] = count % 256;
			count++;
		}
	}
	tst_resm(TINFO, "child 0: %d iovecs allocated and initialized.",
		 nr_iovecs);

	/* passing addr via pipe */
	SAFE_CLOSE(tst_exit, pipe_fd[0]);
	snprintf(buf, BUFSIZ, "%p", (void *)foo);
	SAFE_WRITE(tst_exit, 1, pipe_fd[1], buf, strlen(buf));
	SAFE_CLOSE(tst_exit, pipe_fd[1]);

	/* wait until child_invoke is done reading from our VM */
	safe_semop(semid, 0, -1);
}
示例#9
0
文件: fanotify03.c 项目: ArnoldWu/ltp
static void setup(void)
{
	int fd;

	tst_sig(FORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();
	sprintf(fname, "fname_%d", getpid());
	fd = SAFE_OPEN(cleanup, fname, O_CREAT | O_RDWR, 0644);
	SAFE_WRITE(cleanup, 1, fd, fname, 1);
	SAFE_CLOSE(cleanup, fd);

	if ((fd_notify = myfanotify_init(FAN_CLASS_CONTENT, O_RDONLY)) < 0) {
		if (errno == ENOSYS) {
			tst_brkm(TCONF, cleanup,
				 "fanotify is not configured in this kernel.");
		} else {
			tst_brkm(TBROK | TERRNO, cleanup,
				 "fanotify_init failed");
		}
	}

	if (myfanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS_PERM |
			    FAN_OPEN_PERM, AT_FDCWD, fname) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup,
			 "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS_PERM | "
			 "FAN_OPEN_PERM, AT_FDCWD, %s) failed. "
			 "CONFIG_FANOTIFY_ACCESS_PERMISSIONS not "
			 "configured in kernel?", fd_notify, fname);
	}

}
示例#10
0
文件: splice03.c 项目: MohdVara/ltp
void setup(void)
{
	if ((tst_kvercmp(2, 6, 17)) < 0) {
		tst_brkm(TCONF, cleanup, "This test can only run on kernels "
			"that are 2.6.17 or higher");
	}

	TEST_PAUSE;

	tst_sig(FORK, DEF_HANDLER, cleanup);

	tst_tmpdir();

	SAFE_FILE_PRINTF(cleanup, TEST_FILE, STR);
	rdfd = SAFE_OPEN(cleanup, TEST_FILE, O_RDONLY);

	wrfd = SAFE_OPEN(cleanup, TEST_FILE2,
		O_WRONLY | O_CREAT, 0644);

	appendfd = SAFE_OPEN(cleanup, TEST_FILE3,
		O_RDWR | O_CREAT | O_APPEND, 0644);

	SAFE_PIPE(cleanup, pipes);

	SAFE_WRITE(cleanup, 1, pipes[1], STR, sizeof(STR) - 1);
}
示例#11
0
static void setup(void)
{
	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	sprintf(fname, "tfile_%d", getpid());
	fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700);
	SAFE_WRITE(cleanup, 1, fd, buf, BUF_SIZE);
	SAFE_CLOSE(cleanup, fd);

	fd_notify = syscall(__NR_inotify_init1, O_NONBLOCK);
	if (fd_notify < 0) {
		if (errno == ENOSYS) {
			tst_brkm(TCONF, cleanup,
				 "inotify is not configured in this kernel.");
		} else {
			tst_brkm(TBROK | TERRNO, cleanup,
				 "inotify_init failed");
		}
	}

	wd = myinotify_add_watch(fd_notify, fname, IN_ALL_EVENTS);
	if (wd < 0) {
		tst_brkm(TBROK | TERRNO, cleanup,
			 "inotify_add_watch (%d, %s, IN_ALL_EVENTS) failed",
			 fd_notify, fname);
	};

	SAFE_FILE_SCANF(cleanup, "/proc/sys/fs/inotify/max_queued_events",
			"%d", &max_events);
}
示例#12
0
文件: madvise02.c 项目: kraj/ltp
static void setup(void)
{
	int i, fd;

	fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664);

	pagesize = getpagesize();

	/* Writing 16 pages of random data into this file */
	for (i = 0; i < (pagesize / 2); i++)
		SAFE_WRITE(1, fd, STR, sizeof(STR) - 1);

	SAFE_FSTAT(fd, &st);

	file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	file3 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
	shared_anon = SAFE_MMAP(0, MAP_SIZE, PROT_READ, MAP_SHARED |
			MAP_ANONYMOUS, -1, 0);

	nonalign = file1 + 100;

	ptr_addr = SAFE_MALLOC(st.st_size);
	tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize);

	/* unmap as last step to avoid subsequent mmap(s) pick same address */
	SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize);
	SAFE_CLOSE(fd);

	tcases_filter();
}
示例#13
0
文件: openat02.c 项目: qyxia/ltp
void testfunc_largefile(void)
{
	int fd;
	off64_t offset;

	fd = SAFE_OPEN(cleanup, LARGE_FILE,
				O_LARGEFILE | O_RDWR | O_CREAT, 0777);

	offset = lseek64(fd, 4.1*1024*1024*1024, SEEK_SET);
	if (offset == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "lseek64 failed");

	SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1);

	SAFE_CLOSE(cleanup, fd);

	TEST(openat(AT_FDCWD, LARGE_FILE, O_LARGEFILE | O_RDONLY, 0777));

	if (TEST_RETURN == -1) {
		tst_resm(TFAIL, "test O_LARGEFILE for openat failed");
	} else {
		tst_resm(TPASS, "test O_LARGEFILE for openat success");
		SAFE_CLOSE(cleanup, TEST_RETURN);
	}
}
示例#14
0
文件: fanotify01.c 项目: gibertre/ltp
static void setup(void)
{
	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	sprintf(fname, "tfile_%d", getpid());
	fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700);
	SAFE_WRITE(cleanup, 1, fd, fname, 1);

	/* close the file we have open */
	SAFE_CLOSE(cleanup, fd);

	if ((fd_notify = myfanotify_init(FAN_CLASS_NOTIF, O_RDONLY)) < 0) {
		if (errno == ENOSYS) {
			tst_brkm(TCONF, cleanup,
				 "fanotify is not configured in this kernel.");
		} else {
			tst_brkm(TBROK | TERRNO, cleanup,
				 "fanotify_init failed");
		}
	}
}
示例#15
0
文件: mmap1.c 项目: kraj/ltp
int mkfile(int size)
{
	int fd, i;

	fd = SAFE_OPEN(TEST_FILENAME, O_RDWR | O_CREAT, 0600);
	SAFE_UNLINK(TEST_FILENAME);

	for (i = 0; i < size; i++)
		SAFE_WRITE(1, fd, "a", 1);
	SAFE_WRITE(1, fd, "\0", 1);

	if (fsync(fd) == -1)
		tst_brk(TBROK | TERRNO, "fsync()");

	return fd;
}
示例#16
0
static void write_utf8(buffer_t buffer, VALUE string, char check_null) {
    result_t status = check_string(RSTRING_PTR(string), RSTRING_LEN(string),
                                   0, check_null);
    if (status == HAS_NULL) {
        buffer_free(buffer);
        rb_raise(InvalidDocument, "Key names / regex patterns must not contain the NULL byte");
    }
    SAFE_WRITE(buffer, RSTRING_PTR(string), RSTRING_LEN(string));
}
示例#17
0
文件: myxml.c 项目: AbheekG/chapel
/*dump the tree as a Binary file that the other parts of GASNet can later load*/
void myxml_printTreeBIN(FILE *outstream, myxml_node_t *node) {
  /*first go through and count the number of nodes and the get the size of each one*/
  int num_nodes = myxml_countAndLabelNodes(node, 0);
  uint32_t temp = MYHTONL(num_nodes);
  SAFE_WRITE(&temp, sizeof(uint32_t), outstream);

  dump_TreeBIN(outstream, node);

  fprintf(stdout, "tree size: %d nodes\n", num_nodes);
}
示例#18
0
int iLBC_open(FILE* fp, struct amci_file_desc_t* fmt_desc, int options, long h_codec)
{
  DBG("ilbc_open.\n");
  if(options == AMCI_RDONLY){
    return ilbc_read_header(fp,fmt_desc);
  }  else {
    if (fmt_desc->subtype == ILBC30) {
      DBG("writing: #iLBC30\n");
      SAFE_WRITE("#iLBC30\n",9,fp);
    } else if (fmt_desc->subtype == ILBC20) {
      DBG("writing: #iLBC20\n");
      SAFE_WRITE("#iLBC20\n",9,fp);
    } else {
      ERROR("Unsupported ilbc sub type %d\n", fmt_desc->subtype);
      return -1;
    }
    return 0;
  }
}
示例#19
0
文件: linkat01.c 项目: Nan619/ltp-ddt
static void mylinkat_test(struct test_struct *desc)
{
	int fd;

	TEST(mylinkat
	     (*desc->oldfd, desc->oldfn, *desc->newfd, desc->newfn,
	      desc->flags));

	if (TEST_ERRNO == desc->expected_errno) {

		if (STD_FUNCTIONAL_TEST) {

			if (TEST_RETURN == 0 && desc->referencefn1 != NULL) {
				int tnum = rand(), vnum = ~tnum;
				fd = SAFE_OPEN(cleanup, desc->referencefn1,
					       O_RDWR);
				SAFE_WRITE(cleanup, 1, fd, &tnum, sizeof(tnum));
				SAFE_CLOSE(cleanup, fd);

				fd = SAFE_OPEN(cleanup, desc->referencefn2,
					       O_RDONLY);
				SAFE_READ(cleanup, 1, fd, &vnum, sizeof(vnum));
				SAFE_CLOSE(cleanup, fd);

				if (tnum == vnum)
					tst_resm(TPASS,
						 "linkat is functionality correct");
				else {
					tst_resm(TFAIL,
						 "The link file's content isn't "
						 "as same as the original file's "
						 "although linkat returned 0");
				}
			} else {
				if (TEST_RETURN == 0)
					tst_resm(TPASS,
						 "linkat succeeded as expected");
				else
					tst_resm(TPASS | TTERRNO,
						 "linkat failed as expected");
			}
		} else
			tst_resm(TPASS, "Test passed");
	} else {
		if (TEST_RETURN == 0)
			tst_resm(TFAIL, "linkat succeeded unexpectedly");
		else
			tst_resm(TFAIL | TTERRNO,
				 "linkat failed unexpectedly; expected %d - %s",
				 desc->expected_errno,
				 strerror(desc->expected_errno));
	}
}
示例#20
0
static void write_utf8(bson_buffer_t buffer, VALUE string, char check_null) {
    result_t status = check_string((unsigned char*)RSTRING_PTR(string), RSTRING_LEN(string),
                                   1, check_null);
    if (status == HAS_NULL) {
        bson_buffer_free(buffer);
        rb_raise(InvalidDocument, "Key names / regex patterns must not contain the NULL byte");
    } else if (status == NOT_UTF_8) {
        bson_buffer_free(buffer);
        rb_raise(InvalidStringEncoding, "String not valid UTF-8");
    }
    SAFE_WRITE(buffer, RSTRING_PTR(string), (int)RSTRING_LEN(string));
}
示例#21
0
文件: cbson.c 项目: rubygem/website
static void write_utf8(bson_buffer_t buffer, VALUE string, int allow_null) {
    result_t status = validate_utf8_encoding(
        (const char*)RSTRING_PTR(string), RSTRING_LEN(string), allow_null);

    if (status == HAS_NULL) {
        bson_buffer_free(buffer);
        rb_raise(InvalidDocument, "Key names / regex patterns must not contain the NULL byte");
    } else if (status == INVALID_UTF8) {
        bson_buffer_free(buffer);
        rb_raise(InvalidStringEncoding, "String not valid UTF-8");
    }
    SAFE_WRITE(buffer, RSTRING_PTR(string), (int)RSTRING_LEN(string));
}
示例#22
0
int main(int argc, char **argv)
{
	char buf[10];
	int fds[2];

	buf[9] = '\0';

	system("cp " __FILE__ " " __FILE__ "~");
	printf("%s\n", SAFE_BASENAME(NULL, *argv));
	printf("%s\n", SAFE_DIRNAME(NULL, *argv));
	fd = SAFE_OPEN(cleanup, __FILE__ "~", O_RDWR);
	SAFE_READ(cleanup, 0, fd, buf, 9);
	printf("buf: %s\n", buf);
	SAFE_READ(cleanup, 1, fd, buf, 9);
	printf("buf: %s\n", buf);
	SAFE_WRITE(cleanup, 0, -1, buf, 9);
	SAFE_WRITE(NULL, 0, fd, buf, 9);
	SAFE_WRITE(NULL, 1, fd, buf, 9);
	SAFE_PIPE(NULL, fds);

	return 0;
}
示例#23
0
static void check_epoll_ctl(int opt, int exp_num)
{
	int res;
	unsigned int events;
	char write_buf[] = "test";
	char read_buf[sizeof(write_buf)];
	struct epoll_event res_evs[2];

	events = EPOLLIN;
	if (exp_num == 2)
		events |= EPOLLOUT;

	SAFE_WRITE(1, fd[1], write_buf, sizeof(write_buf));

	while (events) {
		int events_matched = 0;
		bzero(res_evs, sizeof(res_evs));

		res = epoll_wait(epfd, res_evs, 2, -1);
		if (res <= 0) {
			tst_res(TFAIL | TERRNO, "epoll_wait() returned %i",
				res);
			goto end;
		}

		if ((events & EPOLLIN) &&
		    has_event(res_evs, 2, fd[0], EPOLLIN)) {
			events_matched++;
			events &= ~EPOLLIN;
		}

		if ((events & EPOLLOUT) &&
		    has_event(res_evs, 2, fd[1], EPOLLOUT)) {
			events_matched++;
			events &= ~EPOLLOUT;
		}

		if (res != events_matched) {
			tst_res(TFAIL,
				"epoll_wait() returned unexpected events");
			goto end;
		}
	}

	tst_res(TPASS, "epoll_ctl() succeeds with op %i", opt);

end:
	SAFE_READ(1, fd[0], read_buf, sizeof(write_buf));
}
示例#24
0
文件: readv01.c 项目: anasuyams/ltp
static void setup(void)
{
	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	memset(buf, 0x42, sizeof(buf));

	fd = SAFE_OPEN(cleanup, "data_file", O_WRONLY | O_CREAT, 0666);
	SAFE_WRITE(cleanup, 1, fd, buf, sizeof(buf));
	SAFE_CLOSE(cleanup, fd);
	fd = SAFE_OPEN(cleanup, "data_file", O_RDONLY);
}
示例#25
0
static void write_doc(buffer_t buffer, VALUE hash, VALUE check_keys) {
    buffer_position start_position = buffer_get_position(buffer);
    buffer_position length_location = buffer_save_space(buffer, 4);
    buffer_position length;
    VALUE id_str = rb_str_new2("_id");
    VALUE id_sym = ID2SYM(rb_intern("_id"));

    if (length_location == -1) {
        rb_raise(rb_eNoMemError, "failed to allocate memory in buffer.c");
    }

    if (rb_funcall(hash, rb_intern("has_key?"), 1, id_str) == Qtrue) {
        VALUE id = rb_hash_aref(hash, id_str);
        write_element_allow_id(id_str, id, pack_extra(buffer, check_keys), 1);
    } else if (rb_funcall(hash, rb_intern("has_key?"), 1, id_sym) == Qtrue) {
        VALUE id = rb_hash_aref(hash, id_sym);
        write_element_allow_id(id_sym, id, pack_extra(buffer, check_keys), 1);
    }

    // we have to check for an OrderedHash and handle that specially
    if (strcmp(rb_class2name(RBASIC(hash)->klass), "OrderedHash") == 0) {
        VALUE keys = rb_funcall(hash, rb_intern("keys"), 0);
        int i;
        for(i = 0; i < RARRAY_LEN(keys); i++) {
            VALUE key = RARRAY_PTR(keys)[i];
            VALUE value = rb_hash_aref(hash, key);

            write_element(key, value, pack_extra(buffer, check_keys));
        }
    } else {
        rb_hash_foreach(hash, write_element, pack_extra(buffer, check_keys));
    }

    // write null byte and fill in length
    SAFE_WRITE(buffer, &zero, 1);
    length = buffer_get_position(buffer) - start_position;

    // make sure that length doesn't exceed 4MB
    if (length > 4 * 1024 * 1024) {
      buffer_free(buffer);
      rb_raise(InvalidDocument, "Document too large: BSON documents are limited to 4MB.");
      return;
    }
    SAFE_WRITE_AT_POS(buffer, length_location, (const char*)&length, 4);
}
示例#26
0
文件: tee02.c 项目: ruanzhengwang/ltp
static void setup(void)
{
    if ((tst_kvercmp(2, 6, 17)) < 0) {
        tst_brkm(TCONF, cleanup, "This test can only run on kernels "
                 "that are 2.6.17 or higher");
    }

    TEST_PAUSE;

    tst_sig(FORK, DEF_HANDLER, cleanup);

    tst_tmpdir();

    fd = SAFE_OPEN(cleanup, TEST_FILE, O_RDWR | O_CREAT);

    SAFE_PIPE(cleanup, pipes);
    SAFE_WRITE(cleanup, 1, pipes[1], STR, sizeof(STR) - 1);
}
示例#27
0
static void test01(void)
{
    tst_resm(TINFO, "allocate '%zu' bytes", buf_size);

    if (fallocate(fd, 0, 0, buf_size) == -1) {
        if (errno == ENOSYS || errno == EOPNOTSUPP)
            tst_brkm(TCONF, cleanup, "fallocate() not supported");
        tst_brkm(TFAIL | TERRNO, cleanup, "fallocate() failed");
    }

    char buf[buf_size];

    fill_tst_buf(buf);

    SAFE_WRITE(cleanup, 1, fd, buf, buf_size);

    tst_resm(TPASS, "test-case succeeded");
}
static void child_alloc(void)
{
	char *foo;
	char buf[BUFSIZ];

	foo = SAFE_MALLOC(tst_exit, len + 1);
	strncpy(foo, tst_string, len);
	foo[len] = '\0';
	tst_resm(TINFO, "child 0: memory allocated and initialized.");

	/* passing addr of string "foo" via pipe */
	SAFE_CLOSE(tst_exit, pipe_fd[0]);
	snprintf(buf, BUFSIZ, "%p", foo);
	SAFE_WRITE(tst_exit, 1, pipe_fd[1], buf, strlen(buf));
	SAFE_CLOSE(tst_exit, pipe_fd[1]);

	/* wait until child_invoke is done reading from our VM */
	safe_semop(semid, 0, -1);
}
示例#29
0
文件: readahead02.c 项目: kraj/ltp
static void create_testfile(int use_overlay)
{
	int fd;
	char *tmp;
	size_t i;

	sprintf(testfile, "%s/testfile", use_overlay ? OVL_MNT : MNTPOINT);
	tst_res(TINFO, "creating test file of size: %zu", testfile_size);
	tmp = SAFE_MALLOC(pagesize);

	/* round to page size */
	testfile_size = testfile_size & ~((long)pagesize - 1);

	fd = SAFE_CREAT(testfile, 0644);
	for (i = 0; i < testfile_size; i += pagesize)
		SAFE_WRITE(1, fd, tmp, pagesize);
	SAFE_FSYNC(fd);
	SAFE_CLOSE(fd);
	free(tmp);
}
示例#30
0
文件: open12.c 项目: CSRedRat/ltp
static void test_append(void)
{
	off_t len1, len2;

	TEST(open(TEST_FILE, O_RDWR | O_APPEND, 0777));

	if (TEST_RETURN == -1) {
		tst_resm(TFAIL | TTERRNO, "open failed");
		return;
	}

	len1 = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
	SAFE_WRITE(cleanup, 1, TEST_RETURN, TEST_FILE, sizeof(TEST_FILE));
	len2 = SAFE_LSEEK(cleanup, TEST_RETURN, 0, SEEK_CUR);
	SAFE_CLOSE(cleanup, TEST_RETURN);

	if (len2 > len1)
		tst_resm(TPASS, "test O_APPEND for open success");
	else
		tst_resm(TFAIL, "test O_APPEND for open failed");
}