-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlinux_utils.h
297 lines (247 loc) · 7 KB
/
linux_utils.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
/**
* YALAS is a Linux audit system based on SystemTap
* Copyright (C) <2017> Arkady Miasnikov
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Functions and code chunks which do not fit anywhere else and loosely related to the Linux API
*/
#pragma once
#include <stdint.h>
#include <pthread.h>
#include <errno.h>
#include <semaphore.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#if !__KERNEL__
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint8_t u8;
#endif
/**
* !!(X) forces the value of X to be 0 OR 1 and nothing else without any
* performance hit
*/
#ifndef likely
# define likely(x) __builtin_expect(!!(x), 1)
#endif
#ifndef unlikely
# define unlikely(x) __builtin_expect(!!(x), 0)
#endif
enum linux_log_type {
LINUX_LOG_INFO_EXT,
LINUX_LOG_INFO,
LINUX_LOG_WARNING,
LINUX_LOG_ERROR,
LINUX_LOG_LAST,
};
extern int linux_fork(void);
extern void linux_redirect_stdio(void);
extern void linux_log(enum linux_log_type log_flags, const char *fmt, ...);
extern void linux_log_use_syselog(void);
#define LINUX_LOG_LINE() linux_log(LINUX_LOG_ERROR, "%d", __LINE__)
typedef int (*linux_task_t)(void*);
typedef struct {
/**
* Name of the task
*/
const char *name;
/**
* Original task pointer
*/
linux_task_t task;
/**
* Argument to forward to the task
*/
void * task_arg;
} linux_task_properties_t;
typedef struct {
/**
* Set to non-zero to force exit
*/
volatile int exit_flag;
/**
* reference for created pthread
*/
pthread_t thread;
} linux_task_runtime_state_t;
typedef struct {
/**
* This field is set by application
*/
linux_task_properties_t properties;
/**
* Used by task manager
*/
linux_task_runtime_state_t runtime;
} linux_task_state_t;
/**
* Call task in loop forever or until return value is not 1
* @return 1 if Ok
*/
extern int linux_thread_start(linux_task_state_t *state);
/**
* Start all tasks in the list
* End of list is name = NULL
* @return 1 if Ok
*/
extern int linux_thread_start_all(linux_task_state_t *state);
/**
* Wait for task termination
* @return 1 if Ok
*/
extern int linux_thread_join(const linux_task_state_t *state);
extern int linux_thread_yield(void);
/**
* Wait for all tasks to complete
* End of list is name = NULL
* @return 1 if Ok
*/
extern int linux_thread_join_all(linux_task_state_t *state);
/**
* Break the task loop
* @return 1 if Ok
*/
extern int linux_thread_exit(linux_task_state_t *state);
/**
* Stop all tasks in the list
*/
extern int linux_thread_exit_all(linux_task_state_t *state);
/**
* Break the task loop
*/
extern int linux_thread_force_exit(linux_task_state_t *state);
static inline uint32_t linux_inc_index(uint32_t index, uint32_t max_index) {
(index)++;
if (index > max_index) {
index = 0;
}
return index;
}
extern void linux_ms_sleep(unsigned long ms);
extern void linux_micro_sleep(unsigned long micro);
typedef struct {
const char *name;
pthread_mutex_t fd_mutex;
pthread_mutexattr_t fd_mutex_attr;
} linux_mutex_t;
int linux_mutex_init_all(linux_mutex_t *mutex);
int linux_mutex_init(linux_mutex_t *mutex);
/**
* There is a potential to implement something similar to "Benaphore"
* and save CPU cycles in the non-congested systems in some Unix systems
* See http://www.mr-edd.co.uk/blog/sad_state_of_osx_pthread_mutex_t
*/
static inline int linux_mutex_lock(linux_mutex_t *mutex) {
int err;
err = pthread_mutex_lock(&mutex->fd_mutex);
if (err != 0) {
linux_log(LINUX_LOG_ERROR, "Failed to lock mutex %s: %s %d",
mutex->name, strerror(errno), errno);
return 0;
}
return 1;
}
static inline int linux_mutex_unlock(linux_mutex_t *mutex) {
int err;
err = pthread_mutex_unlock(&mutex->fd_mutex);
if (err != 0) {
linux_log(LINUX_LOG_ERROR, "Failed to unlock mutex %s: %s %d",
mutex->name, strerror(errno), errno);
return 0;
}
return 1;
}
typedef struct {
const char *name;
sem_t semaphore;
} linux_semaphore_t;
int linux_semaphore_init(linux_semaphore_t *semaphore);
static inline int linux_semaphore_wait(linux_semaphore_t *semaphore) {
int res = sem_wait(&semaphore->semaphore);
return (res == 0);
}
static inline int linux_semaphore_wait(linux_semaphore_t *semaphore,
const size_t timeout) {
if (timeout > 0) {
#if __gnu_linux__
struct timespec abs_timeout;
int res = clock_gettime(CLOCK_REALTIME_COARSE, &abs_timeout);
if (res < 0)
{
linux_log(LINUX_LOG_ERROR, "clock_gettime failed for %s(%p), timeout=%lu, errno=%s(%d)", semaphore->name, semaphore, timeout, strerror(errno), errno);
}
static const uint64_t NANOS = 1000*1000*1000;
static const uint64_t MILLIS = 1000*1000;
uint64_t nsec = abs_timeout.tv_nsec + (uint64_t)timeout*MILLIS;
abs_timeout.tv_nsec = nsec % NANOS;
abs_timeout.tv_sec += nsec / NANOS;
res = sem_timedwait(&semaphore->semaphore, &abs_timeout);
if ((res == -1) && (errno != ETIMEDOUT))
{
linux_log(LINUX_LOG_ERROR, "sem_timedwait failed for %s(%p), timeout=%lu, errno=%s(%d)", semaphore->name, semaphore, timeout, strerror(errno), errno);
}
else
{
//linux_log(LINUX_LOG_INFO, "sem_timedwait for %s(%p), timeout=%lu, res=%d, errno=%s(%d)", semaphore->name, semaphore, timeout, res, strerror(errno), errno);
}
#else // Patch for compilation in MacOS
// In MacOS/BSD use dispatch_semaphore_XX()
//#warning Patch for non Linux operating systems
int res = sem_trywait(&semaphore->semaphore);
#endif
return (res == 0);
}
if (timeout == 0) {
int res = sem_trywait(&semaphore->semaphore);
return (res == 0);
}
return 0;
}
static inline int linux_semaphore_post(linux_semaphore_t *semaphore) {
#if __gnu_linux__
int res = sem_post(&semaphore->semaphore);
if (res == -1)
linux_log(LINUX_LOG_ERROR, "sem_timedwait failed for %s(%p), errno=%s(%d)", semaphore->name, semaphore, strerror(errno), errno);
return (res != 0);
#else
return 1;
#endif
}
extern int linux_file_exists(const char *path);
extern uint64_t linux_time_seconds();
extern uint64_t linux_time_ms();
class MeasureTime {
public:
MeasureTime() {
entry = linux_time_ms();
}
uint64_t getEntry() {
return entry;
}
uint64_t diff() {
return (linux_time_ms() - entry);
}
uint64_t current() {
return linux_time_ms();
}
protected:
uint64_t entry;
};
typedef bool (*linux_scan_folder_test_t)(int index, const char *name);
typedef void (*linux_scan_folder_process_t)(int index, const char *name);
int linux_scan_folder(const char *folder, linux_scan_folder_test_t test, linux_scan_folder_process_t process);
int linux_set_priority();