コード例 #1
0
ファイル: swapon01.c プロジェクト: LeqiaoP1/ltp
/* setup() - performs all ONE TIME setup for this test */
void setup()
{
	tst_sig(FORK, DEF_HANDLER, cleanup);

	/* Check whether we are root */
	if (geteuid() != 0) {
		tst_brkm(TBROK, NULL, "Test must be run as root");
	}

	TEST_PAUSE;

	tst_tmpdir();

	if (tst_is_cwd_tmpfs()) {
		tst_brkm(TCONF, cleanup,
			 "Cannot do swapon on a file located on a tmpfs filesystem");
	}

	if (tst_is_cwd_nfs()) {
		tst_brkm(TCONF, cleanup,
			 "Cannot do swapon on a file located on a nfs filesystem");
	}

	make_swapfile(cleanup, "swapfile01");
}
コード例 #2
0
ファイル: swapon01.c プロジェクト: CSRedRat/ltp
static void setup(void)
{
	tst_sig(FORK, DEF_HANDLER, cleanup);

	tst_require_root();

	TEST_PAUSE;

	tst_tmpdir();

	switch ((fs_type = tst_fs_type(cleanup, "."))) {
	case TST_NFS_MAGIC:
	case TST_TMPFS_MAGIC:
		tst_brkm(TCONF, cleanup,
			 "Cannot do swapon on a file on %s filesystem",
			 tst_fs_type_name(fs_type));
	break;
	}

	make_swapfile(cleanup, "swapfile01");
}
コード例 #3
0
ファイル: swapon03.c プロジェクト: AbhiramiP/ltp
/*
 * Create 33 and activate 30 swapfiles.
 */
static int setup_swap(void)
{
	pid_t pid;
	int j, fd;
	int status;
	int res = 0;
	char filename[15];
	char buf[BUFSIZ + 1];

	/* Find out how many swapfiles (1 line per entry) already exist */
	swapfiles = 0;

	if (seteuid(0) < 0) {
		tst_brkm(TFAIL | TERRNO, cleanup, "Failed to call seteuid");
	}

	/* This includes the first (header) line */
	if ((fd = open("/proc/swaps", O_RDONLY)) == -1) {
		tst_brkm(TFAIL | TERRNO, cleanup,
			 "Failed to find out existing number of swap files");
	}
	do {
		char *p = buf;
		res = read(fd, buf, BUFSIZ);
		if (res < 0) {
			tst_brkm(TFAIL | TERRNO, cleanup,
				 "Failed to find out existing number of swap "
				 "files");
		}
		buf[res] = '\0';
		while ((p = strchr(p, '\n'))) {
			p++;
			swapfiles++;
		}
	} while (BUFSIZ <= res);
	close(fd);
	if (swapfiles)
		swapfiles--;	/* don't count the /proc/swaps header */

	if (swapfiles < 0) {
		tst_brkm(TFAIL, cleanup,
			 "Failed to find existing number of swapfiles");
	}

	/* Determine how many more files are to be created */
	swapfiles = MAX_SWAPFILES - swapfiles;
	if (swapfiles > MAX_SWAPFILES) {
		swapfiles = MAX_SWAPFILES;
	}

	pid = FORK_OR_VFORK();
	if (pid == 0) {
		/*create and turn on remaining swapfiles */
		for (j = 0; j < swapfiles; j++) {

			/* prepare filename for the iteration */
			if (sprintf(filename, "swapfile%02d", j + 2) < 0) {
				printf("sprintf() failed to create "
				       "filename");
				exit(1);
			}

			/* Create the swapfile */
			make_swapfile(cleanup, filename);

			/* turn on the swap file */
			res = ltp_syscall(__NR_swapon, filename, 0);
			if (res != 0) {
				if (fs_type == TST_BTRFS_MAGIC && errno == EINVAL)
					exit(2);

				if (errno == EPERM) {
					printf("Successfully created %d "
					       "swapfiles\n", j);
					break;
				} else {
					printf("Failed to create "
					       "swapfile: %s\n", filename);
					exit(1);
				}
			}
		}
		exit(0);
	} else
		waitpid(pid, &status, 0);

	switch (WEXITSTATUS(status)) {
	case 0:
	break;
	case 2:
		tst_brkm(TCONF, cleanup, "Swapfile on BTRFS not implemeted");
	break;
	default:
		tst_brkm(TFAIL, cleanup, "Failed to setup swaps");
	break;
	}

	/* Create all needed extra swapfiles for testing */
	for (j = 0; j < testfiles; j++)
		make_swapfile(cleanup, swap_testfiles[j].filename);

	return 0;

}