1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci * You may obtain a copy of the License at 6d9f0492fSopenharmony_ci * 7d9f0492fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci * 9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci * limitations under the License. 14d9f0492fSopenharmony_ci */ 15d9f0492fSopenharmony_ci 16d9f0492fSopenharmony_ci#include "init_socket.h" 17d9f0492fSopenharmony_ci#include <errno.h> 18d9f0492fSopenharmony_ci#include <fcntl.h> 19d9f0492fSopenharmony_ci#include <sys/socket.h> 20d9f0492fSopenharmony_ci#include <sys/un.h> 21d9f0492fSopenharmony_ci#include "beget_ext.h" 22d9f0492fSopenharmony_ci#include "securec.h" 23d9f0492fSopenharmony_ci 24d9f0492fSopenharmony_ci#define N_DEC 10 25d9f0492fSopenharmony_ci#define MAX_SOCKET_ENV_PREFIX_LEN 64 26d9f0492fSopenharmony_ci#define MAX_SOCKET_DIR_LEN 128 27d9f0492fSopenharmony_ci 28d9f0492fSopenharmony_cistatic int GetControlFromEnv(const char *path, int length) 29d9f0492fSopenharmony_ci{ 30d9f0492fSopenharmony_ci BEGET_CHECK_RETURN_VALUE(path != NULL && length > 0, -1); 31d9f0492fSopenharmony_ci const char *val = getenv(path); 32d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(val != NULL, return -1, "Get environment from %s failed", path); 33d9f0492fSopenharmony_ci errno = 0; 34d9f0492fSopenharmony_ci int fd = strtol(val, NULL, N_DEC); 35d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(errno == 0, return -1, "Failed strtol err=%d", errno); 36d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(fcntl(fd, F_GETFD) >= 0, return -1, "Failed fcntl err=%d ", errno); 37d9f0492fSopenharmony_ci return fd; 38d9f0492fSopenharmony_ci} 39d9f0492fSopenharmony_ci 40d9f0492fSopenharmony_ciint GetControlSocket(const char *name) 41d9f0492fSopenharmony_ci{ 42d9f0492fSopenharmony_ci BEGET_CHECK_RETURN_VALUE(name != NULL, -1); 43d9f0492fSopenharmony_ci char path[MAX_SOCKET_ENV_PREFIX_LEN] = {0}; 44d9f0492fSopenharmony_ci BEGET_CHECK_RETURN_VALUE(snprintf_s(path, sizeof(path), sizeof(path) - 1, OHOS_SOCKET_ENV_PREFIX"%s", 45d9f0492fSopenharmony_ci name) != -1, -1); 46d9f0492fSopenharmony_ci int fd = GetControlFromEnv(path, MAX_SOCKET_ENV_PREFIX_LEN); 47d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(fd >= 0, return -1, "Get control fd from environment failed"); 48d9f0492fSopenharmony_ci int addrFamily = 0; 49d9f0492fSopenharmony_ci socklen_t afLen = sizeof(addrFamily); 50d9f0492fSopenharmony_ci int ret = getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &addrFamily, &afLen); 51d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, return -1, "Get socket option fail, err=%d ", errno); 52d9f0492fSopenharmony_ci if (addrFamily != AF_UNIX) { 53d9f0492fSopenharmony_ci return fd; 54d9f0492fSopenharmony_ci } 55d9f0492fSopenharmony_ci struct sockaddr_un addr; 56d9f0492fSopenharmony_ci socklen_t addrlen = sizeof(addr); 57d9f0492fSopenharmony_ci ret = getsockname(fd, (struct sockaddr*)&addr, &addrlen); 58d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret >= 0, return -1, "Failed getsockname err=%d ", errno); 59d9f0492fSopenharmony_ci char sockDir[MAX_SOCKET_DIR_LEN] = {0}; 60d9f0492fSopenharmony_ci BEGET_CHECK_RETURN_VALUE(snprintf_s(sockDir, sizeof(sockDir), sizeof(sockDir) - 1, OHOS_SOCKET_DIR"/%s", 61d9f0492fSopenharmony_ci name) != -1, -1); 62d9f0492fSopenharmony_ci BEGET_LOGV("Compary sockDir %s and addr.sun_path %s", sockDir, addr.sun_path); 63d9f0492fSopenharmony_ci if (strncmp(sockDir, addr.sun_path, strlen(sockDir)) == 0) { 64d9f0492fSopenharmony_ci return fd; 65d9f0492fSopenharmony_ci } 66d9f0492fSopenharmony_ci return -1; 67d9f0492fSopenharmony_ci} 68