int create_server(void)
{
	static int count = 0;

	sockfd = socket(PF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
			 strerror(errno));
		return -1;
	}
	sin1.sin_family = AF_INET;
	sin1.sin_port = htons((getpid() % 32768) + 11000 + count);
	sin1.sin_addr.s_addr = INADDR_ANY;
	count++;
	if (bind(sockfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
		tst_brkm(TBROK, cleanup, "call to bind() failed: %s",
			 strerror(errno));
		return -1;
	}
	child_pid = FORK_OR_VFORK();
	if (child_pid < 0) {
		tst_brkm(TBROK, cleanup, "client/server fork failed: %s",
			 strerror(errno));
		return -1;
	}
	if (!child_pid) {	/* child */
#ifdef UCLINUX
		if (self_exec(argv0, "") < 0) {
			tst_brkm(TBROK, cleanup, "self_exec failed");
			return -1;

		}
#else
		do_child();
#endif
	}

	s = socket(PF_INET, SOCK_DGRAM, 0);
	inet_aton("127.0.0.1", &sin1.sin_addr);
	if (s < 0) {
		tst_brkm(TBROK, cleanup, "call to socket() failed: %s",
			 strerror(errno));
		return -1;
	}
	if (connect(s, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
		tst_brkm(TBROK, cleanup, "call to connect() failed: %s",
			 strerror(errno));
	}
	return s;

}
Exemple #2
0
int main(int ac, char **av)
{
	int lc, pid;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

#ifdef UCLINUX
	maybe_run_child(&dochild, "sdd", filename, &recstart, &reclen);
#endif

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		sprintf(filename, MOUNT_DIR"%s.%d.%d\n", TCID, getpid(), lc);

		if (tst_fill_file(filename, 0, 1024, 8)) {
			tst_brkm(TBROK, cleanup,
			         "Failed to create test file '%s'", filename);
		}

		SAFE_CHMOD(cleanup, filename, 02666);

		reclen = RECLEN;
		/*
		 * want at least RECLEN bytes BEFORE AND AFTER the
		 * record lock.
		 */
		recstart = RECLEN + rand() % (len - 3 * RECLEN);

		if ((pid = FORK_OR_VFORK()) < 0)
			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");

		if (pid == 0) {
#ifdef UCLINUX
			if (self_exec(av[0], "sdd", filename, recstart,
			              reclen) < -1) {
				tst_brkm(TBROK, cleanup, "self_exec() failed");
			}
#else
			dochild();
#endif
		}

		doparent();
	}

	cleanup();
	tst_exit();
}
Exemple #3
0
static void do_fork(void)
{
	int fork_pid, wait_pid;
	int status, i;

	wait_for_parent();

	/*
	 * Fork a kid.  Keep track of the kid's pid, have the kid do_mkdir,
	 * and wait for it. Compare the fork_pid with the wait_pid to be
	 * sure they are the same.
	 */
	for (i = 0; i < 50; i++) {
		fork_pid = FORK_OR_VFORK();
		if (fork_pid < 0) {
			tst_resm(TFAIL, "Fork failed");
			tst_exit();
		}
		if (fork_pid == 0) {
#ifdef UCLINUX
			if (self_exec(argv0, "n", 5) < 0) {
				tst_resm(TFAIL, "do_fork self_exec failed");
				tst_exit();
			}
#else
			do_mkdir();
#endif
		}

		errno = 0;
		while (((wait_pid = waitpid(fork_pid, &status, 0)) != -1) ||
		       (errno == EINTR)) {
			if (wait_pid == -1)
				continue;

			if (fork_pid != wait_pid) {
				tst_resm(TFAIL, "Didnt get a pid returned "
					 "from waitpid that matches the one "
					 "returned by fork");
				tst_resm(TFAIL, "fork pid = %d, wait pid = "
					 "%d", fork_pid, wait_pid);
				fail = 1;
			}
		}
	}

	exit(4);
}
Exemple #4
0
pid_t start_server(struct sockaddr_in *sin0)
{
	pid_t pid;
	socklen_t slen = sizeof(*sin0);

	sin0->sin_family = AF_INET;
	sin0->sin_port = 0; /* pick random free port */
	sin0->sin_addr.s_addr = INADDR_ANY;

	sfd = socket(PF_INET, SOCK_STREAM, 0);
	if (sfd < 0) {
		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
		return -1;
	}
	if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
		return -1;
	}
	if (listen(sfd, 10) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
		return -1;
	}
	if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
		tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");

	switch ((pid = FORK_OR_VFORK())) {
	case 0:		/* child */
#ifdef UCLINUX
		if (self_exec(argv0, "d", sfd) < 0) {
			tst_brkm(TBROK, cleanup, "server self_exec failed");
		}
#else
		do_child();
#endif
		break;
	case -1:
		tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
		/* fall through */
	default:		/* parent */
		(void)close(sfd);
		return pid;
	}

	exit(1);
}
Exemple #5
0
int main(int ac, char **av)
{
	int lc;
	pid_t pid;
	char *argv[2] = {TEST_APP, NULL};
	char *env[1] = {NULL};

	tst_parse_opts(ac, av, NULL, NULL);

#ifdef UCLINUX
	maybe_run_child(&do_child, "");
#endif

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		if ((pid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork failed");
		} else if (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);

		TEST(execve(TEST_APP, argv, env));

		if (TEST_ERRNO != ETXTBSY)
			tst_resm(TFAIL | TTERRNO, "execve succeeded, expected failure");
		else
			tst_resm(TPASS | TTERRNO, "execve failed as expected");

		TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
		SAFE_WAIT(cleanup, NULL);
	}

	cleanup();
	tst_exit();
}
Exemple #6
0
int main(int ac, char **av)
{
	int sig;
	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++) {
		if ((pid = FORK_OR_VFORK()) < 0) {
			tst_brkm(TBROK | TERRNO, NULL, "fork() failed");
		} else if (pid > 0) {
			TST_SAFE_CHECKPOINT_WAIT(NULL, 0);

			for (sig = 1; sig < NUMSIGS; sig++) {
				if (skip_sig(sig))
					continue;
				SAFE_KILL(NULL, pid, sig);
			}

			TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
			tst_record_childstatus(cleanup, pid);
		} else {

#ifdef UCLINUX
			if (self_exec(av[0], "") < 0) {
				tst_brkm(TBROK | TERRNO, NULL,
					 "self_exec() failed");
			}
#else
			do_child();
#endif
		}
	}

	cleanup();
	tst_exit();
}
Exemple #7
0
/*
 * cnt_setup() - set up for the GETNCNT and GETZCNT commands with semctl()
 */
static void cnt_setup(int opval)
{
	int pid, i;

	sops.sem_num = SEM4;
	sops.sem_flg = 0;

	/*
	 * if seting up for GETZCNT, the semaphore value needs to be positive
	 */
	if (opval == 0) {
		/* initialize the semaphore value to ONE */
		sops.sem_op = ONE;
		if (semop(sem_id_1, &sops, 1) == -1)
			tst_brkm(TBROK, cleanup, "semop #1 failed - cnt_setup");
	}

	/* set the correct operation */
	sops.sem_op = opval;
	for (i = 0; i < NCHILD; i++) {
		/* fork five children to wait */
		pid = FORK_OR_VFORK();
		if (pid == -1)
			tst_brkm(TBROK, cleanup, "fork failed in cnt_setup");

		if (pid == 0) {
#ifdef UCLINUX
			sem_op = sops.sem_op;
			if (self_exec(argv0, "ndd", 2, sem_id_1, sem_op) < 0)
				tst_brkm(TBROK, cleanup, "self_exec failed "
					 "in cnt_setup");
#else
			child_cnt();
#endif
		} else {
			TST_PROCESS_STATE_WAIT(cleanup, pid, 'S');
			/* save the pid so we can kill it later */
			pid_arr[i] = pid;
		}
	}
}
Exemple #8
0
/*
 * stat_setup() - Set up for the IPC_STAT command with shmctl().
 *		  Make things interesting by forking some children
 *		  that will either attach or inherit the shared memory.
 */
void stat_setup(void)
{
	void *set_shmat();
	pid_t pid;

	/*
	 * The first time through, let the children attach the memory.
	 * The second time through, attach the memory first and let
	 * the children inherit the memory.
	 */

	if (stat_time == SECOND)
		/*
		 * use the global "set_shared" variable here so that
		 * it can be removed in the stat_func() routine.
		 */
		set_shared = set_shmat();

	tst_flush();
	for (stat_i = 0; stat_i < N_ATTACH; stat_i++) {
		pid = FORK_OR_VFORK();
		if (pid == -1)
			tst_brkm(TBROK, cleanup, "could not fork");

		if (pid == 0) {
#ifdef UCLINUX
			if (self_exec(argv0, "ddd", stat_i, stat_time,
				      shm_id_1) < 0)
				tst_brkm(TBROK, cleanup, "could not self_exec");
#else
			do_child();
#endif

		} else {
			/* save the child's pid for cleanup later */
			pid_arr[stat_i] = pid;
			TST_PROCESS_STATE_WAIT(cleanup, pid, 'S');
		}
	}
}
Exemple #9
0
pid_t start_server(struct sockaddr_in *sin0)
{
	struct sockaddr_in sin1 = *sin0;
	pid_t pid;

	sfd = socket(PF_INET, SOCK_STREAM, 0);
	if (sfd < 0) {
		tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
		return -1;
	}
	if (bind(sfd, (struct sockaddr *)&sin1, sizeof(sin1)) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
		return -1;
	}
	if (listen(sfd, 10) < 0) {
		tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
		return -1;
	}
	switch ((pid = FORK_OR_VFORK())) {
	case 0:		/* child */
#ifdef UCLINUX
		if (self_exec(argv0, "d", sfd) < 0) {
			tst_brkm(TBROK, cleanup, "server self_exec failed");
		}
#else
		do_child();
#endif
		break;
	case -1:
		tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
		/* fall through */
	default:		/* parent */
		(void)close(sfd);
		return pid;
	}

	exit(1);
}
Exemple #10
0
/*
 * pid_setup() - set up for the GETPID command with semctl()
 */
static void pid_setup(void)
{
	int pid;

	/*
	 * Fork a child to do a semop that will pass.
	 */
	pid = FORK_OR_VFORK();
	if (pid == -1)
		tst_brkm(TBROK, cleanup, "fork failed in pid_setup()");

	if (pid == 0) {		/* child */
#ifdef UCLINUX
		if (self_exec(argv0, "nd", 1, sem_id_1) < 0)
			tst_brkm(TBROK, cleanup, "self_exec failed "
				 "in pid_setup()");
#else
		child_pid();
#endif
	} else {
		pid_arr[SEM2] = pid;
		TST_PROCESS_STATE_WAIT(cleanup, pid, 'Z');
	}
}
Exemple #11
0
int main(int argc, char **argv)
{
	int lc;

	int pid, npid, sig, nsig;
	int nexno, status;

	tst_parse_opts(argc, argv, NULL, NULL);
#ifdef UCLINUX
	maybe_run_child(&do_child, "");
#endif

	setup();

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

		sig = SIGFPE;

		pid = FORK_OR_VFORK();

		if (pid < 0)
			tst_brkm(TBROK|TERRNO, NULL, "fork failed");

		if (pid == 0) {
#ifdef UCLINUX
			self_exec(argv[0], "");
			/* No fork() error check is done so don't check here */
#else
			do_child();
#endif
		} else {
			kill(pid, sig);
			errno = 0;
			while (((npid = waitpid(pid, &status, 0)) != -1) ||
			       (errno == EINTR)) {
				if (errno == EINTR)
					continue;

				if (npid != pid) {
					tst_resm(TFAIL, "waitpid error: "
						 "unexpected pid returned");
				} else {
					tst_resm(TPASS,
						 "recieved expected pid");
				}

				nsig = WTERMSIG(status);

				/*
				 * nsig is the signal number returned by
				 * waitpid
				 */
				if (nsig != sig) {
					tst_resm(TFAIL, "waitpid error: "
						 "unexpected signal returned");
				} else {
					tst_resm(TPASS, "recieved expected "
						 "signal");
				}

				/*
				 * nexno is the exit number returned by
				 * waitpid
				 */
				nexno = WEXITSTATUS(status);
				if (nexno != 0) {
					tst_resm(TFAIL, "signal error: "
						 "unexpected exit number "
						 "returned");
				} else {
					tst_resm(TPASS, "recieved expected "
						 "exit value");
				}
			}
		}
	}

	tst_exit();
}
Exemple #12
0
int main(int ac, char **av)
{
	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */
	pid_t c1pid, c2pid;
	int wtstatus;
	int bytesread;
	int acnt = 0, bcnt = 0;

	char rbuf[BUFSIZ];

	/* parse standard options */
	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
#ifdef UCLINUX
	maybe_run_child(&c1func, "ndd", 1, &fildes[0], &fildes[1]);
	maybe_run_child(&c2func, "ndd", 2, &fildes[0], &fildes[1]);
#endif

	setup();

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

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

		if (pipe(fildes) == -1)
			tst_brkm(TBROK, cleanup, "pipe() failed - errno %d",
				 errno);

		if ((c1pid = FORK_OR_VFORK()) == -1)
			tst_brkm(TBROK, cleanup, "fork() failed - "
				 "errno %d", errno);
		if (c1pid == 0)
#ifdef UCLINUX
			if (self_exec(av[0], "ndd", 1, fildes[0], fildes[1]) <
			    0) {
				tst_brkm(TBROK, cleanup, "self_exec failed");
			}
#else
			c1func();
#endif
		if ((c2pid = FORK_OR_VFORK()) == -1)
			tst_brkm(TBROK, cleanup, "fork() failed - "
				 "errno %d", errno);
		if (c2pid == 0)
#ifdef UCLINUX
			if (self_exec(av[0], "ndd", 2, fildes[0], fildes[1]) <
			    0) {
				tst_brkm(TBROK, cleanup, "self_exec failed");
			}
#else
			c2func();
#endif

		/* PARENT */
		if (close(fildes[1]) == -1)
			tst_resm(TWARN, "Could not close fildes[1] - errno %d",
				 errno);
		/*
		 * Read a bit from the children first
		 */
		while ((acnt < 100) && (bcnt < 100)) {
			bytesread = safe_read(fildes[0], rbuf, sizeof(rbuf));
			if (bytesread < 0) {
				tst_resm(TFAIL, "Unable to read from pipe, "
					 "errno=%d", errno);
				break;
			}
			switch (rbuf[1]) {
			case 'A':
				acnt++;
				break;
			case 'b':
				bcnt++;
				break;
			default:
				tst_resm(TFAIL, "Got bogus '%c' "
					 "character", rbuf[1]);
				break;
			}
		}

		/*
		 * Try to kill the children
		 */
		if (kill(c1pid, SIGKILL) == -1)
			tst_resm(TFAIL, "failed to kill child 1, errno=%d",
				 errno);
		if (kill(c2pid, SIGKILL) == -1)
			tst_resm(TFAIL, "failed to kill child 1, errno=%d",
				 errno);

		/*
		 * Set action for the alarm
		 */
		if (signal(SIGALRM, alarmfunc) == SIG_ERR)
			tst_resm(TWARN|TERRNO, "call to signal failed");
		/*
		 * Set an alarm for 60 seconds just in case the child
		 * processes don't die
		 */
		alarm(60);
		if (waitpid(c1pid, &wtstatus, 0) != -1) {
			if (wtstatus != SIGKILL)
				tst_resm(TFAIL|TERRNO, "unexpected wait status "
				    "%d", wtstatus);
			else
				tst_resm(TPASS, "Child 1 killed while "
					 "writing to a pipe");
		}
		if (waitpid(c2pid, &wtstatus, 0) != -1) {
			if (!WIFSIGNALED(wtstatus) ||
			    WTERMSIG(wtstatus) != SIGKILL)
				tst_resm(TFAIL|TERRNO, "unexpected wait status "
				    "%d", wtstatus);
			else
				tst_resm(TPASS, "Child 2 killed while "
					 "writing to a pipe");
		}
		if (alarm(0) <= 0)
			tst_resm(TWARN, "call to alarm(0) failed");
	}
	cleanup();

	tst_exit();
}
Exemple #13
0
int main(int ac, char **av)
{
	int lc;
	int rval;
	const char *msg;

	msg = parse_opts(ac, av, options, &help);
	if (msg != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
#ifdef UCLINUX
	maybe_run_child(&do_child_uclinux, "dS", &parentpid, &childtty);
#endif

	if (!Devflag)
		tst_brkm(TBROK, NULL, "You must specify a tty device with "
			 "the -D option.");

	tst_require_root(NULL);

	setup();

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

		tst_count = 0;

		parenttty = devname;
		childtty = devname;

		parentpid = getpid();

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

		if (childpid == 0) {	/* child */
#ifdef UCLINUX
			if (self_exec(av[0], "dS", parentpid, childtty) < 0)
				tst_brkm(TBROK, cleanup, "self_exec failed");
#else
			do_child();
#endif
		}

		while (!sigusr1)
			sleep(1);

		sigusr1 = 0;

		parentfd = do_parent_setup();
		if (parentfd < 0) {
			kill(childpid, SIGTERM);
			waitpid(childpid, NULL, 0);
			cleanup();
		}

		/* run the parent test */
		rval = run_ptest();
		if (rval == -1) {
			/*
			 * Parent cannot set/get ioctl parameters.
			 * SIGTERM the child and cleanup.
			 */
			kill(childpid, SIGTERM);
			waitpid(childpid, NULL, 0);
			cleanup();
		}

		if (rval != 0)
			tst_resm(TFAIL, "TCGETA/TCSETA tests FAILED with "
				 "%d %s", rval, rval > 1 ? "errors" : "error");
		else
			tst_resm(TPASS, "TCGETA/TCSETA tests SUCCEEDED");

		/* FIXME: check return codes. */
		(void)kill(childpid, SIGTERM);
		(void)waitpid(childpid, NULL, 0);

		/*
		 * Clean up things from the parent by restoring the
		 * tty device information that was saved in setup()
		 * and closing the tty file descriptor.
		 */
		if (ioctl(parentfd, TCSETA, &save_io) == -1)
			tst_resm(TINFO, "ioctl restore failed in main");
		if (close(parentfd) == -1)
			tst_brkm(TBROK, cleanup, "close() failed in main");

		closed = 1;
	}
	cleanup();

	tst_exit();
}
Exemple #14
0
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 {
                    if (kill(pid, SIGHUP) == -1) {
                        tst_brkm(TBROK, cleanup,
                                 "kill failed");
                    }
                }

                /* 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();
}
Exemple #15
0
int main(int ac, char **av)
{
	int lc;
	char *msg;
	pid_t pid, fake_pid;
	int exno, status, fake_status, nsig;

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}
#ifdef UCLINUX
	maybe_run_child(&do_child, "");
#endif

	setup();		/* global setup */

	TEST_EXP_ENOS(exp_enos);

	/* 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;
		status = 1;
		exno = 1;
		pid = FORK_OR_VFORK();
		if (pid < 0) {
			tst_brkm(TBROK, cleanup, "Fork failed");
		} else if (pid == 0) {
#ifdef UCLINUX
			if (self_exec(av[0], "") < 0) {
				tst_brkm(TBROK, cleanup,
					 "self_exec of child failed");
			}
#else
			do_child();
#endif
		} else {
			fake_pid = FORK_OR_VFORK();
			if (fake_pid < 0) {
				tst_brkm(TBROK, cleanup, "Second fork failed");
			} else if (fake_pid == 0) {
#ifdef UCLINUX
				if (self_exec(av[0], "") < 0) {
					tst_brkm(TBROK, cleanup,
						 "second self_exec "
						 "of child failed");
				}
#else
				do_child();
#endif
			}
			kill(fake_pid, TEST_SIG);
			waitpid(fake_pid, &fake_status, 0);
			TEST(kill(fake_pid, TEST_SIG));
			kill(pid, TEST_SIG);
			waitpid(pid, &status, 0);
		}

		if (TEST_RETURN != -1) {
			tst_brkm(TFAIL, cleanup, "%s failed - errno = %d : %s "
				 "Expected a return value of -1 got %ld",
				 TCID, TEST_ERRNO, strerror(TEST_ERRNO),
				 TEST_RETURN);
		}

		if (STD_FUNCTIONAL_TEST) {
			/*
			 * Check to see if the errno was set to the expected
			 * value of 3 : ESRCH
			 */
			nsig = WTERMSIG(status);
			TEST_ERROR_LOG(TEST_ERRNO);
			if (TEST_ERRNO == ESRCH) {
				tst_resm(TPASS, "errno set to %d : %s, as "
					 "expected", TEST_ERRNO,
					 strerror(TEST_ERRNO));
			} else {
				tst_resm(TFAIL, "errno set to %d : %s expected "
					 "%d : %s", TEST_ERRNO,
					 strerror(TEST_ERRNO), 3, strerror(3));
			}
		}
	}
	cleanup();

	tst_exit();
}
Exemple #16
0
int main(int ac, char **av)
{
	int lc;
	char *msg;
	pid_t c_pid;
	int status, e_code;

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}
#ifdef UCLINUX
#define PIPE_NAME	"msgsnd06"
	maybe_run_child(&do_child, "d", &msg_q_1);
#endif

	setup();		/* global setup */

	if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
		tst_brkm(TBROK, cleanup, "sync_pipe_create failed");

	/* 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;

		msgkey = getipckey();

		/* create a message queue with read/write permission */
		if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW))
		    == -1) {
			tst_brkm(TBROK, cleanup, "Can't create message queue");
		}

		/* initialize the message buffer */
		init_buf(&msg_buf, MSGTYPE, MSGSIZE);

		/* write messages to the queue until it is full */
		while (msgsnd(msg_q_1, &msg_buf, MSGSIZE, IPC_NOWAIT) != -1) {
			msg_buf.mtype += 1;
		}

		/*
		 * fork a child that will attempt to write a message
		 * to the queue without IPC_NOWAIT
		 */
		if ((c_pid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "could not fork");
		}

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

#ifdef UCLINUX
			if (self_exec(av[0], "d", msg_q_1) < 0) {
				tst_brkm(TBROK, cleanup, "could not self_exec");
			}
#else
			do_child();
#endif
		} else {	/* parent */

			if (sync_pipe_wait(sync_pipes) == -1)
				tst_brkm(TBROK, cleanup,
					 "sync_pipe_wait failed");

			if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
				tst_brkm(TBROK, cleanup,
					 "sync_pipe_close failed");

			/* After son has been created, give it a chance to execute the
			 * msgsnd command before we continue. Without this sleep, on SMP machine
			 * the father rm_queue could be executed before the son msgsnd.
			 */
			sleep(2);
			/* remove the queue */
			rm_queue(msg_q_1);

			/* 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();
}
Exemple #17
0
int main(int ac, char **av)
{
	int lc;
	char *msg;
	pid_t cpid;		/* Child process id */
	int status;		/* child exit status */

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
#ifdef UCLINUX
	maybe_run_child(&do_child, "dddd", &timereq.tv_sec, &timereq.tv_nsec,
			&timerem.tv_sec, &timerem.tv_nsec);
#endif

	setup();

	/* set the expected errnos... */
	TEST_EXP_ENOS(exp_enos);

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

		tst_count = 0;

		/*
		 * Creat a child process and suspend its
		 * execution using nanosleep()
		 */
		if ((cpid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork() failed");
		}

		if (cpid == 0) {	/* Child process */
#ifdef UCLINUX
			if (self_exec(av[0], "dddd",
				      timereq.tv_sec, timereq.tv_nsec,
				      timerem.tv_sec, timerem.tv_nsec) < 0) {
				tst_brkm(TBROK, cleanup, "self_exec failed");
			}
#else
			do_child();
#endif
		}

		/* wait for child to time slot for execution */
		sleep(1);

		/* Now send signal to child */
		if (kill(cpid, SIGINT) < 0) {
			tst_brkm(TBROK, cleanup,
				 "kill() fails send signal to child");
		}

		/* Wait for child to execute */
		wait(&status);
		if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
			tst_resm(TPASS, "nanosleep() failed, interrupted"
				 " by signal (%d) as expected", EINTR);
		} else {
			tst_resm(TFAIL, "child process exited abnormally; "
				 "status = %d", status);
		}
	}

	cleanup();
	tst_exit();

}
Exemple #18
0
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();
}
Exemple #19
0
int main(int ac, char **av)
{
	int lc;
	char *msg;
	int status;		/* child process exit status */
	int rval;		/* return value for wait() */

	/* Parse standard options given to run the test. */
	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
#ifdef UCLINUX
	maybe_run_child(&do_child_uclinux, "");
#endif

	setup();

	/* set the expected errnos... */
	TEST_EXP_ENOS(exp_enos);

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

		Tst_count = 0;

		/* Creat a new process using fork() */
		if ((cpid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "fork() failed");
		}

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

		/* Parent process */
		/* sleep to ensure the child executes */
		sleep(1);

		/*
		 * Send the SIGINT signal now, so that child
		 * returns from pause and resumes execution.
		 */
		kill(cpid, SIGINT);

		/* Sleep to ensure the signal sent is effected */
		sleep(1);

		/*
		 * In case pause() doesn't return witin 2 seconds,
		 * set the alarm to send SIGKILL for the child.
		 */
		signal(SIGALRM, kill_handle);
		alarm(2);

		/* wait for child to exit */
		wait(&status);

		TEST_ERROR_LOG(status >> 8);

		/* Reset the timer in case pause() returned */
		alarm(0);

		/*
		 * Verify that, wait() returned due to normal
		 * or abnormal termination of child due to
		 * receipt of signal SIGKILL.
		 * Receipt of SIGKILL indicates that pause()
		 * didn't returned after receipt of SIGINT.
		 */
		if (WTERMSIG(status) == SIGKILL) {
			rval = wait(&status);
			if ((rval == -1) && (errno == ECHILD)) {
				tst_resm(TFAIL, "pause() didn't return "
					 "as expected");
			}
		}
	}

	cleanup();
	tst_exit();

}
Exemple #20
0
int main(int ac, char **av)
{
	int lc;
	char *msg;

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}
#ifdef UCLINUX
	maybe_run_child(&do_child_uclinux, "d", &msg_q_1);
#endif

	setup();		/* global setup */

	if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
		tst_brkm(TBROK, cleanup, "sync_pipe_create failed");

	/* 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;

		/*
		 * set up the queue here so that multiple test iterations
		 * will work.
		 */
		msgkey = getipckey();

		/* create a message queue with read/write permission */
		if ((msg_q_1 = msgget(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW))
		    == -1) {
			tst_brkm(TBROK, cleanup, "Can't create message queue");
		}

		/*
		 * fork a child that will attempt to read a non-existent
		 * message from the queue
		 */
		if ((c_pid = FORK_OR_VFORK()) == -1) {
			tst_brkm(TBROK, cleanup, "could not fork");
		}

		if (c_pid == 0) {	/* child */
			/*
			 * Attempt to read a message without IPC_NOWAIT.
			 * With no message to read, the child sleeps.
			 */

#ifdef UCLINUX
			if (self_exec(av[0], "d", msg_q_1) < 0) {
				tst_brkm(TBROK, cleanup, "could not self_exec");
			}
#else
			do_child();
#endif
		} else {	/* parent */

			if (sync_pipe_wait(sync_pipes) == -1)
				tst_brkm(TBROK, cleanup,
					 "sync_pipe_wait failed");

			if (sync_pipe_close(sync_pipes, PIPE_NAME) == -1)
				tst_brkm(TBROK, cleanup,
					 "sync_pipe_close failed");

			sleep(1);

			/* remove the queue */
			rm_queue(msg_q_1);

			waitpid(c_pid, NULL, 0);
		}
	}

	tst_exit();
}
Exemple #21
0
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();
}
Exemple #22
0
int main(int ac, char **av)
{
	int lc;			/* loop counter */
	char *msg;		/* message returned from parse_opts */
	int i;
	pid_t pid;
	void do_child();

	/* parse standard options */
	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
#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++) {

			if (sync_pipe_create(sync_pipes, PIPE_NAME) == -1)
				tst_brkm(TBROK, cleanup,
					 "sync_pipe_create failed");

			/* 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 {	/* parent */

				if (sync_pipe_wait(sync_pipes) == -1)
					tst_brkm(TBROK, cleanup,
						 "sync_pipe_wait failed: %d",
						 errno);

				if (sync_pipe_close(sync_pipes, PIPE_NAME) ==
				    -1)
					tst_brkm(TBROK, cleanup,
						 "sync_pipe_close failed: %d",
						 errno);

				/* After son has been created, give it a chance to execute the
				 * semop command before we continue. Without this sleep, on SMP machine
				 * the father rm_sema/kill could be executed before the son semop.
				 */
				sleep(1);

				/*
				 * 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 {
					if (kill(pid, SIGHUP) == -1) {
						tst_brkm(TBROK, cleanup,
							 "kill failed");
					}
				}

				/* 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();
}
Exemple #23
0
int main(int ac, char **av)
{
	int kid_count, ret_val, status, nkids;
	int i, j, k, found;
	int fork_kid_pid[MAXKIDS], wait_kid_pid[MAXKIDS];
	int runtime;		/* time(sec) to run this process */

	int lc;
	char *msg;

	msg = parse_opts(ac, av, NULL, NULL);
	if (msg != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

#ifdef UCLINUX
	argv0 = av[0];

	maybe_run_child(&do_exit, "n", 1);
	maybe_run_child(&do_compute, "n", 2);
	maybe_run_child(&do_fork, "n", 3);
	maybe_run_child(&do_sleep, "n", 4);
	maybe_run_child(&do_mkdir, "n", 5);
#endif

	/*
	 * process the arg -- If there is one arg, it is the
	 * number of seconds to run.  If there is no arg the program
	 * defaults to 60 sec runtime.
	 */
	if (ac == 2) {
		if (sscanf(av[1], "%d", &runtime) != 1)
			tst_resm(TFAIL, "%s is an invalid argument", av[1]);
	} else {
		runtime = 60;
	}

	setup();

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

		if (signal(SIGALRM, alrmhandlr) == SIG_ERR) {
			tst_resm(TFAIL, "signal SIGALRM failed.  errno = %d",
				 errno);

		}
		alrmintr = 0;

		/*
		 * Set up to catch SIGINT. The kids will wait till a SIGINT
		 * has been received before they proceed.
		 */
		if (signal(SIGINT, inthandlr) == SIG_ERR) {
			tst_resm(TFAIL, "signal SIGINT failed.  errno = %d",
				 errno);

		}
		intintr = 0;

		/* Turn on the real time interval timer. */
		if ((alarm(runtime)) < 0)
			tst_resm(TFAIL, "alarm failed.  errno = %d", errno);

		/* Run the test over and over until the timer expires */
		for (;;) {
			if (alrmintr)
				break;

			/*
			 * Fork 8 kids. There will be 4 sets of 2 processes
			 * doing the same thing. Save all kid pid's in an
			 * array for future use. The kids will first wait for
			 * the parent to send SIGINT. Then will proceed to
			 * their assigned tasks.
			 */
			kid_count = 0;
			/*
			 * Clearing the intinitr flag here for all the children.
			 * So that we may not miss any signals !
			 */
			intintr = 0;
			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 0 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 1) < 0)
					tst_resm(TFAIL, "self_exec 0 failed");
#else
				do_exit();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 0 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 1 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 1) < 0)
					tst_resm(TFAIL, "self_exec 1 failed");
#else
				do_exit();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 1 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 2 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 2) < 0)
					tst_resm(TFAIL, "self_exec 2 failed");
#else
				do_compute();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 2 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 3 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 2) < 0)
					tst_resm(TFAIL, "self_exec 3 failed");
#else
				do_compute();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 3 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 4 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 3) < 0)
					tst_resm(TFAIL, "self_exec 4 failed");
#else
				do_fork();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 4 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 5 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 3) < 0)
					tst_resm(TFAIL, "self_exec 5 failed");
#else
				do_fork();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 5 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 6 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 4) < 0)
					tst_resm(TFAIL, "self_exec 6 failed");
#else
				do_sleep();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 6 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			ret_val = FORK_OR_VFORK();
			if (ret_val == 0) {	/* child 7 */
#ifdef UCLINUX
				if (self_exec(argv0, "n", 4) < 0)
					tst_resm(TFAIL, "self_exec 7 failed");
#else
				do_sleep();
#endif
			}
			if (ret_val < 0) {
				tst_resm(TFAIL, "Fork kid 7 failed. errno = "
					 "%d", errno);

			}

			/* parent */
			fork_kid_pid[kid_count++] = ret_val;

			nkids = kid_count;

			/*
			 * Now send all the kids a SIGINT to tell them to
			 * proceed. We sleep for a while first to allow the
			 * children to initialize their "intintr" variables
			 * and get set up.
			 */
			sleep(15);

			for (i = 0; i < nkids; i++) {
				if (kill(fork_kid_pid[i], SIGINT) < 0) {
					tst_resm(TFAIL, "Kill of child %d "
						 "failed, errno = %d", i,
						 errno);
				}
			}

			/* Wait till all kids have terminated. */
			kid_count = 0;
			errno = 0;
			for (i = 0; i < nkids; i++) {
				while (((ret_val = waitpid(fork_kid_pid[i],
							   &status, 0)) != -1)
				       || (errno == EINTR)) {
					if (ret_val == -1)
						continue;

					wait_kid_pid[kid_count++] = ret_val;
				}
			}

			/*
			 * Check that for every entry in the fork_kid_pid
			 * array, there is a matching pid in the
			 * wait_kid_pid array.
			 */
			for (i = 0; i < MAXKIDS; i++) {
				found = 0;
				for (j = 0; j < MAXKIDS; j++) {
					if (fork_kid_pid[i] == wait_kid_pid[j]) {
						found = 1;
						break;
					}
				}
				if (!found) {
					tst_resm(TFAIL, "Did not find a "
						 "wait_kid_pid for the "
						 "fork_kid_pid of %d",
						 fork_kid_pid[i]);
					for (k = 0; k < nkids; k++) {
						tst_resm(TFAIL,
							 "fork_kid_pid[%d] = "
							 "%d", k,
							 fork_kid_pid[k]);
					}
					for (k = 0; k < kid_count; k++) {
						tst_resm(TFAIL,
							 "wait_kid_pid[%d] = "
							 "%d", k,
							 wait_kid_pid[k]);
					}
					fail = 1;
				}
			}
		}

		/* Kill kids and remove file from do_mkdir */
		rmdir("waitpid14.ttt.ttt");

		if (fail)
			tst_resm(TFAIL, "Test FAILED");
		else
			tst_resm(TPASS, "Test PASSED");
	}

	cleanup();
	tst_exit();
}
Exemple #24
0
int main(int argc, char **argv)
{
	char *msg;

	int kid_count, ret_val, status;
	int i, j, k, found;
	int group1, group2;
	int fork_kid_pid[MAXKIDS], wait_kid_pid[MAXKIDS];
	int pid;

	if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

#ifdef UCLINUX
	maybe_run_child(&do_exit_uclinux, "");
#endif

	setup();

	Tst_count = 0;
	fail = 0;

	/*
	 * Need to have test run from child as test driver causes
	 * test to be a session leader and setpgrp fails.
	 */

	if (0 < (pid = FORK_OR_VFORK())) {
		waitpid(pid, &status, 0);
		if (WEXITSTATUS(status) != 0) {
			tst_resm(TFAIL, "child returned bad status");
			fail = 1;
		}
		if (fail)
			tst_resm(TFAIL, "%s FAILED", TCID);
		else
			tst_resm(TPASS, "%s PASSED", TCID);
		cleanup();
		tst_exit();
	} else if (pid < 0)
		tst_brkm(TBROK, cleanup, "fork failed");

	/*
	 * Set up to catch SIGINT.  The kids will wait till a SIGINT
	 * has been received before they proceed.
	 */
	setup_sigint();

	group1 = getpgrp();

	for (kid_count = 0; kid_count < MAXKIDS; kid_count++) {
		if (kid_count == (MAXKIDS / 2))
			group2 = setpgrp();

		intintr = 0;
		ret_val = FORK_OR_VFORK();
		if (ret_val == 0) {
#ifdef UCLINUX
			if (self_exec(argv[0], "") < 0)
				tst_resm(TFAIL, "self_exec kid %d "
					 "failed", kid_count);
#else
			do_exit();
#endif
		}

		if (ret_val < 0)
			tst_resm(TFAIL|TERRNO, "forking kid %d failed",
			         kid_count);

		/* parent */
		fork_kid_pid[kid_count] = ret_val;
	}

	/* Check that waitpid with WNOHANG returns zero */
	if ((ret_val = waitpid(0, &status, WNOHANG)) != 0) {
		tst_resm(TFAIL, "Waitpid returned wrong value");
		tst_resm(TFAIL, "Expected 0 got %d", ret_val);
		fail = 1;
	}
#ifdef UCLINUX
	/* Give the kids a chance to setup SIGINT again, since this is
	 * cleared by exec().
	 */
	sleep(3);
#endif

	/* Now send all the kids a SIGINT to tell them to proceed */
	for (i = 0; i < MAXKIDS; i++)
		if (kill(fork_kid_pid[i], SIGINT) < 0)
			tst_resm(TFAIL|TERRNO,
			    "killing child %d failed", i);

	/*
	 * Wait till all kids have terminated.  Stash away their
	 * pid's in an array.
	 */
	kid_count = 0;
	errno = 0;
	sleep(2);
	while (((ret_val = waitpid(0, &status, WNOHANG)) != -1) ||
	       (errno == EINTR)) {
		if ((ret_val == -1) || (ret_val == 0))
			continue;

		if (!WIFEXITED(status)) {
			tst_resm(TFAIL, "Child %d did not exit "
				 "normally", ret_val);
			fail = 1;
		} else {
			if (WEXITSTATUS(status) != 3) {
				tst_resm(TFAIL, "Child %d exited with "
					 "wrong status", ret_val);
				tst_resm(TFAIL, "Expected 3 got %d",
					 WEXITSTATUS(status));
				fail = 1;
			}
		}
		wait_kid_pid[kid_count++] = ret_val;
	}

	/*
	 * Check that for every entry in the fork_kid_pid array,
	 * there is a matching pid in the wait_kid_pid array.  If
	 * not, it's an error.
	 */
	for (i = 0; i < kid_count; i++) {
		found = 0;
		for (j = (MAXKIDS / 2); j < MAXKIDS; j++) {
			if (fork_kid_pid[j] == wait_kid_pid[i]) {
				found = 1;
				break;
			}
		}
		if (!found) {
			tst_resm(TFAIL, "Did not find a wait_kid_pid "
				 "for the fork_kid_pid of %d",
				 wait_kid_pid[i]);
			for (k = 0; k < MAXKIDS; k++)
				tst_resm(TFAIL, "fork_kid_pid[%d] = "
					 "%d", k, fork_kid_pid[k]);
			for (k = 0; k < kid_count; k++)
				tst_resm(TFAIL, "wait_kid_pid[%d] = "
					 "%d", k, wait_kid_pid[k]);
			fail = 1;
		}
	}

	if (kid_count != (MAXKIDS / 2)) {
		tst_resm(TFAIL, "Wrong number of children waited on "
			 "for pid = 0");
		tst_resm(TFAIL, "Expected 4 got %d", kid_count);
		fail = 1;
	}

	/* Make sure can pickup children in a diff. process group */

	kid_count = 0;
	errno = 0;
	while (((ret_val = waitpid(-(group1), &status, WNOHANG)) !=
		-1) || (errno == EINTR)) {
		if (ret_val == -1 || ret_val == 0) {
			continue;
		}
		if (!WIFEXITED(status)) {
			tst_resm(TFAIL, "Child %d did not exit "
				 "normally", ret_val);
			fail = 1;
		} else {
			if (WEXITSTATUS(status) != 3) {
				tst_resm(TFAIL, "Child %d exited with "
					 "wrong status", ret_val);
				tst_resm(TFAIL, "Expected 3 got %d",
					 WEXITSTATUS(status));
				fail = 1;
			}
		}
		wait_kid_pid[kid_count++] = ret_val;
	}

	/*
	 * Check that for every entry in the fork_kid_pid array,
	 * there is a matching pid in the wait_kid_pid array.  If
	 * not, it's an error.
	 */
	for (i = 0; i < kid_count; i++) {
		found = 0;
		for (j = 0; j < (MAXKIDS / 2); j++) {
			if (fork_kid_pid[j] == wait_kid_pid[i]) {
				found = 1;
				break;
			}
		}
		if (!found) {
			tst_resm(TFAIL, "Did not find a wait_kid_pid "
				 "for the fork_kid_pid of %d",
				 fork_kid_pid[j]);
			for (k = 0; k < MAXKIDS; k++)
				tst_resm(TFAIL, "fork_kid_pid[%d] = "
					 "%d", k, fork_kid_pid[k]);
			for (k = 0; k < kid_count; k++)
				tst_resm(TFAIL, "wait_kid_pid[%d] = "
					 "%d", k, wait_kid_pid[k]);
			fail = 1;
		}
	}
	if (kid_count != (MAXKIDS / 2)) {
		tst_resm(TFAIL, "Wrong number of children waited on "
			 "for pid = 0");
		tst_resm(TFAIL, "Expected 4 got %d", kid_count);
		fail = 1;
	}

	if (fail)
		tst_resm(TFAIL, "Test FAILED");
	else
		tst_resm(TPASS, "Test PASSED");

	tst_exit();
}
Exemple #25
0
int main(int argc, char **argv)
{
    int lc;
    char *msg;

    int pid, npid, sig, nsig;
    int exno, nexno, status;

    msg = parse_opts(argc, argv, NULL, NULL);
    if (msg != NULL)
        tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
#ifdef UCLINUX
    maybe_run_child(&do_child, "");
#endif

    setup();

    /* check for looping state if -i option is given */
    for (lc = 0; TEST_LOOPING(lc); lc++) {
        /* reset tst_count in case we are looping */
        tst_count = 0;

        exno = 1;
        sig = SIGFPE;

        pid = FORK_OR_VFORK();

        if (pid == 0) {
#ifdef UCLINUX
            self_exec(argv[0], "");
            /* No fork() error check is done so don't check here */
#else
            do_child();
#endif
        } else {
            kill(pid, sig);
            errno = 0;
            while (((npid = waitpid(pid, &status, 0)) != -1) ||
                    (errno == EINTR)) {
                if (errno == EINTR)
                    continue;

                if (npid != pid) {
                    tst_resm(TFAIL, "waitpid error: "
                             "unexpected pid returned");
                } else {
                    tst_resm(TPASS,
                             "recieved expected pid");
                }

                nsig = WTERMSIG(status);

                /*
                 * nsig is the signal number returned by
                 * waitpid
                 */
                if (nsig != sig) {
                    tst_resm(TFAIL, "waitpid error: "
                             "unexpected signal returned");
                } else {
                    tst_resm(TPASS, "recieved expected "
                             "signal");
                }

                /*
                 * nexno is the exit number returned by
                 * waitpid
                 */
                nexno = WEXITSTATUS(status);
                if (nexno != 0) {
                    tst_resm(TFAIL, "signal error: "
                             "unexpected exit number "
                             "returned");
                } else {
                    tst_resm(TPASS, "recieved expected "
                             "exit value");
                }
            }
        }
    }

    cleanup();
    tst_exit();
}
Exemple #26
0
/*
 * do_master_child()
 */
void do_master_child(char **av)
{
	pid_t pid1;
	int status;

	char user1name[] = "nobody";
	char user2name[] = "bin";

	struct passwd *ltpuser1, *ltpuser2;

	TEST_EXP_ENOS(exp_enos);

	tst_count = 0;

	*flag = 0;

	pid1 = FORK_OR_VFORK();

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

	if (pid1 == 0) {
		ltpuser1 = SAFE_GETPWNAM(NULL, user1name);
		if (setreuid(ltpuser1->pw_uid, ltpuser1->pw_uid) == -1) {
			perror("setreuid failed (in child)");
			exit(1);
		}
		*flag = 1;
#ifdef UCLINUX
		if (self_exec(av[0], "") < 0) {
			perror("self_exec failed");
			exit(1);
		}
#else
		do_child();
#endif
	}
	ltpuser2 = SAFE_GETPWNAM(NULL, user2name);
	if (setreuid(ltpuser2->pw_uid, ltpuser2->pw_uid) == -1) {
		perror("seteuid failed");
		exit(1);
	}

	/* wait until child sets its euid */
	wait_for_flag(1);

	TEST(kill(pid1, TEST_SIG));

	/* signal the child that we're done */
	*flag = 2;

	if (waitpid(pid1, &status, 0) == -1) {
		perror("waitpid failed");
		exit(1);
	}

	if (TEST_RETURN != -1) {
		printf("kill succeeded unexpectedly\n");
		exit(1);
	}

	/*
	 * Check to see if the errno was set to the expected
	 * value of 1 : EPERM
	 */
	if (TEST_ERRNO == EPERM) {
		printf("kill failed with EPERM\n");
		exit(0);
	}
	perror("kill failed unexpectedly");
	exit(1);
}
Exemple #27
0
void test_setup(int i, char *argv0)
{
	char nobody_uid[] = "nobody";
	struct passwd *ltpuser;

#ifdef UCLINUX
	i = i + 2;
#endif
	switch (i) {

	case 0:
		break;

	case 1:
		header.version = _LINUX_CAPABILITY_VERSION;
		header.pid = 0;
		break;

	case 2:
		header.version = INVALID_VERSION;
		header.pid = 0;
		break;

	case 3:
		header.version = _LINUX_CAPABILITY_VERSION;
		/*
		 * when a non-zero pid is specified, process should have
		 * CAP_SETPCAP capability to change capabilities.
		 * by default, CAP_SETPCAP is not enabled. So giving
		 * a non-zero pid results in capset() failing with
		 * errno EPERM
		 *
		 * Note: this seems to have changed with recent kernels
		 * => create a child and try to set its capabilities
		 */
		child_pid = FORK_OR_VFORK();
		switch (child_pid) {
		case -1:
			tst_resm(TBROK|TERRNO, "fork() failed");
			cleanup();
			break;
		case 0:
#ifdef UCLINUX
			if (self_exec(argv0, "") < 0) {
				tst_resm(TBROK, "self_exec() failed");
				cleanup();
				break;
			}
#else
			child_func();
#endif
			break;
		default:
			signal(SIGCHLD, SIG_IGN);
			header.pid = child_pid;
			ltpuser = getpwnam(nobody_uid);
			if (seteuid(ltpuser->pw_uid) == -1) {
				tst_resm(TBROK|TERRNO, "seteuid() failed");
				cleanup();
			}

			break;
		}
		break;

	}
}
Exemple #28
0
int main(int argc, char **argv)
{
	int lc;
	char *msg;
	pid_t pid;
	int status;
	int fd;

	if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);

#ifdef UCLINUX
	maybe_run_child(&childfunc_uc, "ds", &fd_uc, FILE_NAME);
#endif

	setup();

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

		fd = open(FILE_NAME, O_RDWR);

		if (fd == -1)
			tst_brkm(TFAIL, cleanup, "parent failed to open the"
				 "file, errno %d", errno);

		pid = FORK_OR_VFORK();

		if (pid == -1)
			tst_brkm(TFAIL, cleanup, "fork() failed, errno %d",
				 errno);
		if (pid == 0) {
#ifdef UCLINUX
			if (self_exec(argv[0], "ds", fd, FILE_NAME) < 0)
				tst_brkm(TFAIL, cleanup, "self_exec failed, "
					 "errno &d", errno);
#else
			childfunc(fd);
#endif
		}

		TEST(flock(fd, LOCK_EX | LOCK_NB));

		if (TEST_RETURN != 0)
			tst_resm(TFAIL,
				 "Parent: Initial attempt to flock() failed, "
				 "errno %d", TEST_ERRNO);
		else
			tst_resm(TPASS,
				 "Parent: Initial attempt to flock() passed");

		TST_CHECKPOINT_SIGNAL_CHILD(cleanup, &checkpoint);

		if ((waitpid(pid, &status, 0)) < 0) {
			tst_resm(TFAIL, "wait() failed");
			continue;
		}
		if ((WIFEXITED(status)) && (WEXITSTATUS(status) == 0))
			tst_resm(TPASS, "flock03 Passed");
		else
			tst_resm(TFAIL, "flock03 Failed");

		close(fd);

	}

	cleanup();
	tst_exit();
}
Exemple #29
0
int main(int ac, char **av)
{
	int lc;
	char *msg;
	pid_t pid1, pid2;
	int exno, status, nsig, i;

	if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
		tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
	}
#ifdef UCLINUX
	maybe_run_child(&do_child, "");
#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;
		status = 1;
		exno = 1;

		/* Fork a process and set the process group so that */
		/* it is different from this one.  Fork 5 more children. */

		pid1 = FORK_OR_VFORK();
		if (pid1 < 0) {
			tst_brkm(TBROK, cleanup, "Fork of first child failed");
		} else if (pid1 == 0) {
			setpgrp();
			for (i = 0; i < 5; i++) {
				pid2 = FORK_OR_VFORK();
				if (pid2 < 0) {
					tst_brkm(TBROK, cleanup, "Fork failed");
				} else if (pid2 == 0) {
#ifdef UCLINUX
					if (self_exec(av[0], "") < 0) {
						tst_brkm(TBROK, cleanup,
							 "self_exec of "
							 "child failed");
					}
#else
					do_child();
#endif
				}
			}
			/* Kill all processes in this process group */
			TEST(kill(0, TEST_SIG));
			pause();
			exit(exno);
		} else {
			waitpid(pid1, &status, 0);
			if (TEST_RETURN != 0) {
				tst_brkm(TFAIL, cleanup, "%s failed - errno = "
					 "%d : %s", TCID, TEST_ERRNO,
					 strerror(TEST_ERRNO));
			}
		}

		if (STD_FUNCTIONAL_TEST) {
			/*
			 * Check to see if the process was terminated with the
			 * expected signal.
			 */
			nsig = WTERMSIG(status);
			if (nsig == TEST_SIG) {
				tst_resm(TPASS, "received expected signal %d",
					 nsig);
			} else {
				tst_resm(TFAIL,
					 "expected signal %d received %d",
					 TEST_SIG, nsig);
			}
		} else {
			tst_resm(TPASS, "call succeeded");
		}
	}
	cleanup();

	tst_exit();
}
Exemple #30
0
/*--------------------------------------------------------------------*/
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. ********/