1 /*
2  * Copyright (C) 2022 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 #ifdef FILLP_LINUX
17 #include <fcntl.h>
18 #endif /* FILLP_LINUX */
19 
20 #include "utils.h"
21 #include "log.h"
22 #include "fillp_function.h"
23 #include "fillp_os.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /*
30 Description: Global LM structure
31 Value Range: None
32 Access: Used to store values for global lm structure.
33 Remarks:
34 */
35 FillpLmGlobal g_fillpLmGlobal = {
36     FILLP_DEBUG_ID_ALL,  /* logModules */
37     FILLP_DBG_LVL_ERROR, /* debugLevel */
38     FILLP_FALSE,         /* funcTrace */
39     FILLP_FALSE,         /* reserve */
40 #ifdef FILLP_64BIT_ALIGN
41     FILLP_FALSE, /* padd */
42 #endif
43     { FILLP_NULL_PTR }, /* lmCallbackFn */
44 };
45 /*
46 Description: System OS Basic Function
47 Value Range: None
48 Access: Used to store system basic OS functions.
49 Remarks:
50 */
51 FillpSysLibBasicCallbackFuncSt g_fillpOsBasicLibFun;
52 
53 /*
54 Description: System OS semophore functions
55 Value Range: None
56 Access: Used to store system semophore library functions.
57 Remarks:
58 */
59 FillpSysLibSemCallbackFuncSt g_fillpOsSemLibFun;
60 
61 /*
62 Description: System OS socket functions
63 Value Range: None
64 Access: Used to store system socket functions.
65 Remarks:
66 */
67 FillpSysLibSockCallbackFuncSt g_fillpOsSocketLibFun;
68 
69 /*
70 Description: System or APP other functions
71 Value Range: None
72 Access: Used to store APP other functions.
73 Remarks:
74 */
75 FillpAppCallbackFunc g_fillpAppCbkFun;
76 
77 #ifdef FILLP_LINUX
78 
SysArchSetSockBlocking(FILLP_INT sock,FILLP_BOOL blocking)79 FILLP_INT SysArchSetSockBlocking(FILLP_INT sock, FILLP_BOOL blocking)
80 {
81     FILLP_INT flags;
82 
83     if (sock < 0) {
84         return ERR_PARAM;
85     }
86 
87     flags = FILLP_FCNTL(sock, F_GETFL, 0);
88     if (flags < 0) {
89         return ERR_COMM;
90     }
91     flags = (FILLP_INT)(blocking ? ((FILLP_UINT)flags & ~(FILLP_UINT)O_NONBLOCK) :
92         ((FILLP_UINT)flags | (FILLP_UINT)O_NONBLOCK));
93     if (FILLP_FCNTL(sock, F_SETFL, flags) < 0) {
94         return ERR_COMM;
95     }
96 
97     return ERR_OK;
98 }
99 
100 #elif defined(FILLP_WIN32)
101 
SysArchSetSockBlocking(FILLP_INT sock,FILLP_BOOL blocking)102 FILLP_INT SysArchSetSockBlocking(FILLP_INT sock, FILLP_BOOL blocking)
103 {
104     FILLP_ULONG mode = blocking ? 0 : 1;
105     if (sock < 0) {
106         return FILLP_ERR_VAL;
107     }
108 
109     return FILLP_IOCTLSOCKET(sock, (FILLP_INT)FIONBIO, &mode);
110 }
111 
112 #else
113 
114 #error "define SysArchSetSockBlocking!!!"
115 
116 #endif
117 
118 /* Common for linux and windows */
SysSetThreadName(FILLP_CHAR * name,FILLP_UINT16 nameLen)119 FILLP_INT SysSetThreadName(FILLP_CHAR *name, FILLP_UINT16 nameLen)
120 {
121     if (name == FILLP_NULL_PTR || nameLen == 0) {
122         FILLP_LOGERR("SysSetThreadName para invalid");
123         return ERR_OK;
124     }
125 #if defined(FILLP_LINUX) && !defined(FILLP_MAC)
126     (void)prctl(PR_SET_NAME, name);
127     return ERR_OK;
128 #else
129     return ERR_OK;
130 #endif /* FILLP_LINUX */
131 }
132 
SysArchSetSockSndbuf(FILLP_INT sock,FILLP_UINT size)133 FILLP_INT SysArchSetSockSndbuf(FILLP_INT sock, FILLP_UINT size)
134 {
135     if (sock < 0) {
136         return ERR_PARAM;
137     }
138 
139     return FILLP_SETSOCKOPT(sock, SOL_SOCKET, SO_SNDBUF, (FILLP_CONST char *)&size, sizeof(FILLP_INT));
140 }
141 
SysArchSetSockRcvbuf(FILLP_INT sock,FILLP_UINT size)142 FILLP_INT SysArchSetSockRcvbuf(FILLP_INT sock, FILLP_UINT size)
143 {
144     if (sock < 0) {
145         return ERR_PARAM;
146     }
147 
148     return FILLP_SETSOCKOPT(sock, SOL_SOCKET, SO_RCVBUF, (FILLP_CONST char *)&size, sizeof(FILLP_INT));
149 }
150 
151 #ifdef __cplusplus
152 }
153 #endif
154