GCC Code Coverage Report


Directory: src/gate/
File: src/gate/system/os_posix.c
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 333 407 81.8%
Functions: 29 31 93.5%
Branches: 115 224 51.3%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2026, Stefan Meislinger |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/system/os.h"
30 #include "gate/results.h"
31
32 #if defined(GATE_SYS_POSIX) && !defined(GATE_SYS_BEOS)
33
34 #include "gate/processes.h"
35 #include "gate/strings.h"
36 #include "gate/files.h"
37 #include "gate/platforms.h"
38 #include <unistd.h>
39
40 #if !defined(GATE_SYS_DARWIN)
41 # include <sys/reboot.h>
42 #endif
43
44
45 #if !defined(GATE_SYS_ANDROID)
46 #endif
47
48 #if defined(GATE_SYS_LINUX)
49 # include <features.h>
50 # if defined(__GLIBC__)
51 //# include <sys/sysctl.h>
52 # endif
53 # include <sys/sysinfo.h>
54 #endif
55
56 #if defined(GATE_SYS_BSD)
57 # include <sys/sysctl.h>
58 # if defined(GATE_SYS_NETBSD)
59 # include <sys/reboot.h>
60 # endif
61 #endif
62
63 #if defined(GATE_SYS_DARWIN)
64 # include <sys/sysctl.h>
65 #endif
66
67 #include <sys/utsname.h>
68
69 #include <string.h>
70 #include <stdio.h>
71
72 typedef struct linux_os_release_info_class
73 {
74 char os_id[256];
75 char os_id_like[256];
76 char os_name[256];
77 char os_pretty_name[256];
78 char os_version[256];
79 char os_version_id[256];
80 } linux_os_release_info_t;
81
82 static char const* linux_os_release_files[] = {
83 "/etc/os-release"
84 };
85
86 129 static gate_bool_t decode_line(char const* line, gate_size_t linelen, char const* key, gate_size_t keylen, char* value, gate_size_t valuelen)
87 {
88 gate_string_t tmp;
89
2/2
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 114 times.
129 if (gate_str_starts_with(line, linelen, key, keylen))
90 {
91 15 gate_string_create_static_len(&tmp, &line[keylen], linelen - keylen);
92 15 gate_string_trim(&tmp, &tmp);
93
4/6
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
15 if ((tmp.length >= 2) && (tmp.str[0] == '\"') && (tmp.str[tmp.length - 1] == '\"'))
94 {
95 12 gate_str_print_text(value, valuelen, tmp.str + 1, tmp.length - 2);
96 }
97 else
98 {
99 3 gate_str_print_text(value, valuelen, tmp.str, tmp.length);
100 }
101 15 gate_string_release(&tmp);
102 15 return true;
103 }
104 else
105 {
106 114 return false;
107 }
108 }
109
110 #if defined(GATE_SYS_BSD)
111 static bool gate_get_sysctrl_value(int mib[], size_t miblen, char* buffer, gate_size_t* buffer_used)
112 {
113 size_t buffer_len = *buffer_used - 1;
114 if (sysctl(mib, (int)miblen, buffer, &buffer_len, 0, 0) == 0)
115 {
116 if (buffer_used)
117 {
118 *buffer_used = buffer_len;
119 }
120 buffer[buffer_len] = 0;
121 return true;
122 }
123 return false;
124 }
125 #endif
126
127
128 3 static gate_result_t gate_linux_read_os_release_info(linux_os_release_info_t* info)
129 {
130 3 gate_result_t ret = GATE_RESULT_FAILED;
131 3 gate_mem_clear(info, sizeof(linux_os_release_info_t));
132 3 gate_size_t count = sizeof(linux_os_release_files) / sizeof(linux_os_release_files[0]);
133 gate_size_t index;
134 gate_string_t filename;
135 3 gate_controlstream_t* filestream = NULL;
136 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
137 gate_size_t linelen;
138
139
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (index = 0; index != count; ++index)
140 {
141 3 gate_string_create_static(&filename, linux_os_release_files[index]);
142 3 ret = gate_file_openstream(&filename, GATE_STREAM_OPEN_READ, &filestream);
143 3 gate_string_release(&filename);
144
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (GATE_SUCCEEDED(ret))
145 {
146
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 while (GATE_SUCCEEDED(ret = gate_stream_read_line((gate_stream_t*)filestream, buffer, sizeof(buffer), &linelen)))
147 {
148
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 27 times.
30 if (linelen == 0)
149 {
150 3 break;
151 }
152
153
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 24 times.
27 if (decode_line(buffer, linelen, "ID=", 3, info->os_id, sizeof(info->os_id))) continue;
154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
24 if (decode_line(buffer, linelen, "ID_LIKE=", 8, info->os_id_like, sizeof(info->os_id_like))) continue;
155
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 21 times.
24 if (decode_line(buffer, linelen, "NAME=", 5, info->os_name, sizeof(info->os_name))) continue;
156
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 18 times.
21 if (decode_line(buffer, linelen, "PRETTY_NAME=", 12, info->os_pretty_name, sizeof(info->os_pretty_name))) continue;
157
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 15 times.
18 if (decode_line(buffer, linelen, "VERSION=", 8, info->os_version, sizeof(info->os_version))) continue;
158
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 12 times.
15 if (decode_line(buffer, linelen, "VERSION_ID=", 11, info->os_version_id, sizeof(info->os_version_id))) continue;
159
160 }
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_BREAK_IF_FAILED(ret);
162 3 break;
163 }
164 }
165
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (filestream != NULL)
166 {
167 3 gate_object_release(filestream);
168 }
169
170 3 return ret;
171 }
172
173 2 gate_result_t gate_os_get_platform(gate_uint32_t* ptr_platform)
174 {
175 2 gate_result_t ret = GATE_RESULT_OK;
176 #if defined(GATE_SYS_ANDROID)
177 *ptr_platform = GATE_OS_PLATFORM_LINUX_ANDROID;
178 #elif defined(GATE_SYS_FREEBSD)
179 *ptr_platform = GATE_OS_PLATFORM_BSD_FREEBSD;
180 #elif defined(GATE_SYS_NETBSD)
181 *ptr_platform = GATE_OS_PLATFORM_BSD_NETBSD;
182 #elif defined(GATE_SYS_OPENBSD)
183 *ptr_platform = GATE_OS_PLATFORM_BSD_OPENBSD;
184 #elif defined(GATE_SYS_BSD)
185 *ptr_platform = GATE_OS_PLATFORM_BSD;
186 #elif defined(GATE_SYS_DARWIN)
187 *ptr_platform = GATE_OS_PLATFORM_DARWIN;
188 #elif defined(GATE_SYS_LINUX)
189 2 *ptr_platform = GATE_OS_PLATFORM_LINUX;
190 #else
191 ret = GATE_RESULT_FAILED;
192 #endif
193
194 2 return ret;
195 }
196
197 1 gate_uint32_t gate_os_address_space()
198 {
199 1 return sizeof(void*) * 8;
200 }
201 2 gate_uint32_t gate_os_up_time_seconds()
202 {
203 2 gate_uint32_t ret = 0;
204 gate_result_t result;
205 char buffer[1024];
206 2 gate_size_t buffer_used = sizeof(buffer);
207 2 float flt_uptime = 0.0f;
208 static gate_string_t const proc_path_uptime = { "/proc/uptime", 12, NULL };
209 #if defined(GATE_SYS_LINUX)
210 struct sysinfo info;
211 #endif
212
213 do
214 {
215 2 result = gate_file_get_content_buffer(&proc_path_uptime, buffer, &buffer_used);
216
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_SUCCEEDED(result))
217 {
218 2 buffer[buffer_used] = 0;
219
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (sscanf(buffer, "%f", &flt_uptime) == 1)
220 {
221 2 ret = (gate_uint32_t)flt_uptime;
222 2 break;
223 }
224 }
225 #if defined(GATE_SYS_LINUX)
226
227 ✗ gate_mem_clear(&info, sizeof(info));
228 ✗ if (sysinfo(&info) == 0)
229 {
230 ✗ ret = (gate_uint32_t)info.uptime;
231 }
232 #endif
233 } while (0);
234
235 2 return ret;
236 }
237 1 gate_result_t gate_os_print_osname(char* buffer, gate_size_t buffer_len, gate_size_t* buffer_used)
238 {
239 1 gate_result_t ret = GATE_RESULT_FAILED;
240
241 #if defined(GATE_SYS_LINUX)
242 1 char const* ptr_data = NULL;
243 struct utsname utsname_buffer;
244 linux_os_release_info_t info;
245 gate_size_t len;
246
247 1 ret = gate_linux_read_os_release_info(&info);
248
249
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
250 {
251
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(info.os_name))
252 {
253 1 ptr_data = info.os_name;
254 }
255 ✗ else if (!gate_str_is_empty(info.os_id))
256 {
257 ✗ ptr_data = info.os_id;
258 }
259 ✗ else if (!gate_str_is_empty(info.os_id_like))
260 {
261 ✗ ptr_data = info.os_id_like;
262 }
263 }
264
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ptr_data == NULL)
265 {
266 ✗ if (uname(&utsname_buffer) == 0)
267 {
268 ✗ ptr_data = utsname_buffer.sysname;
269 }
270 }
271
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_data != NULL)
272 {
273 1 len = gate_str_print_text(buffer, buffer_len, ptr_data, gate_str_length(ptr_data));
274
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (buffer_used)
275 {
276 1 *buffer_used = len;
277 }
278 1 ret = GATE_RESULT_OK;
279 }
280 else
281 {
282 ✗ ret = GATE_RESULT_FAILED;
283 }
284
285 #elif defined(GATE_SYS_DARWIN)
286 char name[256];
287 size_t namelen = sizeof(name) - 1;
288
289 name[sizeof(name) - 1] = 0;
290 if (sysctlbyname("kern.ostype", name, &namelen, NULL, 0) == 0)
291 {
292 const gate_size_t name_used = gate_str_length_max(name, sizeof(name));
293 const gate_size_t used = gate_str_print_text(buffer, buffer_len, name, namelen);
294 if (buffer_used) *buffer_used = used;
295 return GATE_RESULT_OK;
296 }
297 else
298 {
299 return GATE_RESULT_FAILED;
300 }
301
302 #elif defined(GATE_SYS_BSD)
303 int mib[] = { CTL_KERN, KERN_OSTYPE };
304 size_t miblen = sizeof(mib) / sizeof(mib[0]);
305 *buffer_used = buffer_len;
306 if (gate_get_sysctrl_value(mib, miblen, buffer, buffer_used))
307 {
308 ret = GATE_RESULT_OK;
309 }
310 #endif
311 1 return ret;
312 }
313
314
315 1 gate_result_t gate_os_print_productname(char* buffer, gate_size_t buffer_len, gate_size_t* buffer_used)
316 {
317 1 gate_result_t ret = GATE_RESULT_FAILED;
318
319 #if defined(GATE_SYS_LINUX)
320 1 char const* ptr_data = NULL;
321 gate_size_t len;
322 struct utsname utsname_buffer;
323 linux_os_release_info_t info;
324 1 ret = gate_linux_read_os_release_info(&info);
325
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
326 {
327
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_str_is_empty(info.os_pretty_name))
328 {
329 1 ptr_data = info.os_pretty_name;
330 }
331 ✗ else if (!gate_str_is_empty(info.os_version))
332 {
333 ✗ ptr_data = info.os_id;
334 }
335 }
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ptr_data == NULL)
337 {
338 ✗ if (uname(&utsname_buffer) == 0)
339 {
340 ✗ ptr_data = utsname_buffer.sysname;
341 }
342 }
343
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_data != NULL)
344 {
345 1 len = gate_str_print_text(buffer, buffer_len, ptr_data, gate_str_length(ptr_data));
346
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (buffer_used)
347 {
348 1 *buffer_used = len;
349 }
350 1 ret = GATE_RESULT_OK;
351 }
352 else
353 {
354 ✗ ret = GATE_RESULT_FAILED;
355 }
356 #elif defined(GATE_SYS_DARWIN)
357 char name[256];
358 size_t namelen = sizeof(name) - 1;
359
360 name[sizeof(name) - 1] = 0;
361 if (sysctlbyname("kern.osproductversion", name, &namelen, NULL, 0) == 0)
362 {
363 const gate_size_t name_used = gate_str_length_max(name, sizeof(name));
364 const gate_size_t used = gate_str_print_text(buffer, buffer_len, name, namelen);
365 if (buffer_used) *buffer_used = used;
366 return GATE_RESULT_OK;
367 }
368 else
369 {
370 return GATE_RESULT_FAILED;
371 }
372
373 #elif defined(GATE_SYS_BSD)
374 int mib[] = { CTL_KERN, KERN_OSRELEASE };
375 size_t miblen = sizeof(mib) / sizeof(mib[0]);
376 *buffer_used = buffer_len;
377 if (gate_get_sysctrl_value(mib, miblen, buffer, buffer_used))
378 {
379 ret = GATE_RESULT_OK;
380 }
381
382 #endif
383 1 return ret;
384 }
385
386 1 gate_result_t gate_os_get_version(gate_version_t* ptr_version)
387 {
388 1 int a = 0, b = 0, c = 0;
389 struct utsname uname_info;
390
391 #if defined(GATE_SYS_LINUX)
392 do
393 {
394 linux_os_release_info_t info;
395 1 gate_result_t result = gate_linux_read_os_release_info(&info);
396
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(result))
397 {
398 1 sscanf(info.os_version_id, "%d.%d.%d", &a, &b, &c);
399
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_version)
400 {
401 1 ptr_version->major = a;
402 1 ptr_version->minor = b;
403 1 ptr_version->patch = c;
404 1 ptr_version->build = 0;
405 }
406 1 return GATE_RESULT_OK;
407 }
408 } while (0);
409 #endif /* GATE_SYS_LINUX */
410
411 ✗ if (0 == uname(&uname_info))
412 {
413 ✗ sscanf(uname_info.release, "%d.%d.%d", &a, &b, &c);
414 ✗ if (ptr_version)
415 {
416 ✗ ptr_version->major = a;
417 ✗ ptr_version->minor = b;
418 ✗ ptr_version->patch = c;
419 ✗ ptr_version->build = 0;
420 }
421 ✗ return GATE_RESULT_OK;
422 }
423 ✗ return GATE_RESULT_NOTAVAILABLE;
424 }
425
426
427 2 gate_result_t gate_os_get_hostname_str(char* buffer, gate_size_t buffer_len, gate_size_t* buffer_used)
428 {
429 gate_result_t ret;
430 2 char tmp_buffer[GATE_MAX_FILENAME_LENGTH] = GATE_INIT_EMPTY;
431 gate_size_t used;
432
433 2 int error = gethostname(tmp_buffer, sizeof(tmp_buffer));
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (error)
435 {
436 ✗ gate_posix_errno(&ret);
437 }
438 else
439 {
440 2 used = gate_str_length_max(tmp_buffer, sizeof(tmp_buffer));
441 2 gate_str_print_text(buffer, buffer_len, tmp_buffer, used);
442
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (buffer_used != NULL)
443 {
444 2 *buffer_used = used;
445 }
446 2 ret = GATE_RESULT_OK;
447 }
448 2 return ret;
449 }
450 1 gate_result_t gate_os_set_hostname(gate_string_t const* hostname)
451 {
452 1 gate_result_t result = GATE_RESULT_NOTSUPPORTED;
453 static gate_string_t const hostname_file_path = GATE_STRING_INIT_STATIC("/etc/hostname");
454 int error_state;
455
456
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (GATE_SUCCEEDED(gate_file_exists(&hostname_file_path)))
457 {
458 1 result = gate_file_set_content(&hostname_file_path, hostname);
459 }
460
461 #if defined(GATE_SYS_ANDROID) || defined(GATE_SYS_WASM_WASI)
462 (void)error_state;
463 #else
464 1 error_state = sethostname(hostname->str, hostname->length);
465
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (error_state)
466 {
467 1 gate_posix_errno(&result);
468 }
469 else
470 {
471 ✗ result = GATE_RESULT_OK;
472 }
473 #endif
474 1 return result;
475 }
476 2 gate_result_t gate_os_get_hostdomainname_str(char* buffer, gate_size_t buffer_len, gate_size_t* buffer_used)
477 {
478 gate_result_t ret;
479 2 char tmp_buffer[GATE_MAX_FILENAME_LENGTH] = GATE_INIT_EMPTY;
480 gate_size_t used;
481
482 int error;
483 do
484 {
485 #if defined(GATE_SYS_ANDROID) || defined(GATE_SYS_WASM_WASI)
486 (void)error;
487 ret = GATE_RESULT_NOTSUPPORTED;
488 break;
489 #else
490 2 error = getdomainname(tmp_buffer, sizeof(tmp_buffer));
491
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (error)
492 {
493 ✗ gate_posix_errno(&ret);
494 ✗ break;
495 }
496 #endif
497
498 2 used = gate_str_length_max(tmp_buffer, sizeof(tmp_buffer));
499 2 gate_str_print_text(buffer, buffer_len, tmp_buffer, used);
500
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (buffer_used != NULL)
501 {
502 2 *buffer_used = used;
503 }
504 2 ret = GATE_RESULT_OK;
505 } while (0);
506 2 return ret;
507 }
508 1 gate_result_t gate_os_set_hostdomainname(gate_string_t const* domainname)
509 {
510 1 gate_result_t result = GATE_RESULT_NOTSUPPORTED;
511 #if !defined(GATE_SYS_ANDROID) && !defined(GATE_SYS_WASM_WASI)
512 1 int error = setdomainname(domainname->str, domainname->length);
513
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (error)
514 {
515 1 gate_posix_errno(&result);
516 }
517 else
518 {
519 ✗ result = GATE_RESULT_OK;
520 }
521 #endif
522 1 return result;
523 }
524
525 1 gate_result_t gate_os_get_uid(gate_uint8_t* buffer, gate_size_t buffer_len, gate_size_t* buffer_used)
526 {
527 1 gate_result_t ret = GATE_RESULT_FAILED;
528 static gate_string_t const etc_machine_id = GATE_STRING_INIT_STATIC("/etc/machine-id");
529 char id_buffer[1024];
530 1 gate_size_t id_buffer_size = sizeof(id_buffer);
531 gate_size_t pos_nl;
532 1 long os_hostid = 0;
533
534 do
535 {
536 1 ret = gate_file_get_content_buffer(&etc_machine_id, id_buffer, &id_buffer_size);
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_SUCCEEDED(ret))
538 {
539 ✗ pos_nl = gate_str_char_pos(id_buffer, id_buffer_size, '\n', 0);
540 ✗ if (pos_nl != GATE_STR_NPOS)
541 {
542 ✗ id_buffer_size = pos_nl;
543 }
544 ✗ if (id_buffer_size > 0)
545 {
546 ✗ if (id_buffer_size > buffer_len)
547 {
548 ✗ ret = GATE_RESULT_BUFFERTOOSMALL;
549 ✗ if (buffer_used) *buffer_used = id_buffer_size;
550 ✗ break;
551 }
552 else
553 {
554 ✗ gate_mem_copy(buffer, id_buffer, id_buffer_size);
555 ✗ if (buffer_used) *buffer_used = id_buffer_size;
556 ✗ ret = GATE_RESULT_OK;
557 ✗ break;
558 }
559 }
560 }
561
562 /* fallback: try bad-old hostid() */
563 #if defined(GATE_SYS_ANDROID)
564 (void)os_hostid;
565 #else
566 1 os_hostid = gethostid();
567
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (buffer_len < sizeof(os_hostid))
568 {
569 ✗ if (buffer_used) *buffer_used = id_buffer_size;
570 ✗ ret = GATE_RESULT_BUFFERTOOSMALL;
571 ✗ break;
572 }
573 else
574 {
575 1 gate_mem_copy(buffer, &os_hostid, sizeof(os_hostid));
576
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (buffer_used) *buffer_used = sizeof(os_hostid);
577 1 ret = GATE_RESULT_OK;
578 1 break;
579 }
580 #endif
581
582 } while (0);
583
584 1 return ret;
585 }
586
587 #if !defined(RB_POWER_OFF) && defined(GATE_SYS_BSD)
588 # if defined(GATE_SYS_OPENBSD) || defined(GATE_SYS_NETBSD)
589 # define RB_POWER_OFF (RB_POWERDOWN | RB_HALT)
590 # else
591 # define RB_POWER_OFF RB_POWEROFF
592 # endif
593 #endif
594
595 #if defined(GATE_SYS_NETBSD)
596 # define GATE_POSIX_REBOOT_EXT_ARGS , NULL
597 #else
598 # define GATE_POSIX_REBOOT_EXT_ARGS
599 #endif
600
601 1 gate_result_t gate_os_shutdown()
602 {
603 #define CMD_SHUTDOWN_DIR "/sbin"
604 #define CMD_SHUTDOWN_PATH "/sbin/shutdown"
605
606 gate_result_t ret;
607 1 int exit_code = -1;
608 int error_code;
609
610 1 gate_string_t command_path = GATE_STRING_INIT_STATIC(CMD_SHUTDOWN_PATH);
611 1 gate_string_t workdir = GATE_STRING_INIT_STATIC(CMD_SHUTDOWN_DIR);
612 do
613 {
614 1 ret = gate_process_run(&command_path, NULL, 0, &workdir, NULL, 0,
615 GATE_PROCESS_START_NOTERMINAL | GATE_PROCESS_START_NOINHERIT,
616 NULL, &exit_code);
617
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_SUCCEEDED(ret))
619 {
620 ✗ if (exit_code == 0)
621 {
622 /* script was executed -> seems to be successful */
623 ✗ break;
624 }
625 }
626
627 #if !defined(GATE_SYS_WASM_WASI)
628 /* try to invoke APIs to get extended error codes */
629 1 sync();
630 #endif
631
632 #if !defined(GATE_SYS_DARWIN)
633 1 error_code = reboot(RB_POWER_OFF GATE_POSIX_REBOOT_EXT_ARGS);
634
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (error_code)
635 {
636 1 gate_posix_errno(&ret);
637 }
638 else
639 {
640 ✗ ret = GATE_RESULT_OK;
641 }
642 #endif
643
644 } while (0);
645
646 1 return ret;
647 }
648 1 gate_result_t gate_os_reboot()
649 {
650 #define CMD_REBOOT_DIR "/sbin"
651 #define CMD_REBOOT_PATH "/sbin/reboot"
652
653 gate_result_t ret;
654 1 int exit_code = -1;
655 int error_code;
656
657 1 gate_string_t command_path = GATE_STRING_INIT_STATIC(CMD_REBOOT_PATH);
658 1 gate_string_t workdir = GATE_STRING_INIT_STATIC(CMD_REBOOT_DIR);
659 do
660 {
661 1 ret = gate_process_run(&command_path, NULL, 0, &workdir, NULL, 0,
662 GATE_PROCESS_START_NOTERMINAL | GATE_PROCESS_START_NOINHERIT,
663 NULL, &exit_code);
664
665
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (GATE_SUCCEEDED(ret))
666 {
667 ✗ if (exit_code == 0)
668 {
669 /* script was executed -> seems to be successful */
670 ✗ break;
671 }
672 }
673
674 #if !defined(GATE_SYS_WASM_WASI)
675 /* try to invoke APIs to get extended error codes */
676 1 sync();
677 #endif
678
679 #if !defined(GATE_SYS_DARWIN)
680 1 error_code = reboot(RB_AUTOBOOT GATE_POSIX_REBOOT_EXT_ARGS);
681
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (error_code)
682 {
683 1 gate_posix_errno(&ret);
684 }
685 else
686 {
687 ✗ ret = GATE_RESULT_OK;
688 }
689 #endif
690
691 } while (0);
692
693 1 return ret;
694 }
695
696
697
698
699
700 1 gate_result_t gate_os_get_cpu_architecture(gate_enumint_t* ptr_arch)
701 {
702 1 gate_result_t ret = GATE_RESULT_OK;
703 do
704 {
705 #if (GATE_ARCH == GATE_ARCH_X86IA32)
706 * ptr_arch = GATE_OS_ARCH_X86_32;
707 #elif (GATE_ARCH == GATE_ARCH_X86X64)
708 1 * ptr_arch = GATE_OS_ARCH_X86_64;
709 #elif (GATE_ARCH == GATE_ARCH_X86IA16)
710 * ptr_arch = GATE_OS_ARCH_X86_16;
711 #elif (GATE_ARCH == GATE_ARCH_X86IA64)
712 * ptr_arch = GATE_OS_ARCH_ITANIUM;
713 #elif (GATE_ARCH == GATE_ARCH_ARM64)
714 * ptr_arch = GATE_OS_ARCH_ARM_64;
715 #elif (GATE_ARCH == GATE_ARCH_ARM32)
716 * ptr_arch = GATE_OS_ARCH_ARM_32;
717 #elif (GATE_ARCH == GATE_ARCH_ALPHA)
718 * ptr_arch = GATE_OS_ARCH_ALPHA;
719 #else
720 * ptr_arch = GATE_OS_ARCH_UNKNOWN;
721 #endif
722 } while (0);
723
724 1 return ret;
725 }
726
727 static gate_string_t const gate_os_cpu_key_processor = GATE_STRING_INIT_STATIC("processor");
728 static gate_string_t const gate_os_cpu_key_vendor_id = GATE_STRING_INIT_STATIC("vendor_id");
729 static gate_string_t const gate_os_cpu_key_model_name = GATE_STRING_INIT_STATIC("model name");
730 static gate_string_t const gate_os_cpu_key_cpu_family = GATE_STRING_INIT_STATIC("cpu family");
731 static gate_string_t const gate_os_cpu_key_features = GATE_STRING_INIT_STATIC("features");
732 static gate_string_t const gate_os_cpu_key_model = GATE_STRING_INIT_STATIC("model");
733 static gate_string_t const gate_os_cpu_key_stepping = GATE_STRING_INIT_STATIC("stepping");
734 static gate_string_t const gate_os_cpu_key_flags = GATE_STRING_INIT_STATIC("flags");
735 static gate_string_t const gate_os_cpu_key_cpu_mhz = GATE_STRING_INIT_STATIC("cpu MHz");
736 static gate_string_t const gate_os_cpu_key_cache_size = GATE_STRING_INIT_STATIC("cache size");
737
738 #if defined(GATE_SYS_DARWIN)
739
740 #include <sys/sysctl.h>
741 #include "gate/platforms.h"
742 #include <dlfcn.h>
743
744 //#define GATE_MACOS_HAVE_OS_HEADERS
745
746 #if defined(GATE_MACOS_HAVE_OS_HEADERS)
747 # include <IOKit/IOKitLib.h>
748 # include <CoreFoundation/CoreFoundation.h>
749 #else
750
751 typedef unsigned char UInt8;
752 typedef unsigned int UInt32;
753 typedef signed long CFIndex;
754 typedef int kern_return_t;
755 typedef unsigned long CFTypeID;
756 typedef UInt32 IOOptionBits;
757 typedef UInt32 CFStringEncoding;
758 typedef unsigned int mach_port_t;
759 typedef char io_string_t[512];
760 typedef mach_port_t io_object_t;
761 typedef io_object_t io_registry_entry_t;
762 typedef const void * CFTypeRef;
763 typedef const struct __CFString * CFStringRef;
764 typedef const struct __CFAllocator * CFAllocatorRef;
765 typedef const struct __CFData * CFDataRef;
766 typedef struct
767 {
768 CFIndex location;
769 CFIndex length;
770 } CFRange;
771 #define MACH_PORT_NULL 0
772 enum CFStringEncoding
773 {
774 kCFStringEncodingMacRoman = 0,
775 kCFStringEncodingWindowsLatin1 = 0x0500,
776 kCFStringEncodingISOLatin1 = 0x0201,
777 kCFStringEncodingNextStepLatin = 0x0B01,
778 kCFStringEncodingASCII = 0x0600,
779 kCFStringEncodingUnicode = 0x0100,
780 kCFStringEncodingUTF8 = 0x08000100,
781 kCFStringEncodingNonLossyASCII = 0x0BFF,
782
783 kCFStringEncodingUTF16 = 0x0100,
784 kCFStringEncodingUTF16BE = 0x10000100,
785 kCFStringEncodingUTF16LE = 0x14000100,
786
787 kCFStringEncodingUTF32 = 0x0c000100,
788 kCFStringEncodingUTF32BE = 0x18000100,
789 kCFStringEncodingUTF32LE = 0x1c000100
790 };
791
792 #endif /* !defined(GATE_MACOS_HAVE_OS_HEADERS) */
793
794 static gate_size_t darwin_get_cpu_name(char* buffer, gate_size_t buffer_capacity)
795 {
796 char name[256] = GATE_INIT_EMPTY;
797 size_t size = sizeof(name);
798
799 if (sysctlbyname("machdep.cpu.brand_string", &name, &size, NULL, 0) == 0)
800 {
801 return gate_str_print_text(buffer, buffer_capacity, name, gate_str_length(name));
802 }
803 else
804 {
805 buffer[0] = 0;
806 return 0;
807 }
808 }
809
810 static gate_uint32_t darwin_get_cpu_cores()
811 {
812 int logical = 0;
813 size_t size = sizeof(int);
814 sysctlbyname("hw.logicalcpu", &logical, &size, NULL, 0);
815 return logical;
816 }
817
818 typedef struct darwin_cpu_functions
819 {
820 io_registry_entry_t (*callIORegistryEntryFromPath)(mach_port_t, const io_string_t);
821 CFTypeRef (*callIORegistryEntryCreateCFProperty)(io_registry_entry_t, CFStringRef, CFAllocatorRef, IOOptionBits);
822 kern_return_t (*callIOObjectRelease)(io_object_t);
823 mach_port_t* symkIOMainPortDefault;
824
825 CFTypeID (*callCFGetTypeID)(CFTypeRef);
826 CFTypeID (*callCFDataGetTypeID)(void);
827 void (*callCFDataGetBytes)(CFDataRef, CFRange, UInt8 *);
828 void (*callCFRelease)(CFTypeRef);
829 CFStringRef (*callCFStringCreateWithCString)(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
830 CFAllocatorRef* symkCFAllocatorDefault;
831
832 } darwin_cpu_functions_t;
833
834 static gate_bool_t load_sym(void* lib, char const* func_name, void* ptr_funcptr)
835 {
836 void* ptrfunc = gate_posix_dlsym(lib, func_name);
837 if (NULL == ptrfunc)
838 {
839 return false;
840 }
841 else
842 {
843 *((void**)ptr_funcptr) = ptrfunc;
844 return true;
845 }
846 }
847 #define LOAD_SYM(lib, func_name, ptr_funcptr) \
848 if(!load_sym(lib,func_name, ptr_funcptr)) \
849 break;
850
851 static darwin_cpu_functions_t* load_darwin_cpu_functions()
852 {
853 static void* lib_iokit = NULL;
854 static void* lib_corefoundation = NULL;
855 static darwin_cpu_functions_t funcs;
856 static gate_bool_t is_initialized = false;
857
858 if (!is_initialized)
859 {
860 if (lib_iokit == NULL)
861 {
862 lib_iokit = gate_posix_dlopen("/System/Library/Frameworks/IOKit.framework/IOKit", RTLD_LAZY | RTLD_LOCAL);
863 if (lib_iokit == NULL) return NULL;
864 }
865 if (lib_corefoundation == NULL)
866 {
867 lib_corefoundation = gate_posix_dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_LAZY | RTLD_LOCAL);
868 if (lib_corefoundation == NULL) return NULL;
869 }
870 do
871 {
872 LOAD_SYM(lib_iokit, "IORegistryEntryFromPath", &funcs.callIORegistryEntryFromPath);
873 LOAD_SYM(lib_iokit, "IORegistryEntryCreateCFProperty", &funcs.callIORegistryEntryCreateCFProperty);
874 LOAD_SYM(lib_iokit, "IORegistryEntryFromPath", &funcs.callIOObjectRelease);
875 LOAD_SYM(lib_iokit, "kIOMainPortDefault", &funcs.symkIOMainPortDefault);
876
877 LOAD_SYM(lib_corefoundation, "CFGetTypeID", &funcs.callCFGetTypeID);
878 LOAD_SYM(lib_corefoundation, "CFDataGetTypeID", &funcs.callCFDataGetTypeID);
879 LOAD_SYM(lib_corefoundation, "CFDataGetBytes", &funcs.callCFDataGetBytes);
880 LOAD_SYM(lib_corefoundation, "CFRelease", &funcs.callCFRelease);
881 LOAD_SYM(lib_corefoundation, "CFStringCreateWithCString", &funcs.callCFStringCreateWithCString);
882 LOAD_SYM(lib_corefoundation, "kCFAllocatorDefault", &funcs.symkCFAllocatorDefault);
883
884 /* all functions loaded -> success */
885 is_initialized = true;
886 return &funcs;
887 } while (0);
888 /* a break has occured -> error, cancel */
889 return NULL;
890 }
891 return &funcs;
892 }
893
894 static gate_uint64_t darwing_get_cpu_speed_hz()
895 {
896 darwin_cpu_functions_t* funcs = load_darwin_cpu_functions();
897 static CFStringRef cfstr_clock_frequency = NULL;
898 gate_uint64_t ret = 0;
899 io_registry_entry_t cpu = MACH_PORT_NULL;
900 CFTypeRef freq = NULL;
901 do
902 {
903 if (!funcs) break;
904
905 cpu = funcs->callIORegistryEntryFromPath(*funcs->symkIOMainPortDefault, "IODeviceTree:/cpus/cpu0");
906 if (cpu == MACH_PORT_NULL) break;
907
908 if (cfstr_clock_frequency == NULL)
909 {
910 cfstr_clock_frequency = funcs->callCFStringCreateWithCString(*funcs->symkCFAllocatorDefault, "clock-frequency", kCFStringEncodingUTF8);
911 if (cfstr_clock_frequency == NULL)
912 {
913 break;
914 }
915 }
916
917 freq = funcs->callIORegistryEntryCreateCFProperty(cpu, cfstr_clock_frequency, *funcs->symkCFAllocatorDefault, 0);
918 if (freq && funcs->callCFGetTypeID(freq) == funcs->callCFDataGetTypeID())
919 {
920 uint32_t hz;
921 CFRange range;
922 range.location = 0;
923 range.length = sizeof(hz);
924 funcs->callCFDataGetBytes(freq, range, (UInt8*)&hz);
925 ret = hz;
926 }
927 } while (0);
928
929 if (MACH_PORT_NULL != cpu) funcs->callIOObjectRelease(cpu);
930 if (freq) funcs->callCFRelease(freq);
931 return ret;
932 }
933
934 static gate_result_t gate_os_get_cpu_info_darwin(gate_os_cpuinfo_t* info)
935 {
936 gate_result_t ret = GATE_RESULT_FAILED;
937
938 do
939 {
940 gate_mem_clear(info, sizeof(gate_os_cpuinfo_t));
941
942 darwin_get_cpu_name(info->cpu_name, sizeof(info->cpu_name));
943 info->cpu_cores = darwin_get_cpu_cores();
944 info->cpu_speed_mhz = (gate_uint32_t)(darwing_get_cpu_speed_hz() / 1000000);
945
946 ret = GATE_RESULT_OK;
947 } while (0);
948
949 return ret;
950 }
951
952 #endif /* GATE_SYS_DARWIN */
953
954 2 static gate_result_t gate_os_get_cpu_info_procfs(gate_os_cpuinfo_t* info, gate_string_t* ptr_features)
955 {
956 gate_result_t ret;
957 static gate_string_t const procfs_cpuinfo = GATE_STRING_INIT_STATIC("/proc/cpuinfo");
958 char buffer[4092 * 3];
959 2 gate_size_t buffer_len = sizeof(buffer);
960 gate_string_t data;
961 gate_string_t line;
962 gate_string_t tail;
963 gate_string_t key;
964 gate_string_t value;
965 gate_size_t pos;
966 gate_uint64_t ui64;
967 2 gate_uint32_t family = 0;
968 2 gate_uint32_t model = 0;
969 2 gate_uint32_t stepping = 0;
970
971 do
972 {
973 2 gate_bool_t features_processed = false;
974 2 gate_mem_clear(info, sizeof(gate_os_cpuinfo_t));
975 2 ret = gate_file_get_content_buffer(&procfs_cpuinfo, buffer, &buffer_len);
976
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
977
978 2 gate_string_create_static_len(&data, buffer, buffer_len);
979
980
2/2
✓ Branch 1 taken 108 times.
✓ Branch 2 taken 2 times.
110 while (NULL != gate_string_read_line(&line, &data, &tail))
981 {
982 108 data = tail;
983
984 108 pos = gate_string_char_pos(&line, ':', 0);
985
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 4 times.
108 if (pos != GATE_STR_NPOS)
986 {
987 104 gate_string_create_static_len(&key, line.str, pos);
988 104 gate_string_create_static_len(&value, line.str + pos + 1, line.length - pos - 1);
989 104 gate_string_trim(&key, &key);
990 104 gate_string_trim(&value, &value);
991
992
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_processor))
993 {
994 4 ++info->cpu_cores;
995 }
996
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_features))
997 {
998 ✗ if (!features_processed)
999 {
1000 ✗ features_processed = true;
1001 ✗ if (ptr_features != NULL)
1002 {
1003 ✗ gate_string_create(ptr_features, value.str, value.length);
1004 }
1005 }
1006 }
1007
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_vendor_id))
1008 {
1009 4 gate_str_print_text(info->cpu_vendor, sizeof(info->cpu_vendor), value.str, value.length);
1010 }
1011
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_model_name))
1012 {
1013 4 gate_str_print_text(info->cpu_name, sizeof(info->cpu_name), value.str, value.length);
1014 4 gate_str_print_text(info->cpu_description, sizeof(info->cpu_description), value.str, value.length);
1015 }
1016
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_model_name))
1017 {
1018 4 gate_str_print_text(info->cpu_description, sizeof(info->cpu_description), value.str, value.length);
1019 }
1020
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_cpu_family))
1021 {
1022
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (0 != gate_str_parse_uint64(value.str, value.length, &ui64))
1023 {
1024 4 family = (gate_uint32_t)ui64;
1025 }
1026 }
1027
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_model))
1028 {
1029
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (0 != gate_str_parse_uint64(value.str, value.length, &ui64))
1030 {
1031 4 model = (gate_uint32_t)ui64;
1032 }
1033 }
1034
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_stepping))
1035 {
1036
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (0 != gate_str_parse_uint64(value.str, value.length, &ui64))
1037 {
1038 4 stepping = (gate_uint32_t)ui64;
1039 }
1040 }
1041
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if(0 == gate_string_comp_ic(&key, &gate_os_cpu_key_flags))
1042 {
1043
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!features_processed)
1044 {
1045 2 features_processed = true;
1046
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ptr_features != NULL)
1047 {
1048 1 gate_string_create(ptr_features, value.str, value.length);
1049 }
1050 }
1051 }
1052
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_cpu_mhz))
1053 {
1054
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (0 != gate_str_parse_uint64(value.str, value.length, &ui64))
1055 {
1056 4 info->cpu_speed_mhz = (gate_uint32_t)ui64;
1057 }
1058 }
1059
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 100 times.
104 if (0 == gate_string_comp_ic(&key, &gate_os_cpu_key_cache_size))
1060 {
1061
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (0 != gate_str_parse_uint64(value.str, value.length, &ui64))
1062 {
1063 4 info->cpu_cache_bytes = (gate_uint32_t)(ui64 * 1024);
1064 }
1065 }
1066 }
1067 } // while(...readline...)
1068 2 info->cpu_version = ((family & 0xff) << 24) | ((model & 0xff) << 16) | (stepping & 0xffff);
1069
1070 } while (0);
1071
1072 2 return ret;
1073 }
1074
1075 1 gate_result_t gate_os_get_cpu_info(gate_os_cpuinfo_t* info)
1076 {
1077 #if defined(GATE_SYS_DARWIN)
1078 return gate_os_get_cpu_info_darwin(info);
1079 #else
1080 1 return gate_os_get_cpu_info_procfs(info, NULL);
1081 #endif
1082 }
1083
1084 1 gate_result_t gate_os_enum_cpu_features(gate_os_cpu_feature_callback_t callback, void* param)
1085 {
1086 1 gate_result_t ret = GATE_RESULT_OK;
1087 gate_os_cpuinfo_t dummy;
1088 1 gate_string_t features = GATE_INIT_EMPTY;
1089 static gate_string_t const separator = GATE_STRING_INIT_STATIC(" ");
1090 1 gate_os_get_cpu_info_procfs(&dummy, &features);
1091
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!gate_string_is_empty(&features))
1092 {
1093 1 gate_string_t text = GATE_INIT_EMPTY;
1094 1 gate_string_t head = GATE_INIT_EMPTY;
1095 1 gate_string_t tail = GATE_INIT_EMPTY;
1096 gate_size_t parsed;
1097 1 gate_bool_t continue_parsing = true;
1098 1 gate_string_duplicate(&text, &features);
1099
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 while(continue_parsing)
1100 {
1101 99 parsed = gate_string_parse(&text, &separator, 0, &head, &tail, false);
1102
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 98 times.
99 if (parsed == 0)
1103 {
1104 1 break;
1105 }
1106 else
1107 {
1108 98 char buffer[32] = GATE_INIT_EMPTY;
1109 98 gate_str_print_text(buffer, sizeof(buffer), gate_string_ptr(&head, 0), gate_string_length(&head));
1110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 98 times.
98 if(!callback(buffer, param))
1111 {
1112 ✗ continue_parsing = false;
1113 }
1114 98 gate_string_release(&text);
1115 98 gate_string_release(&head);
1116 98 gate_mem_copy(&text, &tail, sizeof(text)); /* move tail into text*/
1117 98 gate_mem_clear(&tail, sizeof(tail));
1118 98 gate_string_ltrim(&text, &text);
1119 }
1120 }
1121
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if (continue_parsing && !gate_string_is_empty(&text))
1122 {
1123 1 char buffer[32] = GATE_INIT_EMPTY;
1124 1 gate_str_print_text(buffer, sizeof(buffer), gate_string_ptr(&text, 0), gate_string_length(&text));
1125 1 callback(buffer, param);
1126 }
1127 1 gate_string_release(&text);
1128 1 gate_string_release(&head);
1129 1 gate_string_release(&tail);
1130 }
1131 1 gate_string_release(&features);
1132 1 return ret;
1133 }
1134
1135 ✗ gate_result_t gate_os_get_process_cpu_affinity(gate_os_cpu_activation_t* affinity)
1136 {
1137 ✗ gate_result_t ret = GATE_RESULT_FAILED;
1138 do
1139 {
1140 ✗ gate_mem_clear(affinity, sizeof(gate_os_cpu_activation_t));
1141 } while (0);
1142 ✗ return ret;
1143 }
1144 ✗ gate_result_t gate_os_set_process_cpu_affinity(gate_os_cpu_activation_t const* affinity)
1145 {
1146 ✗ gate_result_t ret = GATE_RESULT_FAILED;
1147 do
1148 {
1149 /* TODO */
1150 } while (0);
1151 ✗ return ret;
1152 }
1153
1154
1155 #if defined(GATE_SYS_DARWIN)
1156
1157 #include <mach/mach.h>
1158
1159 static gate_result_t gate_os_cpu_load_darwin(gate_int64_t* ptr_total, gate_int64_t* ptr_work)
1160 {
1161 natural_t cpu_count = 0;
1162 processor_info_array_t cpu_info = NULL;
1163 mach_msg_type_number_t num_cpu_info;
1164
1165 kern_return_t kr = host_processor_info(
1166 mach_host_self(),
1167 PROCESSOR_CPU_LOAD_INFO,
1168 &cpu_count,
1169 &cpu_info,
1170 &num_cpu_info
1171 );
1172
1173 if (kr != KERN_SUCCESS)
1174 {
1175 return GATE_RESULT_FAILED;
1176 }
1177 else
1178 {
1179 natural_t ndx;
1180 gate_int64_t total_capacity = 0;
1181 gate_int64_t work_load = 0;
1182 for (ndx = 0; ndx < cpu_count; ++ndx)
1183 {
1184 const unsigned long user = cpu_info[(CPU_STATE_MAX * ndx) + CPU_STATE_USER];
1185 const unsigned long system = cpu_info[(CPU_STATE_MAX * ndx) + CPU_STATE_SYSTEM];
1186 const unsigned long idle = cpu_info[(CPU_STATE_MAX * ndx) + CPU_STATE_IDLE];
1187 const unsigned long nice = cpu_info[(CPU_STATE_MAX * ndx) + CPU_STATE_NICE];
1188
1189 total_capacity += (user + system + idle + nice);
1190 work_load += (user + system + nice);
1191 }
1192 vm_deallocate(mach_task_self(), (vm_address_t)cpu_info, num_cpu_info);
1193 *ptr_total = total_capacity;
1194 *ptr_work = work_load;
1195 return GATE_RESULT_OK;
1196 }
1197 }
1198
1199 #endif /* GATE_SYS_DARWIN */
1200
1201 2 static gate_result_t gate_os_cpu_load_procfs(gate_int64_t* ptr_total, gate_int64_t* ptr_work)
1202 {
1203 gate_result_t ret;
1204 static gate_string_t const procfs_stat = GATE_STRING_INIT_STATIC("/proc/stat");
1205 char stat_buffer[4096];
1206 2 gate_size_t stat_buffer_len = sizeof(stat_buffer);
1207 gate_string_t data;
1208 gate_string_t strvalues;
1209 gate_string_t line;
1210 gate_string_t tail;
1211 gate_string_t value;
1212 gate_size_t cnt;
1213 gate_int64_t values[7];
1214
1215 do
1216 {
1217 2 ret = gate_file_get_content_buffer(&procfs_stat, stat_buffer, &stat_buffer_len);
1218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 GATE_BREAK_IF_FAILED(ret);
1219
1220 2 gate_string_create_static_len(&data, stat_buffer, stat_buffer_len);
1221 2 ret = GATE_RESULT_FAILED;
1222
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 while (NULL != gate_string_read_line(&line, &data, &tail))
1223 {
1224 2 data = tail;
1225
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (gate_str_starts_with_ic(line.str, line.length, "cpu ", 4))
1226 {
1227 2 gate_string_substr(&strvalues, &line, 4, line.length - 4);
1228
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
16 for (cnt = 0; cnt != 7; ++cnt)
1229 {
1230
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
14 if (NULL == gate_string_read_token(&value, &strvalues, &tail))
1231 {
1232 ✗ break;
1233 }
1234 14 values[cnt] = 0;
1235 14 gate_str_parse_int64(value.str, value.length, &values[cnt]);
1236 14 strvalues = tail;
1237 }
1238
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (cnt == 7)
1239 {
1240 2 *ptr_total = values[0] + values[1] + values[2] + values[3] + values[4] + values[5] + values[6];
1241 2 *ptr_work = values[0] + values[1] + values[2];
1242 2 ret = GATE_RESULT_OK;
1243 }
1244 2 break;
1245 }
1246 }
1247 } while (0);
1248 2 return ret;
1249 }
1250
1251 2 static gate_result_t gate_os_cpu_load(gate_int64_t* ptr_total, gate_int64_t* ptr_work)
1252 {
1253 #if defined(GATE_SYS_DARWIN)
1254 return gate_os_cpu_load_darwin(ptr_total, ptr_work);
1255 #else
1256 2 return gate_os_cpu_load_procfs(ptr_total, ptr_work);
1257 #endif
1258 }
1259
1260 1 gate_result_t gate_os_cpu_load_init(gate_os_cpu_load_state_t* state)
1261 {
1262 1 gate_int64_t* ptr_total = (gate_int64_t*)&state->records[0];
1263 1 gate_int64_t* ptr_work = (gate_int64_t*)&state->records[4];
1264
1265 1 return gate_os_cpu_load(ptr_total, ptr_work);
1266 }
1267 1 gate_result_t gate_os_cpu_load_update(gate_os_cpu_load_state_t* state, gate_uint16_t* load65535)
1268 {
1269 gate_result_t ret;
1270 1 gate_int64_t* ptr_total = (gate_int64_t*)&state->records[0];
1271 1 gate_int64_t* ptr_work = (gate_int64_t*)&state->records[4];
1272 gate_int64_t new_total;
1273 gate_int64_t new_work;
1274 gate_int64_t diff_total;
1275 gate_int64_t diff_work;
1276 gate_int64_t result;
1277 1 ret = gate_os_cpu_load(&new_total, &new_work);
1278
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
1279 {
1280 1 diff_total = new_total - *ptr_total;
1281 1 diff_work = new_work - *ptr_work;
1282 1 *ptr_total = new_total;
1283 1 *ptr_work = new_work;
1284
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (diff_total > 0)
1285 {
1286 1 result = (diff_work * (gate_int64_t)65535 / diff_total);
1287
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 *load65535 = (gate_uint16_t)(result > 65535 ? 65535 : result);
1288 }
1289 else
1290 {
1291 ✗ *load65535 = 65535;
1292 }
1293 }
1294 1 return ret;
1295 }
1296 1 gate_result_t gate_os_cpu_load_uninit(gate_os_cpu_load_state_t* state)
1297 {
1298 1 return GATE_RESULT_OK;
1299 }
1300
1301 static gate_string_t const filepath_proc_meminfo = GATE_STRING_INIT_STATIC("/proc/meminfo");
1302
1303 2 static gate_size_t gate_os_parse_meminfo_line(char const* data, gate_size_t data_len,
1304 char const* key, gate_size_t key_len, char* value, gate_size_t value_len)
1305 {
1306 2 gate_size_t pos = gate_str_pos(data, data_len, key, key_len, 0);
1307 gate_string_t line;
1308 gate_string_t source;
1309 gate_string_t tail;
1310
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (pos != GATE_STR_NPOS)
1311 {
1312 2 gate_string_create_static_len(&source, data + pos + key_len, data_len - pos - key_len);
1313
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (NULL != gate_string_read_line(&line, &source, &tail))
1314 {
1315 2 gate_string_trim(&line, &line);
1316 2 return gate_string_to_buffer(&line, value, value_len);
1317 }
1318 }
1319 ✗ return 0;
1320 }
1321 2 static gate_bool_t gate_os_parse_meminfo_num(char const* data, gate_size_t data_len, char const* key, gate_size_t key_len, gate_uint64_t* num)
1322 {
1323 char value[1024];
1324 2 gate_size_t value_used = gate_os_parse_meminfo_line(data, data_len, key, key_len, value, sizeof(value));
1325
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (value_used)
1326 {
1327 2 *num = 0;
1328
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (gate_str_parse_uint64(value, value_used, num) > 0)
1329 {
1330 2 return true;
1331 }
1332 }
1333 ✗ return false;
1334 }
1335
1336 #if defined(GATE_SYS_DARWIN)
1337 static gate_result_t gate_os_get_physical_memory_darwin(gate_uint64_t* ptr_total, gate_uint64_t* ptr_available)
1338 {
1339 uint64_t usable = 0;
1340 uint64_t memsize = 0;
1341 size_t len = sizeof(memsize);
1342
1343 if (sysctlbyname("hw.memsize", &memsize, &len, NULL, 0) != 0)
1344 {
1345 return GATE_RESULT_NOTAVAILABLE;
1346 }
1347 else
1348 {
1349 mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
1350 vm_statistics64_data_t vm;
1351 kern_return_t kr = host_statistics64(
1352 mach_host_self(),
1353 HOST_VM_INFO64,
1354 (host_info64_t)&vm,
1355 &count
1356 );
1357
1358 if (kr == KERN_SUCCESS) {
1359 const uint64_t pageSize = vm_page_size;
1360 const uint64_t freeBytes = vm.free_count * pageSize;
1361 const uint64_t inactiveBytes = vm.inactive_count * pageSize;
1362 const uint64_t speculative = vm.speculative_count * pageSize;
1363 usable = freeBytes + inactiveBytes + speculative;
1364 }
1365 }
1366
1367 if (ptr_total) *ptr_total = memsize;
1368 if (ptr_available) *ptr_available = usable;
1369 return GATE_RESULT_OK;
1370 }
1371
1372 #endif /* defined(GATE_SYS_DARWIN) */
1373
1374
1375 1 static gate_result_t gate_os_get_physical_memory_generic(gate_uint64_t* ptr_total, gate_uint64_t* ptr_available)
1376 {
1377 1 gate_result_t ret = GATE_RESULT_OK;
1378 char buffer[8192];
1379 1 gate_size_t buffer_len = sizeof(buffer);
1380 1 gate_uint64_t total = 0;
1381 1 gate_uint64_t available = 0;
1382 1 gate_uint64_t pgsize
1383 #if defined(_SC_PAGESIZE)
1384 1 = (gate_uint64_t)sysconf(_SC_PAGESIZE);
1385 #elif defined(_SC_PAGE_SIZE)
1386 = (gate_uint64_t)sysconf(_SC_PAGE_SIZE);
1387 #else
1388 = 4096;
1389 #endif
1390
1391 #if defined(_SC_PHYS_PAGES)
1392 1 long phys = sysconf(_SC_PHYS_PAGES);
1393 1 total = (gate_uint64_t)(phys) * (gate_uint64_t)(pgsize);
1394 #elif defined(_SC_AIX_REALMEM)
1395 total = (gate_uint64_t)(sysconf(_SC_AIX_REALMEM)) * 1024;
1396 #endif
1397
1398 #if defined(_SC_AVPHYS_PAGES)
1399 1 available = (gate_uint64_t)(sysconf(_SC_AVPHYS_PAGES)) * pgsize;
1400 #endif
1401
1402
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if ((total == 0) || (available == 0))
1403 {
1404 ✗ ret = gate_file_get_content_buffer(&filepath_proc_meminfo, buffer, &buffer_len);
1405 ✗ if (GATE_SUCCEEDED(ret))
1406 {
1407 ✗ if (total)
1408 {
1409 ✗ gate_os_parse_meminfo_num(buffer, buffer_len, "MemTotal:", 9, &total);
1410 ✗ total *= 1024;
1411 }
1412 ✗ if (available)
1413 {
1414 ✗ gate_os_parse_meminfo_num(buffer, buffer_len, "MemFree:", 8, &available);
1415 ✗ available *= 1024;
1416 }
1417 }
1418 }
1419
1420
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_total)
1421 {
1422 1 *ptr_total = total;
1423 }
1424
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_available)
1425 {
1426 1 *ptr_available = available;
1427 }
1428 1 return ret;
1429 }
1430
1431 1 gate_result_t gate_os_get_physical_memory(gate_uint64_t* ptr_total, gate_uint64_t* ptr_available)
1432 {
1433 #if defined(GATE_SYS_DARWIN)
1434 return gate_os_get_physical_memory_darwin(ptr_total, ptr_available);
1435 #else
1436 1 return gate_os_get_physical_memory_generic(ptr_total, ptr_available);
1437 #endif
1438 }
1439
1440 #if defined(GATE_SYS_DARWIN)
1441
1442 gate_result_t gate_os_get_virtual_memory(gate_uint64_t* ptr_total, gate_uint64_t* ptr_available)
1443 {
1444 uint64_t usermem;
1445 size_t len = sizeof(usermem);
1446
1447 /* TODO: this is a dummy implementation, it does not return the desired values */
1448
1449 if (sysctlbyname("hw.usermem", &usermem, &len, NULL, 0) == -1)
1450 {
1451 return GATE_RESULT_FAILED;
1452 }
1453 else
1454 {
1455 if (ptr_total) *ptr_total = usermem;
1456 if (ptr_available) *ptr_available = 0;
1457 return GATE_RESULT_OK;
1458 }
1459 }
1460
1461 #else
1462
1463 1 gate_result_t gate_os_get_virtual_memory(gate_uint64_t* ptr_total, gate_uint64_t* ptr_available)
1464 {
1465 1 gate_result_t ret = GATE_RESULT_NOTSUPPORTED;
1466 char buffer[8192];
1467 1 gate_uint64_t vmtotal = 0;
1468 1 gate_uint64_t vmused = 0;
1469 1 gate_size_t buffer_len = sizeof(buffer);
1470 1 ret = gate_file_get_content_buffer(&filepath_proc_meminfo, buffer, &buffer_len);
1471
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (GATE_SUCCEEDED(ret))
1472 {
1473 1 gate_os_parse_meminfo_num(buffer, buffer_len, "VmallocTotal:", 13, &vmtotal);
1474 1 gate_os_parse_meminfo_num(buffer, buffer_len, "VmallocUsed:", 12, &vmused);
1475
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_total)
1476 {
1477 1 *ptr_total = vmtotal * 1024;
1478 }
1479
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ptr_available)
1480 {
1481 1 *ptr_available = (vmtotal - vmused) * 1024;
1482 }
1483 }
1484 1 return ret;
1485 }
1486
1487 #endif /* GATE_SYS_DARWIN */
1488
1489
1490 #endif
1491