1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci
3f08c3bdfSopenharmony_ci/*
4f08c3bdfSopenharmony_ci *  Copyright (c) International Business Machines  Corp., 2002
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/* 12/03/2002	Port to LTP     robbiew@us.ibm.com */
8f08c3bdfSopenharmony_ci/* 06/30/2001	Port to Linux	nsharoff@us.ibm.com */
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci/*\
11f08c3bdfSopenharmony_ci * [DOCUMENTATION]
12f08c3bdfSopenharmony_ci * Verify that acct() returns proper errno on failure.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <sys/types.h>
16f08c3bdfSopenharmony_ci#include <sys/stat.h>
17f08c3bdfSopenharmony_ci#include <errno.h>
18f08c3bdfSopenharmony_ci#include <fcntl.h>
19f08c3bdfSopenharmony_ci#include <pwd.h>
20f08c3bdfSopenharmony_ci#include <stdio.h>
21f08c3bdfSopenharmony_ci#include <stdlib.h>
22f08c3bdfSopenharmony_ci#include <unistd.h>
23f08c3bdfSopenharmony_ci#include <sys/mount.h>
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#include "tst_test.h"
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_ci#define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
28f08c3bdfSopenharmony_ci			 S_IXGRP|S_IROTH|S_IXOTH)
29f08c3bdfSopenharmony_ci#define FILE_EISDIR		"."
30f08c3bdfSopenharmony_ci#define FILE_EACCESS		"/dev/null"
31f08c3bdfSopenharmony_ci#define FILE_ENOENT		"/tmp/does/not/exist"
32f08c3bdfSopenharmony_ci#define FILE_ENOTDIR		"./tmpfile/"
33f08c3bdfSopenharmony_ci#define FILE_TMPFILE		"./tmpfile"
34f08c3bdfSopenharmony_ci#define FILE_ELOOP		"test_file_eloop1"
35f08c3bdfSopenharmony_ci#define FILE_EROFS		"ro_mntpoint/file"
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_cistatic struct passwd *ltpuser;
38f08c3bdfSopenharmony_ci
39f08c3bdfSopenharmony_cistatic char *file_eisdir;
40f08c3bdfSopenharmony_cistatic char *file_eaccess;
41f08c3bdfSopenharmony_cistatic char *file_enoent;
42f08c3bdfSopenharmony_cistatic char *file_enotdir;
43f08c3bdfSopenharmony_cistatic char *file_tmpfile;
44f08c3bdfSopenharmony_cistatic char *file_eloop;
45f08c3bdfSopenharmony_cistatic char *file_enametoolong;
46f08c3bdfSopenharmony_cistatic char *file_erofs;
47f08c3bdfSopenharmony_cistatic char *file_null;
48f08c3bdfSopenharmony_ci
49f08c3bdfSopenharmony_cistatic void setup_euid(void)
50f08c3bdfSopenharmony_ci{
51f08c3bdfSopenharmony_ci	SAFE_SETEUID(ltpuser->pw_uid);
52f08c3bdfSopenharmony_ci}
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_cistatic void cleanup_euid(void)
55f08c3bdfSopenharmony_ci{
56f08c3bdfSopenharmony_ci	SAFE_SETEUID(0);
57f08c3bdfSopenharmony_ci}
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_cistatic struct test_case {
60f08c3bdfSopenharmony_ci	char **filename;
61f08c3bdfSopenharmony_ci	char *desc;
62f08c3bdfSopenharmony_ci	int exp_errno;
63f08c3bdfSopenharmony_ci	void (*setupfunc) ();
64f08c3bdfSopenharmony_ci	void (*cleanfunc) ();
65f08c3bdfSopenharmony_ci} tcases[] = {
66f08c3bdfSopenharmony_ci	{&file_eisdir,  FILE_EISDIR,  EISDIR,  NULL,   NULL},
67f08c3bdfSopenharmony_ci	{&file_eaccess, FILE_EACCESS, EACCES,  NULL,   NULL},
68f08c3bdfSopenharmony_ci	{&file_enoent,  FILE_ENOENT,  ENOENT,  NULL,   NULL},
69f08c3bdfSopenharmony_ci	{&file_enotdir, FILE_ENOTDIR, ENOTDIR, NULL,   NULL},
70f08c3bdfSopenharmony_ci	{&file_tmpfile, FILE_TMPFILE, EPERM,   setup_euid, cleanup_euid},
71f08c3bdfSopenharmony_ci	{&file_null,    "NULL",       EPERM,   setup_euid, cleanup_euid},
72f08c3bdfSopenharmony_ci	{&file_eloop,   FILE_ELOOP,   ELOOP,        NULL, NULL},
73f08c3bdfSopenharmony_ci	{&file_enametoolong, "aaaa...", ENAMETOOLONG, NULL, NULL},
74f08c3bdfSopenharmony_ci	{&file_erofs,   FILE_EROFS,   EROFS,        NULL, NULL},
75f08c3bdfSopenharmony_ci};
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_cistatic void setup(void)
78f08c3bdfSopenharmony_ci{
79f08c3bdfSopenharmony_ci	int fd;
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ci	TEST(acct(NULL));
82f08c3bdfSopenharmony_ci	if (TST_RET == -1 && TST_ERR == ENOSYS)
83f08c3bdfSopenharmony_ci		tst_brk(TCONF, "acct() system call isn't configured in kernel");
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	ltpuser = SAFE_GETPWNAM("nobody");
86f08c3bdfSopenharmony_ci
87f08c3bdfSopenharmony_ci	fd = SAFE_CREAT(FILE_TMPFILE, 0777);
88f08c3bdfSopenharmony_ci	SAFE_CLOSE(fd);
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	TEST(acct(FILE_TMPFILE));
91f08c3bdfSopenharmony_ci	if (TST_RET == -1)
92f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "acct failed unexpectedly");
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_ci	/* turn off acct, so we are in a known state */
95f08c3bdfSopenharmony_ci	TEST(acct(NULL));
96f08c3bdfSopenharmony_ci	if (TST_RET == -1)
97f08c3bdfSopenharmony_ci		tst_brk(TBROK | TTERRNO, "acct(NULL) failed");
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	/* ELOOP SETTING */
100f08c3bdfSopenharmony_ci	SAFE_SYMLINK(FILE_ELOOP, "test_file_eloop2");
101f08c3bdfSopenharmony_ci	SAFE_SYMLINK("test_file_eloop2", FILE_ELOOP);
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	memset(file_enametoolong, 'a', PATH_MAX+1);
104f08c3bdfSopenharmony_ci	file_enametoolong[PATH_MAX+1] = 0;
105f08c3bdfSopenharmony_ci}
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_cistatic void verify_acct(unsigned int nr)
108f08c3bdfSopenharmony_ci{
109f08c3bdfSopenharmony_ci	struct test_case *tcase = &tcases[nr];
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci	if (tcase->setupfunc)
112f08c3bdfSopenharmony_ci		tcase->setupfunc();
113f08c3bdfSopenharmony_ci
114f08c3bdfSopenharmony_ci	TST_EXP_FAIL(acct(*tcase->filename), tcase->exp_errno,
115f08c3bdfSopenharmony_ci	             "acct(%s)", tcase->desc);
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci	if (tcase->cleanfunc)
118f08c3bdfSopenharmony_ci		tcase->cleanfunc();
119f08c3bdfSopenharmony_ci}
120f08c3bdfSopenharmony_ci
121f08c3bdfSopenharmony_cistatic struct tst_test test = {
122f08c3bdfSopenharmony_ci	.needs_root = 1,
123f08c3bdfSopenharmony_ci	.mntpoint = "ro_mntpoint",
124f08c3bdfSopenharmony_ci	.needs_rofs = 1,
125f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
126f08c3bdfSopenharmony_ci	.setup = setup,
127f08c3bdfSopenharmony_ci	.test = verify_acct,
128f08c3bdfSopenharmony_ci	.bufs = (struct tst_buffers []) {
129f08c3bdfSopenharmony_ci		{&file_eisdir, .str = FILE_EISDIR},
130f08c3bdfSopenharmony_ci		{&file_eaccess, .str = FILE_EACCESS},
131f08c3bdfSopenharmony_ci		{&file_enoent, .str = FILE_ENOENT},
132f08c3bdfSopenharmony_ci		{&file_enotdir, .str = FILE_ENOTDIR},
133f08c3bdfSopenharmony_ci		{&file_tmpfile, .str = FILE_TMPFILE},
134f08c3bdfSopenharmony_ci		{&file_eloop, .str = FILE_ELOOP},
135f08c3bdfSopenharmony_ci		{&file_enametoolong, .size = PATH_MAX+2},
136f08c3bdfSopenharmony_ci		{&file_erofs, .str = FILE_EROFS},
137f08c3bdfSopenharmony_ci		{}
138f08c3bdfSopenharmony_ci	}
139f08c3bdfSopenharmony_ci};
140