xref: /aosp_15_r20/external/ltp/testcases/kernel/containers/userns/common.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) Huawei Technologies Co., Ltd., 2015
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2022 SUSE LLC Andrea Cervesato <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #ifndef COMMON_H
8*49cdfc7eSAndroid Build Coastguard Worker #define COMMON_H
9*49cdfc7eSAndroid Build Coastguard Worker 
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #define UID_MAP 0
13*49cdfc7eSAndroid Build Coastguard Worker #define GID_MAP 1
14*49cdfc7eSAndroid Build Coastguard Worker 
updatemap(int cpid,int type,int idnum,int parentmappid)15*49cdfc7eSAndroid Build Coastguard Worker static inline void updatemap(int cpid, int type, int idnum, int parentmappid)
16*49cdfc7eSAndroid Build Coastguard Worker {
17*49cdfc7eSAndroid Build Coastguard Worker 	char path[BUFSIZ];
18*49cdfc7eSAndroid Build Coastguard Worker 	char content[BUFSIZ];
19*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker 	switch (type) {
22*49cdfc7eSAndroid Build Coastguard Worker 	case UID_MAP:
23*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(path, "/proc/%d/uid_map", cpid);
24*49cdfc7eSAndroid Build Coastguard Worker 		break;
25*49cdfc7eSAndroid Build Coastguard Worker 	case GID_MAP:
26*49cdfc7eSAndroid Build Coastguard Worker 		sprintf(path, "/proc/%d/gid_map", cpid);
27*49cdfc7eSAndroid Build Coastguard Worker 		break;
28*49cdfc7eSAndroid Build Coastguard Worker 	default:
29*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "invalid type parameter");
30*49cdfc7eSAndroid Build Coastguard Worker 		break;
31*49cdfc7eSAndroid Build Coastguard Worker 	}
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(content, "%d %d 1", idnum, parentmappid);
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN(path, O_WRONLY, 0644);
36*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, content, strlen(content));
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker #endif
41