• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines  Corp., 2001
4  *	07/2001 John George
5  * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
6  *
7  * Check the return value, and errno of vhangup(2) when a non-root user calls
8  * vhangup().
9  */
10 #include <unistd.h>
11 #include <errno.h>
12 #include <pwd.h>
13 #include <sys/wait.h>
14 
15 #include "tst_test.h"
16 #include "lapi/syscalls.h"
17 
18 static uid_t nobody_uid;
19 
run(void)20 static void run(void)
21 {
22 	pid_t pid;
23 	int retval;
24 
25 	pid = SAFE_FORK();
26 	if (pid > 0) {
27 		waitpid(pid, NULL, 0);
28 	} else {
29 		retval = setreuid(nobody_uid, nobody_uid);
30 		if (retval < 0)
31 			tst_brk(TBROK | TTERRNO, "setreuid failed");
32 		TEST(tst_syscall(__NR_vhangup));
33 		if (TST_RET != -1)
34 			tst_brk(TFAIL, "vhangup() failed to fail");
35 		else if (TST_ERR == EPERM)
36 			tst_res(TPASS, "Got EPERM as expected.");
37 		else
38 			tst_res(TFAIL, "expected EPERM got %d", TST_ERR);
39 	}
40 }
41 
setup(void)42 static void setup(void)
43 {
44 	struct passwd *pw;
45 
46 	pw = SAFE_GETPWNAM("nobody");
47 	nobody_uid = pw->pw_uid;
48 }
49 
50 static struct tst_test test = {
51 	.test_all = run,
52 	.setup = setup,
53 	.needs_root = 1,
54 	.forks_child = 1,
55 };
56