[LTP] [PATCH 2/4] syscalls: lchown01: Convert to new API

Andrea Cervesato andrea.cervesato@suse.com
Thu Jul 17 13:30:04 CEST 2025


Hi!

On 7/2/25 12:25 PM, Ricardo B. Marlière via ltp wrote:
> From: Ricardo B. Marlière <rbm@suse.com>
>
> Signed-off-by: Ricardo B. Marlière <rbm@suse.com>
> ---
>   testcases/kernel/syscalls/lchown/lchown01.c | 198 +++++++++-------------------
>   1 file changed, 59 insertions(+), 139 deletions(-)
>
> diff --git a/testcases/kernel/syscalls/lchown/lchown01.c b/testcases/kernel/syscalls/lchown/lchown01.c
> index aaa0ef4038a1aad23212ec2d188274ddd7fcf50d..992ff72949c11963e345928cb81efae5be93d128 100644
> --- a/testcases/kernel/syscalls/lchown/lchown01.c
> +++ b/testcases/kernel/syscalls/lchown/lchown01.c
> @@ -1,59 +1,25 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>   /*
>    * Copyright (c) International Business Machines  Corp., 2001
> - *
> - * This program is free software;  you can redistribute it and/or modify
> - * it under the terms of the GNU General Public License as published by
> - * the Free Software Foundation; either version 2 of the License, or
> - * (at your option) any later version.
> - *
> - * This program is distributed in the hope that it will be useful,
> - * but WITHOUT ANY WARRANTY;  without even the implied warranty of
> - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
> - * the GNU General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program;  if not, write to the Free Software
> - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> - */
> -
> -/*
> - * Test Description:
> - *  Verify that, lchown(2) succeeds to change the owner and group of a file
> - *  specified by path to any numeric owner(uid)/group(gid) values when invoked
> - *  by super-user.
> - *
> - * Expected Result:
> - *  lchown(2) should return 0 and the ownership set on the file should match
> - *  the numeric values contained in owner and group respectively.
> - *
> - * HISTORY
>    *	07/2001 Ported by Wayne Boyer
>    *	11/2010 Code cleanup by Cyril Hrubis chrubis@suse.cz
> + * Copyright (c) 2025 SUSE LLC Ricardo B. Marlière <rbm@suse.com>
>    */
>   
> -#include <stdio.h>
> -#include <sys/types.h>
> -#include <sys/stat.h>
> -#include <fcntl.h>
> -#include <errno.h>
> -#include <string.h>
> -#include <signal.h>
> -
> -#include "test.h"
> -#include "safe_macros.h"
> -
> -/*
> - * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
> - * rewriting all tests to the new API.
> +/*\
> + * Verify that, lchown(2) succeeds to change the owner and group of a file
> + * specified by path to any numeric owner(uid)/group(gid) values when invoked
> + * by super-user.
> + *
> + * lchown(2) should return 0 and the ownership set on the file should match
> + * the numeric values contained in owner and group respectively.
>    */
> -#include "compat_16.h"
>   
> -#define FILE_MODE	(S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
> -#define TESTFILE	"testfile"
> -#define SFILE		"slink_file"
> +#include "tst_test.h"
>   
> -TCID_DEFINE(lchown01);
> -int TST_TOTAL = 5;
> +#define FILE_MODE (S_IFREG | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

By running make check:

lchown01.c:20: WARNING: Symbolic permissions 'S_IRUSR | S_IWUSR | 
S_IRGRP | S_IROTH' are not preferred. Consider using octal permissions 
'0644'.
> +#define TESTFILE "testfile"
> +#define SFILE "slink_file"
>   
>   struct test_case_t {
>   	char *desc;
> @@ -62,114 +28,68 @@ struct test_case_t {
>   };
>   
>   static struct test_case_t test_cases[] = {
> -	{"Change Owner/Group ids", 700, 701},
> -	{"Change Owner id only", 702, -1},
> -	{"Change Owner/Group ids", 703, 701},
> -	{"Change Group id only", -1, 704},
> -	{"Change Group/Group ids", 703, 705},
> -	{"Change none", -1, -1},
> -	{NULL, 0, 0}
> +	{ "Change Owner/Group ids", 700, 701 },
> +	{ "Change Owner id only", 702, -1 },
> +	{ "Change Owner/Group ids", 703, 701 },
> +	{ "Change Group id only", -1, 704 },
> +	{ "Change Group/Group ids", 703, 705 },
> +	{ "Change none", -1, -1 },
>   };
we usually define test_cases array along with test_case_t struct.
>   
> -static void setup(void);
> -static void cleanup(void);
> -
> -int main(int argc, char *argv[])
> +static void run(unsigned int i)
>   {
>   	struct stat stat_buf;
> -	int lc;
> -	int i;
> -
> -	tst_parse_opts(argc, argv, NULL, NULL);
> -
> -	setup();
> +	struct test_case_t *tc = &test_cases[i];
>   
> -	for (lc = 0; TEST_LOOPING(lc); lc++) {
> +	uid_t user_id = tc->user_id;
> +	gid_t group_id = tc->group_id;
> +	char *test_desc = tc->desc;
Here we add a tst_res(TINFO, "%s", tc->desc), then read below.
>   
> -		tst_count = 0;
> +	/*
> +	 * Call lchown(2) with different user id and group id (numeric values)
> +	 * to set it on symlink of testfile.
> +	 */
Comment is not needed.
> +	SAFE_LCHOWN(SFILE, user_id, group_id);
>   
> -		for (i = 0; test_cases[i].desc != NULL; i++) {
> -			uid_t user_id = test_cases[i].user_id;
> -			gid_t group_id = test_cases[i].group_id;
> -			char *test_desc = test_cases[i].desc;
> +	SAFE_LSTAT(SFILE, &stat_buf);
>   
> -			/*
> -			 * Call lchown(2) with different user id and
> -			 * group id (numeric values) to set it on
> -			 * symlink of testfile.
> -			 */
> -			TEST(LCHOWN(cleanup, SFILE, user_id, group_id));
> -
> -			if (TEST_RETURN == -1) {
> -				tst_resm(TFAIL,
> -					 "lchown() Fails to %s, errno %d",
> -					 test_desc, TEST_ERRNO);
> -				continue;
> -			}
> -
> -			if (lstat(SFILE, &stat_buf) < 0) {
> -				tst_brkm(TFAIL, cleanup, "lstat(2) "
> -					 "%s failed, errno %d",
> -					 SFILE, TEST_ERRNO);
> -			}
> -
> -			if ((int)user_id == -1) {
> -				if (i > 0)
> -					user_id =
> -					    test_cases[i - 1].user_id;
> -				else
> -					user_id = geteuid();
> -			}
> -
> -			if ((int)group_id == -1) {
> -				if (i > 0)
> -					group_id =
> -					    test_cases[i - 1].group_id;
> -				else
> -					group_id = getegid();
> -			}
> +	if ((int)user_id == -1) {
> +		if (i > 0)
> +			user_id = test_cases[i - 1].user_id;
> +		else
> +			user_id = geteuid();
> +	}
>   
> -			/*
> -			 * Check for expected Ownership ids
> -			 * set on testfile.
> -			 */
> -			if ((stat_buf.st_uid != user_id) ||
> -			    (stat_buf.st_gid != group_id)) {
> -				tst_resm(TFAIL,
> -					 "%s: incorrect ownership set, "
> -					 "Expected %d %d", SFILE,
> -					 user_id, group_id);
> -			} else {
> -				tst_resm(TPASS, "lchown() succeeds to "
> -					 "%s of %s", test_desc, SFILE);
> -			}
> -		}
> +	if ((int)group_id == -1) {
> +		if (i > 0)
> +			group_id = test_cases[i - 1].group_id;
> +		else
> +			group_id = getegid();
>   	}
This method looks really weird to me, since we are running the test as root.
Instead of initializing user_id and group_id with getuid() and getgid(), 
we just need to initialize test cases with 0 instead of -1 and to get 
rid of these assignments.
>   
> -	cleanup();
> -	tst_exit();
> +	/* Check for expected ownership ids set on testfile. */
> +	if ((stat_buf.st_uid != user_id) || (stat_buf.st_gid != group_id)) {
> +		tst_res(TFAIL, "%s: incorrect ownership set, expected %d %d",
> +			SFILE, user_id, group_id);
> +	} else {
> +		tst_res(TPASS, "lchown() succeeds to %s of %s", test_desc,
> +			SFILE);
> +	}
     TST_EXP_EQ_LI(stat_buf.st_uid, user_id);
     TST_EXP_EQ_LI(stat_buf.st_gid, group_id);

>   }
>   
>   static void setup(void)
>   {
>   	int fd;
>   
> -	tst_sig(NOFORK, DEF_HANDLER, cleanup);
> -
> -	tst_require_root();
> -
> -	TEST_PAUSE;
> -	tst_tmpdir();
> -
> -	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
> -		tst_brkm(TBROK, cleanup, "open failed");
> -	}
> -	SAFE_CLOSE(cleanup, fd);
> -
> -	SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
> +	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
> +	SAFE_CLOSE(fd);
Here maybe we can use the more concise way:

     SAFE_TOUCH(TESTFILE, ...);

> +	SAFE_SYMLINK(TESTFILE, SFILE);
>   }
>   
> -static void cleanup(void)
> -{
> -	tst_rmdir();
> -}
> +static struct tst_test test = {
> +	.tcnt = ARRAY_SIZE(test_cases),
> +	.test = run,
> +	.setup = setup,
> +	.needs_tmpdir = 1,
> +	.needs_root = 1,
> +};
>
- Andrea


More information about the ltp mailing list