• 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 
16 #include "init_socket.h"
17 #include <ctype.h>
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <netinet/in.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/stat.h>
24 #include <sys/uio.h>
25 #include <sys/un.h>
26 #include "beget_ext.h"
27 #include "securec.h"
28 
29 #define N_DEC 10
30 #define MAX_SOCKET_ENV_PREFIX_LEN 64
31 #define MAX_SOCKET_DIR_LEN 128
32 
GetControlFromEnv(const char * path,int length)33 static int GetControlFromEnv(const char *path, int length)
34 {
35     BEGET_CHECK_RETURN_VALUE(path != NULL && length > 0, -1);
36     BEGET_LOGI("GetControlFromEnv path is %s ", path);
37     const char *val = getenv(path);
38     BEGET_ERROR_CHECK(val != NULL, return -1, "GetControlFromEnv val is null %d", errno);
39     errno = 0;
40     int fd = strtol(val, NULL, N_DEC);
41     BEGET_CHECK_RETURN_VALUE(errno == 0, -1);
42     BEGET_LOGI("GetControlFromEnv fd is %d ", fd);
43     BEGET_ERROR_CHECK(fcntl(fd, F_GETFD) >= 0, return -1, "GetControlFromEnv errno %d ", errno);
44     return fd;
45 }
46 
GetControlSocket(const char * name)47 int GetControlSocket(const char *name)
48 {
49     BEGET_CHECK_RETURN_VALUE(name != NULL, -1);
50     char path[MAX_SOCKET_ENV_PREFIX_LEN] = {0};
51     BEGET_CHECK_RETURN_VALUE(snprintf_s(path, sizeof(path), sizeof(path) - 1, OHOS_SOCKET_ENV_PREFIX"%s",
52         name) != -1, -1);
53     BEGET_LOGI("GetControlSocket path is %s ", path);
54     int fd = GetControlFromEnv(path, MAX_SOCKET_ENV_PREFIX_LEN);
55     BEGET_ERROR_CHECK(fd >= 0, return -1, "GetControlFromEnv fail ");
56     int addrFamily = 0;
57     socklen_t afLen = sizeof(addrFamily);
58     int ret = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &addrFamily, &afLen);
59     BEGET_ERROR_CHECK(ret == 0, return -1, "get socket option fail, errno %d ", errno);
60     BEGET_LOGI("socket %s fd %d address family %d", name, fd, addrFamily);
61     if (addrFamily != AF_UNIX) {
62         return fd;
63     }
64     struct sockaddr_un addr;
65     socklen_t addrlen = sizeof(addr);
66     ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen);
67     BEGET_ERROR_CHECK(ret >= 0, return -1, "GetControlSocket errno %d ", errno);
68     char sockDir[MAX_SOCKET_DIR_LEN] = {0};
69     BEGET_CHECK_RETURN_VALUE(snprintf_s(sockDir, sizeof(sockDir), sizeof(sockDir) - 1, OHOS_SOCKET_DIR"/%s",
70         name) != -1, -1);
71     BEGET_LOGI("sockDir %s ", sockDir);
72     BEGET_LOGI("addr.sun_path %s ", addr.sun_path);
73     if (strncmp(sockDir, addr.sun_path, strlen(sockDir)) == 0) {
74         return fd;
75     }
76     return -1;
77 }
78