1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <sched.h>
18 #include <sys/syscall.h>
19 #include <unistd.h>
20
21 #include "test.h"
22
23 /**
24 * @tc.name : setns_0100
25 * @tc.desc : reassociate thread with a namespace
26 * @tc.level : Level 0
27 */
setns_0100(void)28 void setns_0100(void)
29 {
30 pid_t pid = getpid();
31 if (pid < 0) {
32 t_error("%s failed: pid = %d\n", __func__, pid);
33 }
34
35 int fd = syscall(SYS_pidfd_open, pid, 0);
36 if (fd < 0) {
37 t_error("%s failed: fd = %d\n", __func__, fd);
38 }
39
40 errno = 0;
41 int result = setns(fd, 0);
42 if (result != -1 || errno != EINVAL) {
43 t_error("%s failed: result = %d\n", __func__, result);
44 t_error("%s failed: errno = %d\n", __func__, errno);
45 }
46 }
47
48 /**
49 * @tc.name : setns_0200
50 * @tc.desc : reassociate an invliad fd with a namespace
51 * @tc.level : Level 2
52 */
setns_0200(void)53 void setns_0200(void)
54 {
55 int fd = -1;
56 int nstype = 0;
57
58 errno = 0;
59 int result = setns(fd, nstype);
60 if (result == 0 || errno == 0) {
61 t_error("%s failed: result = %d\n", __func__, result);
62 t_error("%s failed: errno = %d\n", __func__, errno);
63 }
64 }
65
main(int argc,char * argv[])66 int main(int argc, char *argv[])
67 {
68 setns_0100();
69 setns_0200();
70
71 return t_status;
72 }
73