This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
91 lines (76 loc) · 1.61 KB
/
utils.c
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
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include "utils.h"
void fail(const char *format, ...)
{
va_list vas;
va_start(vas, format);
vfprintf(stderr, format, vas);
va_end(vas);
fprintf(stderr, "\n");
exit(EXIT_FAILURE);
}
static void log_base(const char *prefix, const char *format, va_list vas)
{
fputs(prefix, stderr);
fputc(' ', stderr);
vfprintf(stderr, format, vas);
if (format[strlen(format) - 1] != '\n') {
// This is for libvncserver
fputc('\n', stderr);
}
fputs("\x1b[0m", stderr);
}
void log_info(const char *format, ...)
{
va_list vas;
va_start(vas, format);
log_base("\x1b[1m\x1b[94m[I]", format, vas);
va_end(vas);
}
void log_error(const char *format, ...)
{
va_list vas;
va_start(vas, format);
log_base("\x1b[1m\x1b[91m[E]", format, vas);
va_end(vas);
}
void *xmalloc(size_t size)
{
void *ptr = malloc(size);
if (ptr == NULL) {
fail("Memory allocation failed");
}
memset(ptr, 0, size);
return ptr;
}
uint64_t time_monotonic()
{
struct timespec time;
clock_gettime(CLOCK_MONOTONIC_RAW, &time);
return time.tv_sec * 1000000 + time.tv_nsec / 1000;
}
int shm_create()
{
const char *filename_format = "/wvnc-%d";
char filename[sizeof(filename_format) + 10];
int fd = -1;
for (int i = 0; i < 10000; i++) {
snprintf(filename, sizeof(filename), filename_format, i);
fd = shm_open(filename, O_RDWR | O_EXCL | O_CREAT | O_TRUNC, 0660);
if (fd >= 0) {
// Just the fd matters now
shm_unlink(filename);
break;
}
}
if (fd < 0) {
fail("Failed to open SHM file");
}
return fd;
}