1 /* Copyright (c) International Business Machines Corp., 2001
2 * 07/2001 Ported by Wayne Boyer
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /* DESCRIPTION
19 * This test will verify the mkdir(2) creates a new directory successfully and
20 * it is owned by the effective UID and GID of the process.
21 */
22
23 #include <errno.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <pwd.h>
28 #include "tst_test.h"
29
30 #define PERMS 0777
31 #define TESTDIR "testdir"
32
verify_mkdir(void)33 static void verify_mkdir(void)
34 {
35 struct stat buf;
36
37 TEST(mkdir(TESTDIR, PERMS));
38 if (TST_RET == -1) {
39 tst_res(TFAIL | TTERRNO, "mkdir() Failed");
40 return;
41 }
42
43 SAFE_STAT(TESTDIR, &buf);
44
45 if (buf.st_uid != geteuid()) {
46 tst_res(TFAIL, "mkdir() FAILED to set owner ID "
47 "as process's effective ID");
48 return;
49 }
50
51 if (buf.st_gid != getegid()) {
52 tst_res(TFAIL, "mkdir() failed to set group ID "
53 "as the process's group ID");
54 return;
55 }
56
57 tst_res(TPASS, "mkdir() functionality is correct");
58
59 SAFE_RMDIR(TESTDIR);
60 }
61
setup(void)62 void setup(void)
63 {
64 struct passwd *pw;
65
66 pw = SAFE_GETPWNAM("nobody");
67
68 SAFE_SETUID(pw->pw_uid);
69 }
70
71 static struct tst_test test = {
72 .test_all = verify_mkdir,
73 .needs_tmpdir = 1,
74 .needs_root = 1,
75 .setup = setup,
76 };
77