1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci * Ported to LTP: Wayne Boyer
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Testcase to check creat(2) fails with EACCES
9f08c3bdfSopenharmony_ci */
10f08c3bdfSopenharmony_ci
11f08c3bdfSopenharmony_ci#include <stdio.h>
12f08c3bdfSopenharmony_ci#include <errno.h>
13f08c3bdfSopenharmony_ci#include <fcntl.h>
14f08c3bdfSopenharmony_ci#include <pwd.h>
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <sys/stat.h>
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "tst_test.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define DIRNAME "testdir"
21f08c3bdfSopenharmony_ci#define FILENAME DIRNAME"/file1"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic uid_t nobody_uid;
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic struct tcase {
26f08c3bdfSopenharmony_ci	const char *fname;
27f08c3bdfSopenharmony_ci} tcases[] = {
28f08c3bdfSopenharmony_ci	{DIRNAME "/file"},
29f08c3bdfSopenharmony_ci	{FILENAME}
30f08c3bdfSopenharmony_ci};
31f08c3bdfSopenharmony_ci
32f08c3bdfSopenharmony_cistatic void child_fn(unsigned int i)
33f08c3bdfSopenharmony_ci{
34f08c3bdfSopenharmony_ci	SAFE_SETEUID(nobody_uid);
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci	TEST(creat(tcases[i].fname, 0444));
37f08c3bdfSopenharmony_ci
38f08c3bdfSopenharmony_ci	if (TST_RET != -1) {
39f08c3bdfSopenharmony_ci		SAFE_UNLINK(tcases[i].fname);
40f08c3bdfSopenharmony_ci		tst_res(TFAIL, "call succeeded unexpectedly");
41f08c3bdfSopenharmony_ci		return;
42f08c3bdfSopenharmony_ci	}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	if (TST_ERR != EACCES) {
45f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "Expected EACCES");
46f08c3bdfSopenharmony_ci		return;
47f08c3bdfSopenharmony_ci	}
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_ci	tst_res(TPASS, "call failed with EACCES as expected");
50f08c3bdfSopenharmony_ci}
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic void verify_creat(unsigned int i)
53f08c3bdfSopenharmony_ci{
54f08c3bdfSopenharmony_ci	if (SAFE_FORK() == 0)
55f08c3bdfSopenharmony_ci		child_fn(i);
56f08c3bdfSopenharmony_ci}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_cistatic void setup(void)
59f08c3bdfSopenharmony_ci{
60f08c3bdfSopenharmony_ci	struct passwd *pw;
61f08c3bdfSopenharmony_ci	int fd;
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ci	pw = SAFE_GETPWNAM("nobody");
64f08c3bdfSopenharmony_ci	nobody_uid = pw->pw_uid;
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	SAFE_MKDIR(DIRNAME, 0700);
67f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, 0444);
68f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
69f08c3bdfSopenharmony_ci}
70f08c3bdfSopenharmony_ci
71f08c3bdfSopenharmony_cistatic struct tst_test test = {
72f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
73f08c3bdfSopenharmony_ci	.test = verify_creat,
74f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
75f08c3bdfSopenharmony_ci	.needs_root = 1,
76f08c3bdfSopenharmony_ci	.forks_child = 1,
77f08c3bdfSopenharmony_ci	.setup = setup,
78f08c3bdfSopenharmony_ci};
79