1 /*
2 * Copyright (c) International Business Machines Corp., 2001
3 * 07/2001 John George
4 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 /*
21 * Check the return value, and errno of vhangup(2) when a non-root user calls
22 * vhangup().
23 */
24 #include <unistd.h>
25 #include <errno.h>
26 #include <pwd.h>
27 #include <sys/wait.h>
28
29 #include "test.h"
30 #include "safe_macros.h"
31
32 static void setup(void);
33
34 char *TCID = "vhangup01";
35 int TST_TOTAL = 1;
36
37 static uid_t nobody_uid;
38
main(int argc,char ** argv)39 int main(int argc, char **argv)
40 {
41 int lc;
42
43 pid_t pid;
44 int retval;
45
46 tst_parse_opts(argc, argv, NULL, NULL);
47
48 setup();
49
50 for (lc = 0; TEST_LOOPING(lc); lc++) {
51 tst_count = 0;
52
53 if ((pid = FORK_OR_VFORK()) < 0) {
54 tst_brkm(TFAIL, NULL, "fork failed");
55 } else if (pid > 0) {
56 tst_record_childstatus(NULL, pid);
57 } else {
58 retval = setreuid(nobody_uid, nobody_uid);
59 if (retval < 0) {
60 perror("setreuid");
61 tst_brkm(TFAIL, NULL, "setreuid failed");
62 }
63 TEST(vhangup());
64 if (TEST_RETURN != -1) {
65 tst_brkm(TFAIL, NULL, "vhangup() failed to "
66 "fail");
67 } else if (TEST_ERRNO == EPERM) {
68 tst_resm(TPASS, "Got EPERM as expected.");
69 } else {
70 tst_resm(TFAIL, "expected EPERM got %d",
71 TEST_ERRNO);
72 }
73 }
74 }
75
76 tst_exit();
77 }
78
setup(void)79 static void setup(void)
80 {
81 struct passwd *pw;
82
83 tst_require_root();
84
85 pw = SAFE_GETPWNAM(NULL, "nobody");
86 nobody_uid = pw->pw_uid;
87
88 TEST_PAUSE;
89 }
90