EDGESEC  0.1.0-alpha.0+sha.ca29a8277b72f80785649ea9ef9cd7edf642d939
Secure router - reference implementation
os.h
Go to the documentation of this file.
1 
11 #ifndef OS_H
12 #define OS_H
13 
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19 #include <sys/stat.h>
20 #include <sys/time.h> // required for `struct timeval`
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <utarray.h>
24 
25 #include "hashmap.h"
26 #include "log.h"
27 
28 /* Common costant definitions */
29 #define MAX_OS_PATH_LEN 4096
30 #define MAX_WEB_PATH_LEN 2048
31 #define MAX_RANDOM_UUID_LEN 37
32 
33 #define OS_HOST_NAME_MAX 64
34 
35 #ifndef BIT
36 #define BIT(x) (1U << (x))
37 #endif
38 
39 #ifndef ARRAY_SIZE
40 #define ARRAY_SIZE(s) (sizeof(s) / sizeof(s[0]))
41 #endif
42 
43 #define BD_NO_CHDIR 01 /* Don't chdir("/") */
44 #define BD_NO_CLOSE_FILES 02 /* Don't close all open files */
45 #define BD_NO_REOPEN_STD_FDS \
46  04 /* Don't reopen stdin, stdout, and \
47  stderr to /dev/null */
48 #define BD_NO_UMASK0 010 /* Don't do a umask(0) */
49 #define BD_MAX_CLOSE \
50  8192 /* Maximum file descriptors to close if \
51  sysconf(_SC_OPEN_MAX) is indeterminate */
52 
53 #ifndef os_strlen
54 // Used by code taken from hostapd
55 #define os_strlen(s) strlen((s))
56 #endif
57 
58 #ifndef os_snprintf
59 #define os_snprintf(s, maxlen, ...) snprintf((s), (maxlen), __VA_ARGS__)
60 #endif
61 
62 #ifndef os_strncmp
63 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
64 #endif
65 
66 #ifndef os_strstr
72 #define os_strstr(s1, s2) strstr((s1), (s2))
73 #endif
74 
75 #ifndef os_strcmp
76 #define os_strcmp(s1, s2) strcmp((s1), (s2))
77 #endif
78 
79 #ifndef os_strchr
85 #define os_strchr(s, c) strchr((s), (c))
86 #endif
87 
88 struct find_dir_type {
89  int proc_running;
90  const char *proc_name;
91 };
92 
93 typedef long os_time_t;
94 
95 struct os_time {
96  os_time_t sec;
98 };
99 
100 struct os_reltime {
101  os_time_t sec;
102  os_time_t usec;
103 };
104 
112 int become_daemon(int flags);
113 
114 #define os_get_time(t) edge_os_get_time(t)
115 
123 int edge_os_get_time(struct os_time *t);
124 
135 int edge_os_get_reltime(struct os_reltime *t);
136 
137 #ifndef os_get_reltime
138 #define os_get_reltime(t) edge_os_get_reltime((t))
139 #endif
140 
148 static inline int os_reltime_before(const struct os_reltime *a,
149  const struct os_reltime *b) {
150  return (a->sec < b->sec) || (a->sec == b->sec && a->usec < b->usec);
151 }
152 
160 static inline void os_reltime_sub(const struct os_reltime *a,
161  const struct os_reltime *b,
162  struct os_reltime *res) {
163  *res = (struct os_reltime){
164  .sec = a->sec - b->sec,
165  .usec = a->usec - b->usec,
166  };
167  if (res->usec < 0) {
168  res->sec--;
169  res->usec += 1000000;
170  }
171 }
172 
179 int os_get_timestamp(uint64_t *timestamp);
180 
187 void os_to_timestamp(struct timeval ts, uint64_t *timestamp);
188 
189 #define os_get_random(buf, len) edge_os_get_random((buf), (len))
190 
198 int edge_os_get_random(unsigned char *buf, size_t len);
199 
207 int os_get_random_int_range(int low, int up);
208 
213 void os_init_random_seed(void);
214 
222 int os_get_random_number_s(unsigned char *buf, size_t len);
223 
233 int8_t hex2num(char c);
234 
244 int edge_hexstr2bin(const char *hex, uint8_t *buf, size_t len);
245 
252 bool is_number(const char *ptr);
253 
254 #define os_strlcpy(dest, src, siz) edge_os_strlcpy((dest), (src), (siz))
255 
267 size_t edge_os_strlcpy(char *restrict dest, const char *restrict src,
268  size_t siz);
269 
278 size_t os_strnlen_s(const char *str, size_t max_len);
279 
280 #define os_memcmp_const(a, b, len) edge_os_memcmp_const((a), (b), (len))
281 
298 int edge_os_memcmp_const(const void *a, const void *b, size_t len);
299 
300 /*
301  * gcc 4.4 ends up generating strict-aliasing warnings about some very common
302  * networking socket uses that do not really result in a real problem and
303  * cannot be easily avoided with union-based type-punning due to struct
304  * definitions including another struct in system header files. To avoid having
305  * to fully disable strict-aliasing warnings, provide a mechanism to hide the
306  * typecast from aliasing for now. A cleaner solution will hopefully be found
307  * in the future to handle these cases.
308  */
309 void *__hide_aliasing_typecast(void *foo);
310 #define aliasing_hide_typecast(a, t) (t *)__hide_aliasing_typecast((a))
311 
319 typedef void (*process_callback_fn)(void *ctx, void *buf, size_t count);
320 
340 char **copy_argv(const char *const argv[]);
341 
351 int run_command(char *const argv[], char *const envp[], process_callback_fn fn,
352  void *ctx);
353 
364 int run_argv_command(const char *path, const char *const argv[],
365  process_callback_fn fn, void *ctx);
366 
374 void replace_string_char(char *s, char in, char out);
375 
376 typedef int (*split_string_fn)(const char *, size_t, void *);
377 
387 ssize_t split_string(const char *str, char sep, split_string_fn fun,
388  void *data);
389 
398 ssize_t split_string_array(const char *str, char sep, UT_array *arr);
399 
408 char *concat_paths(const char *path_left, const char *path_right);
409 
417 char *get_valid_path(const char *path);
418 
427 char *construct_path(const char *path_left, const char *path_right);
428 
437 char *get_secure_path(const UT_array *bin_path_arr, const char *filename,
438  bool real);
439 
440 typedef bool (*list_dir_fn)(char *, void *args);
441 
452 int list_dir(const char *dirpath, list_dir_fn fun, void *args);
453 
471 pid_t is_proc_app(const char *path, const char *proc_name);
472 
479 bool kill_process(char *proc_name);
480 
488 bool signal_process(const char *proc_name, int sig);
489 
503 int run_process(char *argv[], pid_t *child_pid);
504 
511 int is_proc_running(const char *name);
512 
519 int make_file_exec_fd(int fd);
520 
529 char *rtrim(char *str, const char *seps);
530 
538 char *string_array2string(const char *const strings[]);
539 
546 void generate_radom_uuid(char rid[static MAX_RANDOM_UUID_LEN]);
547 
555 bool find_dir_proc_fn(char *path, void *args);
556 
563 int exist_dir(const char *dirpath);
564 
587 int make_dirs_to_path(const char *file_path, mode_t mode);
588 
598 int create_dir(const char *dirpath, mode_t mode);
599 
606 int create_pipe_file(const char *path);
607 
615 int check_file_exists(const char *path, struct stat *sb);
616 
623 int check_sock_file_exists(const char *path);
624 
634 int get_hostname(char buf[static OS_HOST_NAME_MAX]);
635 
650 int create_pid_file(const char *pid_file, int flags);
651 
660 ssize_t read_file(const char *path, uint8_t **out);
661 
670 int read_file_string(const char *path, char **out);
671 
686 ssize_t open_write_nonblock(const char *path, int *fd, const uint8_t *buffer,
687  size_t length);
688 
698 int get_commands_paths(const char *commands[], const UT_array *bin_path_arr,
699  hmap_str_keychar **hmap_bin_paths);
700 
710 char *string_append_char(const char *str, char character);
711 #endif /* OS_H */
File containing the definition of the hashmap utilities.
File containing the implementation of the logging functions.
int edge_os_get_random(unsigned char *buf, size_t len)
Get cryptographically strong pseudo random data.
Definition: os.c:237
void generate_radom_uuid(char rid[static MAX_RANDOM_UUID_LEN])
Generates a random UUID string of MAX_RANDOM_UUID_LEN - 1 characters long not including '\0'.
Definition: os.c:1153
size_t edge_os_strlcpy(char *restrict dest, const char *restrict src, size_t siz)
Copy a string with size bound and NUL-termination.
Definition: os.c:210
int list_dir(const char *dirpath, list_dir_fn fun, void *args)
List the files in a directory.
Definition: os.c:773
int become_daemon(int flags)
Becomes a daemon.
Definition: os.c:44
char * construct_path(const char *path_left, const char *path_right)
Construct a valid path from two paths.
Definition: os.c:679
size_t os_strnlen_s(const char *str, size_t max_len)
Returns the size of string with a give max length.
Definition: os.c:1159
void(* process_callback_fn)(void *ctx, void *buf, size_t count)
Callback function for run_command() and similar functions.
Definition: os.h:317
int get_hostname(char buf[static OS_HOST_NAME_MAX])
Get the hostname of the running machine.
Definition: os.c:1293
int create_pipe_file(const char *path)
Creates a FIFO pipe file.
Definition: os.c:1247
int check_file_exists(const char *path, struct stat *sb)
Check if a file exists.
Definition: os.c:1266
char * rtrim(char *str, const char *seps)
Right trim the string.
Definition: os.c:1100
int run_argv_command(const char *path, const char *const argv[], process_callback_fn fn, void *ctx)
Executes a command with argument.
Definition: os.c:497
char * get_valid_path(const char *path)
Get the valid path string.
Definition: os.c:642
ssize_t split_string(const char *str, char sep, split_string_fn fun, void *data)
Splits a string into substrings (execute callback function)
Definition: os.c:561
int make_file_exec_fd(int fd)
Makes a file given by descriptor executable.
Definition: os.c:1079
bool is_number(const char *ptr)
Check if a string is a number.
Definition: os.c:118
int exist_dir(const char *dirpath)
Check if folder exists.
Definition: os.c:1178
void replace_string_char(char *s, char in, char out)
Replace a character in a string with a given characater.
Definition: os.c:1121
int read_file_string(const char *path, char **out)
Read the entire file into a string.
Definition: os.c:1448
void * __hide_aliasing_typecast(void *foo)
Definition: os.c:276
int edge_hexstr2bin(const char *hex, uint8_t *buf, size_t len)
Convert ASCII hex string into binary data.
Definition: os.c:196
int edge_os_memcmp_const(const void *a, const void *b, size_t len)
Constant time memory comparison.
Definition: os.c:225
void os_init_random_seed(void)
Initialises the random seed.
Definition: os.c:257
char * string_append_char(const char *str, char character)
Append a character to a string and return the new string.
Definition: os.c:1534
#define OS_HOST_NAME_MAX
Definition: os.h:33
ssize_t split_string_array(const char *str, char sep, UT_array *arr)
Splits a string into substrings (save to array)
Definition: os.c:598
int create_dir(const char *dirpath, mode_t mode)
Creates a folder recursively.
Definition: os.c:1222
int run_process(char *argv[], pid_t *child_pid)
Executes a background process with an array of string arguments.
Definition: os.c:1006
char * string_array2string(const char *const strings[])
Concatenates an array of strings into a single string.
Definition: os.c:980
bool kill_process(char *proc_name)
Kill a process by name.
Definition: os.c:976
char ** copy_argv(const char *const argv[])
Makes a copy of argv.
Definition: os.c:301
bool find_dir_proc_fn(char *path, void *args)
Callback function for list_dir function to check if process running.
Definition: os.c:1168
int os_get_timestamp(uint64_t *timestamp)
get the timestamp in microseconds from system time
Definition: os.c:1140
void os_to_timestamp(struct timeval ts, uint64_t *timestamp)
get the timestamp in microseconds from struct timeval
Definition: os.c:1133
char * concat_paths(const char *path_left, const char *path_right)
Concatenate two string paths.
Definition: os.c:607
int(* split_string_fn)(const char *, size_t, void *)
Definition: os.h:374
int check_sock_file_exists(const char *path)
Check if a socket file exists.
Definition: os.c:1279
int make_dirs_to_path(const char *file_path, mode_t mode)
Recurisvely create directories to the given path.
Definition: os.c:1197
int run_command(char *const argv[], char *const envp[], process_callback_fn fn, void *ctx)
Executes a command.
Definition: os.c:365
char * get_secure_path(const UT_array *bin_path_arr, const char *filename, bool real)
Get the secure path string of a binary.
Definition: os.c:714
int is_proc_running(const char *name)
Check if a process is running.
Definition: os.c:762
int edge_os_get_time(struct os_time *t)
Get current time (sec, usec) in seconds since UNIX epoch.
Definition: os.c:148
long os_time_t
Definition: os.h:91
ssize_t read_file(const char *path, uint8_t **out)
Read the entire file.
Definition: os.c:1396
ssize_t open_write_nonblock(const char *path, int *fd, const uint8_t *buffer, size_t length)
Opens a file for writing and write a a buffer in nonblocking mode.
Definition: os.c:1472
int os_get_random_number_s(unsigned char *buf, size_t len)
Get a random number string.
Definition: os.c:262
int get_commands_paths(const char *commands[], const UT_array *bin_path_arr, hmap_str_keychar **hmap_bin_paths)
Returns the absolute path of system binaries.
Definition: os.c:1500
pid_t is_proc_app(const char *path, const char *proc_name)
Check if the given process's basename matches proc_name.
Definition: os.c:875
#define MAX_RANDOM_UUID_LEN
Definition: os.h:31
int os_get_random_int_range(int low, int up)
Return a random int from a give range.
Definition: os.c:253
int edge_os_get_reltime(struct os_reltime *t)
Get relative time (sec, usec)
Definition: os.c:157
bool signal_process(const char *proc_name, int sig)
Signal a process.
Definition: os.c:947
bool(* list_dir_fn)(char *, void *args)
Definition: os.h:438
int create_pid_file(const char *pid_file, int flags)
Open/create the file named in 'pidFile', lock it, optionally set the close-on-exec flag for the file ...
Definition: os.c:1320
int8_t hex2num(char c)
Hex char to number.
Definition: os.c:138
Definition: os.h:86
int proc_running
Definition: os.h:87
const char * proc_name
Definition: os.h:88
keyd array hasmap structure definition
Definition: hashmap.h:23
Definition: os.h:98
os_time_t usec
Definition: os.h:100
os_time_t sec
Definition: os.h:99
Definition: os.h:93
os_time_t usec
Definition: os.h:95
os_time_t sec
Definition: os.h:94