1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2001
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * NAME
22  * 	setpgid02.c
23  *
24  * DESCRIPTION
25  *	Testcase to check that setpgid() sets errno correctly.
26  *
27  * CALLS
28  * 	setpgid
29  *
30  * ALGORITHM
31  * 	Checks that setpgid returns the correct errno values in case of
32  * 	negative testing.
33  * 	test 1: EINVAL - Pass '-1' as the pgid parameter to setpgid
34  * 	test 2: ESRCH - Pass '-1' as the pid parameter to setpgid
35  *	test 3: EPERM - Pass an invalid pgid parameter to setpgid
36  *
37  * USAGE:  <for command-line>
38  *  setpgid02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
39  *     where,  -c n : Run n copies concurrently.
40  *             -e   : Turn on errno logging.
41  *             -i n : Execute test n times.
42  *             -I x : Execute test for x seconds.
43  *             -P x : Pause for x seconds between iterations.
44  *             -t   : Turn on syscall timing.
45  *
46  * HISTORY
47  *	07/2001 Ported by Wayne Boyer
48  *
49  * RESTRICTIONS
50  * 	None
51  */
52 #include <errno.h>
53 #include <unistd.h>
54 #include <sys/wait.h>
55 #include "test.h"
56 
57 static void setup(void);
58 static void cleanup(void);
59 
60 char *TCID = "setpgid02";
61 int TST_TOTAL = 3;
62 
63 static pid_t pgid, pid;
64 static pid_t bad_pid = -1;
65 static pid_t zero_pid;
66 static pid_t unused_pid;
67 static pid_t inval_pid = 99999;
68 
69 struct test_case_t {
70 	pid_t *pid;
71 	pid_t *pgid;
72 	int error;
73 } TC[] = {
74 	/* pgid is less than zero - EINVAL */
75 	{
76 	&pid, &bad_pid, EINVAL},
77 	    /* pid doesn't match any process - ESRCH */
78 	{
79 	&unused_pid, &pgid, ESRCH},
80 	    /* pgid doesn't exist - EPERM */
81 	{
82 	&zero_pid, &inval_pid, EPERM}
83 };
84 
main(int ac,char ** av)85 int main(int ac, char **av)
86 {
87 	int lc;
88 	int i;
89 
90 	tst_parse_opts(ac, av, NULL, NULL);
91 
92 	setup();
93 
94 	for (lc = 0; TEST_LOOPING(lc); lc++) {
95 
96 		/* reset tst_count in case we are looping */
97 		tst_count = 0;
98 
99 		/* loop through the test cases */
100 		for (i = 0; i < TST_TOTAL; i++) {
101 
102 			TEST(setpgid(*TC[i].pid, *TC[i].pgid));
103 
104 			if (TEST_RETURN != -1) {
105 				tst_resm(TFAIL, "call succeeded unexpectedly");
106 				continue;
107 			}
108 
109 			if (TEST_ERRNO == TC[i].error) {
110 				tst_resm(TPASS, "expected failure - "
111 					 "errno = %d : %s", TEST_ERRNO,
112 					 strerror(TEST_ERRNO));
113 			} else {
114 				tst_resm(TFAIL, "unexpected error - %d : %s - "
115 					 "expected %d", TEST_ERRNO,
116 					 strerror(TEST_ERRNO), TC[i].error);
117 			}
118 		}
119 	}
120 	cleanup();
121 
122 	tst_exit();
123 }
124 
125 /*
126  * setup - performs all ONE TIME setup for this test
127  */
setup(void)128 static void setup(void)
129 {
130 
131 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
132 
133 	TEST_PAUSE;
134 
135 	pgid = getpgrp();
136 	pid = getpid();
137 
138 	unused_pid = tst_get_unused_pid(cleanup);
139 }
140 
141 /*
142  * cleanup - Performs all ONE TIME cleanup for this test at completion or
143  * 	     premature exit
144  */
cleanup(void)145 static void cleanup(void)
146 {
147 
148 }
149