plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * 07/2001 Ported by Wayne Boyer |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 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 Foundation, |
| 17 | * 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 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * DESCRIPTION |
| 22 | * This test will verify the mkdir(2) syscall basic functionality |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | */ |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | #include <errno.h> |
| 26 | #include <string.h> |
| 27 | #include <signal.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <unistd.h> |
robbiew | f3681d9 | 2001-09-12 17:01:44 +0000 | [diff] [blame] | 32 | #include <pwd.h> |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 33 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | |
| 35 | void setup(); |
| 36 | void cleanup(); |
| 37 | |
| 38 | #define PERMS 0777 |
| 39 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 40 | char *TCID = "mkdir05"; |
| 41 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 42 | |
robbiew | f3681d9 | 2001-09-12 17:01:44 +0000 | [diff] [blame] | 43 | char nobody_uid[] = "nobody"; |
| 44 | struct passwd *ltpuser; |
| 45 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | char tstdir1[100]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 47 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 48 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 50 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 51 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 52 | struct stat buf; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * parse standard options |
| 56 | */ |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 57 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 58 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /* |
| 62 | * perform global setup for test |
| 63 | */ |
| 64 | setup(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 65 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | /* |
| 67 | * check looping state if -i option given |
| 68 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 69 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 70 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 71 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * TEST mkdir() base functionality |
| 75 | */ |
| 76 | |
| 77 | /* Initialize the test directory name */ |
| 78 | sprintf(tstdir1, "tstdir1.%d", getpid()); |
| 79 | |
| 80 | /* Call mkdir(2) using the TEST macro */ |
| 81 | TEST(mkdir(tstdir1, PERMS)); |
| 82 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 83 | if (TEST_RETURN == -1) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 84 | tst_resm(TFAIL, "mkdir(%s, %#o) Failed", |
| 85 | tstdir1, PERMS); |
| 86 | continue; |
| 87 | } |
| 88 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 89 | if (stat(tstdir1, &buf) == -1) { |
| 90 | tst_brkm(TBROK, cleanup, "failed to stat the " |
| 91 | "new directory"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 92 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 93 | /* check the owner */ |
| 94 | if (buf.st_uid != geteuid()) { |
| 95 | tst_resm(TFAIL, "mkdir() FAILED to set owner ID" |
| 96 | " as process's effective ID"); |
| 97 | continue; |
| 98 | } |
| 99 | /* check the group ID */ |
| 100 | if (buf.st_gid != getegid()) { |
| 101 | tst_resm(TFAIL, "mkdir() failed to set group ID" |
| 102 | " as the process's group ID"); |
| 103 | continue; |
| 104 | } |
| 105 | tst_resm(TPASS, "mkdir() functionality is correct"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | |
| 107 | /* clean up things in case we are looping */ |
| 108 | if (rmdir(tstdir1) == -1) { |
| 109 | tst_brkm(TBROK, cleanup, "could not remove directory"); |
| 110 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 111 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 112 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 113 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 114 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 115 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 116 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | |
| 118 | /* |
| 119 | * setup() - performs all ONE TIME setup for this test. |
| 120 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 121 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 122 | { |
Nicolas Joly | d4ceb37 | 2014-06-22 17:03:57 +0200 | [diff] [blame] | 123 | tst_require_root(NULL); |
| 124 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | ltpuser = getpwnam(nobody_uid); |
| 126 | if (setuid(ltpuser->pw_uid) == -1) { |
| 127 | tst_resm(TINFO, "setuid failed to " |
| 128 | "to set the effective uid to %d", ltpuser->pw_uid); |
| 129 | perror("setuid"); |
| 130 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 131 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 133 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 134 | TEST_PAUSE; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 135 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | /* Create a temporary directory and make it current. */ |
| 137 | tst_tmpdir(); |
| 138 | } |
| 139 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 140 | /* |
| 141 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 142 | * completion or premature exit. |
| 143 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 144 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * Remove the temporary directory. |
| 149 | */ |
| 150 | tst_rmdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 151 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 152 | /* |
| 153 | * Exit with return code appropriate for results. |
| 154 | */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 155 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 156 | } |