• 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 
32 /* Default options. */
33 static struct DhcpClientCfg g_clientCfg = {"", "", "", "", "", 0, 0, DHCP_IP_TYPE_NONE, {'\0'}, NULL, false, "", "", 0};
34 
GetDhcpClientCfg(void)35 struct DhcpClientCfg *GetDhcpClientCfg(void)
36 {
37     return &g_clientCfg;
38 }
39 
StartProcess(void)40 int StartProcess(void)
41 {
42     LOGI("StartProcess() begin.");
43     if (InitSignalHandle() != DHCP_OPT_SUCCESS) {
44         return DHCP_OPT_FAILED;
45     }
46 
47     if ((g_clientCfg.getMode == DHCP_IP_TYPE_ALL) || (g_clientCfg.getMode == DHCP_IP_TYPE_V4)) {
48         /* Handle dhcp v4. */
49         StartIpv4();
50     }
51     return DHCP_OPT_SUCCESS;
52 }
53 
StopProcess(const char * pidFile)54 int StopProcess(const char *pidFile)
55 {
56     LOGI("StopProcess() begin, pidFile:%{public}s.", pidFile);
57     pid_t pid = GetPID(pidFile);
58     if (pid <= 0) {
59         LOGW("StopProcess() GetPID pidFile:%{public}s, pid == -1!", pidFile);
60         return DHCP_OPT_SUCCESS;
61     }
62 
63     LOGI("StopProcess() sending signal SIGTERM to process:%{public}d.", pid);
64     if (kill(pid, SIGTERM) == -1) {
65         if (ESRCH == errno) {
66             LOGW("StopProcess() pidFile:%{public}s,pid:%{public}d is not exist.", pidFile, pid);
67             unlink(pidFile);
68             return DHCP_OPT_SUCCESS;
69         }
70         LOGE("StopProcess() cmd: [kill %{public}d] failed, kill error:%{public}d!", pid, errno);
71         return DHCP_OPT_FAILED;
72     }
73 
74     unlink(pidFile);
75     return DHCP_OPT_SUCCESS;
76 }
77 
GetProStatus(const char * pidFile)78 int GetProStatus(const char *pidFile)
79 {
80     pid_t pid = GetPID(pidFile);
81     if (pid <= 0) {
82         LOGI("GetProStatus() %{public}s pid:%{public}d, %{public}s is not running.", pidFile, pid, DHCPC_NAME);
83         return 0;
84     }
85     LOGI("GetProStatus() %{public}s pid:%{public}d, %{public}s is running.", pidFile, pid, DHCPC_NAME);
86     return 1;
87 }
88