1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci 16570af302Sopenharmony_ci#include <malloc.h> 17570af302Sopenharmony_ci#include <sched.h> 18570af302Sopenharmony_ci#include <signal.h> 19570af302Sopenharmony_ci#include <unistd.h> 20570af302Sopenharmony_ci#include <pthread.h> 21570af302Sopenharmony_ci#include <stdlib.h> 22570af302Sopenharmony_ci#include <sys/wait.h> 23570af302Sopenharmony_ci#include "functionalext.h" 24570af302Sopenharmony_ci 25570af302Sopenharmony_ci/** 26570af302Sopenharmony_ci * @tc.name : getprocpid_0100 27570af302Sopenharmony_ci * @tc.desc : Get the pid by parsing the proc information 28570af302Sopenharmony_ci * @tc.level : Level 0 29570af302Sopenharmony_ci */ 30570af302Sopenharmony_civoid getprocpid_0100(void) 31570af302Sopenharmony_ci{ 32570af302Sopenharmony_ci pid_t pid1 = getprocpid(); 33570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0100", pid1 > 1, true); 34570af302Sopenharmony_ci pid_t pid2 = getprocpid(); 35570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0100", pid1, pid2); 36570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0100", pid1, getpid()); 37570af302Sopenharmony_ci} 38570af302Sopenharmony_ci 39570af302Sopenharmony_ci/** 40570af302Sopenharmony_ci * @tc.name : getprocpid_0200 41570af302Sopenharmony_ci * @tc.desc : Get the pid by parsing the proc information 42570af302Sopenharmony_ci * @tc.level : Level 0 43570af302Sopenharmony_ci */ 44570af302Sopenharmony_civoid getprocpid_0200(void) 45570af302Sopenharmony_ci{ 46570af302Sopenharmony_ci pid_t pid1 = getprocpid(); 47570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0200", pid1, getpid()); 48570af302Sopenharmony_ci 49570af302Sopenharmony_ci pid_t child = fork(); 50570af302Sopenharmony_ci if (child == 0) { 51570af302Sopenharmony_ci pid_t pid2 = getprocpid(); 52570af302Sopenharmony_ci if (pid2 != getpid()) { 53570af302Sopenharmony_ci exit(1); 54570af302Sopenharmony_ci } 55570af302Sopenharmony_ci exit(0); 56570af302Sopenharmony_ci } 57570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0200", child > 0, true); 58570af302Sopenharmony_ci 59570af302Sopenharmony_ci int status = 0; 60570af302Sopenharmony_ci int ret = waitpid(child, &status, 0); 61570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0200", ret, child); 62570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0200", WIFEXITED(status), true); 63570af302Sopenharmony_ci} 64570af302Sopenharmony_ci 65570af302Sopenharmony_cistatic int child_func(void *arg) 66570af302Sopenharmony_ci{ 67570af302Sopenharmony_ci pid_t parent = *(pid_t *)arg; 68570af302Sopenharmony_ci if (getpid() != 1) { 69570af302Sopenharmony_ci return ESRCH; 70570af302Sopenharmony_ci } 71570af302Sopenharmony_ci 72570af302Sopenharmony_ci pid_t pid = getprocpid(); 73570af302Sopenharmony_ci if (pid == parent) { 74570af302Sopenharmony_ci return ENOSYS; 75570af302Sopenharmony_ci } 76570af302Sopenharmony_ci return 0; 77570af302Sopenharmony_ci} 78570af302Sopenharmony_ci 79570af302Sopenharmony_ci/** 80570af302Sopenharmony_ci * @tc.name : getprocpid_0300 81570af302Sopenharmony_ci * @tc.desc : Get the pid by parsing the proc information 82570af302Sopenharmony_ci * @tc.level : Level 0 83570af302Sopenharmony_ci */ 84570af302Sopenharmony_civoid getprocpid_0300(void) 85570af302Sopenharmony_ci{ 86570af302Sopenharmony_ci pid_t pid1 = getprocpid(); 87570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0300", pid1, getpid()); 88570af302Sopenharmony_ci 89570af302Sopenharmony_ci pid_t child = clone(child_func, NULL, CLONE_NEWPID | SIGCHLD, &pid1); 90570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0300", child > 0, true); 91570af302Sopenharmony_ci 92570af302Sopenharmony_ci int status = 0; 93570af302Sopenharmony_ci int ret = waitpid(child, &status, 0); 94570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0300", ret, child); 95570af302Sopenharmony_ci EXPECT_EQ("getprocpid_0300", WIFEXITED(status), true); 96570af302Sopenharmony_ci} 97570af302Sopenharmony_ci 98570af302Sopenharmony_cistatic void *pthread_func(void *arg) 99570af302Sopenharmony_ci{ 100570af302Sopenharmony_ci pid_t parent = *(pid_t *)arg; 101570af302Sopenharmony_ci pid_t pid1 = getproctid(); 102570af302Sopenharmony_ci if (parent == pid1) { 103570af302Sopenharmony_ci return (void *)ESRCH; 104570af302Sopenharmony_ci } 105570af302Sopenharmony_ci pid_t pid2 = gettid(); 106570af302Sopenharmony_ci if (pid1 != pid2) { 107570af302Sopenharmony_ci return (void *)ENOSYS; 108570af302Sopenharmony_ci } 109570af302Sopenharmony_ci return NULL; 110570af302Sopenharmony_ci} 111570af302Sopenharmony_ci 112570af302Sopenharmony_ci/** 113570af302Sopenharmony_ci * @tc.name : getproctid_0100 114570af302Sopenharmony_ci * @tc.desc : Get the tid by parsing the proc information 115570af302Sopenharmony_ci * @tc.level : Level 0 116570af302Sopenharmony_ci */ 117570af302Sopenharmony_civoid getproctid_0100(void) 118570af302Sopenharmony_ci{ 119570af302Sopenharmony_ci pid_t pid1 = getproctid(); 120570af302Sopenharmony_ci EXPECT_EQ("getproctid_0100", pid1 > 1, true); 121570af302Sopenharmony_ci EXPECT_EQ("getproctid_0100", pid1, gettid()); 122570af302Sopenharmony_ci 123570af302Sopenharmony_ci void *retVal = NULL; 124570af302Sopenharmony_ci pthread_t thread; 125570af302Sopenharmony_ci int ret = pthread_create(&thread, NULL, pthread_func, &pid1); 126570af302Sopenharmony_ci EXPECT_EQ("getproctid_0100", ret, 0); 127570af302Sopenharmony_ci 128570af302Sopenharmony_ci ret = pthread_join(thread, &retVal); 129570af302Sopenharmony_ci EXPECT_EQ("getproctid_0100", ret, 0); 130570af302Sopenharmony_ci EXPECT_EQ("getproctid_0100", retVal, NULL); 131570af302Sopenharmony_ci} 132570af302Sopenharmony_ci 133570af302Sopenharmony_ci/** 134570af302Sopenharmony_ci * @tc.name : getproctid_0200 135570af302Sopenharmony_ci * @tc.desc : Get the tid by parsing the proc information 136570af302Sopenharmony_ci * @tc.level : Level 0 137570af302Sopenharmony_ci */ 138570af302Sopenharmony_civoid getproctid_0200(void) 139570af302Sopenharmony_ci{ 140570af302Sopenharmony_ci pid_t pid1 = getproctid(); 141570af302Sopenharmony_ci EXPECT_EQ("getproctid_0200", pid1, gettid()); 142570af302Sopenharmony_ci 143570af302Sopenharmony_ci pid_t child = fork(); 144570af302Sopenharmony_ci if (child == 0) { 145570af302Sopenharmony_ci pid_t pid2 = getproctid(); 146570af302Sopenharmony_ci if (pid2 != gettid()) { 147570af302Sopenharmony_ci exit(1); 148570af302Sopenharmony_ci } 149570af302Sopenharmony_ci exit(0); 150570af302Sopenharmony_ci } 151570af302Sopenharmony_ci 152570af302Sopenharmony_ci EXPECT_EQ("getproctid_0200", child > 0, true); 153570af302Sopenharmony_ci 154570af302Sopenharmony_ci int status = 0; 155570af302Sopenharmony_ci int ret = waitpid(child, &status, 0); 156570af302Sopenharmony_ci EXPECT_EQ("getproctid_0200", ret, child); 157570af302Sopenharmony_ci EXPECT_EQ("getproctid_0200", WIFEXITED(status), true); 158570af302Sopenharmony_ci} 159570af302Sopenharmony_ci 160570af302Sopenharmony_cistatic int child_func_tid(void *arg) 161570af302Sopenharmony_ci{ 162570af302Sopenharmony_ci pid_t parent = *(pid_t *)arg; 163570af302Sopenharmony_ci if (getpid() != 1) { 164570af302Sopenharmony_ci return ESRCH; 165570af302Sopenharmony_ci } 166570af302Sopenharmony_ci 167570af302Sopenharmony_ci pid_t pid = getproctid(); 168570af302Sopenharmony_ci if (pid == parent) { 169570af302Sopenharmony_ci return ENOSYS; 170570af302Sopenharmony_ci } 171570af302Sopenharmony_ci return 0; 172570af302Sopenharmony_ci} 173570af302Sopenharmony_ci 174570af302Sopenharmony_ci/** 175570af302Sopenharmony_ci * @tc.name : getproctid_0300 176570af302Sopenharmony_ci * @tc.desc : Get the tid by parsing the proc information 177570af302Sopenharmony_ci * @tc.level : Level 0 178570af302Sopenharmony_ci */ 179570af302Sopenharmony_civoid getproctid_0300(void) 180570af302Sopenharmony_ci{ 181570af302Sopenharmony_ci pid_t pid1 = getproctid(); 182570af302Sopenharmony_ci EXPECT_EQ("getproctid_0300", pid1, gettid()); 183570af302Sopenharmony_ci 184570af302Sopenharmony_ci pid_t child = clone(child_func_tid, NULL, CLONE_NEWPID | SIGCHLD, &pid1); 185570af302Sopenharmony_ci EXPECT_EQ("getproctid_0300", child > 0, true); 186570af302Sopenharmony_ci 187570af302Sopenharmony_ci int status = 0; 188570af302Sopenharmony_ci int ret = waitpid(child, &status, 0); 189570af302Sopenharmony_ci EXPECT_EQ("getproctid_0300", ret, child); 190570af302Sopenharmony_ci EXPECT_EQ("getproctid_0300", WIFEXITED(status), true); 191570af302Sopenharmony_ci} 192570af302Sopenharmony_ci 193570af302Sopenharmony_ciint main(int argc, char *argv[]) 194570af302Sopenharmony_ci{ 195570af302Sopenharmony_ci getprocpid_0100(); 196570af302Sopenharmony_ci getprocpid_0200(); 197570af302Sopenharmony_ci getprocpid_0300(); 198570af302Sopenharmony_ci getproctid_0100(); 199570af302Sopenharmony_ci getproctid_0200(); 200570af302Sopenharmony_ci getproctid_0300(); 201570af302Sopenharmony_ci return t_status; 202570af302Sopenharmony_ci} 203