1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* Copyright (c) International Business Machines Corp., 2001 3f08c3bdfSopenharmony_ci * 07/2001 John George 4f08c3bdfSopenharmony_ci * -Ported 5f08c3bdfSopenharmony_ci * Copyright (c) Linux Test Project, 2002-2022 6f08c3bdfSopenharmony_ci */ 7f08c3bdfSopenharmony_ci 8f08c3bdfSopenharmony_ci/*\ 9f08c3bdfSopenharmony_ci * [Description] 10f08c3bdfSopenharmony_ci * 11f08c3bdfSopenharmony_ci * Verify that, stat(2) succeeds to get the status of a file and fills the 12f08c3bdfSopenharmony_ci * stat structure elements regardless of whether process has or doesn't 13f08c3bdfSopenharmony_ci * have read access to the file. 14f08c3bdfSopenharmony_ci */ 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include <pwd.h> 17f08c3bdfSopenharmony_ci#include "tst_test.h" 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci#define FILE_SIZE 1024 20f08c3bdfSopenharmony_ci#define TST_FILEREAD "test_fileread" 21f08c3bdfSopenharmony_ci#define TST_FILENOREAD "test_filenoread" 22f08c3bdfSopenharmony_ci#define READ_MODE 0666 23f08c3bdfSopenharmony_ci#define NEW_MODE 0222 24f08c3bdfSopenharmony_ci#define MASK 0777 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_cistatic uid_t user_id; 27f08c3bdfSopenharmony_cistatic gid_t group_id; 28f08c3bdfSopenharmony_cistatic struct passwd *ltpuser; 29f08c3bdfSopenharmony_ci 30f08c3bdfSopenharmony_cistatic struct tcase{ 31f08c3bdfSopenharmony_ci char *pathname; 32f08c3bdfSopenharmony_ci unsigned int mode; 33f08c3bdfSopenharmony_ci} TC[] = { 34f08c3bdfSopenharmony_ci {TST_FILEREAD, READ_MODE}, 35f08c3bdfSopenharmony_ci {TST_FILENOREAD, NEW_MODE} 36f08c3bdfSopenharmony_ci}; 37f08c3bdfSopenharmony_ci 38f08c3bdfSopenharmony_cistatic void verify_stat(unsigned int n) 39f08c3bdfSopenharmony_ci{ 40f08c3bdfSopenharmony_ci struct tcase *tc = TC + n; 41f08c3bdfSopenharmony_ci struct stat stat_buf; 42f08c3bdfSopenharmony_ci 43f08c3bdfSopenharmony_ci TST_EXP_PASS(stat(tc->pathname, &stat_buf)); 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci TST_EXP_EQ_LU(stat_buf.st_uid, user_id); 46f08c3bdfSopenharmony_ci TST_EXP_EQ_LU(stat_buf.st_gid, group_id); 47f08c3bdfSopenharmony_ci TST_EXP_EQ_LI(stat_buf.st_size, FILE_SIZE); 48f08c3bdfSopenharmony_ci TST_EXP_EQ_LU(stat_buf.st_mode & MASK, tc->mode); 49f08c3bdfSopenharmony_ci TST_EXP_EQ_LU(stat_buf.st_nlink, 1); 50f08c3bdfSopenharmony_ci} 51f08c3bdfSopenharmony_ci 52f08c3bdfSopenharmony_cistatic void setup(void) 53f08c3bdfSopenharmony_ci{ 54f08c3bdfSopenharmony_ci unsigned int i; 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci ltpuser = SAFE_GETPWNAM("nobody"); 57f08c3bdfSopenharmony_ci SAFE_SETUID(ltpuser->pw_uid); 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci for (i = 0; i < ARRAY_SIZE(TC); i++) { 60f08c3bdfSopenharmony_ci if (tst_fill_file(TC[i].pathname, 'a', 256, 4)) 61f08c3bdfSopenharmony_ci tst_brk(TBROK, "Failed to create tst file %s", 62f08c3bdfSopenharmony_ci TC[i].pathname); 63f08c3bdfSopenharmony_ci SAFE_CHMOD(TC[i].pathname, TC[i].mode); 64f08c3bdfSopenharmony_ci } 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci user_id = getuid(); 67f08c3bdfSopenharmony_ci group_id = getgid(); 68f08c3bdfSopenharmony_ci} 69f08c3bdfSopenharmony_ci 70f08c3bdfSopenharmony_cistatic struct tst_test test = { 71f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(TC), 72f08c3bdfSopenharmony_ci .setup = setup, 73f08c3bdfSopenharmony_ci .test = verify_stat, 74f08c3bdfSopenharmony_ci .needs_root = 1, 75f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 76f08c3bdfSopenharmony_ci}; 77