blob: e3c6ba418a399d4d958daef898088aa3cc0f2c2a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * vhangup01.c
23 *
24 * DESCRIPTION
25 * Check the return value, and errno of vhangup(2)
26 * when a non-root user calls vhangup().
27 *
28 * USAGE: <for command-line>
29 * vhangup01 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
30 * where, -c n : Run n copies concurrently.
31 * -e : Turn on errno logging.
32 * -i n : Execute test n times.
33 * -I x : Execute test for x seconds.
34 * -P x : Pause for x seconds between iterations.
35 * -t : Turn on syscall timing.
36 *
37 * History
38 * 07/2001 John George
39 * -Ported
40 *
41 * Restrictions
42 * None
43 */
44
45#include <unistd.h>
46#include <errno.h>
47#include <pwd.h>
48#include <wait.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080049#include "test.h"
plars865695b2001-08-27 22:15:12 +000050
51void setup(void);
52void cleanup(void);
53
54char *TCID = "vhangup01";
55int TST_TOTAL = 1;
56
plars865695b2001-08-27 22:15:12 +000057int fail;
58char user1name[] = "nobody";
subrata_modak56207ce2009-03-23 13:35:39 +000059extern struct passwd *my_getpwnam(char *);
plars865695b2001-08-27 22:15:12 +000060
plars74948ad2002-11-14 16:16:14 +000061int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000062{
63 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020064 const char *msg;
plars865695b2001-08-27 22:15:12 +000065
66 pid_t pid;
67 int retval, status;
68
subrata_modak4bb656a2009-02-26 12:02:09 +000069 struct passwd *nobody;
plars865695b2001-08-27 22:15:12 +000070
Wanlong Gao354ebb42012-12-07 10:10:04 +080071 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000072 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
73 }
74
75 setup();
76
plars865695b2001-08-27 22:15:12 +000077 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080078 /* reset tst_count in case we are looping */
79 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000080
81 nobody = my_getpwnam(user1name);
82
robbiewd34d5812005-07-11 22:28:09 +000083 if ((pid = FORK_OR_VFORK()) < 0) {
plars865695b2001-08-27 22:15:12 +000084 tst_brkm(TFAIL, cleanup, "fork failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +080085 } else if (pid > 0) { /* parent */
plars865695b2001-08-27 22:15:12 +000086 waitpid(pid, &status, 0);
87 _exit(0); /*
88 * Exit here and let the child clean up.
89 * This allows the errno information set
90 * by the TEST_ERROR_LOG macro and the
91 * PASS/FAIL status to be preserved for
92 * use during cleanup.
93 */
94
subrata_modak56207ce2009-03-23 13:35:39 +000095 } else { /* child */
plars865695b2001-08-27 22:15:12 +000096 retval = setreuid(nobody->pw_uid, nobody->pw_uid);
97 if (retval < 0) {
98 perror("setreuid");
99 tst_brkm(TFAIL, cleanup, "setreuid failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800100 }
plars865695b2001-08-27 22:15:12 +0000101 TEST(vhangup());
102 if (TEST_RETURN != -1) {
subrata_modak56207ce2009-03-23 13:35:39 +0000103 tst_brkm(TFAIL, cleanup, "vhangup() failed to "
104 "fail");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 } else if (TEST_ERRNO == EPERM) {
plars865695b2001-08-27 22:15:12 +0000106 tst_resm(TPASS, "Got EPERM as expected.");
107 } else {
plars865695b2001-08-27 22:15:12 +0000108 tst_resm(TFAIL, "expected EPERM got %d",
109 TEST_ERRNO);
110 }
111 }
112 }
113 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800114 tst_exit();
robbiew10d86462003-03-27 22:37:45 +0000115
plars865695b2001-08-27 22:15:12 +0000116}
117
118/*
119 * setup()
120 * performs all ONE TIME setup for this test
121 */
subrata_modak56207ce2009-03-23 13:35:39 +0000122void setup(void)
plars865695b2001-08-27 22:15:12 +0000123{
plars865695b2001-08-27 22:15:12 +0000124 TEST_PAUSE;
125}
126
127/*
128 * cleanup()
129 * performs all ONE TIME cleanup for this test at
130 * completion or premature exit
131 */
subrata_modak56207ce2009-03-23 13:35:39 +0000132void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000133{
plars865695b2001-08-27 22:15:12 +0000134
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135}