• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Description:
20  * The testcase checks the various errnos of the unlink(2).
21  * 1) unlink() returns EACCES when deleting file in unwritable directory
22  *    as an unprivileged user.
23  * 2) unlink() returns EACCES when deleting file in "unsearchable directory
24  *    as an unprivileged user.
25  * 3) unlink() returns EISDIR when deleting directory for root
26  * 4) unlink() returns EISDIR when deleting directory for regular user
27  */
28 
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <pwd.h>
33 #include "tst_test.h"
34 
35 static struct passwd *pw;
36 
37 static struct test_case_t {
38 	char *name;
39 	char *desc;
40 	int exp_errno;
41 	int exp_user;
42 } tcases[] = {
43 	{"unwrite_dir/file", "unwritable directory", EACCES, 1},
44 	{"unsearch_dir/file", "unsearchable directory", EACCES, 1},
45 	{"regdir", "directory", EISDIR, 0},
46 	{"regdir", "directory", EISDIR, 1},
47 };
48 
verify_unlink(struct test_case_t * tc)49 static void verify_unlink(struct test_case_t *tc)
50 {
51 	TEST(unlink(tc->name));
52 	if (TST_RET != -1) {
53 		tst_res(TFAIL, "unlink(<%s>) succeeded unexpectedly",
54 			tc->desc);
55 		return;
56 	}
57 
58 	if (TST_ERR == tc->exp_errno) {
59 		tst_res(TPASS | TTERRNO, "unlink(<%s>) failed as expected",
60 			tc->desc);
61 	} else {
62 		tst_res(TFAIL | TTERRNO,
63 			"unlink(<%s>) failed, expected errno: %s",
64 			tc->desc, tst_strerrno(tc->exp_errno));
65 	}
66 }
67 
do_unlink(unsigned int n)68 static void do_unlink(unsigned int n)
69 {
70 	struct test_case_t *cases = &tcases[n];
71 	pid_t pid;
72 
73 	if (cases->exp_user) {
74 		pid = SAFE_FORK();
75 		if (!pid) {
76 			SAFE_SETUID(pw->pw_uid);
77 			verify_unlink(cases);
78 			exit(0);
79 		}
80 
81 		SAFE_WAITPID(pid, NULL, 0);
82 	} else {
83 		verify_unlink(cases);
84 	}
85 }
86 
setup(void)87 static void setup(void)
88 {
89 	SAFE_MKDIR("unwrite_dir", 0777);
90 	SAFE_TOUCH("unwrite_dir/file", 0777, NULL);
91 	SAFE_CHMOD("unwrite_dir", 0555);
92 
93 	SAFE_MKDIR("unsearch_dir", 0777);
94 	SAFE_TOUCH("unsearch_dir/file", 0777, NULL);
95 	SAFE_CHMOD("unsearch_dir", 0666);
96 
97 	SAFE_MKDIR("regdir", 0777);
98 
99 	pw = SAFE_GETPWNAM("nobody");
100 }
101 
102 static struct tst_test test = {
103 	.needs_root = 1,
104 	.forks_child = 1,
105 	.needs_tmpdir = 1,
106 	.setup = setup,
107 	.tcnt = ARRAY_SIZE(tcases),
108 	.test = do_unlink,
109 };
110