blob: ebaac4313c461e19d23f301e994f74c35cf2cbbf [file] [log] [blame]
subrata_modakc2aab6c2007-12-28 09:48:30 +00001/*
2* Copyright (c) International Business Machines Corp., 2007
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* You should have received a copy of the GNU General Public License
13* along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080014* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
subrata_modakc2aab6c2007-12-28 09:48:30 +000015*
16***************************************************************************/
subrata_modak0c5f9702007-04-26 11:02:47 +000017#include "libclone.h"
18
subrata_modakce23f152009-01-19 09:23:02 +000019int do_clone_tests(unsigned long clone_flags,
Wanlong Gao354ebb42012-12-07 10:10:04 +080020 int (*fn1) (void *arg), void *arg1,
21 int (*fn2) (void *arg), void *arg2)
subrata_modakce23f152009-01-19 09:23:02 +000022{
23 int ret;
24
vapierf6d7f092009-11-03 20:07:35 +000025 ret = ltp_clone_quick(clone_flags | SIGCHLD, fn1, arg1);
subrata_modakce23f152009-01-19 09:23:02 +000026
27 if (ret == -1) {
subrata_modak0c5f9702007-04-26 11:02:47 +000028 return -1;
29 }
30 if (fn2)
31 ret = fn2(arg2);
32 else
33 ret = 0;
34
35 return ret;
36}
37
38int do_unshare_tests(unsigned long clone_flags,
Wanlong Gao354ebb42012-12-07 10:10:04 +080039 int (*fn1) (void *arg), void *arg1,
40 int (*fn2) (void *arg), void *arg2)
subrata_modak0c5f9702007-04-26 11:02:47 +000041{
42 int pid, ret = 0;
43 int retpipe[2];
44 char buf[2];
45
46 if (pipe(retpipe) == -1) {
47 perror("pipe");
48 return -1;
49 }
50 pid = fork();
51 if (pid == -1) {
52 perror("fork");
53 close(retpipe[0]);
54 close(retpipe[1]);
55 return -1;
56 }
57 if (pid == 0) {
58 close(retpipe[0]);
Jan Stancek359980f2013-02-15 10:16:05 +010059 ret = ltp_syscall(SYS_unshare, clone_flags);
subrata_modak0c5f9702007-04-26 11:02:47 +000060 if (ret == -1) {
yaberauneya0ccf2de2009-11-04 06:44:01 +000061 if (write(retpipe[1], "0", 2) < 0) {
62 perror("unshare:write(retpipe[1], ..)");
63 }
subrata_modak0c5f9702007-04-26 11:02:47 +000064 close(retpipe[1]);
subrata_modak0c5f9702007-04-26 11:02:47 +000065 exit(1);
yaberauneya0ccf2de2009-11-04 06:44:01 +000066 } else {
67 if (write(retpipe[1], "1", 2) < 0) {
68 perror("unshare:write(retpipe[1], ..)");
69 }
70 }
subrata_modak0c5f9702007-04-26 11:02:47 +000071 close(retpipe[1]);
72 ret = fn1(arg1);
73 exit(ret);
74 } else {
75 close(retpipe[1]);
yaberauneya0ccf2de2009-11-04 06:44:01 +000076 if (read(retpipe[0], &buf, 2) < 0) {
77 perror("unshare:read(retpipe[0], ..)");
78 }
subrata_modak0c5f9702007-04-26 11:02:47 +000079 close(retpipe[0]);
80 if (*buf == '0')
81 return -1;
82 if (fn2)
83 ret = fn2(arg2);
84 }
85
86 return ret;
87}
88
Wanlong Gao354ebb42012-12-07 10:10:04 +080089int do_plain_tests(int (*fn1) (void *arg), void *arg1,
90 int (*fn2) (void *arg), void *arg2)
subrata_modak0c5f9702007-04-26 11:02:47 +000091{
92 int ret = 0, pid;
93
94 pid = fork();
95 if (pid == -1) {
96 perror("fork");
97 return -1;
98 }
99 if (pid == 0)
100 return fn1(arg1);
101 if (fn2)
102 ret = fn2(arg2);
103 return ret;
104}
105
106int do_clone_unshare_test(int use_clone, unsigned long clone_flags,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800107 int (*fn1) (void *arg), void *arg1)
subrata_modak0c5f9702007-04-26 11:02:47 +0000108{
109 switch (use_clone) {
110 case T_NONE:
111 return do_plain_tests(fn1, arg1, NULL, NULL);
112 case T_CLONE:
113 return do_clone_tests(clone_flags, fn1, arg1, NULL, NULL);
114 case T_UNSHARE:
115 return do_unshare_tests(clone_flags, fn1, arg1, NULL, NULL);
116 default:
117 printf("%s: bad use_clone option: %d\n", __FUNCTION__,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 use_clone);
subrata_modak0c5f9702007-04-26 11:02:47 +0000119 return -1;
120 }
121}
122
subrata_modak0c5f9702007-04-26 11:02:47 +0000123/*
124 * Run fn1 in a unshared environmnent, and fn2 in the original context
125 */
126int do_clone_unshare_tests(int use_clone, unsigned long clone_flags,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800127 int (*fn1) (void *arg), void *arg1,
128 int (*fn2) (void *arg), void *arg2)
subrata_modak0c5f9702007-04-26 11:02:47 +0000129{
130 switch (use_clone) {
131 case T_NONE:
132 return do_plain_tests(fn1, arg1, fn2, arg2);
133 case T_CLONE:
134 return do_clone_tests(clone_flags, fn1, arg1, fn2, arg2);
135 case T_UNSHARE:
136 return do_unshare_tests(clone_flags, fn1, arg1, fn2, arg2);
137 default:
138 printf("%s: bad use_clone option: %d\n", __FUNCTION__,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800139 use_clone);
subrata_modak0c5f9702007-04-26 11:02:47 +0000140 return -1;
141 }
Chris Dearmanec6edca2012-10-17 19:54:01 -0700142}