Esempio n. 1
0
int main(int ac, char **av)
{
	int lc;
	int incr;
	uintptr_t nbrkpt;		/* new brk point value */
	uintptr_t cur_brk_val;	/* current size returned by sbrk */
	uintptr_t aft_brk_val;	/* current size returned by sbrk */

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	/*
	 * Attempt to control how fast we get to test max size.
	 * Every MAX_SIZE_LC'th lc will be fastest test will reach max size.
	 */
	incr = (Max_brk_byte_size - Beg_brk_val) / (MAX_SIZE_LC / 2);

	if ((incr * 2) < 4096)	/* make sure that process will grow */
		incr += 4096 / 2;

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

		tst_count = 0;

		/*
		 * Determine new value to give brk
		 * Every even lc value, grow by 2 incr and
		 * every odd lc value, strink by one incr.
		 * If lc is equal to 3, no change, special case.
		 */
		cur_brk_val = (uintptr_t)sbrk(0);
		if (lc == 3) {
			nbrkpt = cur_brk_val;	/* no change, special one time case */
		} else if ((lc % 2) == 0) {
			/*
			 * grow
			 */
			nbrkpt = cur_brk_val + (2 * incr);

			if (nbrkpt > Max_brk_byte_size)
				nbrkpt = Beg_brk_val;	/* start over */

		} else {
			/*
			 * shrink
			 */
			nbrkpt = cur_brk_val - incr;
		}

/****
    printf("cur_brk_val = %d, nbrkpt = %d, incr = %d, lc = %d\n",
	cur_brk_val, nbrkpt, incr, lc);
****/

		TEST(brk((char *)nbrkpt));

		if (TEST_RETURN == -1) {

			aft_brk_val = (uintptr_t)sbrk(0);
			tst_resm(TFAIL | TTERRNO,
				 "brk(%"PRIuPTR") failed (size before %"PRIuPTR", after %"PRIuPTR")",
				 nbrkpt, cur_brk_val, aft_brk_val);

		} else {
			aft_brk_val = (uintptr_t)sbrk(0);
			if (aft_brk_val == nbrkpt) {

				tst_resm(TPASS,
					 "brk(%"PRIuPTR") returned %"PRIuPTR", new size verified by sbrk",
					 nbrkpt, TEST_RETURN);
			} else {
				tst_resm(TFAIL,
					 "brk(%"PRIuPTR") returned %"PRIuPTR", sbrk before %"PRIuPTR", after %"PRIuPTR"",
					 nbrkpt, TEST_RETURN,
					 cur_brk_val, aft_brk_val);
			}
		}

	}

	cleanup();
	tst_exit();
}
Esempio n. 2
0
File: mkdir04.c Progetto: Nan619/ltp
int main(int ac, char **av)
{
	int lc;
	int rval;
	pid_t pid, pid1;
	int status;

	/*
	 * parse standard options
	 */
	tst_parse_opts(ac, av, NULL, NULL);

	/*
	 * perform global setup for test
	 */
	setup();

	/*
	 * check looping state if -i option given
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		tst_count = 0;

		/* Initialize the test directories name */
		sprintf(tstdir1, "tstdir1.%d", getpid());
		if ((pid = FORK_OR_VFORK()) < 0) {
			tst_brkm(TBROK, cleanup, "fork #1 failed");
		}

		if (pid == 0) {	/* first child */
			rval = setreuid(nobody_uid, nobody_uid);
			if (rval < 0) {
				tst_resm(TFAIL, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 nobody_uid, nobody_uid);
				perror("setreuid");
				exit(1);
			}
			/* create the parent directory with 0700 permits */
			if (mkdir(tstdir1, PERMS) == -1) {
				tst_resm(TFAIL, "mkdir(%s, %#o) Failed",
					 tstdir1, PERMS);
				exit(1);
			}
			/* create tstdir1 succeeded */
			exit(0);
		}
		wait(&status);
		if (WEXITSTATUS(status) != 0) {
			tst_brkm(TFAIL, cleanup,
				 "Test to check mkdir EACCES failed"
				 "in create parent directory");
		}

		sprintf(tstdir2, "%s/tst", tstdir1);

		if ((pid1 = FORK_OR_VFORK()) < 0) {
			tst_brkm(TBROK, cleanup, "fork #2 failed");
		}

		if (pid1 == 0) {	/* second child */
			rval = setreuid(bin_uid, bin_uid);
			if (rval < 0) {
				tst_resm(TFAIL, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 bin_uid, bin_uid);
				perror("setreuid");
				exit(1);
			}
			if (mkdir(tstdir2, PERMS) != -1) {
				tst_resm(TFAIL, "mkdir(%s, %#o) unexpected "
					 "succeeded", tstdir2, PERMS);
				exit(1);
			}
			if (errno != EACCES) {
				tst_resm(TFAIL, "Expected EACCES got %d",
					 errno);
				exit(1);
			}
			/* PASS */
			exit(0);
		}
		waitpid(pid1, &status, 0);
		if (WEXITSTATUS(status) == 0) {
			tst_resm(TPASS, "Test to attempt to creat a directory "
				 "in a directory having no permissions "
				 "SUCCEEDED in setting errno to EACCES");
		} else {
			tst_resm(TFAIL, "Test to attempt to creat a directory "
				 "in a directory having no permissions FAILED");
			cleanup();
		}
	}

	/*
	 * cleanup and exit
	 */
	cleanup();
	tst_exit();

}
int main(int ac, char **av)
{
	int lc;
	int Hflag = 0;
	int sflag = 0;
	int huge_pagesize = 0;

	option_t options[] = {
		{"H:", &Hflag, &Hopt},
		{"s:", &sflag, &nr_opt},
		{NULL, NULL, NULL}
	};

	tst_parse_opts(ac, av, options, &help);

	if (!Hflag) {
		tst_tmpdir();
		Hopt = tst_get_tmpdir();
	}
	if (sflag)
		hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX);

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/* Creat a temporary file used for huge mapping */
		fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666);
		if (fildes < 0)
			tst_brkm(TFAIL | TERRNO, cleanup, "open %s failed",
				 TEMPFILE);

		tst_count = 0;

		/* Note the number of free huge pages BEFORE testing */
		freepages = read_meminfo("HugePages_Free:");
		beforetest = freepages;

		/* Note the size of huge page size BEFORE testing */
		huge_pagesize = read_meminfo("Hugepagesize:");
		tst_resm(TINFO, "Size of huge pages is %d KB", huge_pagesize);

#if __WORDSIZE == 32
		tst_resm(TINFO, "Total amount of free huge pages is %d",
			 freepages);
		tst_resm(TINFO, "Max number allowed for 1 mmap file in"
			 " 32-bits is 128");
		if (freepages > 128)
			freepages = 128;
#endif
		mapsize = (long long)freepages *huge_pagesize * 1024;
		addr = mmap(NULL, mapsize, PROT_READ | PROT_WRITE,
			    MAP_SHARED, fildes, 0);
		sleep(2);
		if (addr == MAP_FAILED) {
			tst_resm(TFAIL | TERRNO, "mmap() Failed on %s",
				 TEMPFILE);
			close(fildes);
			continue;
		} else {
			tst_resm(TPASS,
				 "Succeeded mapping file using %ld pages",
				 freepages);
			/* force to allocate page and change HugePages_Free */
			*(int *)addr = 0;
		}

		/*
		 * Make sure the number of free huge pages
		 * AFTER testing decreased
		 */
		aftertest = read_meminfo("HugePages_Free:");
		hugepagesmapped = beforetest - aftertest;
		if (hugepagesmapped < 1)
			tst_resm(TWARN, "Number of HUGEPAGES_FREE stayed the"
				 " same. Okay if multiple copies running due"
				 " to test collision.");

		/* Clean up things in case we are looping */
		/* Unmap the mapped memory */
		if (munmap(addr, mapsize) != 0)
			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");

		close(fildes);
	}

	cleanup();
	tst_exit();
}
Esempio n. 4
0
File: readdir01.c Progetto: 1587/ltp
/***********************************************************************
 * Main
 ***********************************************************************/
int main(int ac, char **av)
{
	int lc;
	int cnt;
	int nfiles, fd;
	char fname[255];
	DIR *test_dir;
	struct dirent *dptr;

	tst_parse_opts(ac, av, options, &help);

	if (Nflag) {
		if (sscanf(Nfilearg, "%i", &Nfiles) != 1) {
			tst_brkm(TBROK, NULL, "--N option arg is not a number");
		}
	}

    /***************************************************************
     * perform global setup for test
     ***************************************************************/
	/* Next you should run a setup routine to make sure your environment is
	 * sane.
	 */
	setup();

    /***************************************************************
     * check looping state
     ***************************************************************/
	/* TEST_LOOPING() is a macro that will make sure the test continues
	 * looping according to the standard command line args.
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		tst_count = 0;

		if (Nfiles)
			nfiles = Nfiles;
		else
			/* min of 10 links and max of a 100 links */
			nfiles = (lc % 90) + 10;

		/* create a bunch of files to look at */
		for (cnt = 0; cnt < nfiles; cnt++) {

			sprintf(fname, "%s%d", Basename, cnt);
			if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
				tst_brkm(TBROK, cleanup,
					 "open(%s, O_RDWR|O_CREAT,0700) Failed, errno=%d : %s",
					 fname, errno, strerror(errno));
			} else if (write(fd, "hello\n", 6) < 0) {
				tst_brkm(TBROK, cleanup,
					 "write(%s, \"hello\\n\", 6) Failed, errno=%d : %s",
					 fname, errno, strerror(errno));
			} else if (close(fd) < 0) {
				tst_resm(TWARN,
					"close(%s) Failed, errno=%d : %s",
					fname, errno, strerror(errno));
			}
		}

		if ((test_dir = opendir(".")) == NULL) {
			tst_resm(TFAIL, "opendir(\".\") Failed, errno=%d : %s",
				 errno, strerror(errno));
		} else {
			/* count the entries we find to see if any are missing */
			cnt = 0;
			errno = 0;
			while ((dptr = readdir(test_dir)) != 0) {
				if (strcmp(dptr->d_name, ".")
				    && strcmp(dptr->d_name, ".."))
					cnt++;
			}

			if (errno != 0) {
				tst_resm(TFAIL,
					 "readir(test_dir) Failed on try %d, errno=%d : %s",
					 cnt + 1, errno, strerror(errno));
			}
			if (cnt == nfiles) {
				tst_resm(TPASS,
					 "found all %d that were created",
					 nfiles);
			} else if (cnt > nfiles) {
				tst_resm(TFAIL,
					 "found more files than were created");
				tst_resm(TINFO, "created: %d, found: %d",
					 nfiles, cnt);
			} else {
				tst_resm(TFAIL,
					 "found less files than were created");
				tst_resm(TINFO, "created: %d, found: %d",
					 nfiles, cnt);
			}
		}

		/* Here we clean up after the test case so we can do another iteration.
		 */
		for (cnt = 0; cnt < nfiles; cnt++) {

			sprintf(fname, "%s%d", Basename, cnt);

			if (unlink(fname) == -1) {
				tst_resm(TWARN,
					"unlink(%s) Failed, errno=%d : %s",
					Fname, errno, strerror(errno));
			}
		}

	}

    /***************************************************************
     * cleanup and exit
     ***************************************************************/
	cleanup();

	tst_exit();
}
Esempio n. 5
0
File: pipe11.c Progetto: 1587/ltp
int main(int ac, char **av)
{
	int lc;

	int i;
	int fork_ret, status;
	int written;		/* no of chars read and written */

	tst_parse_opts(ac, av, NULL, NULL);
#ifdef UCLINUX
	maybe_run_child(&do_child_uclinux, "ddddd", &fd[0], &fd[1], &kidid,
			&ncperchild, &szcharbuf);
#endif

	setup();

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

		/* reset tst_count in case we are looping */
		tst_count = 0;

		TEST(pipe(fd));

		if (TEST_RETURN != 0) {
			tst_resm(TFAIL, "pipe creation failed");
			continue;
		}

		written = write(fd[1], wrbuf, szcharbuf);
		if (written != szcharbuf) {
			tst_brkm(TBROK, cleanup, "write to pipe failed");
		}

refork:
		++kidid;
		fork_ret = FORK_OR_VFORK();

		if (fork_ret < 0) {
			tst_brkm(TBROK, cleanup, "fork() failed");
		}

		if ((fork_ret != 0) && (fork_ret != -1) && (kidid < numchild)) {
			goto refork;
		}

		if (fork_ret == 0) {	/* child */
#ifdef UCLINUX
			if (self_exec(av[0], "ddddd", fd[0], fd[1], kidid,
				      ncperchild, szcharbuf) < 0) {
				tst_brkm(TBROK, cleanup, "self_exec failed");
			}
#else
			do_child();
#endif
		}

		/* parent */
		sleep(5);
		tst_resm(TINFO, "There are %d children to wait for", kidid);
		for (i = 1; i <= kidid; ++i) {
			wait(&status);
			if (status == 0) {
				tst_resm(TPASS, "child %d exitted successfully",
					 i);
			} else {
				tst_resm(TFAIL, "child %d exitted with bad "
					 "status", i);
			}
		}
	}
	cleanup();

	tst_exit();
}
Esempio n. 6
0
File: utime01.c Progetto: 1587/ltp
int main(int ac, char **av)
{
	struct stat stat_buf;	/* struct buffer to hold file info. */
	int lc;
	long type;
	time_t modf_time, access_time;
	time_t pres_time;	/* file modification/access/present time */

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	switch ((type = tst_fs_type(cleanup, "."))) {
	case TST_NFS_MAGIC:
		if (tst_kvercmp(2, 6, 18) < 0)
			tst_brkm(TCONF, cleanup, "Cannot do utime on a file"
				" on %s filesystem before 2.6.18",
				 tst_fs_type_name(type));
		break;
	case TST_V9FS_MAGIC:
		tst_brkm(TCONF, cleanup,
			 "Cannot do utime on a file on %s filesystem",
			 tst_fs_type_name(type));
		break;
	}

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

		tst_count = 0;

		/*
		 * Invoke utime(2) to set TEMP_FILE access and
		 * modification times to the current time.
		 */
		TEST(utime(TEMP_FILE, NULL));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE);
		} else {
			/*
			 * Sleep for a second so that mod time and
			 * access times will be different from the
			 * current time
			 */
			sleep(2);

			/*
			 * Get the current time now, after calling
			 * utime(2)
			 */
			pres_time = time(NULL);

			/*
			 * Get the modification and access times of
			 * temporary file using stat(2).
			 */
			SAFE_STAT(cleanup, TEMP_FILE, &stat_buf);
			modf_time = stat_buf.st_mtime;
			access_time = stat_buf.st_atime;

			/* Now do the actual verification */
			if (modf_time <= curr_time ||
			    modf_time >= pres_time ||
			    access_time <= curr_time ||
			    access_time >= pres_time) {
				tst_resm(TFAIL, "%s access and "
					 "modification times not set",
					 TEMP_FILE);
			} else {
				tst_resm(TPASS, "Functionality of "
					 "utime(%s, NULL) successful",
					 TEMP_FILE);
			}
		}
		tst_count++;
	}

	cleanup();
	tst_exit();
}
Esempio n. 7
0
File: ptrace05.c Progetto: kraj/ltp
int main(int argc, char **argv)
{

	int end_signum = -1;
	int signum;
	int start_signum = -1;
	int status;

	pid_t child;

	tst_parse_opts(argc, argv, NULL, NULL);

	if (start_signum == -1) {
		start_signum = 0;
	}
	if (end_signum == -1) {
		end_signum = SIGRTMAX;
	}

	for (signum = start_signum; signum <= end_signum; signum++) {

		if (signum >= __SIGRTMIN && signum < SIGRTMIN)
			continue;

		switch (child = fork()) {
		case -1:
			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
		case 0:

			if (ptrace(PTRACE_TRACEME, 0, NULL, NULL) != -1) {
				tst_resm(TINFO, "[child] Sending kill(.., %d)",
					 signum);
				if (kill(getpid(), signum) < 0) {
					tst_resm(TINFO | TERRNO,
						 "[child] kill(.., %d) failed.",
						 signum);
				}
			} else {

				/*
				 * This won't increment the TST_COUNT var.
				 * properly, but it'll show up as a failure
				 * nonetheless.
				 */
				tst_resm(TFAIL | TERRNO,
					 "Failed to ptrace(PTRACE_TRACEME, ...) "
					 "properly");

			}
			/* Shouldn't get here if signum == 0. */
			exit((signum == 0 ? 0 : 2));
			break;

		default:

			waitpid(child, &status, 0);

			switch (signum) {
			case 0:
				if (WIFEXITED(status)
				    && WEXITSTATUS(status) == 0) {
					tst_resm(TPASS,
						 "kill(.., 0) exited "
						 "with 0, as expected.");
				} else {
					tst_resm(TFAIL,
						 "kill(.., 0) didn't exit "
						 "with 0.");
				}
				break;
			case SIGKILL:
				if (WIFSIGNALED(status)) {
					/* SIGKILL must be uncatchable. */
					if (WTERMSIG(status) == SIGKILL) {
						tst_resm(TPASS,
							 "Killed with SIGKILL, "
							 "as expected.");
					} else {
						tst_resm(TPASS,
							 "Didn't die with "
							 "SIGKILL (?!) ");
					}
				} else if (WIFEXITED(status)) {
					tst_resm(TFAIL,
						 "Exited unexpectedly instead "
						 "of dying with SIGKILL.");
				} else if (WIFSTOPPED(status)) {
					tst_resm(TFAIL,
						 "Stopped instead of dying "
						 "with SIGKILL.");
				}
				break;
				/* All other processes should be stopped. */
			default:
				if (WIFSTOPPED(status)) {
					tst_resm(TPASS, "Stopped as expected");
				} else {
					tst_resm(TFAIL, "Didn't stop as "
						 "expected.");
					if (kill(child, 0)) {
						tst_resm(TINFO,
							 "Is still alive!?");
					} else if (WIFEXITED(status)) {
						tst_resm(TINFO,
							 "Exited normally");
					} else if (WIFSIGNALED(status)) {
						tst_resm(TINFO,
							 "Was signaled with "
							 "signum=%d",
							 WTERMSIG(status));
					}

				}

				break;

			}

		}
		/* Make sure the child dies a quick and painless death ... */
		kill(child, 9);

	}

	tst_exit();

}
Esempio n. 8
0
int main(int ac, char **av)
{
	struct stat stat_buf;	/* stat(2) struct contents */
	int lc, i;
	off_t file_length2;	/* test file length */
	off_t file_length1;	/* test file length */
	int rbytes;		/* bytes read from testfile */
	int read_len = 0;	/* total no. of bytes read from testfile */
	int err_flag = 0;	/* error indicator flag */

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

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

		tst_count = 0;

		/*
		 * Call truncate(2) to truncate a test file to a
		 * specified length (TRUNC_LEN1).
		 */
		TEST(truncate(TESTFILE, TRUNC_LEN1));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL,
				 "truncate(%s, %d) Failed, errno=%d : %s",
				 TESTFILE, TRUNC_LEN1, TEST_ERRNO,
				 strerror(TEST_ERRNO));
		} else {
			/*
			 * Get the testfile information using
			 * stat(2).
			 */
			if (stat(TESTFILE, &stat_buf) < 0) {
				tst_brkm(TFAIL, cleanup, "stat(2) of "
					 "%s failed after 1st truncate, "
					 "error:%d", TESTFILE, errno);
			}
			file_length1 = stat_buf.st_size;

			/*
			 * Set the file pointer of testfile to the
			 * beginning of the file.
			 */
			if (lseek(fd, 0, SEEK_SET) < 0) {
				tst_brkm(TFAIL, cleanup, "lseek(2) on "
					 "%s failed after 1st truncate, "
					 "error:%d", TESTFILE, errno);
			}

			/* Read the testfile from the beginning. */
			while ((rbytes = read(fd, tst_buff,
					      sizeof(tst_buff))) > 0) {
				read_len += rbytes;
			}

			/*
			 * Execute truncate(2) again to truncate
			 * testfile to a size TRUNC_LEN2.
			 */
			TEST(truncate(TESTFILE, TRUNC_LEN2));

			if (TEST_RETURN == -1) {
				tst_resm(TFAIL, "truncate of %s to "
					 "size %d Failed, errno=%d : %s",
					 TESTFILE, TRUNC_LEN2,
					 TEST_ERRNO,
					 strerror(TEST_ERRNO));
			}

			/*
			 * Get the testfile information using
			 * stat(2)
			 */
			if (stat(TESTFILE, &stat_buf) < 0) {
				tst_brkm(TFAIL, cleanup, "stat(2) of "
					 "%s failed after 2nd truncate, "
					 "error:%d", TESTFILE, errno);
			}
			file_length2 = stat_buf.st_size;

			/*
			 * Set the file pointer of testfile to the
			 * offset TRUNC_LEN1 of testfile.
			 */
			if (lseek(fd, TRUNC_LEN1, SEEK_SET) < 0) {
				tst_brkm(TFAIL, cleanup, "lseek(2) on "
					 "%s failed after 2nd truncate, "
					 "error:%d", TESTFILE, errno);
			}

			/* Read the testfile contents till EOF */
			while ((rbytes = read(fd, tst_buff,
					      sizeof(tst_buff))) > 0) {
				for (i = 0; i < rbytes; i++) {
					if (tst_buff[i] != 0) {
						err_flag++;
					}
				}
			}

			/*
			 * Check for expected size of testfile after
			 * issuing truncate(2) on it.
			 */
			if ((file_length1 != TRUNC_LEN1) ||
			    (file_length2 != TRUNC_LEN2) ||
			    (read_len != TRUNC_LEN1) ||
			    (err_flag != 0)) {
				tst_resm(TFAIL, "Functionality of "
					 "truncate(2) on %s Failed",
					 TESTFILE);
			} else {
				tst_resm(TPASS,
					 "Functionality of truncate(2) "
					 "on %s successful", TESTFILE);
			}
		}
		tst_count++;	/* incr. TEST_LOOP counter */
	}

	cleanup();
	tst_exit();
}
Esempio n. 9
0
int main(int ac, char **av)
{
	int lc, i;
	int len, stop;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/*
		 * generate events
		 */
		fd = SAFE_OPEN(cleanup, fname, O_RDWR);

		for (i = 0; i < max_events; i++) {
			SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
			SAFE_READ(cleanup, 1, fd, buf, BUF_SIZE);
			SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
			SAFE_WRITE(cleanup, 1, fd, buf, BUF_SIZE);
		}

		SAFE_CLOSE(cleanup, fd);

		stop = 0;
		while (!stop) {
			/*
			 * get list on events
			 */
			len = read(fd_notify, event_buf, EVENT_BUF_LEN);
			if (len < 0) {
				tst_brkm(TBROK | TERRNO, cleanup,
					 "read(%d, buf, %zu) failed",
					 fd_notify, EVENT_BUF_LEN);
			}

			/*
			 * check events
			 */
			i = 0;
			while (i < len) {
				struct inotify_event *event;

				event = (struct inotify_event *)&event_buf[i];
				if (event->mask != IN_ACCESS &&
				    event->mask != IN_MODIFY &&
				    event->mask != IN_OPEN &&
				    event->mask != IN_Q_OVERFLOW) {
					tst_resm(TFAIL,
						 "get event: wd=%d mask=%x "
						 "cookie=%u (expected 0) len=%u",
						 event->wd, event->mask,
						 event->cookie, event->len);
					stop = 1;
					break;
				}
				if (event->mask == IN_Q_OVERFLOW) {
					if (event->len != 0 ||
					    event->cookie != 0 ||
					    event->wd != -1) {
						tst_resm(TFAIL,
							 "invalid overflow event: "
							 "wd=%d mask=%x "
							 "cookie=%u len=%u",
							 event->wd, event->mask,
							 event->cookie,
							 event->len);
						stop = 1;
						break;
					}
					if ((int)(i + EVENT_SIZE) != len) {
						tst_resm(TFAIL,
							 "overflow event is not last");
						stop = 1;
						break;
					}
					tst_resm(TPASS, "get event: wd=%d "
						 "mask=%x cookie=%u len=%u",
						 event->wd, event->mask,
						 event->cookie, event->len);
					stop = 1;
					break;
				}
				i += EVENT_SIZE + event->len;
			}
		}
	}

	cleanup();
	tst_exit();
}
Esempio n. 10
0
File: write03.c Progetto: 1587/ltp
int main(int argc, char **argv)
{
	int lc;

	char wbuf[BUFSIZ], rbuf[BUFSIZ];
	int fd;

	tst_parse_opts(argc, argv, NULL, NULL);

	/* global setup */
	setup();

	/* The following loop checks looping state if -i option given */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		/* reset tst_count in case we are looping */
		tst_count = 0;

//block1:
		tst_resm(TINFO, "Enter Block 1: test to check if write "
			 "corrupts the file when write fails");

		fd = creat(filename, 0644);
		if (fd < 0) {
			tst_resm(TBROK, "creating a new file failed");
			cleanup();
		}

		(void)memset(wbuf, '0', 100);

		if (write(fd, wbuf, 100) == -1) {
			tst_resm(TFAIL, "failed to write to %s", filename);
			cleanup();
		}

		if (write(fd, bad_addr, 100) != -1) {
			tst_resm(TFAIL, "write(2) failed to fail");
			cleanup();
		}
		close(fd);

		if ((fd = open(filename, O_RDONLY)) == -1) {
			tst_resm(TBROK, "open(2) failed, errno: %d", errno);
			cleanup();
		}

		if (read(fd, rbuf, 100) == -1) {
			tst_resm(TBROK, "read(2) failed, errno: %d", errno);
			cleanup();
		}

		if (memcmp(wbuf, rbuf, 100) == 0) {
			tst_resm(TPASS, "failure of write(2) didnot corrupt "
				 "the file");
		} else {
			tst_resm(TFAIL, "failure of write(2) corrupted the "
				 "file");
		}
		tst_resm(TINFO, "Exit block 1");
		close(fd);
	}
	cleanup();
	tst_exit();
}
Esempio n. 11
0
File: fchmod07.c Progetto: 1587/ltp
int main(int ac, char **av)
{
	struct stat stat_buf;	/* stat(2) struct contents */
	int lc;
	int ind;		/* counter variable for chmod(2) tests */
	int mode;		/* file mode permission */

	TST_TOTAL = sizeof(Modes) / sizeof(int);

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

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

		tst_count = 0;

		for (ind = 0; ind < TST_TOTAL; ind++) {
			mode = Modes[ind];

			/*
			 * Call fchmod(2) with different mode permission
			 * bits to set it for "testfile".
			 */
			TEST(fchmod(fd, mode));

			if (TEST_RETURN == -1) {
				tst_resm(TFAIL, "fchmod(%d, %#o) Failed, "
					 "errno=%d : %s", fd, mode, TEST_ERRNO,
					 strerror(TEST_ERRNO));
				continue;
			}
			/*
			 * Get the testfile information using
			 * fstat(2).
			 */
			if (fstat(fd, &stat_buf) < 0) {
				tst_brkm(TFAIL, cleanup,
					 "fstat(2) of "
					 "%s failed, errno:%d",
					 TESTFILE, TEST_ERRNO);
			}
			stat_buf.st_mode &= ~S_IFREG;

			/*
			 * Check for expected mode permissions
			 * on testfile.
			 */
			if (stat_buf.st_mode == mode) {
				tst_resm(TPASS,
					 "Functionality of "
					 "fchmod(%d, %#o) successful",
					 fd, mode);
			} else {
				tst_resm(TFAIL, "%s: Incorrect modes "
					 "0%03o, Expected 0%03o",
					 TESTFILE, stat_buf.st_mode,
					 mode);
			}
		}
	}

	cleanup();
	tst_exit();
}
Esempio n. 12
0
File: unshare01.c Progetto: kraj/ltp
int main(int ac, char **av)
{
	pid_t pid1;
	int lc;
	int rval;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	for (lc = 0; TEST_LOOPING(lc); ++lc) {
		tst_count = 0;
		for (testno = 0; testno < TST_TOTAL; ++testno) {

			pid1 = fork();	//call to fork()
			if (pid1 == -1) {
				tst_brkm(TFAIL | TERRNO, cleanup,
					 "fork failed");
			} else if (pid1 == 0) {
				switch (unshare(CLONE_FILES)) {
				case 0:
					printf("unshare with CLONE_FILES call "
					       "succeeded\n");
					rval = 0;
					break;
				case -1:
					if (errno == ENOSYS)
						rval = 1;
					else {
						perror("unshare failed");
						rval = 2;
					}
				}
				exit(rval);
			} else {
				SAFE_WAIT(cleanup, &rval);
				if (rval != 0 && WIFEXITED(rval)) {
					switch (WEXITSTATUS(rval)) {
					case 1:
						tst_brkm(TCONF, cleanup,
							 "unshare not supported in "
							 "kernel");
						break;
					default:
						tst_brkm(TFAIL, cleanup,
							 "unshare failed");
					}
				}
			}

			pid1 = fork();
			if (pid1 == -1) {
				tst_brkm(TFAIL | TERRNO, cleanup,
					 "fork failed");
			} else if (pid1 == 0) {
				switch (unshare(CLONE_FS)) {
				case 0:
					printf("unshare with CLONE_FS call "
					       "succeeded\n");
					rval = 0;
					break;
				case -1:
					if (errno == ENOSYS)
						rval = 1;
					else {
						perror("unshare failed");
						rval = 2;
					}
				}
				exit(rval);
			} else {
				SAFE_WAIT(cleanup, &rval);
				if (rval != 0 && WIFEXITED(rval)) {
					switch (WEXITSTATUS(rval)) {
					case 1:
						tst_brkm(TCONF, cleanup,
							 "unshare not supported in "
							 "kernel");
						break;
					default:
						tst_brkm(TFAIL, cleanup,
							 "unshare failed");
					}
				}
			}

			pid1 = fork();
			if (pid1 == -1) {
				tst_brkm(TFAIL | TERRNO, cleanup,
					 "fork() failed.");
			} else if (pid1 == 0) {
				switch (unshare(CLONE_NEWNS)) {
				case 0:
					printf("unshare call with CLONE_NEWNS "
					       "succeeded\n");
					rval = 0;
					break;
				case -1:
					if (errno == ENOSYS)
						rval = 1;
					else {
						perror("unshare failed");
						rval = 2;
					}
				}
				exit(rval);
			} else {
				SAFE_WAIT(cleanup, &rval);
				if (rval != 0 && WIFEXITED(rval)) {
					switch (WEXITSTATUS(rval)) {
					case 1:
						tst_brkm(TCONF, cleanup,
							 "unshare not supported in "
							 "kernel");
						break;
					default:
						tst_brkm(TFAIL, cleanup,
							 "unshare failed");
					}

				}

			}

		}

	}
	cleanup();
	tst_exit();
}
Esempio n. 13
0
File: semctl01.c Progetto: 1587/ltp
int main(int argc, char *argv[])
{
	int lc;
	int i;

	tst_parse_opts(argc, argv, NULL, NULL);

#ifdef UCLINUX
	argv0 = argv[0];
	maybe_run_child(&child_pid, "nd", 1, &sem_id_1);
	maybe_run_child(&child_cnt, "ndd", 2, &sem_id_1, &sem_op);
#endif

	setup();

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

		for (i = 0; i < TST_TOTAL; i++) {

			/*
			 * Set up any conditions if needed
			 */
			if (TC[i].func_setup != NULL) {
				/* call the setup function */
				switch (TC[i].cmd) {
				case GETNCNT:
					(*TC[i].func_setup) (-ONE);
					break;
				case GETZCNT:
					(*TC[i].func_setup) (0);
					break;
				default:
					(*TC[i].func_setup) ();
					break;
				}
			}

			TEST(semctl(*(TC[i].semid), TC[i].semnum, TC[i].cmd,
				    TC[i].arg));

			if (TEST_RETURN == -1) {
				tst_resm(TFAIL, "%s call failed - errno = %d "
					 ": %s", TCID, TEST_ERRNO,
					 strerror(TEST_ERRNO));
			} else {
				/*
				 * call the appropriate test function
				 * and pass the return value where it
				 * is needed to perform certain tests.
				 */
				switch (TC[i].cmd) {
				case GETNCNT:
				case GETZCNT:
				case GETPID:
				case GETVAL:
				case IPC_INFO:
				case SEM_STAT:
					(*TC[i].func_test) (TEST_RETURN);
					break;
				default:
					(*TC[i].func_test) ();
					break;
				}
			}

			/*
			 * If testing GETNCNT or GETZCNT, clean up the children.
			 */
			switch (TC[i].cmd) {
			case GETNCNT:
			case GETZCNT:
				kill_all_children();
				break;
			}
		}
		/*
		 * recreate the semaphore resource if looping
		 */
		if (TEST_LOOPING(lc)) {
			sem_id_1 = semget(semkey, PSEMS,
					  IPC_CREAT | IPC_EXCL | SEM_RA);
			if (sem_id_1 == -1)
				tst_brkm(TBROK, cleanup,
					 "couldn't recreate " "semaphore");
		}
	}

	cleanup();

	tst_exit();
}
Esempio n. 14
0
File: mknod06.c Progetto: 1587/ltp
int main(int ac, char **av)
{
	int lc;
	char *node_name;	/* ptr. for node name created */
	char *test_desc;	/* test specific error message */
	int ind;		/* counter to test different test conditions */

	tst_parse_opts(ac, av, NULL, NULL);

	/*
	 * Invoke setup function to call individual test setup functions
	 * for the test which run as root/super-user.
	 */
	setup();

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

		tst_count = 0;

		for (ind = 0; Test_cases[ind].desc != NULL; ind++) {
			node_name = Test_cases[ind].pathname;
			test_desc = Test_cases[ind].desc;

#if !defined(UCLINUX)
			if (node_name == High_address_node) {
				node_name = get_high_address();
			}
#endif

			/*
			 * Call mknod(2) to test different test conditions.
			 * verify that it fails with -1 return value and
			 * sets appropriate errno.
			 */
			TEST(mknod(node_name, MODE_RWX, 0));

			/* Check return code from mknod(2) */
			if (TEST_RETURN != -1) {
				tst_resm(TFAIL,
					 "mknod() returned %ld, expected "
					 "-1, errno:%d", TEST_RETURN,
					 Test_cases[ind].exp_errno);
				continue;
			}

			if (TEST_ERRNO == Test_cases[ind].exp_errno) {
				tst_resm(TPASS, "mknod() fails, %s, errno:%d",
					 test_desc, TEST_ERRNO);
			} else {
				tst_resm(TFAIL, "mknod() fails, %s, errno:%d, "
					 "expected errno:%d", test_desc,
					 TEST_ERRNO, Test_cases[ind].exp_errno);
			}
		}

	}

	/*
	 * Invoke cleanup() to delete the test directories created
	 * in the setup().
	 */
	cleanup();

	tst_exit();
}
Esempio n. 15
0
File: setpgid03.c Progetto: 1587/ltp
int main(int ac, char **av)
{
	int child_pid;
	int status;
	int rval;
	int lc;

	tst_parse_opts(ac, av, NULL, NULL);
#ifdef UCLINUX
	maybe_run_child(&do_child, "");
#endif

	setup();

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

		tst_count = 0;

		/* Child is in new session we are not alowed to change pgid */
		if ((child_pid = FORK_OR_VFORK()) == -1)
			tst_brkm(TBROK, cleanup, "fork() failed");

		if (child_pid == 0) {
#ifdef UCLINUX
			if (self_exec(av[0], "") < 0)
				tst_brkm(TBROK, cleanup, "self_exec failed");
#else
			do_child();
#endif
		}

		TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
		rval = setpgid(child_pid, getppid());
		if (rval == -1 && errno == EPERM) {
			tst_resm(TPASS, "setpgid failed with EPERM");
		} else {
			tst_resm(TFAIL,
				"retval %d, errno %d, expected errno %d",
				rval, errno, EPERM);
		}
		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);

		if (wait(&status) < 0)
			tst_resm(TFAIL | TERRNO, "wait() for child 1 failed");

		if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0))
			tst_resm(TFAIL, "child 1 failed with status %d",
				WEXITSTATUS(status));

		/* Child after exec() we are no longer allowed to set pgid */
		if ((child_pid = FORK_OR_VFORK()) == -1)
			tst_resm(TFAIL, "Fork failed");

		if (child_pid == 0) {
			if (execlp(TEST_APP, TEST_APP, NULL) < 0)
				perror("exec failed");

			exit(127);
		}

		TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
		rval = setpgid(child_pid, getppid());
		if (rval == -1 && errno == EACCES) {
			tst_resm(TPASS, "setpgid failed with EACCES");
		} else {
			tst_resm(TFAIL,
				"retval %d, errno %d, expected errno %d",
				rval, errno, EACCES);
		}
		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);

		if (wait(&status) < 0)
			tst_resm(TFAIL | TERRNO, "wait() for child 2 failed");

		if (!(WIFEXITED(status)) || (WEXITSTATUS(status) != 0))
			tst_resm(TFAIL, "child 2 failed with status %d",
				WEXITSTATUS(status));
	}

	cleanup();
	tst_exit();
}
Esempio n. 16
0
int main(int ac, char **av)
{
	struct stat stat_buf;	/* stat(2) struct contents */
	int lc;
	int i;
	uid_t user_id;		/* user id of the user set for testfile */
	gid_t group_id;		/* group id of the user set for testfile */
	int test_flag;		/* test condition specific flag variable */
	char *file_name;	/* ptr. for test file name */

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

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

		tst_count = 0;

		for (i = 0; i < TST_TOTAL; i++) {

			file_name = test_cases[i].pathname;
			user_id = test_cases[i].user_id;
			group_id = test_cases[i].group_id;
			test_flag = test_cases[i].test_flag;

			/*
			 * Call chown(2) with different user id and
			 * group id (numeric values) to set it on testfile.
			 */
			TEST(CHOWN(cleanup, file_name, user_id, group_id));

			if (TEST_RETURN == -1) {
				tst_resm(TFAIL | TTERRNO,
					 "chown(%s, ..) failed", file_name);
				continue;
			}

			/*
			 * Get the testfile information using stat(2).
			 */
			if (stat(file_name, &stat_buf) < 0) {
				tst_brkm(TFAIL, cleanup, "stat(2) of "
					 "%s failed, errno:%d",
					 file_name, TEST_ERRNO);
			}

			/*
			 * Check for expected Ownership ids
			 * set on testfile.
			 */
			if (stat_buf.st_uid != user_id ||
			    stat_buf.st_gid != group_id) {
				tst_brkm(TFAIL, cleanup, "%s: incorrect"
					 " ownership set, Expected %d "
					 "%d", file_name,
					 user_id, group_id);
			}

			/*
			 * Verify that S_ISUID/S_ISGID bits set on the
			 * testfile(s) in setup()s are cleared by
			 * chown().
			 */
			if (test_flag == 1 &&
			    (stat_buf.st_mode & (S_ISUID | S_ISGID)) != 0) {
				tst_resm(TFAIL,
					 "%s: incorrect mode "
					 "permissions %#o, Expected "
					 "%#o", file_name, NEW_PERMS1,
					 EXP_PERMS);
			} else if (test_flag == 2
				 && (stat_buf.st_mode & S_ISGID) == 0) {
				tst_resm(TFAIL,
					 "%s: Incorrect mode "
					 "permissions %#o, Expected "
					 "%#o", file_name,
					 stat_buf.st_mode, NEW_PERMS2);
			} else {
				tst_resm(TPASS,
					 "chown(%s, ..) succeeded",
					 file_name);
			}
		}
	}

	cleanup();
	tst_exit();
}
Esempio n. 17
0
int main(int ac, char **av)
{
	int exp_eno;
	int lc;

	char osname[OSNAMESZ];
	int osnamelth, status;
	int name[] = { CTL_KERN, KERN_OSTYPE };
	pid_t pid;
	struct passwd *ltpuser;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	if ((tst_kvercmp(2, 6, 32)) <= 0) {
		exp_eno = EPERM;
	} else {
		/* ^^ Look above this warning. ^^ */
		tst_resm(TINFO,
			 "this test's results are based on potentially undocumented behavior in the kernel. read the NOTE in the source file for more details");
		exp_eno = EACCES;
		exp_enos[0] = EACCES;
	}

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

		/* reset tst_count in case we are looping */
		tst_count = 0;

		strcpy(osname, "Linux");
		osnamelth = SIZE(osname);

		TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth));

		if (TEST_RETURN != -1) {
			tst_resm(TFAIL, "sysctl(2) succeeded unexpectedly");
		} else {
			if (TEST_ERRNO == exp_eno) {
				tst_resm(TPASS | TTERRNO, "Got expected error");
			} else if (errno == ENOSYS) {
				tst_resm(TCONF,
					 "You may need to make CONFIG_SYSCTL_SYSCALL=y"
					 " to your kernel config.");
			} else {
				tst_resm(TFAIL | TTERRNO,
					 "Got unexpected error");
			}
		}

		osnamelth = SIZE(osname);
		if ((ltpuser = getpwnam("nobody")) == NULL) {
			tst_brkm(TBROK, cleanup, "getpwnam() failed");
		}

		/* set process ID to "ltpuser1" */
		if (seteuid(ltpuser->pw_uid) == -1) {
			tst_brkm(TBROK, cleanup,
				 "seteuid() failed, errno %d", errno);
		}

		if ((pid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork() failed");
		}

		if (pid == 0) {
			TEST(sysctl(name, SIZE(name), 0, 0, osname, osnamelth));

			if (TEST_RETURN != -1) {
				tst_resm(TFAIL, "call succeeded unexpectedly");
			} else {
				if (TEST_ERRNO == exp_eno) {
					tst_resm(TPASS | TTERRNO,
						 "Got expected error");
				} else if (TEST_ERRNO == ENOSYS) {
					tst_resm(TCONF,
						 "You may need to make CONFIG_SYSCTL_SYSCALL=y"
						 " to your kernel config.");
				} else {
					tst_resm(TFAIL | TTERRNO,
						 "Got unexpected error");
				}
			}

			cleanup();

		} else {
			/* wait for the child to finish */
			wait(&status);
		}

		/* set process ID back to root */
		if (seteuid(0) == -1) {
			tst_brkm(TBROK, cleanup, "seteuid() failed");
		}
	}
	cleanup();
	tst_exit();
}
Esempio n. 18
0
int main(int ac, char **av)
{
	int lc;
	struct itimerval *value, *ovalue;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();		/* global setup */

	/* The following loop checks looping state if -i option given */

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/* reset tst_count in case we are looping */
		tst_count = 0;

		/* allocate some space for timer structures */

		if ((value = malloc((size_t)sizeof(struct itimerval))) ==
		    NULL) {
			tst_brkm(TBROK, cleanup, "value malloc failed");
		}

		if ((ovalue = malloc((size_t)sizeof(struct itimerval))) ==
		    NULL) {
			tst_brkm(TBROK, cleanup, "value malloc failed");
		}

		/* set up some reasonable values */

		value->it_value.tv_sec = 30;
		value->it_value.tv_usec = 0;
		value->it_interval.tv_sec = 0;
		value->it_interval.tv_usec = 0;

		/*
		 * issue the system call with the TEST() macro
		 * ITIMER_REAL = 0, ITIMER_VIRTUAL = 1 and ITIMER_PROF = 2
		 */

		/* make the first value negative to get a failure */
		TEST(setitimer(-ITIMER_PROF, value, ovalue));

		if (TEST_RETURN == 0) {
			tst_resm(TFAIL, "call failed to produce expected error "
				 "- errno = %d - %s", TEST_ERRNO,
				 strerror(TEST_ERRNO));
			continue;
		}

		switch (TEST_ERRNO) {
		case EINVAL:
			tst_resm(TPASS, "expected failure - errno = %d - %s",
				 TEST_ERRNO, strerror(TEST_ERRNO));
			break;
		default:
			tst_resm(TFAIL, "call failed to produce expected error "
				 "- errno = %d - %s", TEST_ERRNO,
				 strerror(TEST_ERRNO));
		}

		/*
		 * clean up things in case we are looping
		 */
		free(value);
		free(ovalue);
		value = NULL;
		ovalue = NULL;
	}

	cleanup();
	tst_exit();

}
Esempio n. 19
0
int main(int ac, char **av)
{
	int lc;
	int gidsetsize;		/* total no. of groups */
	int i;
	char *test_desc;	/* test specific error message */
	int ngroups_max = sysconf(_SC_NGROUPS_MAX);	/* max no. of groups in the current system */

	tst_parse_opts(ac, av, NULL, NULL);

	groups_list = malloc(ngroups_max * sizeof(GID_T));
	if (groups_list == NULL) {
		tst_brkm(TBROK, NULL, "malloc failed to alloc %zu errno "
			 " %d ", ngroups_max * sizeof(GID_T), errno);
	}

	setup();

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

		tst_count = 0;

		for (i = 0; i < TST_TOTAL; i++) {
			if (Test_cases[i].setupfunc != NULL) {
				Test_cases[i].setupfunc();
			}

			gidsetsize = ngroups_max + Test_cases[i].gsize_add;
			test_desc = Test_cases[i].desc;

			/*
			 * Call setgroups() to test different test conditions
			 * verify that it fails with -1 return value and
			 * sets appropriate errno.
			 */
			TEST(SETGROUPS(cleanup, gidsetsize, groups_list));

			if (TEST_RETURN != -1) {
				tst_resm(TFAIL, "setgroups(%d) returned %ld, "
					 "expected -1, errno=%d", gidsetsize,
					 TEST_RETURN, Test_cases[i].exp_errno);
				continue;
			}

			if (TEST_ERRNO == Test_cases[i].exp_errno) {
				tst_resm(TPASS,
					 "setgroups(%d) fails, %s, errno=%d",
					 gidsetsize, test_desc, TEST_ERRNO);
			} else {
				tst_resm(TFAIL, "setgroups(%d) fails, %s, "
					 "errno=%d, expected errno=%d",
					 gidsetsize, test_desc, TEST_ERRNO,
					 Test_cases[i].exp_errno);
			}
		}

	}

	cleanup();

	tst_exit();
}
Esempio n. 20
0
int main(int argc, char **argv)
{
    int lc;
    struct robust_list_head head;
    size_t len_ptr;		/* size of structure struct robust_list_head */

    tst_parse_opts(argc, argv, NULL, NULL);

    setup();

    len_ptr = sizeof(struct robust_list_head);

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

        /*
         * The get_robust_list function fails with EFAULT if the size of the
         * struct robust_list_head can't be stored in the memory address space
         * specified by len_ptr argument, or the head of the robust list can't
         * be stored in the memory address space specified by the head_ptr
         * argument.
         */

        TEST(ltp_syscall(__NR_get_robust_list, 0,
                         (struct robust_list_head *)&head,
                         NULL));

        if (TEST_RETURN == -1) {
            if (TEST_ERRNO == EFAULT)
                tst_resm(TPASS,
                         "get_robust_list failed as expected with "
                         "EFAULT");
            else
                tst_resm(TFAIL | TTERRNO,
                         "get_robust_list failed unexpectedly");
        } else
            tst_resm(TFAIL,
                     "get_robust_list succeeded unexpectedly");

        TEST(ltp_syscall(__NR_get_robust_list, 0,
                         NULL,
                         &len_ptr));

        if (TEST_RETURN) {
            if (TEST_ERRNO == EFAULT)
                tst_resm(TPASS,
                         "get_robust_list failed as expected with "
                         "EFAULT");
            else
                tst_resm(TFAIL | TTERRNO,
                         "get_robust_list failed unexpectedly");
        } else
            tst_resm(TFAIL,
                     "get_robust_list succeeded unexpectedly");

        /*
         * The get_robust_list function fails with ESRCH if it can't
         * find the task specified by the pid argument.
         */

        TEST(ltp_syscall(__NR_get_robust_list, unused_pid,
                         (struct robust_list_head *)&head,
                         &len_ptr));

        if (TEST_RETURN == -1) {
            if (TEST_ERRNO == ESRCH)
                tst_resm(TPASS,
                         "get_robust_list failed as expected with "
                         "ESRCH");
            else
                tst_resm(TFAIL | TTERRNO,
                         "get_robust_list failed unexpectedly");
        } else
            tst_resm(TFAIL,
                     "get_robust_list succeeded unexpectedly");

        TEST(ltp_syscall(__NR_get_robust_list, 0,
                         (struct robust_list_head **)&head,
                         &len_ptr));

        if (TEST_RETURN == 0)
            tst_resm(TPASS, "get_robust_list succeeded");
        else
            tst_resm(TFAIL | TTERRNO,
                     "get_robust_list failed unexpectedly");

        if (setuid(1) == -1)
            tst_brkm(TBROK | TERRNO, cleanup, "setuid(1) failed");

        TEST(ltp_syscall(__NR_get_robust_list, 1,
                         (struct robust_list_head *)&head,
                         &len_ptr));

        if (TEST_RETURN == -1) {
            if (TEST_ERRNO == EPERM)
                tst_resm(TPASS,
                         "get_robust_list failed as expected with "
                         "EPERM");
            else
                tst_resm(TFAIL | TERRNO,
                         "get_robust_list failed unexpectedly");
        } else
            tst_resm(TFAIL,
                     "get_robust_list succeeded unexpectedly");
    }

    cleanup();

    tst_exit();
}
Esempio n. 21
0
File: mkdir05.c Progetto: 1587/ltp
int main(int ac, char **av)
{
	int lc;
	struct stat buf;

	/*
	 * parse standard options
	 */
	tst_parse_opts(ac, av, NULL, NULL);

	/*
	 * perform global setup for test
	 */
	setup();

	/*
	 * check looping state if -i option given
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		tst_count = 0;

		/*
		 * TEST mkdir() base functionality
		 */

		/* Initialize the test directory name */
		sprintf(tstdir1, "tstdir1.%d", getpid());

		/* Call mkdir(2) using the TEST macro */
		TEST(mkdir(tstdir1, PERMS));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL, "mkdir(%s, %#o) Failed",
				 tstdir1, PERMS);
			continue;
		}

		if (stat(tstdir1, &buf) == -1) {
			tst_brkm(TBROK, cleanup, "failed to stat the "
				 "new directory");
		}
		/* check the owner */
		if (buf.st_uid != geteuid()) {
			tst_resm(TFAIL, "mkdir() FAILED to set owner ID"
				 " as process's effective ID");
			continue;
		}
		/* check the group ID */
		if (buf.st_gid != getegid()) {
			tst_resm(TFAIL, "mkdir() failed to set group ID"
				 " as the process's group ID");
			continue;
		}
		tst_resm(TPASS, "mkdir() functionality is correct");

		/* clean up things in case we are looping */
		if (rmdir(tstdir1) == -1) {
			tst_brkm(TBROK, cleanup, "could not remove directory");
		}

	}

	cleanup();
	tst_exit();
}
Esempio n. 22
0
int main(int ac, char **av)
{
	int lc;

	int fd[2];		/* fds for pipe read/write */
	char wrbuf[BUFSIZ], rebuf[BUFSIZ];
	int red, written;	/* no of chars read and */
	/* written to pipe */
	int length, greater, forkstat;
	int retval = 0, status, e_code;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

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

		/* reset tst_count in case we are looping */
		tst_count = 0;

		TEST(pipe(fd));

		if (TEST_RETURN == -1) {
			retval = 1;
			tst_resm(TFAIL, "pipe creation failed");
			continue;
		}

		strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz");
		length = strlen(wrbuf) + 1;

		written = write(fd[1], wrbuf, length);

		/* did write write at least some chars */
		if ((written < 0) || (written > length)) {
			tst_brkm(TBROK, cleanup, "write to pipe failed");
		}

		forkstat = FORK_OR_VFORK();

		if (forkstat == -1) {
			tst_brkm(TBROK, cleanup, "fork() failed");
		}

		if (forkstat == 0) {	/* child */
			red = safe_read(fd[0], rebuf, written);

			/* did read , get at least some chars */
			if ((red < 0) || (red > written)) {
				tst_brkm(TBROK, cleanup, "read pipe failed");
			}

			greater = strcmp(rebuf, wrbuf);

			/* are the strings written and read equal */
			if (greater == 0) {
				tst_resm(TPASS, "functionality is correct");
			} else {
				retval = 1;
				tst_resm(TFAIL, "read & write strings do "
					 "not match");
			}
			exit(retval);
		} else {	/* parent */
			/* wait for the child to finish */
			wait(&status);
			/* make sure the child returned a good exit status */
			e_code = status >> 8;
			if (e_code != 0) {
				tst_resm(TFAIL, "Failures reported above");
			}
		}
	}
	cleanup();

	tst_exit();
}
Esempio n. 23
0
File: aio01.c Progetto: 1587/ltp
int main(int argc, char **argv)
{
	int i, j, sec, usec;
	int failflag = 0;
	int bflag = 0, nflag = 0, Fflag = 0;
	char *optb, *optn, *optF;
	struct io_event event;
	static struct timespec ts;
	struct timeval stv, etv;

	option_t options[] = {
		{"b:", &bflag, &optb},
		{"n:", &nflag, &optn},
		{"F:", &Fflag, &optF},
		{NULL, NULL, NULL}
	};

	tst_parse_opts(argc, argv, options, &help);

	bufsize = (bflag ? atoi(optb) : 8192);
	nr = (nflag ? atoi(optn) : 10);
	if (Fflag) {
		sprintf(fname, "%s", optF);
	} else {
		sprintf(fname, "aiofile");
	}

	setup();

/* TEST 1 */
	pos = 0;
	gettimeofday(&stv, NULL);
	io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
	for (i = 0; i < nr; i++) {
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 1: io_submit failed - retval=%ld"
				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		gettimeofday(&etv, NULL);
	}
	if (!failflag) {
		sec = etv.tv_sec - stv.tv_sec;
		usec = etv.tv_usec - stv.tv_usec;
		if (usec < 0) {
			usec += 1000000;
			sec--;
		}
		tst_resm(TPASS, "Test 1: %d writes in %3d.%06d sec",
			 nr, sec, usec);
	}

/* TEST 2 */
	pos = 0;
	failflag = 0;
	gettimeofday(&stv, NULL);
	io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
	for (i = 0; i < nr; i++) {
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 2: io_submit failed - retval=%ld"
				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		gettimeofday(&etv, NULL);
	}
	if (!failflag) {
		sec = etv.tv_sec - stv.tv_sec;
		usec = etv.tv_usec - stv.tv_usec;
		if (usec < 0) {
			usec += 1000000;
			sec--;
		}
		tst_resm(TPASS, "Test 2: %d reads in %3d.%06d sec",
			 nr, sec, usec);
	}

/* TEST 3 */
	pos = 0;
	failflag = 0;
	gettimeofday(&stv, NULL);
	for (i = 0; i < nr; i++) {
		io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 3: io_submit failed - retval=%ld"
				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		gettimeofday(&etv, NULL);
	}
	if (!failflag) {
		sec = etv.tv_sec - stv.tv_sec;
		usec = etv.tv_usec - stv.tv_usec;
		if (usec < 0) {
			usec += 1000000;
			sec--;
		}
		tst_resm(TPASS, "Test 3: %d prep,writes in %3d.%06d sec",
			 nr, sec, usec);
	}

/* TEST 4 */
	pos = 0;
	failflag = 0;
	gettimeofday(&stv, NULL);
	for (i = 0; i < nr; i++) {
		io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 4: io_submit failed - retval=%ld"
				 ", errno=%d", TEST_RETURN, TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		gettimeofday(&etv, NULL);
	}
	if (!failflag) {
		sec = etv.tv_sec - stv.tv_sec;
		usec = etv.tv_usec - stv.tv_usec;
		if (usec < 0) {
			usec += 1000000;
			sec--;
		}
		tst_resm(TPASS, "Test 4: %d prep,reads in %3d.%06d sec",
			 nr, sec, usec);
	}

/* TEST 5 */
	pos = 0;
	failflag = 0;
	gettimeofday(&stv, NULL);
	for (i = 0; i < nr; i++) {
		io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 5: write io_submit failed - "
				 "retval=%ld, errno=%d", TEST_RETURN,
				 TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 5: read io_submit failed - "
				 "retval=%ld, errno=%d", TEST_RETURN,
				 TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		gettimeofday(&etv, NULL);
	}
	if (!failflag) {
		sec = etv.tv_sec - stv.tv_sec;
		usec = etv.tv_usec - stv.tv_usec;
		if (usec < 0) {
			usec += 1000000;
			sec--;
		}
		tst_resm(TPASS, "Test 5: %d reads and writes in %3d.%06d sec",
			 nr, sec, usec);
	}

/* TEST 6 */
	pos = 0;
	failflag = 0;
	gettimeofday(&stv, NULL);
	for (i = 0; i < nr; i++) {
		io_prep_pwrite(iocbs[0], fd, srcbuf, bufsize, pos);
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 6: write io_submit failed - "
				 "retval=%ld, errno=%d", TEST_RETURN,
				 TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		io_prep_pread(iocbs[0], fd, dstbuf, bufsize, pos);
		ts.tv_sec = 30;
		ts.tv_nsec = 0;
		do {
			TEST(io_submit(io_ctx, 1, iocbs));
		} while (TEST_RETURN == -EAGAIN);
		if (TEST_RETURN < 0) {
			tst_resm(TFAIL, "Test 6: read io_submit failed - "
				 "retval=%ld, errno=%d", TEST_RETURN,
				 TEST_ERRNO);
			failflag = 1;
			continue;
		}
		while (io_getevents(io_ctx, 1, 1, &event, &ts) != 1) ;
		for (j = 0; j < bufsize; j++) {
			if (srcbuf[j] != dstbuf[j]) {
				tst_resm(TFAIL, "Test 6: compare failed - "
					 "read: %c, " "actual: %c",
					 dstbuf[j], srcbuf[j]);
				break;
			}
		}
		gettimeofday(&etv, NULL);
	}
	if (!failflag) {
		sec = etv.tv_sec - stv.tv_sec;
		usec = etv.tv_usec - stv.tv_usec;
		if (usec < 0) {
			usec += 1000000;
			sec--;
		}
		tst_resm(TPASS, "Test 6: %d read,write,verify in %d.%06d sec",
			 i, sec, usec);
	}

	cleanup();

	tst_exit();
}
Esempio n. 24
0
int main(int ac, char **av)
{
	int lc, i;
	int len;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/*
		 * generate events
		 */
		for (i = 0; i < MAX_EVENTS + 1; i++) {
			sprintf(fname, "fname_%d", i);
			fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0644);
			SAFE_CLOSE(cleanup, fd);
		}

		while (1) {
			/*
			 * get list on events
			 */
			len = read(fd_notify, &event, sizeof(event));
			if (len < 0) {
				if (errno == -EAGAIN) {
					tst_resm(TFAIL, "Overflow event not "
						 "generated!\n");
					break;
				}
				tst_brkm(TBROK | TERRNO, cleanup,
					 "read of notification event failed");
				break;
			}
			if (event.fd != FAN_NOFD)
				close(event.fd);

			/*
			 * check events
			 */
			if (event.mask != FAN_OPEN &&
			    event.mask != FAN_Q_OVERFLOW) {
				tst_resm(TFAIL,
					 "get event: mask=%llx (expected %llx)"
					 "pid=%u fd=%d",
					 (unsigned long long)event.mask,
					 (unsigned long long)FAN_OPEN,
					 (unsigned)event.pid, event.fd);
				break;
			}
			if (event.mask == FAN_Q_OVERFLOW) {
				if (event.fd != FAN_NOFD) {
					tst_resm(TFAIL,
						 "invalid overflow event: "
						 "mask=%llx pid=%u fd=%d",
						 (unsigned long long)event.mask,
						 (unsigned)event.pid,
						 event.fd);
					break;
				}
				tst_resm(TPASS,
					 "get event: mask=%llx pid=%u fd=%d",
					 (unsigned long long)event.mask,
					 (unsigned)event.pid, event.fd);
					break;
			}
		}
	}

	cleanup();
	tst_exit();
}
Esempio n. 25
0
int main(int ac, char **av)
{
	int lc;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

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

		tst_count = 0;

		/*
		 * TEST CASE:
		 *  Dont change either real or effective gid
		 */
		gid = getgid();
		GID16_CHECK(gid, setregid, NULL);

		egid = getegid();
		GID16_CHECK(egid, setregid, NULL);

		TEST(SETREGID(NULL, -1, -1));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL,
				 "setregid -  Dont change either real or effective gid failed, errno=%d : %s",
				 TEST_ERRNO, strerror(TEST_ERRNO));
		} else {
			tst_resm(TPASS,
				 "setregid -  Dont change either real or effective gid returned %ld",
				 TEST_RETURN);
		}

		/*
		 * TEST CASE:
		 *  change effective to effective gid
		 */

		TEST(SETREGID(NULL, -1, egid));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL,
				 "setregid -  change effective to effective gid failed, errno=%d : %s",
				 TEST_ERRNO, strerror(TEST_ERRNO));
		} else {
			tst_resm(TPASS,
				 "setregid -  change effective to effective gid returned %ld",
				 TEST_RETURN);
		}

		/*
		 * TEST CASE:
		 *  change real to real gid
		 */

		TEST(SETREGID(NULL, gid, -1));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL,
				 "setregid -  change real to real gid failed, errno=%d : %s",
				 TEST_ERRNO, strerror(TEST_ERRNO));
		} else {
			tst_resm(TPASS,
				 "setregid -  change real to real gid returned %ld",
				 TEST_RETURN);
		}

		/*
		 * TEST CASE:
		 *  change effective to real gid
		 */

		TEST(SETREGID(NULL, -1, gid));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL,
				 "setregid -  change effective to real gid failed, errno=%d : %s",
				 TEST_ERRNO, strerror(TEST_ERRNO));
		} else {
			tst_resm(TPASS,
				 "setregid -  change effective to real gid returned %ld",
				 TEST_RETURN);
		}

		/*
		 * TEST CASE:
		 *  try to change real to current real
		 */

		TEST(SETREGID(NULL, gid, gid));

		if (TEST_RETURN == -1) {
			tst_resm(TFAIL | TTERRNO, "setregid failed");
		} else {
			tst_resm(TPASS, "setregid return %ld",
				 TEST_RETURN);
		}

	}

	tst_exit();
}
Esempio n. 26
0
File: rename09.c Progetto: kraj/ltp
int main(int ac, char **av)
{
	int lc;
	int rval;
	pid_t pid, pid1;
	int status;

	/*
	 * parse standard options
	 */
	tst_parse_opts(ac, av, NULL, NULL);

	/*
	 * perform global setup for test
	 */
	setup();

	/*
	 * check looping state if -i option given
	 */
	for (lc = 0; TEST_LOOPING(lc); lc++) {

		tst_count = 0;

		if ((pid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork() #1 failed");
		}

		if (pid == 0) {	/* first child */
			/* set to nobody */
			rval = setreuid(nobody_uid, nobody_uid);
			if (rval < 0) {
				tst_resm(TWARN, "setreuid failed to "
					 "to set the real uid to %d and "
					 "effective uid to %d",
					 nobody_uid, nobody_uid);
				perror("setreuid");
				exit(1);
			}

			/* create the a directory with 0700 permits */
			if (mkdir(fdir, PERMS) == -1) {
				tst_resm(TWARN, "mkdir(%s, %#o) Failed",
					 fdir, PERMS);
				exit(1);
			}

			/* create "old" file under it */
			SAFE_TOUCH(cleanup, fname, 0700, NULL);

			exit(0);
		}

		/* wait for child to exit */
		wait(&status);
		if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
			tst_brkm(TBROK, cleanup, "First child failed to set "
				 "up conditions for the test");
		}

		if ((pid1 = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork() #2 failed");
		}

		if (pid1 == 0) {	/* second child */
			/* set to bin */
			if ((rval = seteuid(bin_uid)) == -1) {
				tst_resm(TWARN, "seteuid() failed");
				perror("setreuid");
				exit(1);
			}

			/* create "new" directory */
			if (mkdir(mdir, PERMS) == -1) {
				tst_resm(TWARN, "mkdir(%s, %#o) failed",
					 mdir, PERMS);
				exit(1);
			}

			SAFE_TOUCH(cleanup, mname, 0700, NULL);

			/* rename "old" to "new" */
			TEST(rename(fname, mname));
			if (TEST_RETURN != -1) {
				tst_resm(TFAIL, "call succeeded unexpectedly");
				continue;
			}

			if (TEST_ERRNO != EACCES) {
				tst_resm(TFAIL, "Expected EACCES got %d",
					 TEST_ERRNO);
			} else {
				tst_resm(TPASS, "rename() returned EACCES");
			}

			/* set the process id back to root */
			if (seteuid(0) == -1) {
				tst_resm(TWARN, "seteuid(0) failed");
				exit(1);
			}

			/* clean up things in case we are looping */
			SAFE_UNLINK(cleanup, fname);
			SAFE_UNLINK(cleanup, mname);
			SAFE_RMDIR(cleanup, fdir);
			SAFE_RMDIR(cleanup, mdir);
		} else {
			/* parent - let the second child carry on */
			waitpid(pid1, &status, 0);
			if (!WIFEXITED(status) || (WEXITSTATUS(status) != 0)) {
				exit(WEXITSTATUS(status));
			} else {
				exit(0);
			}
		}
	}

	/*
	 * cleanup and exit
	 */
	cleanup();
	tst_exit();

}
Esempio n. 27
0
File: semop05.c Progetto: kraj/ltp
int main(int ac, char **av)
{
	int lc;
	int i;
	pid_t pid;
	void do_child();

	tst_parse_opts(ac, av, NULL, NULL);

#ifdef UCLINUX
	maybe_run_child(&do_child_uclinux, "dd", &i_uclinux, &sem_id_1);
#endif

	setup();		/* global setup */

	/* The following loop checks looping state if -i option given */

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		/* reset tst_count in case we are looping */
		tst_count = 0;

		for (i = 0; i < TST_TOTAL; i++) {

			/* initialize the s_buf buffer */
			s_buf.sem_op = TC[i].op;
			s_buf.sem_flg = TC[i].flg;
			s_buf.sem_num = TC[i].num;

			/* initialize all of the primitive semaphores */
			if (semctl(sem_id_1, TC[i].num, SETVAL, TC[i].semunptr)
			    == -1) {
				tst_brkm(TBROK, cleanup, "semctl() failed");
			}

			if ((pid = FORK_OR_VFORK()) == -1) {
				tst_brkm(TBROK, cleanup, "could not fork");
			}

			if (pid == 0) {	/* child */

#ifdef UCLINUX
				if (self_exec(av[0], "dd", i, sem_id_1) < 0) {
					tst_brkm(TBROK, cleanup,
						 "could not self_exec");
				}
#else
				do_child(i);
#endif
			} else {
				TST_PROCESS_STATE_WAIT(cleanup, pid, 'S');

				/*
				 * If we are testing for EIDRM then remove
				 * the semaphore, else send a signal that
				 * must be caught as we are testing for
				 * EINTR.
				 */
				if (TC[i].error == EIDRM) {
					/* remove the semaphore resource */
					rm_sema(sem_id_1);
				} else {
					SAFE_KILL(cleanup, pid, SIGHUP);
				}

				/* let the child carry on */
				waitpid(pid, NULL, 0);
			}

			/*
			 * recreate the semaphore resource if needed
			 */
			if (TC[i].error == EINTR) {
				continue;
			}

			if ((sem_id_1 = semget(semkey, PSEMS, IPC_CREAT |
					       IPC_EXCL | SEM_RA)) == -1) {
				tst_brkm(TBROK, cleanup, "couldn't recreate "
					 "semaphore");
			}
		}
	}

	cleanup();

	tst_exit();
}
Esempio n. 28
0
int main(int ac, char **av)
{
    int lc;

    pid_t pid, pid1;
    int status;

    tst_parse_opts(ac, av, NULL, NULL);

    setup();

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

        if ((pid = FORK_OR_VFORK()) < 0) {
            tst_brkm(TBROK, cleanup, "first fork failed");
        }

        if (pid == 0) {
            if (setreuid(nobody_uid, nobody_uid) != 0) {
                perror("setreuid failed in child #1");
                exit(1);
            }
            if (mkdir(good_dir, 00700) != 0) {
                perror("mkdir failed in child #1");
                exit(1);
            }
            exit(0);
        }
        wait(&status);

        if ((pid1 = FORK_OR_VFORK()) < 0)
            tst_brkm(TBROK, cleanup, "second fork failed");

        if (pid1 == 0) {	/* second child */

            int rval;

            /*
             * set the child's ID to ltpuser2 using seteuid()
             * so that the ID can be changed back after the
             * TEST call is made.
             */
            if (seteuid(bin_uid) != 0) {
                perror("setreuid failed in child #2");
                exit(1);
            }

            TEST(chdir(good_dir));

            if (TEST_RETURN != -1) {
                printf("call succeeded unexpectedly\n");
                rval = 1;
            } else if (TEST_ERRNO != EACCES) {
                printf("didn't get EACCES as expected; got ");
                rval = 1;
            } else {
                printf("got EACCES as expected\n");
                rval = 0;
            }
            /* Only really required with vfork. */
            if (seteuid(0) != 0) {
                perror("seteuid(0) failed");
                rval = 1;
            }

            exit(rval);

        } else {
            if (wait(&status) == -1)
                tst_brkm(TBROK | TERRNO, cleanup,
                         "wait failed");
            if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
                tst_brkm(TBROK, cleanup,
                         "child exited abnormally");
            tst_resm(TPASS, "child reported success");
        }
        if (rmdir(good_dir) == -1) {
            tst_brkm(TBROK | TERRNO, cleanup,
                     "rmdir(%s) failed", good_dir);
        }
    }

    cleanup();
    tst_exit();
}
Esempio n. 29
0
File: kill12.c Progetto: 1587/ltp
/*--------------------------------------------------------------------*/
int main(int argc, char **argv)
{
/***** BEGINNING OF MAIN. *****/
	int pid, npid;
	int nsig, exno, nexno, status;
	int ret_val = 0;
	int core;
	void chsig();

#ifdef UCLINUX

	tst_parse_opts(argc, argv, NULL, NULL);

	maybe_run_child(&do_child, "dd", &temp, &sig);
#endif

	setup();
	//tempdir();            /* move to new directory */ 12/20/2003
	blenter();

	exno = 1;

	if (sigset(SIGCHLD, chsig) == SIG_ERR) {
		fprintf(temp, "\tsigset failed, errno = %d\n", errno);
		fail_exit();
	}

	for (sig = 1; sig < 14; sig++) {
		fflush(temp);
		chflag = 0;

		pid = FORK_OR_VFORK();
		if (pid < 0) {
			forkfail();
		}

		if (pid == 0) {
#ifdef UCLINUX
			if (self_exec(argv[0], "dd", temp, sig) < 0) {
				tst_brkm(TBROK, NULL, "self_exec FAILED - "
					 "terminating test.");
			}
#else
			do_child();
#endif
		} else {
			//fprintf(temp, "Testing signal %d\n", sig);

			while (!chflag)	/* wait for child */
				sleep(1);

			kill(pid, sig);	/* child should ignroe this sig */
			kill(pid, SIGCHLD);	/* child should exit */

#ifdef BCS
			while ((npid = wait(&status)) != pid
			       || (npid == -1 && errno == EINTR)) ;
			if (npid != pid) {
				fprintf(temp,
					"wait error: wait returned wrong pid\n");
				ret_val = 1;
			}
#else
			while ((npid = waitpid(pid, &status, 0)) != -1
			       || errno == EINTR) ;
#endif

			/*
			   nsig = status & 0177;
			   core = status & 0200;
			   nexno = (status & 0xff00) >> 8;
			 */
			/*****  LTP Port        *****/
			nsig = WTERMSIG(status);
#ifdef WCOREDUMP
			core = WCOREDUMP(status);
#endif
			nexno = WIFEXITED(status);
			/*****  **      **      *****/

			/* nsig is the signal number returned by wait
			   it should be 0, except when sig = 9          */

			if ((sig == 9) && (nsig != sig)) {
				fprintf(temp, "wait error: unexpected signal"
					" returned when the signal sent was 9"
					" The status of the process is %d \n",
					status);
				ret_val = 1;
			}
			if ((sig != 9) && (nsig != 0)) {
				fprintf(temp, "wait error: unexpected signal "
					"returned, the status of the process is "
					"%d  \n", status);
				ret_val = 1;
			}

			/* nexno is the exit number returned by wait
			   it should be 1, except when sig = 9          */

			if (sig == 9)
				if (nexno != 0) {
					fprintf(temp, "signal error: unexpected"
						" exit number returned when"
						" signal sent was 9, the status"
						" of the process is %d \n",
						status);
					ret_val = 1;
				} else;
			else if (nexno != 1) {
				fprintf(temp, "signal error: unexpected exit "
					"number returned,the status of the"
					" process is %d\n", status);
				ret_val = 1;
			}
		}
	}
	if (ret_val)
		local_flag = FAILED;

/*--------------------------------------------------------------------*/
	anyfail();
	tst_exit();
}					/******** END OF MAIN. ********/
Esempio n. 30
0
int main(int ac, char **av)
{
	int lc;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		int ret, len, i = 0, test_num = 0;

		tst_count = 0;

		if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_ACCESS |
				    FAN_MODIFY | FAN_CLOSE | FAN_OPEN |
				    FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD,
				  ".") < 0) {
			tst_brkm(TBROK | TERRNO, cleanup,
			    "fanotify_mark (%d, FAN_MARK_ADD, FAN_ACCESS | "
			    "FAN_MODIFY | FAN_CLOSE | FAN_OPEN | "
			    "FAN_EVENT_ON_CHILD | FAN_ONDIR, AT_FDCWD, '.') "
			    "failed", fd_notify);
		}

		/*
		 * generate sequence of events
		 */
		fd = SAFE_OPEN(cleanup, fname, O_RDWR | O_CREAT, 0700);
		event_set[tst_count] = FAN_OPEN;
		tst_count++;

		SAFE_WRITE(cleanup, 1, fd, fname, strlen(fname));
		event_set[tst_count] = FAN_MODIFY;
		tst_count++;

		SAFE_CLOSE(cleanup, fd);
		event_set[tst_count] = FAN_CLOSE_WRITE;
		tst_count++;

		/*
		 * Get list of events so far. We get events here to avoid
		 * merging of following events with the previous ones.
		 */
		ret = SAFE_READ(cleanup, 0, fd_notify, event_buf,
				EVENT_BUF_LEN);
		len = ret;

		fd = SAFE_OPEN(cleanup, fname, O_RDONLY);
		event_set[tst_count] = FAN_OPEN;
		tst_count++;

		SAFE_READ(cleanup, 0, fd, buf, BUF_SIZE);
		event_set[tst_count] = FAN_ACCESS;
		tst_count++;

		SAFE_CLOSE(cleanup, fd);
		event_set[tst_count] = FAN_CLOSE_NOWRITE;
		tst_count++;

		/*
		 * get next events
		 */
		ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len,
				EVENT_BUF_LEN - len);
		len += ret;

		/*
		 * now remove child mark
		 */
		if (fanotify_mark(fd_notify, FAN_MARK_REMOVE,
				    FAN_EVENT_ON_CHILD, AT_FDCWD, ".") < 0) {
			tst_brkm(TBROK | TERRNO, cleanup,
			    "fanotify_mark (%d, FAN_MARK REMOVE, "
			    "FAN_EVENT_ON_CHILD, AT_FDCWD, '.') failed",
			    fd_notify);
		}

		/*
		 * Do something to verify events didn't get generated
		 */
		fd = SAFE_OPEN(cleanup, fname, O_RDONLY);

		SAFE_CLOSE(cleanup, fd);

		fd = SAFE_OPEN(cleanup, ".", O_RDONLY | O_DIRECTORY);
		event_set[tst_count] = FAN_OPEN;
		tst_count++;

		SAFE_CLOSE(cleanup, fd);
		event_set[tst_count] = FAN_CLOSE_NOWRITE;
		tst_count++;

		/*
		 * Check events got generated only for the directory
		 */
		ret = SAFE_READ(cleanup, 0, fd_notify, event_buf + len,
				EVENT_BUF_LEN - len);
		len += ret;

		if (TST_TOTAL != tst_count) {
			tst_brkm(TBROK, cleanup,
				 "TST_TOTAL and tst_count are not equal");
		}
		tst_count = 0;

		/*
		 * check events
		 */
		while (i < len) {
			struct fanotify_event_metadata *event;

			event = (struct fanotify_event_metadata *)&event_buf[i];
			if (test_num >= TST_TOTAL) {
				tst_resm(TFAIL,
					 "get unnecessary event: mask=%llx "
					 "pid=%u fd=%u",
					 (unsigned long long)event->mask,
					 (unsigned)event->pid, event->fd);
			} else if (!(event->mask & event_set[test_num])) {
				tst_resm(TFAIL,
					 "get event: mask=%llx (expected %llx) "
					 "pid=%u fd=%u",
					 (unsigned long long)event->mask,
					 event_set[test_num],
					 (unsigned)event->pid, event->fd);
			} else if (event->pid != getpid()) {
				tst_resm(TFAIL,
					 "get event: mask=%llx pid=%u "
					 "(expected %u) fd=%u",
					 (unsigned long long)event->mask,
					 (unsigned)event->pid,
					 (unsigned)getpid(),
					 event->fd);
			} else {
				tst_resm(TPASS,
					    "get event: mask=%llx pid=%u fd=%u",
					    (unsigned long long)event->mask,
					    (unsigned)event->pid, event->fd);
			}
			event->mask &= ~event_set[test_num];
			/* No events left in current mask? Go for next event */
			if (event->mask == 0) {
				i += event->event_len;
				close(event->fd);
			}
			test_num++;
		}
		for (; test_num < TST_TOTAL; test_num++) {
			tst_resm(TFAIL, "didn't get event: mask=%llx",
				 event_set[test_num]);

		}
	}

	cleanup();
	tst_exit();
}