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
|
// Copyright 2018 Mimecast Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef UTILS_H
#define UTILS_H
// For asprintf in stdio.h
#define _GNU_SOURCE
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <linux/types.h>
#include <linux/unistd.h>
#include <pthread.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "../macros.h"
#include "../defaults.h"
/**
* @brief Check whether allocated memory is not NULL
*
* This function is used in conjunction with malloc() and co. It
* introduces an extra sanity check whether the memory could be
* allocated successfully or not. If not it will print out a error
* message stating the position in the source code where the memory
* allocation failed!
*
* @param p The pointer being checked
* @param file The source file name the memory was allocated in
* @param line The source line number the memory was allocated at
* @param count The amount of memory being allocated
* @return The pointer to the allocated memory
*/
void* notnull(void *p, char *file, int line, int count);
/**
* @brief Check whether opened file handle is not NULL
*
* This function is used in conjunction with fopen(). It
* introduces an extra sanity check whether the file could be
* opened successfully or not. If not it will print out a error
* message stating the position in the source code where the open
* failed!
*
* @param fd The fd stream to be checked.
* @param path The file path opened
* @param file The source file name
* @param line The source line number
* @return The pointer to the allocated memory
*/
FILE* fnotnull(FILE *fd, const char *path, char *file, int line);
/**
* @brief Check whether allocated memory via mmap is not null
*
* This function is used in conjunction with mmap() and co. It
* introduces an extra sanity check whether the memory could be
* allocated successfully or not. If not it will print out a error
* message stating the position in the source code where the memory
* allocation failed!
*
* @param addr The pointer being checked
* @param file The source file name the memory was allocated in
* @param line The source line number the memory was allocated at
* @return The pointer to the allocated memory
*/
void* mmapok(void *addr, char *file, int line);
/**
* @brief A version of strtok_r supporting multi char delims
*
* @param str The input string
* @param delim The multi-char delimiter
* @param saveptr A temp storage location
* @return The next match if != NULL
*/
char* strtok2_r(char *str, char *delim, char **saveptr);
/**
* @brief Replaces a character with another one in a string
*
* @param str The input string
* @param replace The character to be replaced
* @param with The character to replace with
*/
void chreplace(char *str, char replace, char with);
/**
* @brief Removes quotes from a string
*
* @param str The input sting
*/
void strunquote(char *str);
/**
* @brief Drop root privileges
*
* @param user The user to switch to
*/
void drop_root(const char *user);
/**
* @brief Retrieve current 1 min Linux load average
*
* @param readbuf The buffer to store the load average as a string
*/
void get_loadavg_s(char *readbuf);
/**
* @brief Retrieve current 1 min Linux load average
*
* This function is not thread safe!
*
* @return The 1 minute load average of the system
*/
double get_loadavg();
/**
* @brief Check whether a string represents a number
*
* @param str The input string
* @return true if all characters of the input string are a digits
*/
bool is_number(char *str);
/**
* @brief Wrapper around pthread_create
*
* The wrapper also checks whether the thread has been created successfully
* or not! It will exit the process if not.
*
* @param thread The POSIX thread variable
* @param cb The threadss start callback routine
* @param data A data pointer passed to the thread.
*/
void start_pthread(pthread_t *thread, void*(*cb)(void*), void *data);
#endif // UTILS_H
|