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