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 "socket_client.h"
17
18 #include <cerrno>
19 #include <iostream>
20 #include <securec.h>
21 #include <sys/socket.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include "hilog_common.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
SocketClient(std::string serverPath,uint32_t socketType)29 SocketClient::SocketClient(std::string serverPath, uint32_t socketType) : Socket(socketType)
30 {
31 serverAddr.sun_family = AF_UNIX;
32 std::string sockPath(SOCKET_FILE_DIR);
33 sockPath += serverPath;
34 if (strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), sockPath.c_str()) != EOK) {
35 return;
36 }
37 serverAddr.sun_path[sizeof(serverAddr.sun_path) - 1] = '\0';
38 }
39
Connect()40 int SocketClient::Connect()
41 {
42 return TEMP_FAILURE_RETRY(connect(socketHandler, reinterpret_cast<sockaddr*>(&serverAddr), sizeof(serverAddr)));
43 }
44 } // namespace HiviewDFX
45 } // namespace OHOS
46