• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "dhcp_client.h"
16 
17 #include <string.h>
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <errno.h>
21 #include <signal.h>
22 #include <unistd.h>
23 #include <string.h>
24 
25 #include "securec.h"
26 #include "dhcp_function.h"
27 #include "dhcp_ipv4.h"
28 
29 #undef LOG_TAG
30 #define LOG_TAG "WifiDhcpClient"
31 #define CHECK_PARENTPROCESS_STATUS_SLEEP_TIME (500 * 1000)
32 
33 /* Default options. */
34 static struct DhcpClientCfg g_clientCfg = {"", "", "", "", "", 0, 0, DHCP_IP_TYPE_NONE, {'\0'}, NULL,
35     false, "", "", 0, -1};
36 
GetDhcpClientCfg(void)37 struct DhcpClientCfg *GetDhcpClientCfg(void)
38 {
39     return &g_clientCfg;
40 }
41 
StartProcess(void)42 int StartProcess(void)
43 {
44     LOGI("StartProcess() begin.");
45     if (InitSignalHandle() != DHCP_OPT_SUCCESS) {
46         return DHCP_OPT_FAILED;
47     }
48 
49     /* Create a thread to check the parent process status that creates the daemon */
50     pthread_t tid;
51     int ret = pthread_create(&tid, NULL, CheckParentProcessIsExist, NULL);
52     if (ret != 0) {
53         LOGE("Create thread failed!");
54         return DHCP_OPT_FAILED;
55     }
56     pthread_detach(tid);
57 
58     if ((g_clientCfg.getMode == DHCP_IP_TYPE_ALL) || (g_clientCfg.getMode == DHCP_IP_TYPE_V4)) {
59         /* Handle dhcp v4. */
60         StartIpv4();
61     }
62     return DHCP_OPT_SUCCESS;
63 }
64 
StopProcess(const char * pidFile)65 int StopProcess(const char *pidFile)
66 {
67     LOGI("StopProcess() begin, pidFile:%{public}s.", pidFile);
68     pid_t pid = GetPID(pidFile);
69     if (pid <= 0) {
70         LOGW("StopProcess() GetPID pidFile:%{public}s, pid == -1!", pidFile);
71         return DHCP_OPT_SUCCESS;
72     }
73 
74     LOGI("StopProcess() sending signal SIGTERM to process:%{public}d.", pid);
75     if (kill(pid, SIGTERM) == -1) {
76         if (ESRCH == errno) {
77             LOGW("StopProcess() pidFile:%{public}s,pid:%{public}d is not exist.", pidFile, pid);
78             unlink(pidFile);
79             return DHCP_OPT_SUCCESS;
80         }
81         LOGE("StopProcess() cmd: [kill %{public}d] failed, kill error:%{public}d!", pid, errno);
82         return DHCP_OPT_FAILED;
83     }
84 
85     unlink(pidFile);
86     return DHCP_OPT_SUCCESS;
87 }
88 
GetProStatus(const char * pidFile)89 int GetProStatus(const char *pidFile)
90 {
91     pid_t pid = GetPID(pidFile);
92     if (pid <= 0) {
93         LOGI("GetProStatus() %{public}s pid:%{public}d, %{public}s is not running.", pidFile, pid, DHCPC_NAME);
94         return 0;
95     }
96     LOGI("GetProStatus() %{public}s pid:%{public}d, %{public}s is running.", pidFile, pid, DHCPC_NAME);
97     return 1;
98 }
99 
CheckParentProcessIsExist(void * arg)100 void *CheckParentProcessIsExist(void *arg)
101 {
102     while (1) {
103         if (kill(g_clientCfg.parentProcessId, 0) == -1) {
104             LOGE("CheckParentProcessIsExist, Parent Process %{public}d is not exist!", g_clientCfg.parentProcessId);
105             StopProcess(g_clientCfg.pidFile);
106             return NULL;
107         }
108 
109         usleep(CHECK_PARENTPROCESS_STATUS_SLEEP_TIME);
110         continue;
111     }
112 }