• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  * conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific prior written
16  * permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 #include "It_container_test.h"
31 
32 const int STR_LEN = 50;
33 const int SEC = 600;
34 const int NSEC = 800000000;
35 
childFunc(void * arg)36 static int childFunc(void *arg)
37 {
38     char path[STR_LEN];
39     char timeOff[STR_LEN];
40     char readBuf[STR_LEN];
41     int ret;
42 
43     ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
44     if (ret <= 0) {
45         return EXIT_CODE_ERRNO_4;
46     }
47     ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", getpid());
48     if (ret <= 0) {
49         return EXIT_CODE_ERRNO_5;
50     }
51 
52     int fd = open(path, O_RDWR);
53     if (fd == -1) {
54         return EXIT_CODE_ERRNO_1;
55     }
56 
57     ret = read(fd, readBuf, STR_LEN);
58     if (ret == -1) {
59         close(fd);
60         return EXIT_CODE_ERRNO_2;
61     }
62     close(fd);
63     ret = strncmp(timeOff, readBuf, strlen(timeOff));
64     if (ret != 0) {
65         return EXIT_CODE_ERRNO_3;
66     }
67 
68     return 0;
69 }
70 
WriteProcTime(int pid)71 static int WriteProcTime(int pid)
72 {
73     int ret = 0;
74     char path[STR_LEN];
75     char timeOff[STR_LEN];
76 
77     ret = sprintf_s(timeOff, STR_LEN, "monotonic %d %d", SEC, NSEC);
78     if (ret <= 0) {
79         return EXIT_CODE_ERRNO_6;
80     }
81     ret = sprintf_s(path, STR_LEN, "/proc/%d/time_offsets", pid);
82     if (ret <= 0) {
83         return EXIT_CODE_ERRNO_7;
84     }
85 
86     int strLen = strlen(timeOff);
87     int fd = open(path, O_WRONLY);
88     if (ret == -1) {
89         return EXIT_CODE_ERRNO_8;
90     }
91 
92     ret = write(fd, timeOff, strLen);
93     if (ret != strLen) {
94         close(fd);
95         return EXIT_CODE_ERRNO_9;
96     }
97 
98     close(fd);
99     return 0;
100 }
101 
TimeContainerUnshare(void)102 static void TimeContainerUnshare(void)
103 {
104     int ret;
105     int status;
106     char *containerType = "time";
107     char *containerType1 = "time_for_children";
108 
109     char *stack = (char *)mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE, CLONE_STACK_MMAP_FLAG, -1, 0);
110     ASSERT_NE(stack, nullptr);
111     char *stackTop = stack + STACK_SIZE;
112 
113     ret = unshare(CLONE_NEWTIME);
114     ASSERT_EQ(ret, 0);
115 
116     ret = unshare(CLONE_NEWTIME);
117     ASSERT_EQ(ret, -1);
118 
119     ret = WriteProcTime(getpid());
120     ASSERT_EQ(ret, 0);
121 
122     auto linkBuffer = ReadlinkContainer(getpid(), containerType);
123     auto linkBuffer1 = ReadlinkContainer(getpid(), containerType1);
124     ret = linkBuffer.compare(linkBuffer1);
125     ASSERT_TRUE(ret != 0);
126 
127     int arg = CHILD_FUNC_ARG;
128     auto pid = clone(childFunc, stackTop, CLONE_NEWTIME | SIGCHLD, &arg);
129     ASSERT_TRUE(pid != -1);
130 
131     auto linkBuffer2 = ReadlinkContainer(pid, containerType);
132     ret = linkBuffer1.compare(linkBuffer2);
133     ASSERT_EQ(ret, 0);
134 
135     ret = unshare(CLONE_NEWTIME);
136     ASSERT_EQ(ret, -1);
137 
138     ret = waitpid(pid, &status, 0);
139     ASSERT_EQ(ret, pid);
140 
141     ret = WIFEXITED(status);
142     ASSERT_TRUE(ret != 0);
143 
144     int exitCode = WEXITSTATUS(status);
145     ASSERT_EQ(exitCode, 0);
146 
147     exit(0);
148 }
149 
ItTimeContainer002(void)150 void ItTimeContainer002(void)
151 {
152     int status = 0;
153     auto pid = fork();
154     ASSERT_TRUE(pid != -1);
155     if (pid == 0) {
156         TimeContainerUnshare();
157         exit(EXIT_CODE_ERRNO_1);
158     }
159     auto ret = waitpid(pid, &status, 0);
160     ASSERT_EQ(ret, pid);
161     ret = WIFEXITED(status);
162     ASSERT_NE(ret, 0);
163     ret = WEXITSTATUS(status);
164     ASSERT_EQ(ret, 0);
165 }
166