plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 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 Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 49 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
| 51 | void setup(void); |
| 52 | void cleanup(void); |
| 53 | |
| 54 | char *TCID = "vhangup01"; |
| 55 | int TST_TOTAL = 1; |
| 56 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | int fail; |
| 58 | char user1name[] = "nobody"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 59 | extern struct passwd *my_getpwnam(char *); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 61 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | { |
| 63 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 64 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | |
| 66 | pid_t pid; |
| 67 | int retval, status; |
| 68 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 69 | struct passwd *nobody; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 71 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 73 | } |
| 74 | |
| 75 | setup(); |
| 76 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 78 | /* reset tst_count in case we are looping */ |
| 79 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | |
| 81 | nobody = my_getpwnam(user1name); |
| 82 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 83 | if ((pid = FORK_OR_VFORK()) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_brkm(TFAIL, cleanup, "fork failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 85 | } else if (pid > 0) { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | } else { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | retval = setreuid(nobody->pw_uid, nobody->pw_uid); |
| 97 | if (retval < 0) { |
| 98 | perror("setreuid"); |
| 99 | tst_brkm(TFAIL, cleanup, "setreuid failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 100 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | TEST(vhangup()); |
| 102 | if (TEST_RETURN != -1) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | tst_brkm(TFAIL, cleanup, "vhangup() failed to " |
| 104 | "fail"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | } else if (TEST_ERRNO == EPERM) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | tst_resm(TPASS, "Got EPERM as expected."); |
| 107 | } else { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | tst_resm(TFAIL, "expected EPERM got %d", |
| 109 | TEST_ERRNO); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 114 | tst_exit(); |
robbiew | 10d8646 | 2003-03-27 22:37:45 +0000 | [diff] [blame] | 115 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /* |
| 119 | * setup() |
| 120 | * performs all ONE TIME setup for this test |
| 121 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 122 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | TEST_PAUSE; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * cleanup() |
| 129 | * performs all ONE TIME cleanup for this test at |
| 130 | * completion or premature exit |
| 131 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 132 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 135 | } |