1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
4f08c3bdfSopenharmony_ci * Author: Nirmala Devi Dhanasekar <nirmala.devi@wipro.com>
5f08c3bdfSopenharmony_ci *
6f08c3bdfSopenharmony_ci * Verify that umount(2) returns -1 and sets errno to  EPERM if the user
7f08c3bdfSopenharmony_ci * is not the super-user.
8f08c3bdfSopenharmony_ci */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include <errno.h>
11f08c3bdfSopenharmony_ci#include <pwd.h>
12f08c3bdfSopenharmony_ci#include <sys/mount.h>
13f08c3bdfSopenharmony_ci#include <sys/types.h>
14f08c3bdfSopenharmony_ci#include <unistd.h>
15f08c3bdfSopenharmony_ci#include "tst_test.h"
16f08c3bdfSopenharmony_ci
17f08c3bdfSopenharmony_ci#define MNTPOINT	"mntpoint"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_cistatic int mount_flag;
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic void verify_umount(void)
22f08c3bdfSopenharmony_ci{
23f08c3bdfSopenharmony_ci	TEST(umount(MNTPOINT));
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
26f08c3bdfSopenharmony_ci		tst_res(TFAIL, "umount() succeeds unexpectedly");
27f08c3bdfSopenharmony_ci		return;
28f08c3bdfSopenharmony_ci	}
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	if (TST_ERR != EPERM) {
31f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "umount() should fail with EPERM");
32f08c3bdfSopenharmony_ci		return;
33f08c3bdfSopenharmony_ci	}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	tst_res(TPASS | TTERRNO, "umount() fails as expected");
36f08c3bdfSopenharmony_ci}
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_cistatic void setup(void)
39f08c3bdfSopenharmony_ci{
40f08c3bdfSopenharmony_ci	struct passwd *pw;
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci	SAFE_MKDIR(MNTPOINT, 0775);
43f08c3bdfSopenharmony_ci	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, NULL);
44f08c3bdfSopenharmony_ci	mount_flag = 1;
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM("nobody");
47f08c3bdfSopenharmony_ci	SAFE_SETEUID(pw->pw_uid);
48f08c3bdfSopenharmony_ci}
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cistatic void cleanup(void)
51f08c3bdfSopenharmony_ci{
52f08c3bdfSopenharmony_ci	if (seteuid(0))
53f08c3bdfSopenharmony_ci		tst_res(TWARN | TERRNO, "seteuid(0) Failed");
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	if (mount_flag)
56f08c3bdfSopenharmony_ci		tst_umount(MNTPOINT);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct tst_test test = {
60f08c3bdfSopenharmony_ci	.needs_root = 1,
61f08c3bdfSopenharmony_ci	.format_device = 1,
62f08c3bdfSopenharmony_ci	.setup = setup,
63f08c3bdfSopenharmony_ci	.cleanup = cleanup,
64f08c3bdfSopenharmony_ci	.test_all = verify_umount,
65f08c3bdfSopenharmony_ci};
66