1/*
2 * Copyright (c) Huawei Technologies Co., Ltd., 2015
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 * the GNU General Public License for more details.
12 */
13
14#include "../libclone/libclone.h"
15#include "test.h"
16#include "safe_macros.h"
17#include <stdbool.h>
18
19#define UID_MAP 0
20#define GID_MAP 1
21
22static int dummy_child(void *v)
23{
24	(void) v;
25	return 0;
26}
27
28static int check_newuser(void)
29{
30	int pid, status;
31
32	if (tst_kvercmp(3, 8, 0) < 0)
33		tst_brkm(TCONF, NULL, "CLONE_NEWUSER not supported");
34
35	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWUSER, dummy_child, NULL);
36	if (pid == -1)
37		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWUSER not supported");
38	SAFE_WAIT(NULL, &status);
39
40	return 0;
41}
42
43LTP_ATTRIBUTE_UNUSED static int updatemap(int cpid, bool type, int idnum,
44	int parentmappid, void (*cleanup)(void))
45{
46	char path[BUFSIZ];
47	char content[BUFSIZ];
48	int fd;
49
50	if (type == UID_MAP)
51		sprintf(path, "/proc/%d/uid_map", cpid);
52	else if (type == GID_MAP)
53		sprintf(path, "/proc/%d/gid_map", cpid);
54	else
55		tst_brkm(TBROK, cleanup, "invalid type parameter");
56
57	sprintf(content, "%d %d 1", idnum, parentmappid);
58	fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
59	SAFE_WRITE(cleanup, 1, fd, content, strlen(content));
60	SAFE_CLOSE(cleanup, fd);
61	return 0;
62}
63