xref: /linux/tools/testing/selftests/filesystems/utils.h (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef __IDMAP_UTILS_H
4 #define __IDMAP_UTILS_H
5 
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 #include <errno.h>
10 #include <linux/types.h>
11 #include <sched.h>
12 #include <signal.h>
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <syscall.h>
18 #include <sys/capability.h>
19 #include <sys/fsuid.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 extern int get_userns_fd(unsigned long nsid, unsigned long hostid,
24 			 unsigned long range);
25 
26 extern int caps_down(void);
27 extern int cap_down(cap_value_t down);
28 
29 extern bool switch_ids(uid_t uid, gid_t gid);
30 extern int setup_userns(void);
31 
switch_userns(int fd,uid_t uid,gid_t gid,bool drop_caps)32 static inline bool switch_userns(int fd, uid_t uid, gid_t gid, bool drop_caps)
33 {
34 	if (setns(fd, CLONE_NEWUSER))
35 		return false;
36 
37 	if (!switch_ids(uid, gid))
38 		return false;
39 
40 	if (drop_caps && !caps_down())
41 		return false;
42 
43 	return true;
44 }
45 
46 extern uint64_t get_unique_mnt_id(const char *path);
47 
48 #endif /* __IDMAP_UTILS_H */
49