forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinux_portable.h
357 lines (285 loc) · 5.79 KB
/
linux_portable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
//--------------------------------------------------------------------
// 文件名: linux_portable.h
// 内 容:
// 说 明:
// 创建日期: 2010年12月14日
// 创建人: 陆利民
// 版权所有: 苏州蜗牛电子有限公司
//--------------------------------------------------------------------
#ifndef _SYSTEM_LINUX_PORTABLE_H
#define _SYSTEM_LINUX_PORTABLE_H
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <fcntl.h>
#ifdef __ANDROID__
#include "uuid.h"
#else
#include <uuid/uuid.h>
#endif
// 日期和时间
struct port_date_time_t
{
int nYear;
int nMonth;
int nDay;
int nHour;
int nMinute;
int nSecond;
int nMilliseconds;
int nDayOfWeek;
};
// 内存使用情况
struct port_memory_info_t
{
double dMemoryLoad;
double dTotalMemory;
double dAvailMemory;
};
// 唯一标识符
struct port_uuid_t
{
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
};
// 获得本地当前时间
inline bool Port_GetLocalTime(port_date_time_t* dt)
{
struct timeval tv;
gettimeofday(&tv, NULL);
tm* pt = localtime(&tv.tv_sec);
dt->nYear = pt->tm_year + 1900;
dt->nMonth = pt->tm_mon + 1;
dt->nDay = pt->tm_mday;
dt->nHour = pt->tm_hour;
dt->nMinute = pt->tm_min;
dt->nSecond = pt->tm_sec;
dt->nMilliseconds = tv.tv_usec / 1000;
dt->nDayOfWeek = pt->tm_wday;
return true;
}
// 获得系统计时
inline unsigned int Port_GetTickCount()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}
// 获得当前高精度时刻
inline double Port_GetPerformanceTime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (double)tv.tv_sec + (double)tv.tv_usec * 0.000001;
}
// 系统休眠
inline void Port_Sleep(unsigned int ms)
{
struct timespec ts;
ts.tv_sec = 0;
ts.tv_nsec = ms * 1000000;
nanosleep(&ts, NULL);
}
// 是否非法的浮点数
inline int Port_IsNaN(float value)
{
return isnan(value);
}
// 创建唯一标识符
inline const char* Port_CreateGuid(char* buffer, size_t size)
{
#ifdef __ANDROID__
uuid_t id;
uuid_generate(id);
snprintf(buffer, size -1, "%s", id);
buffer[size - 1] = 0;
return buffer;
#else
port_uuid_t id;
uuid_generate((unsigned char*)&id);
snprintf(buffer, size - 1,
"%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X",
id.Data1, id.Data2, id.Data3,
id.Data4[0], id.Data4[1], id.Data4[2], id.Data4[3],
id.Data4[4], id.Data4[5], id.Data4[6], id.Data4[7]);
buffer[size - 1] = 0;
return buffer;
#endif
}
// 弹出信息窗口
inline void Port_PopupMsgBox(const char* info, const char* title)
{
printf("(msg)%s:%s\n", title, info);
}
// 弹出错误信息窗口
inline void Port_PopupErrorMsgBox(const char* info, const char* title)
{
printf("(err)%s:%s\n", title, info);
}
// 获得当前的进程ID
inline unsigned int Port_GetCurrentProcessId()
{
return getpid();
}
// 获得当前执行文件的路径
inline bool Port_GetCurrentModuleFileName(char* buffer, size_t size)
{
ssize_t res = readlink("/proc/self/exe", buffer, size);
if ((res < 0) || (res >= size))
{
buffer[0] = 0;
return false;
}
buffer[res] = 0;
return true;
}
// 终止进程
inline bool Port_KillProcess(unsigned int proc_id)
{
return kill(proc_id, SIGKILL);
}
inline char* skip_token(char* fp)
{
char* ptr = fp;
while (*ptr && (*ptr != ' '))
{
ptr++;
}
while (*ptr == ' ')
{
ptr++;
}
return ptr;
}
// 获得内存使用情况
inline bool Port_GetMemoryInfo(port_memory_info_t* pInfo)
{
memset(pInfo, 0, sizeof(port_memory_info_t));
// get system wide memory usage
char buffer[1024];
char* p;
int fd = open("/proc/meminfo" , O_RDONLY);
int len = read(fd, buffer, sizeof (buffer) - 1);
close(fd);
buffer[len] = '\0';
p = buffer;
p = skip_token(p);
pInfo->dTotalMemory = strtoul(p, &p, 10); // total memory
p = strchr(p, '\n');
p = skip_token(p);
pInfo->dAvailMemory = strtoul(p, &p, 10); // free memory
pInfo->dMemoryLoad = (1.0 - pInfo->dAvailMemory / pInfo->dTotalMemory);
return true;
}
// 获得CPU使用率
inline double Port_GetCpuRate()
{
char buffer[256];
char* p;
int fd = open("/proc/loadavg", O_RDONLY);
int len = read(fd, buffer, sizeof(buffer) - 1);
close(fd);
buffer[len] = '\0';
double load_avg0 = strtod(buffer, &p);
double load_avg1 = strtod(p, &p);
double load_avg2 = strtod(p, &p);
return load_avg1;
}
// 初始化守护进程
inline bool Port_InitDaemon()
{
#ifndef NOFILE
#define NOFILE 0
#endif
int pid = fork();
if (pid > 0)
{
// 是父进程,结束父进程
exit(0);
}
else if (pid < 0)
{
// fork失败,退出
exit(1);
}
// 是第一子进程,后台继续执行
//第一子进程成为新的会话组长和进程组长
setsid();
// 并与控制终端分离
pid = fork();
if (pid > 0)
{
// 是第一子进程,结束第一子进程
exit(0);
}
else if (pid < 0)
{
// fork失败,退出
exit(1);
}
// 是第二子进程,继续
// 第二子进程不再是会话组长
//关闭打开的文件描述符
for (int i = 0; i < NOFILE; ++i)
{
close(i);
}
umask(0); //重设文件创建掩模
return true;
}
// 启动控制台程序
inline bool Port_Execute(const char* cmd_line)
{
char cmd[256];
size_t size = strlen(cmd_line) + 1;
if (size > sizeof(cmd))
{
return false;
}
memcpy(cmd, cmd_line, size);
char* argv[16] = { NULL };
char* envp[1] = { NULL };
int argc = 0;
argv[argc++] = cmd;
char* p = strchr(cmd, ' ');
while (p)
{
*p = 0;
while (*(++p) == ' ')
{
*p = 0;
}
argv[argc++] = p;
if (argc == (sizeof(argv) / sizeof(argv[0]) - 1))
{
break;
}
}
int pid = fork();
if (0 == pid)
{
// 子进程
if (execve(cmd, argv, envp) == -1)
{
// 启动程序失败,关闭子进程
exit(0);
}
}
return pid > 0;
}
// 设置当前进程与CPU核心的亲和性
inline bool Port_SetProcessAffinity(int core_id)
{
return false;
}
#endif // _SYSTEM_LINUX_PORTABLE_H