1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2006-2021
4f08c3bdfSopenharmony_ci * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
5f08c3bdfSopenharmony_ci * Author: William Roske
6f08c3bdfSopenharmony_ci * Ported to LTP: Dave Fenner
7f08c3bdfSopenharmony_ci */
8f08c3bdfSopenharmony_ci
9f08c3bdfSopenharmony_ci/*\
10f08c3bdfSopenharmony_ci * [Description]
11f08c3bdfSopenharmony_ci *
12f08c3bdfSopenharmony_ci * Basic test for fchown(). Call fchown() on a fd and expect it to pass.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <fcntl.h>
16f08c3bdfSopenharmony_ci#include <sys/stat.h>
17f08c3bdfSopenharmony_ci#include <sys/types.h>
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci#include "compat_tst_16.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#define FILENAME "fchown01_testfile"
23f08c3bdfSopenharmony_ci#define MODE 0700
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_cistatic int fd;
26f08c3bdfSopenharmony_cistatic uid_t uid;
27f08c3bdfSopenharmony_cistatic gid_t gid;
28f08c3bdfSopenharmony_ci
29f08c3bdfSopenharmony_cistatic void run(void)
30f08c3bdfSopenharmony_ci{
31f08c3bdfSopenharmony_ci	TST_EXP_PASS(FCHOWN(fd, uid, gid),
32f08c3bdfSopenharmony_ci		"fchown(%i, %i, %i)", fd, uid, gid);
33f08c3bdfSopenharmony_ci}
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void setup(void)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	UID16_CHECK(uid = geteuid(), "fchown");
38f08c3bdfSopenharmony_ci	GID16_CHECK(gid = getegid(), "fchown");
39f08c3bdfSopenharmony_ci	fd = SAFE_OPEN(FILENAME, O_RDWR | O_CREAT, MODE);
40f08c3bdfSopenharmony_ci}
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void cleanup(void)
43f08c3bdfSopenharmony_ci{
44f08c3bdfSopenharmony_ci	if (fd > 0)
45f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic struct tst_test test = {
49f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
50f08c3bdfSopenharmony_ci	.setup = setup,
51f08c3bdfSopenharmony_ci	.cleanup = cleanup,
52f08c3bdfSopenharmony_ci	.test_all = run,
53f08c3bdfSopenharmony_ci};
54