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 | * vhangup02.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * To test the basic functionality of vhangup(2) |
| 26 | * |
| 27 | * |
| 28 | * USAGE: <for command-line> |
| 29 | * vhangup02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 30 | * where, -c n : Run n copies concurrently. |
| 31 | * -i n : Execute test n times. |
| 32 | * -I x : Execute test for x seconds. |
| 33 | * -P x : Pause for x seconds between iterations. |
| 34 | * -t : Turn on syscall timing. |
| 35 | * History |
| 36 | * 07/2001 John George |
| 37 | * -Ported |
| 38 | * |
| 39 | * Restrictions |
| 40 | * None |
| 41 | */ |
| 42 | |
| 43 | #include <unistd.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <errno.h> |
| 46 | #include <pwd.h> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 47 | #include <sys/wait.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 48 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
| 50 | void setup(void); |
| 51 | void cleanup(void); |
| 52 | |
| 53 | char *TCID = "vhangup02"; |
| 54 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 55 | |
| 56 | int fail; |
| 57 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 58 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | { |
| 60 | int lc; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | |
| 62 | pid_t pid, pid1; |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 63 | int status; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 65 | tst_parse_opts(argc, argv, NULL, NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
| 67 | setup(); |
| 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 70 | /* reset tst_count in case we are looping */ |
| 71 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | fail = 0; |
| 74 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 75 | if ((pid = FORK_OR_VFORK()) < 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | tst_brkm(TFAIL, cleanup, "fork failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 77 | } else if (pid > 0) { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 78 | waitpid(pid, &status, 0); |
| 79 | _exit(0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 80 | } else { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | pid1 = setsid(); |
| 82 | if (pid1 < 0) { |
| 83 | tst_brkm(TFAIL, cleanup, "setsid failed"); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 84 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | TEST(vhangup()); |
| 86 | if (TEST_RETURN == -1) { |
| 87 | tst_resm(TFAIL, "vhangup() failed, errno:%d", |
| 88 | errno); |
| 89 | } else { |
| 90 | tst_resm(TPASS, "vhangup() succeeded"); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 95 | tst_exit(); |
robbiew | 10d8646 | 2003-03-27 22:37:45 +0000 | [diff] [blame] | 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * setup() |
| 101 | * performs all ONE TIME setup for this test |
| 102 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 103 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | { |
| 105 | /* Pause if that option was specified |
| 106 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 107 | */ |
| 108 | TEST_PAUSE; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * cleanup() |
| 113 | * performs all ONE TIME cleanup for this test at |
| 114 | * completion or premature exit |
| 115 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 119 | } |