1 /*
2 * Copyright (c) 2023-2025 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_server.h"
17
18 #include "accesstoken_kit.h"
19
20 #include "devicestatus_define.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "SocketServer"
24
25 namespace OHOS {
26 namespace Msdp {
27 namespace DeviceStatus {
28
SocketServer(IContext * context)29 SocketServer::SocketServer(IContext *context) : context_(context) { }
30
Socket(CallingContext & context,const std::string & programName,int32_t moduleType,int & socketFd,int32_t & tokenType)31 int32_t SocketServer::Socket(CallingContext &context, const std::string& programName, int32_t moduleType,
32 int& socketFd, int32_t& tokenType)
33 {
34 CALL_INFO_TRACE;
35 FI_HILOGI("programName:%{public}s, moduleType:%{public}d, socketFd:%{private}d, tokenType:%{private}d",
36 programName.c_str(), moduleType, socketFd, tokenType);
37 int32_t clientTokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
38 int32_t clientFd { -1 };
39 CHKPR(context_, RET_ERR);
40 int32_t ret = context_->GetSocketSessionManager().AllocSocketFd(
41 programName, moduleType, clientTokenType, context.uid, context.pid, clientFd);
42 if (ret != RET_OK) {
43 FI_HILOGE("AllocSocketFd failed");
44 if (clientFd >= 0 && close(clientFd) < 0) {
45 FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{private}d", strerror(errno), clientFd);
46 }
47 return RET_ERR;
48 }
49 socketFd = clientFd;
50 tokenType = clientTokenType;
51 return RET_OK;
52 }
53 } // namespace DeviceStatus
54 } // namespace Msdp
55 } // namespace OHOS
56