1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/* Copyright (c) International Business Machines Corp., 2001
3f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/* DESCRIPTION
7f08c3bdfSopenharmony_ci * This test will verify the mkdir(2) creates a new directory successfully and
8f08c3bdfSopenharmony_ci * it is owned by the effective UID and GID of the process.
9f08c3bdfSopenharmony_ci */
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci#include <errno.h>
12f08c3bdfSopenharmony_ci#include <sys/stat.h>
13f08c3bdfSopenharmony_ci#include <sys/types.h>
14f08c3bdfSopenharmony_ci#include <unistd.h>
15f08c3bdfSopenharmony_ci#include <pwd.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define PERMS		0777
19f08c3bdfSopenharmony_ci#define TESTDIR		"testdir"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void verify_mkdir(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	struct stat buf;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	TEST(mkdir(TESTDIR, PERMS));
26f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
27f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "mkdir() Failed");
28f08c3bdfSopenharmony_ci		return;
29f08c3bdfSopenharmony_ci	}
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	SAFE_STAT(TESTDIR, &buf);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	if (buf.st_uid != geteuid()) {
34f08c3bdfSopenharmony_ci		tst_res(TFAIL, "mkdir() FAILED to set owner ID "
35f08c3bdfSopenharmony_ci			"as process's effective ID");
36f08c3bdfSopenharmony_ci		return;
37f08c3bdfSopenharmony_ci	}
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_ci	if (buf.st_gid != getegid()) {
40f08c3bdfSopenharmony_ci		tst_res(TFAIL, "mkdir() failed to set group ID "
41f08c3bdfSopenharmony_ci			"as the process's group ID");
42f08c3bdfSopenharmony_ci		return;
43f08c3bdfSopenharmony_ci	}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci	tst_res(TPASS, "mkdir() functionality is correct");
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	SAFE_RMDIR(TESTDIR);
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_civoid setup(void)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	struct passwd *pw;
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM("nobody");
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	SAFE_SETUID(pw->pw_uid);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct tst_test test = {
60f08c3bdfSopenharmony_ci	.test_all = verify_mkdir,
61f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
62f08c3bdfSopenharmony_ci	.needs_root = 1,
63f08c3bdfSopenharmony_ci	.setup = setup,
64f08c3bdfSopenharmony_ci};
65