blob: 37e165dff2f6a318f8aa02c5be34666f1c18d9da [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubise38b9612014-06-02 17:20:57 +02002 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 Ported by Wayne Boyer
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubise38b9612014-06-02 17:20:57 +02005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubise38b9612014-06-02 17:20:57 +020010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubise38b9612014-06-02 17:20:57 +020015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
plars865695b2001-08-27 22:15:12 +000021 * DESCRIPTION
22 * This test will verify the mkdir(2) syscall basic functionality
plars865695b2001-08-27 22:15:12 +000023 */
subrata_modak4bb656a2009-02-26 12:02:09 +000024
plars865695b2001-08-27 22:15:12 +000025#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>
robbiewf3681d92001-09-12 17:01:44 +000032#include <pwd.h>
subrata_modak4bb656a2009-02-26 12:02:09 +000033#include "test.h"
plars865695b2001-08-27 22:15:12 +000034
35void setup();
36void cleanup();
37
38#define PERMS 0777
39
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020040char *TCID = "mkdir05";
41int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000042
robbiewf3681d92001-09-12 17:01:44 +000043char nobody_uid[] = "nobody";
44struct passwd *ltpuser;
45
subrata_modak56207ce2009-03-23 13:35:39 +000046char tstdir1[100];
plars865695b2001-08-27 22:15:12 +000047
subrata_modak56207ce2009-03-23 13:35:39 +000048int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000049{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020050 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020051 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +000052 struct stat buf;
plars865695b2001-08-27 22:15:12 +000053
54 /*
55 * parse standard options
56 */
Garrett Cooper45e285d2010-11-22 12:19:25 -080057 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -080058 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000059 }
60
61 /*
62 * perform global setup for test
63 */
64 setup();
subrata_modakbdbaec52009-02-26 12:14:51 +000065
plars865695b2001-08-27 22:15:12 +000066 /*
67 * check looping state if -i option given
68 */
subrata_modak56207ce2009-03-23 13:35:39 +000069 for (lc = 0; TEST_LOOPING(lc); lc++) {
70
Caspar Zhangd59a6592013-03-07 14:59:12 +080071 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000072
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_modak56207ce2009-03-23 13:35:39 +000083 if (TEST_RETURN == -1) {
plars865695b2001-08-27 22:15:12 +000084 tst_resm(TFAIL, "mkdir(%s, %#o) Failed",
85 tstdir1, PERMS);
86 continue;
87 }
88
Cyril Hrubise38b9612014-06-02 17:20:57 +020089 if (stat(tstdir1, &buf) == -1) {
90 tst_brkm(TBROK, cleanup, "failed to stat the "
91 "new directory");
plars865695b2001-08-27 22:15:12 +000092 }
Cyril Hrubise38b9612014-06-02 17:20:57 +020093 /* 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");
plars865695b2001-08-27 22:15:12 +0000106
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_modakbdbaec52009-02-26 12:14:51 +0000111
Garrett Cooper2c282152010-12-16 00:55:50 -0800112 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000113
plars865695b2001-08-27 22:15:12 +0000114 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800115 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800116}
plars865695b2001-08-27 22:15:12 +0000117
118/*
119 * setup() - performs all ONE TIME setup for this test.
120 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400121void setup(void)
plars865695b2001-08-27 22:15:12 +0000122{
Nicolas Jolyd4ceb372014-06-22 17:03:57 +0200123 tst_require_root(NULL);
124
subrata_modak56207ce2009-03-23 13:35:39 +0000125 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 }
plars865695b2001-08-27 22:15:12 +0000131
plars865695b2001-08-27 22:15:12 +0000132 tst_sig(FORK, DEF_HANDLER, cleanup);
133
plars865695b2001-08-27 22:15:12 +0000134 TEST_PAUSE;
subrata_modakbdbaec52009-02-26 12:14:51 +0000135
plars865695b2001-08-27 22:15:12 +0000136 /* Create a temporary directory and make it current. */
137 tst_tmpdir();
138}
139
plars865695b2001-08-27 22:15:12 +0000140/*
141 * cleanup() - performs all ONE TIME cleanup for this test at
142 * completion or premature exit.
143 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400144void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000145{
plars865695b2001-08-27 22:15:12 +0000146
147 /*
148 * Remove the temporary directory.
149 */
150 tst_rmdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000151
plars865695b2001-08-27 22:15:12 +0000152 /*
153 * Exit with return code appropriate for results.
154 */
Garrett Cooper2c282152010-12-16 00:55:50 -0800155
Chris Dearmanec6edca2012-10-17 19:54:01 -0700156}