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 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 20 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * NAME |
| 22 | * sched_setscheduler01.c |
| 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to test whether sched_setscheduler(2) sets the errnos |
| 26 | * correctly. |
| 27 | * |
| 28 | * ALGORITHM |
| 29 | * 1. Call sched_setscheduler as a non-root uid, and expect EPERM |
| 30 | * to be returned. |
| 31 | * |
| 32 | * USAGE: <for command-line> |
| 33 | * sched_setscheduler02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] |
| 34 | * where, -c n : Run n copies concurrently. |
| 35 | * -e : Turn on errno logging. |
| 36 | * -i n : Execute test n times. |
| 37 | * -I x : Execute test for x seconds. |
| 38 | * -P x : Pause for x seconds between iterations. |
| 39 | * -t : Turn on syscall timing. |
| 40 | * |
| 41 | * HISTORY |
| 42 | * 07/2001 Ported by Wayne Boyer |
| 43 | * |
| 44 | * RESTRICTIONS |
| 45 | * Must run test as root. |
| 46 | */ |
| 47 | #include <stdio.h> |
| 48 | #include <errno.h> |
| 49 | #include <sched.h> |
| 50 | #include <pwd.h> |
robbiew | 7edec5a | 2001-09-17 20:03:35 +0000 | [diff] [blame] | 51 | #include <sys/types.h> |
| 52 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | #define SCHED_INVALID 99 |
| 56 | #define INVALID_PID 999999 |
| 57 | |
| 58 | char *TCID = "sched_setscheduler02"; |
| 59 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 60 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 61 | extern struct passwd *my_getpwnam(char *); |
| 62 | |
| 63 | void setup(void); |
| 64 | void cleanup(void); |
| 65 | |
| 66 | char user1name[] = "nobody"; |
| 67 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 68 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 70 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 71 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | struct passwd *nobody; |
| 74 | pid_t pid; |
plars | 0834560 | 2002-02-26 20:54:07 +0000 | [diff] [blame] | 75 | struct sched_param param; |
robbiew | 7edec5a | 2001-09-17 20:03:35 +0000 | [diff] [blame] | 76 | int status; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 77 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 78 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 79 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
| 82 | setup(); |
| 83 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 85 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 86 | /* reset tst_count in case we are looping */ |
| 87 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | |
robbiew | d34d581 | 2005-07-11 22:28:09 +0000 | [diff] [blame] | 89 | if ((pid = FORK_OR_VFORK()) == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | tst_brkm(TBROK, cleanup, "fork failed"); |
| 91 | } |
| 92 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | if (pid == 0) { /* child */ |
plars | 0834560 | 2002-02-26 20:54:07 +0000 | [diff] [blame] | 94 | param.sched_priority = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | |
| 96 | nobody = my_getpwnam(user1name); |
| 97 | |
| 98 | if (seteuid(nobody->pw_uid) == -1) { |
| 99 | tst_brkm(TBROK, cleanup, "seteuid() failed"); |
| 100 | } |
| 101 | |
plars | 0834560 | 2002-02-26 20:54:07 +0000 | [diff] [blame] | 102 | TEST(sched_setscheduler(pid, SCHED_FIFO, ¶m)); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | |
| 104 | if (TEST_ERRNO) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | if (TEST_RETURN != -1) { |
| 108 | tst_resm(TFAIL, "sched_setscheduler(2) passed " |
| 109 | "with non root priveledges"); |
| 110 | } else if (TEST_ERRNO != EPERM) { |
| 111 | tst_resm(TFAIL, "Expected EPERM, got %d", |
| 112 | TEST_ERRNO); |
| 113 | } else { |
| 114 | tst_resm(TPASS, "got EPERM"); |
| 115 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 116 | } else { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | /* let the child carry on */ |
robbiew | 7edec5a | 2001-09-17 20:03:35 +0000 | [diff] [blame] | 118 | wait(&status); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 119 | if (WIFEXITED(status) != 0) { /* Exit with errors */ |
robbiew | 7edec5a | 2001-09-17 20:03:35 +0000 | [diff] [blame] | 120 | exit(WEXITSTATUS(status)); |
| 121 | } else { |
| 122 | exit(0); |
| 123 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (seteuid(0) == -1) { |
| 127 | tst_brkm(TBROK, cleanup, "seteuid(0) failed"); |
| 128 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 129 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 131 | tst_exit(); |
robbiew | aa01abd | 2003-03-27 18:39:24 +0000 | [diff] [blame] | 132 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * setup() - performs all ONE TIME setup for this test. |
| 137 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 138 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 139 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 140 | tst_require_root(NULL); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 142 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 143 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | TEST_PAUSE; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 149 | * completion or premature exit. |
| 150 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 151 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 153 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 154 | } |