1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Huawei Technologies Co., Ltd., 2015
4 * Copyright (C) 2022 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
5 */
6
7 #ifndef COMMON_H
8 #define COMMON_H
9
10 #include "tst_test.h"
11 #include "lapi/sched.h"
12
13 #define UID_MAP 0
14 #define GID_MAP 1
15
dummy_child(void * v)16 static int dummy_child(void *v)
17 {
18 (void)v;
19 return 0;
20 }
21
check_newuser(void)22 static inline void check_newuser(void)
23 {
24 int pid, status;
25
26 pid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, dummy_child, NULL);
27 if (pid == -1)
28 tst_brk(TCONF | TTERRNO, "CLONE_NEWUSER not supported");
29
30 SAFE_WAIT(&status);
31 }
32
updatemap(int cpid,int type,int idnum,int parentmappid)33 static inline void updatemap(int cpid, int type, int idnum, int parentmappid)
34 {
35 char path[BUFSIZ];
36 char content[BUFSIZ];
37 int fd;
38
39 switch(type) {
40 case UID_MAP:
41 sprintf(path, "/proc/%d/uid_map", cpid);
42 break;
43 case GID_MAP:
44 sprintf(path, "/proc/%d/gid_map", cpid);
45 break;
46 default:
47 tst_brk(TBROK, "invalid type parameter");
48 break;
49 }
50
51 sprintf(content, "%d %d 1", idnum, parentmappid);
52
53 fd = SAFE_OPEN(path, O_WRONLY, 0644);
54 SAFE_WRITE(SAFE_WRITE_ALL, fd, content, strlen(content));
55 SAFE_CLOSE(fd);
56 }
57
58 #endif
59