xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fchmodat/fchmodat01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2006
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Linux Test Project, 2003-2023
5*49cdfc7eSAndroid Build Coastguard Worker  * 08/28/2006 AUTHOR: Yi Yang <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Check the basic functionality of the fchmodat() system call.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * - fchmodat() passes if dir_fd is file descriptor to the directory
14*49cdfc7eSAndroid Build Coastguard Worker  *   where the file is located and pathname is relative path of the file.
15*49cdfc7eSAndroid Build Coastguard Worker  * - fchmodat() passes if pathname is absolute, then dirfd is ignored.
16*49cdfc7eSAndroid Build Coastguard Worker  * - fchmodat() passes if dir_fd is AT_FDCWD and pathname is interpreted
17*49cdfc7eSAndroid Build Coastguard Worker  *   relative to the current working directory of the calling process.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define TESTDIR         "fchmodatdir"
25*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE        "fchmodatfile"
26*49cdfc7eSAndroid Build Coastguard Worker #define FILEPATH        "fchmodatdir/fchmodatfile"
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static int dir_fd, file_fd;
29*49cdfc7eSAndroid Build Coastguard Worker static int atcwd_fd = AT_FDCWD;
30*49cdfc7eSAndroid Build Coastguard Worker static char *abs_path;
31*49cdfc7eSAndroid Build Coastguard Worker static char *test_file;
32*49cdfc7eSAndroid Build Coastguard Worker static char *file_path;
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
35*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
36*49cdfc7eSAndroid Build Coastguard Worker 	char **filenames;
37*49cdfc7eSAndroid Build Coastguard Worker 	char **full_path;
38*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
39*49cdfc7eSAndroid Build Coastguard Worker 	{&dir_fd, &test_file, &file_path},
40*49cdfc7eSAndroid Build Coastguard Worker 	{&file_fd, &abs_path, &abs_path},
41*49cdfc7eSAndroid Build Coastguard Worker 	{&atcwd_fd, &file_path, &file_path},
42*49cdfc7eSAndroid Build Coastguard Worker };
43*49cdfc7eSAndroid Build Coastguard Worker 
verify_fchmodat(unsigned int i)44*49cdfc7eSAndroid Build Coastguard Worker static void verify_fchmodat(unsigned int i)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[i];
47*49cdfc7eSAndroid Build Coastguard Worker 	struct stat st;
48*49cdfc7eSAndroid Build Coastguard Worker 
49*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(fchmodat(*tc->fd, *tc->filenames, 0600, 0),
50*49cdfc7eSAndroid Build Coastguard Worker 		     "fchmodat(%d, %s, 0600, 0)",
51*49cdfc7eSAndroid Build Coastguard Worker 		     *tc->fd, *tc->filenames);
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSTAT(*tc->full_path, &st);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	if ((st.st_mode & ~S_IFREG) == 0600)
56*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "File permission changed correctly");
57*49cdfc7eSAndroid Build Coastguard Worker 	else
58*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "File permission not changed correctly");
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)61*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker 	char *tmpdir_path = tst_get_tmpdir();
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker 	abs_path = tst_aprintf("%s/%s", tmpdir_path, FILEPATH);
66*49cdfc7eSAndroid Build Coastguard Worker 	free(tmpdir_path);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TESTDIR, 0700);
69*49cdfc7eSAndroid Build Coastguard Worker 	dir_fd = SAFE_OPEN(TESTDIR, O_DIRECTORY);
70*49cdfc7eSAndroid Build Coastguard Worker 	file_fd = SAFE_OPEN(FILEPATH, O_CREAT | O_RDWR, 0600);
71*49cdfc7eSAndroid Build Coastguard Worker }
72*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)73*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
74*49cdfc7eSAndroid Build Coastguard Worker {
75*49cdfc7eSAndroid Build Coastguard Worker 	if (dir_fd > -1)
76*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(dir_fd);
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	if (file_fd > -1)
79*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(file_fd);
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
83*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
84*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_fchmodat,
85*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
86*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
87*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers []) {
88*49cdfc7eSAndroid Build Coastguard Worker 		{&test_file, .str = TESTFILE},
89*49cdfc7eSAndroid Build Coastguard Worker 		{&file_path, .str = FILEPATH},
90*49cdfc7eSAndroid Build Coastguard Worker 		{},
91*49cdfc7eSAndroid Build Coastguard Worker 	},
92*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
93*49cdfc7eSAndroid Build Coastguard Worker };
94