diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b83dc8a7..f4f6fb9c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -131,8 +131,6 @@ set(sources log.h main.c main.h - memory.c - memory.h overTime.c overTime.h regex.c @@ -174,6 +172,7 @@ add_executable(pihole-FTL $ $ $ + $ ) if(STATIC STREQUAL "true") set_target_properties(pihole-FTL PROPERTIES LINK_SEARCH_START_STATIC ON) @@ -228,3 +227,4 @@ add_subdirectory(database) add_subdirectory(dnsmasq) add_subdirectory(lua) add_subdirectory(tre-regex) +add_subdirectory(syscalls) diff --git a/src/FTL.h b/src/FTL.h index cf645050..4b659f85 100644 --- a/src/FTL.h +++ b/src/FTL.h @@ -114,16 +114,35 @@ // Important: This number has to be smaller than 256 for this mechanism to work #define NUM_RECHECKS 3 -// Use out own memory handling functions that will detect possible errors +// Use out own syscalls handling functions that will detect possible errors // and report accordingly in the log. This will make debugging FTL crashs // caused by insufficient memory or by code bugs (not properly dealing // with NULL pointers) much easier. +#undef strdup // strdup() is a macro in itself, it needs special handling #define free(ptr) FTLfree(ptr, __FILE__, __FUNCTION__, __LINE__) -#define lib_strdup() strdup() -#undef strdup #define strdup(str_in) FTLstrdup(str_in, __FILE__, __FUNCTION__, __LINE__) #define calloc(numer_of_elements, element_size) FTLcalloc(numer_of_elements, element_size, __FILE__, __FUNCTION__, __LINE__) #define realloc(ptr, new_size) FTLrealloc(ptr, new_size, __FILE__, __FUNCTION__, __LINE__) +#define printf(format, ...) FTLfprintf(stdout, __FILE__, __FUNCTION__, __LINE__, format, ##__VA_ARGS__) +#define fprintf(stream, format, ...) FTLfprintf(stream, __FILE__, __FUNCTION__, __LINE__, format, ##__VA_ARGS__) +#define vprintf(format, args) FTLvfprintf(stdout, __FILE__, __FUNCTION__, __LINE__, format, args) +#define vfprintf(stream, format, args) FTLvfprintf(stream, __FILE__, __FUNCTION__, __LINE__, format, args) +#define sprintf(buffer, format, ...) FTLsprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, ##__VA_ARGS__) +#define vsprintf(buffer, format, args) FTLvsprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, args) +#define asprintf(buffer, format, ...) FTLasprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, ##__VA_ARGS__) +#define vasprintf(buffer, format, args) FTLvasprintf(__FILE__, __FUNCTION__, __LINE__, buffer, format, args) +#define snprintf(buffer, maxlen, format, ...) FTLsnprintf(__FILE__, __FUNCTION__, __LINE__, buffer, maxlen, format, ##__VA_ARGS__) +#define vsnprintf(buffer, maxlen, format, args) FTLvsnprintf(__FILE__, __FUNCTION__, __LINE__, buffer, maxlen, format, args) +#define write(fd, buf, n) FTLwrite(fd, buf, n, __FILE__, __FUNCTION__, __LINE__) +#define accept(sockfd, addr, addrlen) FTLaccept(sockfd, addr, addrlen, __FILE__, __FUNCTION__, __LINE__) +#define recv(sockfd, buf, len, flags) FTLrecv(sockfd, buf, len, flags, __FILE__, __FUNCTION__, __LINE__) +#define recvfrom(sockfd, buf, len, flags, src_addr, addrlen) FTLrecvfrom(sockfd, buf, len, flags, src_addr, addrlen, __FILE__, __FUNCTION__, __LINE__) +#define sendto(sockfd, buf, len, flags, dest_addr, addrlen) FTLsendto(sockfd, buf, len, flags, dest_addr, addrlen, __FILE__, __FUNCTION__, __LINE__) +#define select(nfds, readfds, writefds, exceptfds, timeout) FTLselect(nfds, readfds, writefds, exceptfds, timeout, __FILE__, __FUNCTION__, __LINE__) +#define pthread_mutex_lock(mutex) FTLpthread_mutex_lock(mutex, __FILE__, __FUNCTION__, __LINE__) +#define fopen(pathname, mode) FTLfopen(pathname, mode, __FILE__, __FUNCTION__, __LINE__) +#define ftlallocate(fd, offset, len) FTLfallocate(fd, offset, len, __FILE__, __FUNCTION__, __LINE__) +#include "syscalls/syscalls.h" // Preprocessor help functions #define str(x) # x diff --git a/src/api/msgpack.c b/src/api/msgpack.c index eae1ff53..ede6065f 100644 --- a/src/api/msgpack.c +++ b/src/api/msgpack.c @@ -16,12 +16,12 @@ void pack_eom(const int sock) { // This byte is explicitly never used in the MessagePack spec, so it is perfect to use as an EOM for this API. uint8_t eom = 0xc1; - swrite(sock, &eom, sizeof(eom)); + write(sock, &eom, sizeof(eom)); } static void pack_basic(const int sock, const uint8_t format, const void *value, const size_t size) { - swrite(sock, &format, sizeof(format)); - swrite(sock, value, size); + write(sock, &format, sizeof(format)); + write(sock, value, size); } static uint64_t __attribute__((const)) leToBe64(const uint64_t value) { @@ -42,7 +42,7 @@ static uint64_t __attribute__((const)) leToBe64(const uint64_t value) { void pack_bool(const int sock, const bool value) { uint8_t packed = (uint8_t) (value ? 0xc3 : 0xc2); - swrite(sock, &packed, sizeof(packed)); + write(sock, &packed, sizeof(packed)); } void pack_uint8(const int sock, const uint8_t value) { @@ -87,8 +87,8 @@ bool pack_fixstr(const int sock, const char *string) { } const uint8_t format = (uint8_t) (0xA0 | length); - swrite(sock, &format, sizeof(format)); - swrite(sock, string, length); + write(sock, &format, sizeof(format)); + write(sock, string, length); return true; } @@ -104,17 +104,17 @@ bool pack_str32(const int sock, const char *string) { } const uint8_t format = 0xdb; - swrite(sock, &format, sizeof(format)); + write(sock, &format, sizeof(format)); const uint32_t bigELength = htonl((uint32_t) length); - swrite(sock, &bigELength, sizeof(bigELength)); - swrite(sock, string, length); + write(sock, &bigELength, sizeof(bigELength)); + write(sock, string, length); return true; } void pack_map16_start(const int sock, const uint16_t length) { const uint8_t format = 0xde; - swrite(sock, &format, sizeof(format)); + write(sock, &format, sizeof(format)); const uint16_t bigELength = htons(length); - swrite(sock, &bigELength, sizeof(bigELength)); + write(sock, &bigELength, sizeof(bigELength)); } diff --git a/src/api/socket.c b/src/api/socket.c index a5fb901d..eba21f26 100644 --- a/src/api/socket.c +++ b/src/api/socket.c @@ -10,13 +10,12 @@ #include "FTL.h" #include "api.h" -#include "log.h" +#include "../log.h" #include "socket.h" #include "request.h" -#include "config.h" -#include "memory.h" +#include "../config.h" // global variable killed -#include "signals.h" +#include "../signals.h" // The backlog argument defines the maximum length // to which the queue of pending connections for @@ -223,21 +222,15 @@ void __attribute__ ((format (gnu_printf, 2, 3))) ssend(const int sock, const cha char *buffer; va_list args; va_start(args, format); - int ret = vasprintf(&buffer, format, args); + int bytes = vasprintf(&buffer, format, args); va_end(args); - if(ret > 0) + if(bytes > 0 && buffer != NULL) { - if(!write(sock, buffer, strlen(buffer))) - logg("WARNING: Socket write returned error %s (%i)", strerror(errno), errno); + write(sock, buffer, bytes); free(buffer); } } -void swrite(const int sock, const void *value, size_t size) { - if(write(sock, value, size) == -1) - logg("WARNING: Socket write returned error code %i", errno); -} - static inline int checkClientLimit(const int socket) { if(socket < MAXCONNS) { @@ -519,8 +512,11 @@ void *socket_listening_thread(void *args) // Return early to avoid CPU spinning if Unix socket is not available sock_avail = bind_to_unix_socket(&socketfd); - if(sock_avail) + if(!sock_avail) + { + logg("INFO: Unix socket will not be available"); return NULL; + } // Listen as long as FTL is not killed while(!killed) diff --git a/src/api/socket.h b/src/api/socket.h index 630ba960..bfb7150f 100644 --- a/src/api/socket.h +++ b/src/api/socket.h @@ -15,7 +15,6 @@ void close_telnet_socket(void); void close_unix_socket(bool unlink_file); void seom(const int sock); void ssend(const int sock, const char *format, ...) __attribute__ ((format (gnu_printf, 2, 3))); -void swrite(const int sock, const void* value, const size_t size); void *telnet_listening_thread_IPv4(void *args); void *telnet_listening_thread_IPv6(void *args); void *socket_listening_thread(void *args); diff --git a/src/args.c b/src/args.c index 9d144311..82343a71 100644 --- a/src/args.c +++ b/src/args.c @@ -16,7 +16,6 @@ #include "FTL.h" #include "args.h" #include "version.h" -#include "memory.h" #include "main.h" #include "log.h" // global variable killed diff --git a/src/capabilities.c b/src/capabilities.c index 6478526c..013bba64 100644 --- a/src/capabilities.c +++ b/src/capabilities.c @@ -14,7 +14,6 @@ #undef __USE_XOPEN #include "FTL.h" #include "capabilities.h" -#include "memory.h" #include "config.h" #include "log.h" diff --git a/src/config.c b/src/config.c index 7d840524..64fdffa8 100644 --- a/src/config.c +++ b/src/config.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "config.h" -#include "memory.h" #include "setupVars.h" #include "log.h" // nice() diff --git a/src/daemon.c b/src/daemon.c index e9a29d93..2d72d20b 100644 --- a/src/daemon.c +++ b/src/daemon.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "daemon.h" -#include "memory.h" #include "config.h" #include "log.h" // sleepms() diff --git a/src/database/aliasclients.c b/src/database/aliasclients.c index 2097d0c7..023c402a 100644 --- a/src/database/aliasclients.c +++ b/src/database/aliasclients.c @@ -17,8 +17,6 @@ #include "../config.h" // logg() #include "../log.h" -// calloc() -#include "../memory.h" // getAliasclientIDfromIP() #include "network-table.h" diff --git a/src/database/common.c b/src/database/common.c index 2f50b1e6..2839255c 100644 --- a/src/database/common.c +++ b/src/database/common.c @@ -13,7 +13,6 @@ #include "network-table.h" #include "message-table.h" #include "../shmem.h" -#include "../memory.h" // struct config #include "../config.h" // logg() diff --git a/src/database/gravity-db.c b/src/database/gravity-db.c index 34d92620..c9acb0d5 100644 --- a/src/database/gravity-db.c +++ b/src/database/gravity-db.c @@ -30,6 +30,9 @@ // reset_aliasclient() #include "aliasclients.h" +// Definition of struct regex_data +#include "../regex_r.h" + // Prefix of interface names in the client table #define INTERFACE_SEP ":" diff --git a/src/database/gravity-db.h b/src/database/gravity-db.h index e4ce7bf0..c92f671b 100644 --- a/src/database/gravity-db.h +++ b/src/database/gravity-db.h @@ -10,12 +10,9 @@ #ifndef GRAVITY_H #define GRAVITY_H -// global variable counters -#include "memory.h" -// clients data structure -#include "datastructure.h" - -// Definition of struct regex_data +// clientsData +#include "../datastructure.h" +// regex_data #include "../regex_r.h" // Table indices diff --git a/src/database/network-table.c b/src/database/network-table.c index ef12136d..40c9f9ab 100644 --- a/src/database/network-table.c +++ b/src/database/network-table.c @@ -12,8 +12,6 @@ #include "network-table.h" #include "common.h" #include "../shmem.h" -// strdup() -#include "../memory.h" #include "../log.h" // timer_elapsed_msec() #include "../timers.h" diff --git a/src/database/query-table.c b/src/database/query-table.c index 3bd3ca93..6b04f9ed 100644 --- a/src/database/query-table.c +++ b/src/database/query-table.c @@ -25,8 +25,6 @@ #include "../config.h" // getstr() #include "../shmem.h" -// free() -#include "../memory.h" static bool saving_failed_before = false; diff --git a/src/datastructure.c b/src/datastructure.c index 317cfea0..03ff9efd 100644 --- a/src/datastructure.c +++ b/src/datastructure.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "datastructure.h" -#include "memory.h" #include "shmem.h" #include "log.h" // enum REGEX diff --git a/src/dnsmasq_interface.c b/src/dnsmasq_interface.c index c97b68d2..0801bd32 100644 --- a/src/dnsmasq_interface.c +++ b/src/dnsmasq_interface.c @@ -16,7 +16,6 @@ #include "dnsmasq_interface.h" #include "shmem.h" #include "overTime.h" -#include "memory.h" #include "database/common.h" #include "database/database-thread.h" #include "datastructure.h" @@ -1775,7 +1774,9 @@ void FTL_fork_and_bind_sockets(struct passwd *ent_pw) // option states to run as a different user/group (e.g. "nobody") if(getuid() == 0) { - if(ent_pw != NULL) + // Only print this and change ownership of shmem objects when + // we're actually dropping root (user/group my be set to root) + if(ent_pw != NULL && ent_pw->pw_uid != 0) { logg("INFO: FTL is going to drop from root to user %s (UID %d)", ent_pw->pw_name, (int)ent_pw->pw_uid); diff --git a/src/files.c b/src/files.c index 7f1d3d94..cdcbeb80 100644 --- a/src/files.c +++ b/src/files.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "files.h" -#include "memory.h" #include "config.h" #include "setupVars.h" #include "log.h" diff --git a/src/gc.c b/src/gc.c index 618fa2e1..efe0c9e7 100644 --- a/src/gc.c +++ b/src/gc.c @@ -16,8 +16,6 @@ #include "overTime.h" #include "database/common.h" #include "log.h" -// global variable counters -#include "memory.h" // global variable killed #include "signals.h" // data getter functions diff --git a/src/log.c b/src/log.c index 9cde7d37..51c171d3 100644 --- a/src/log.c +++ b/src/log.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "version.h" -#include "memory.h" // is_fork() #include "daemon.h" #include "config.h" @@ -28,6 +27,7 @@ static pthread_mutex_t lock; static FILE *logfile = NULL; +static bool FTL_log_ready = false; static bool print_log = true, print_stdout = true; void log_ctrl(bool plog, bool pstdout) @@ -42,27 +42,25 @@ static void close_FTL_log(void) fclose(logfile); } -void init_FTL_log(void) +void open_FTL_log(const bool init) { - if (pthread_mutex_init(&lock, NULL) != 0) + if(init) { - printf("FATAL: Log mutex init failed\n"); - // Return failure - exit(EXIT_FAILURE); - } -} + // Initialize logging mutex + if (pthread_mutex_init(&lock, NULL) != 0) + { + printf("FATAL: Log mutex init failed\n"); + // Return failure + exit(EXIT_FAILURE); + } -void open_FTL_log(const bool test) -{ - if(test) - { // Obtain log file location getLogFilePath(); } // Open the log file in append/create mode logfile = fopen(FTLfiles.log, "a+"); - if((logfile == NULL) && test){ + if((logfile == NULL) && init){ syslog(LOG_ERR, "Opening of FTL\'s log file failed!"); printf("FATAL: Opening of FTL log (%s) failed!\n",FTLfiles.log); printf(" Make sure it exists and is writeable by user %s\n", username); @@ -70,7 +68,10 @@ void open_FTL_log(const bool test) exit(EXIT_FAILURE); } - if(test) + // Set log as ready (we were able to open it) + FTL_log_ready = true; + + if(init) { close_FTL_log(); } @@ -141,7 +142,7 @@ void _FTL_log(const bool newline, const char *format, ...) printf("\n"); } - if(print_log) + if(print_log && FTL_log_ready) { // Open log file open_FTL_log(false); diff --git a/src/log.h b/src/log.h index c6cd0516..eb99cd52 100644 --- a/src/log.h +++ b/src/log.h @@ -14,7 +14,7 @@ #include void init_FTL_log(void); -void open_FTL_log(const bool test); +void open_FTL_log(const bool init); void log_counter_info(void); void format_memory_size(char * const prefix, unsigned long long int bytes, double * const formated); diff --git a/src/main.c b/src/main.c index 8b6864a1..aa68c320 100644 --- a/src/main.c +++ b/src/main.c @@ -39,12 +39,6 @@ int main (int argc, char* argv[]) // it if needed username = getUserName(); - // This only prepares the log file lock, we - // do not want to log already here (parsing - // args may bring up something we want to do - // separated from the log in foreground) - init_FTL_log(); - // Parse arguments // We run this also for no direct arguments // to have arg{c,v}_dnsmasq initialized diff --git a/src/memory.c b/src/memory.c deleted file mode 100644 index 81dae6be..00000000 --- a/src/memory.c +++ /dev/null @@ -1,95 +0,0 @@ -/* Pi-hole: A black hole for Internet advertisements -* (c) 2017 Pi-hole, LLC (https://pi-hole.net) -* Network-wide ad blocking via your own hardware. -* -* FTL Engine -* Global variable definitions and memory reallocation handling -* -* This file is copyright under the latest version of the EUPL. -* Please see LICENSE file for your rights under this license. */ - -#include "FTL.h" -#include "shmem.h" -#include "memory.h" -#include "log.h" - -// The special memory handling routines have to be the last ones in this source file -// as we restore the original definition of the strdup, free, calloc, and realloc -// functions in here, i.e. if anything extra would come below these lines, it would -// not be protected by our (error logging) functions! - -#undef strdup -char* __attribute__((malloc)) FTLstrdup(const char *src, const char * file, const char * function, const int line) -{ - // The FTLstrdup() function returns a pointer to a new string which is a - // duplicate of the string s. Memory for the new string is obtained with - // calloc(3), and can be freed with free(3). - if(src == NULL) - { - logg("WARN: Trying to copy a NULL string in %s() (%s:%i)", function, file, line); - return NULL; - } - const size_t len = strlen(src); - char *dest = calloc(len+1, sizeof(char)); - if(dest == NULL) - { - logg("FATAL: Memory allocation failed in %s() (%s:%i)", function, file, line); - return NULL; - } - // Use memcpy as memory areas cannot overlap - memcpy(dest, src, len); - dest[len] = '\0'; - - return dest; -} - -#undef calloc -void* __attribute__((malloc)) __attribute__((alloc_size(1,2))) FTLcalloc(const size_t nmemb, const size_t size, const char * file, const char * function, const int line) -{ - // The FTLcalloc() function allocates memory for an array of nmemb elements - // of size bytes each and returns a pointer to the allocated memory. The - // memory is set to zero. If nmemb or size is 0, then calloc() returns - // either NULL, or a unique pointer value that can later be successfully - // passed to free(). - void *ptr = calloc(nmemb, size); - if(ptr == NULL) - logg("FATAL: Memory allocation (%zu x %zu) failed in %s() (%s:%i)", - nmemb, size, function, file, line); - - return ptr; -} - -#undef realloc -void __attribute__((alloc_size(2))) *FTLrealloc(void *ptr_in, const size_t size, const char * file, const char * function, const int line) -{ - // The FTLrealloc() function changes the size of the memory block pointed to - // by ptr to size bytes. The contents will be unchanged in the range from - // the start of the region up to the minimum of the old and new sizes. If - // the new size is larger than the old size, the added memory will not be - // initialized. If ptr is NULL, then the call is equivalent to malloc(size), - // for all values of size; if size is equal to zero, and ptr is - // not NULL, then the call is equivalent to free(ptr). Unless ptr is - // NULL, it must have been returned by an earlier call to malloc(), cal‐ - // loc() or realloc(). If the area pointed to was moved, a free(ptr) is - // done. - void *ptr_out = realloc(ptr_in, size); - if(ptr_out == NULL) - logg("FATAL: Memory reallocation (%p -> %zu) failed in %s() (%s:%i)", - ptr_in, size, function, file, line); - - return ptr_out; -} - -#undef free -void FTLfree(void *ptr, const char * file, const char * function, const int line) -{ - // The free() function frees the memory space pointed to by ptr, which - // must have been returned by a previous call to malloc(), calloc(), or - // realloc(). Otherwise, or if free(ptr) has already been called before, - // undefined behavior occurs. If ptr is NULL, no operation is performed. - if(ptr == NULL) - logg("FATAL: Trying to free NULL pointer in %s() (%s:%i)", function, file, line); - - // We intentionally run free() nevertheless to see the crash in the debugger - free(ptr); -} diff --git a/src/memory.h b/src/memory.h deleted file mode 100644 index 72ea92b0..00000000 --- a/src/memory.h +++ /dev/null @@ -1,20 +0,0 @@ -/* Pi-hole: A black hole for Internet advertisements -* (c) 2019 Pi-hole, LLC (https://pi-hole.net) -* Network-wide ad blocking via your own hardware. -* -* FTL Engine -* Memory prototypes -* -* This file is copyright under the latest version of the EUPL. -* Please see LICENSE file for your rights under this license. */ -#ifndef MEMORY_H -#define MEMORY_H - -#include "enums.h" - -char *FTLstrdup(const char *src, const char *file, const char *function, const int line) __attribute__((malloc)); -void *FTLcalloc(size_t n, size_t size, const char *file, const char *function, const int line) __attribute__((malloc)) __attribute__((alloc_size(1,2))); -void *FTLrealloc(void *ptr_in, size_t size, const char *file, const char *function, const int line) __attribute__((alloc_size(2))); -void FTLfree(void *ptr, const char* file, const char *function, const int line); - -#endif //MEMORY_H diff --git a/src/overTime.c b/src/overTime.c index fab07b8e..0529658c 100644 --- a/src/overTime.c +++ b/src/overTime.c @@ -13,8 +13,6 @@ #include "shmem.h" #include "config.h" #include "log.h" -// global variable counters -#include "memory.h" // data getter functions #include "datastructure.h" diff --git a/src/regex.c b/src/regex.c index b5b22fee..7e08b517 100644 --- a/src/regex.c +++ b/src/regex.c @@ -11,7 +11,6 @@ #include "FTL.h" #include "regex_r.h" #include "timers.h" -#include "memory.h" #include "log.h" #include "config.h" // data getter functions diff --git a/src/resolve.c b/src/resolve.c index cd71996e..4719f94c 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -11,7 +11,6 @@ #include "FTL.h" #include "resolve.h" #include "shmem.h" -#include "memory.h" // struct config #include "config.h" // sleepms() diff --git a/src/setupVars.c b/src/setupVars.c index d018a797..34c00f32 100644 --- a/src/setupVars.c +++ b/src/setupVars.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "log.h" -#include "memory.h" #include "config.h" #include "setupVars.h" diff --git a/src/shmem.c b/src/shmem.c index 4d0b8362..23266a65 100644 --- a/src/shmem.c +++ b/src/shmem.c @@ -12,7 +12,6 @@ #include "shmem.h" #include "overTime.h" #include "log.h" -#include "memory.h" #include "config.h" // data getter functions #include "datastructure.h" @@ -510,10 +509,10 @@ SharedMemory create_shm(const char *name, const size_t size, bool create_new) } // Allocate shared memory object to specified size - // Using fallocate() will ensure that there's actually space for + // Using f[tl]allocate() will ensure that there's actually space for // this file. Otherwise we end up with a sparse file that can give // SIGBUS if we run out of space while writing to it. - const int ret = fallocate(fd, 0, 0U, size); + const int ret = ftlallocate(fd, 0U, size); if(ret != 0) { logg("FATAL: create_shm(): Failed to resize \"%s\" (%i) to %zu: %s (%i)", @@ -634,10 +633,10 @@ bool realloc_shm(SharedMemory *sharedMemory, const size_t size1, const size_t si } // Allocate shared memory object to specified size - // Using fallocate() will ensure that there's actually space for + // Using f[tl]allocate() will ensure that there's actually space for // this file. Otherwise we end up with a sparse file that can give // SIGBUS if we run out of space while writing to it. - const int ret = fallocate(fd, 0, 0U, size); + const int ret = ftlallocate(fd, 0U, size); if(ret != 0) { logg("FATAL: realloc_shm(): Failed to resize \"%s\" (%i) to %zu: %s (%i)", @@ -646,7 +645,7 @@ bool realloc_shm(SharedMemory *sharedMemory, const size_t size1, const size_t si } // Close shared memory object file descriptor as it is no longer - // needed after having called fallocate() + // needed after having called f[tl]allocate() close(fd); // Update shm counters to indicate that at least one shared memory object changed diff --git a/src/signals.c b/src/signals.c index b5ec061d..ebab301c 100644 --- a/src/signals.c +++ b/src/signals.c @@ -15,8 +15,6 @@ #include "signals.h" // logg() #include "log.h" -// free() -#include "memory.h" // ls_dir() #include "files.h" // gettid() @@ -247,10 +245,17 @@ static void __attribute__((noreturn)) signal_handler(int sig, siginfo_t *si, voi } static void SIGRT_handler(int signum, siginfo_t *si, void *unused) -{ +{ + // Backup errno + const int _errno = errno; + // Ignore real-time signals outside of the main process (TCP forks) if(mpid != getpid()) + { + // Restore errno before returning + errno = _errno; return; + } int rtsig = signum - SIGRTMIN; logg("Received: %s (%d -> %d)", strsignal(signum), signum, rtsig); @@ -290,6 +295,9 @@ static void SIGRT_handler(int signum, siginfo_t *si, void *unused) // Parse neighbor cache set_event(PARSE_NEIGHBOR_CACHE); } + + // Restore errno before returning back to previous context + errno = _errno; } // Register SIGSEGV handler diff --git a/src/syscalls/CMakeLists.txt b/src/syscalls/CMakeLists.txt new file mode 100644 index 00000000..8582ac51 --- /dev/null +++ b/src/syscalls/CMakeLists.txt @@ -0,0 +1,37 @@ +# Pi-hole: A black hole for Internet advertisements +# (c) 2020 Pi-hole, LLC (https://pi-hole.net) +# Network-wide ad blocking via your own hardware. +# +# FTL Engine +# /src/syscalls/CMakeList.txt +# +# This file is copyright under the latest version of the EUPL. +# Please see LICENSE file for your rights under this license. + +set(sources + accept.c + asprintf.c + calloc.c + ftlallocate.c + fopen.c + fprintf.c + free.c + pthread_mutex_lock.c + realloc.c + recv.c + recvfrom.c + select.c + sendto.c + snprintf.c + sprintf.c + strdup.c + syscalls.h + vasprintf.c + vfprintf.c + vsnprintf.c + vsprintf.c + write.c + ) + +add_library(syscalls OBJECT ${sources}) +target_compile_options(syscalls PRIVATE ${EXTRAWARN}) diff --git a/src/syscalls/accept.c b/src/syscalls/accept.c new file mode 100644 index 00000000..5b9cdd94 --- /dev/null +++ b/src/syscalls/accept.c @@ -0,0 +1,36 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for accept +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#undef accept +int FTLaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen, const char *file, const char *func, const int line) +{ + int ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = accept(sockfd, addr, addrlen); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not accept() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/asprintf.c b/src/syscalls/asprintf.c new file mode 100644 index 00000000..4da1ea82 --- /dev/null +++ b/src/syscalls/asprintf.c @@ -0,0 +1,23 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for asprintf +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +int FTLasprintf(const char *file, const char *func, const int line, char **buffer, const char *format, ...) +{ + va_list args; + va_start(args, format); + const int length = FTLvasprintf(file, func, line, buffer, format, args); + va_end(args); + + return length; +} diff --git a/src/syscalls/calloc.c b/src/syscalls/calloc.c new file mode 100644 index 00000000..b79ad5fd --- /dev/null +++ b/src/syscalls/calloc.c @@ -0,0 +1,39 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for calloc +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#undef calloc +void* __attribute__((malloc)) __attribute__((alloc_size(1,2))) FTLcalloc(const size_t nmemb, const size_t size, const char *file, const char *func, const int line) +{ + // The FTLcalloc() func allocates memory for an array of nmemb elements + // of size bytes each and returns a pointer to the allocated memory. The + // memory is set to zero. If nmemb or size is 0, then calloc() returns + // either NULL, or a unique pointer value that can later be successfully + // passed to free(). + void *ptr = NULL; + do + { + errno = 0; + ptr = calloc(nmemb, size); + } + // Try again to allocate memory if this failed due to an interruption by + // an incoming signal + while(ptr == NULL && errno == EINTR); + + // Handle other errors than EINTR + if(ptr == NULL) + logg("FATAL: Memory allocation (%zu x %zu) failed in %s() (%s:%i)", + nmemb, size, func, file, line); + + return ptr; +} diff --git a/src/syscalls/fopen.c b/src/syscalls/fopen.c new file mode 100644 index 00000000..96f4bb07 --- /dev/null +++ b/src/syscalls/fopen.c @@ -0,0 +1,36 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for fopen +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#undef fopen +FILE *FTLfopen(const char *pathname, const char *mode, const char *file, const char *func, const int line) +{ + FILE *file_ptr = 0; + do + { + // Reset errno before trying to write + errno = 0; + file_ptr = fopen(pathname, mode); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(file_ptr == NULL && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(file_ptr == NULL) + logg("WARN: Could not fopen(\"%s\", \"%s\") in %s() (%s:%i): %s", + pathname, mode, func, file, line, strerror(errno)); + + return file_ptr; +} \ No newline at end of file diff --git a/src/syscalls/fprintf.c b/src/syscalls/fprintf.c new file mode 100644 index 00000000..be3bee68 --- /dev/null +++ b/src/syscalls/fprintf.c @@ -0,0 +1,23 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for fprintf +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +int FTLfprintf(FILE *stream, const char *file, const char *func, const int line, const char *format, ...) +{ + va_list args; + va_start(args, format); + const int length = FTLvfprintf(stream, file, func, line, format, args); + va_end(args); + + return length; +} diff --git a/src/syscalls/free.c b/src/syscalls/free.c new file mode 100644 index 00000000..d3a48ad9 --- /dev/null +++ b/src/syscalls/free.c @@ -0,0 +1,29 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for free +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#undef free +void FTLfree(void *ptr, const char *file, const char *func, const int line) +{ + // The free() function frees the memory space pointed to by ptr, which + // must have been returned by a previous call to malloc(), calloc(), or + // realloc(). Otherwise, or if free(ptr) has already been called before, + // undefined behavior occurs. If ptr is NULL, no operation is performed. + if(ptr == NULL) + { + logg("WARN: Trying to free NULL pointer in %s() (%s:%i)", func, file, line); + return; + } + + free(ptr); +} diff --git a/src/syscalls/ftlallocate.c b/src/syscalls/ftlallocate.c new file mode 100644 index 00000000..5095d296 --- /dev/null +++ b/src/syscalls/ftlallocate.c @@ -0,0 +1,37 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for fallocate +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" +#include + +// off_t is automatically set as off64_t when this is a 64bit system +int FTLfallocate(const int fd, const off_t offset, const off_t len, const char *file, const char *func, const int line) +{ + int ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = posix_fallocate(fd, offset, len); + } + // Try again if the last posix_fallocate() call failed due to an + // interruption by an incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not fallocate() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/pthread_mutex_lock.c b/src/syscalls/pthread_mutex_lock.c new file mode 100644 index 00000000..da741764 --- /dev/null +++ b/src/syscalls/pthread_mutex_lock.c @@ -0,0 +1,38 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for pthread_mutex_lock +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#include + +#undef pthread_mutex_lock +int FTLpthread_mutex_lock(pthread_mutex_t *__mutex, const char *file, const char *func, const int line) +{ + ssize_t ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = pthread_mutex_lock(__mutex); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not pthread_mutex_lock() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/realloc.c b/src/syscalls/realloc.c new file mode 100644 index 00000000..53df93b1 --- /dev/null +++ b/src/syscalls/realloc.c @@ -0,0 +1,43 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for realloc +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#undef realloc +void __attribute__((alloc_size(2))) *FTLrealloc(void *ptr_in, const size_t size, const char * file, const char * func, const int line) +{ + // The FTLrealloc() function changes the size of the memory block pointed to + // by ptr to size bytes. The contents will be unchanged in the range from + // the start of the region up to the minimum of the old and new sizes. If + // the new size is larger than the old size, the added memory will not be + // initialized. If ptr is NULL, then the call is equivalent to malloc(size), + // for all values of size; if size is equal to zero, and ptr is not NULL, + // then the call is equivalent to free(ptr). Unless ptr is NULL, it must + // have been returned by an earlier call to malloc(), calloc() or realloc(). + // If the area pointed to was moved, a free(ptr) is done implicitly. + void *ptr_out = NULL; + do + { + errno = 0; + ptr_out = realloc(ptr_in, size); + } + // Try again to allocate memory if this failed due to an interruption by + // an incoming signal + while(ptr_out == NULL && errno == EINTR); + + // Handle other errors than EINTR + if(ptr_out == NULL) + logg("FATAL: Memory reallocation (%p -> %zu) failed in %s() (%s:%i)", + ptr_in, size, func, file, line); + + return ptr_out; +} \ No newline at end of file diff --git a/src/syscalls/recv.c b/src/syscalls/recv.c new file mode 100644 index 00000000..84f2d4bd --- /dev/null +++ b/src/syscalls/recv.c @@ -0,0 +1,38 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for recv +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#include + +#undef recv +ssize_t FTLrecv(int sockfd, void *buf, size_t len, int flags, const char *file, const char *func, const int line) +{ + ssize_t ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = recv(sockfd, buf, len, flags); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not recv() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/recvfrom.c b/src/syscalls/recvfrom.c new file mode 100644 index 00000000..f28c9066 --- /dev/null +++ b/src/syscalls/recvfrom.c @@ -0,0 +1,39 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for recvfrom +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#include +#include + +#undef recvfrom +ssize_t FTLrecvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, const char *file, const char *func, const int line) +{ + ssize_t ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = recvfrom(sockfd, buf, len, flags, src_addr, addrlen); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not recvfrom() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/select.c b/src/syscalls/select.c new file mode 100644 index 00000000..5c26352d --- /dev/null +++ b/src/syscalls/select.c @@ -0,0 +1,38 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for select +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#include + +#undef select +int FTLselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout, const char *file, const char *func, const int line) +{ + int ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = select(nfds, readfds, writefds, exceptfds, timeout); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not select() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/sendto.c b/src/syscalls/sendto.c new file mode 100644 index 00000000..9c0e165e --- /dev/null +++ b/src/syscalls/sendto.c @@ -0,0 +1,39 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for sendto +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#include +#include + +#undef sendto +ssize_t FTLsendto(int sockfd, void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen, const char *file, const char *func, const int line) +{ + ssize_t ret = 0; + do + { + // Reset errno before trying to write + errno = 0; + ret = sendto(sockfd, buf, len, flags, dest_addr, addrlen); + } + // Try again if the last accept() call failed due to an interruption by an + // incoming signal + while(ret < 0 && errno == EINTR); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(ret < 0) + logg("WARN: Could not sendto() in %s() (%s:%i): %s", + func, file, line, strerror(errno)); + + return ret; +} \ No newline at end of file diff --git a/src/syscalls/snprintf.c b/src/syscalls/snprintf.c new file mode 100644 index 00000000..699d942c --- /dev/null +++ b/src/syscalls/snprintf.c @@ -0,0 +1,23 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for snprintf +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +int FTLsnprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const size_t maxlen, const char *format, ...) +{ + va_list args; + va_start(args, format); + const int length = FTLvsnprintf(file, func, line, buffer, maxlen, format, args); + va_end(args); + + return length; +} diff --git a/src/syscalls/sprintf.c b/src/syscalls/sprintf.c new file mode 100644 index 00000000..a6cc4094 --- /dev/null +++ b/src/syscalls/sprintf.c @@ -0,0 +1,23 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for sprintf +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +int FTLsprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const char *format, ...) +{ + va_list args; + va_start(args, format); + const int length = FTLvsprintf(file, func, line, buffer, format, args); + va_end(args); + + return length; +} diff --git a/src/syscalls/strdup.c b/src/syscalls/strdup.c new file mode 100644 index 00000000..cb3543de --- /dev/null +++ b/src/syscalls/strdup.c @@ -0,0 +1,38 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for strdup +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +char* __attribute__((malloc)) FTLstrdup(const char *src, const char *file, const char *func, const int line) +{ + // The FTLstrdup() function returns a pointer to a new string which is a + // duplicate of the string s. Memory for the new string is obtained with + // calloc(3), and can be freed with free(3). + if(src == NULL) + { + logg("WARN: Trying to copy a NULL string in %s() (%s:%i)", func, file, line); + return NULL; + } + const size_t len = strlen(src); + char *dest = FTLcalloc(len+1, sizeof(char), file, func, line); + + // Return early in case of an unrecoverable error, error reporting has + // already been done in FTLcalloc() + if(dest == NULL) + return NULL; + + // Use memcpy as memory areas cannot overlap + memcpy(dest, src, len); + dest[len] = '\0'; + + return dest; +} \ No newline at end of file diff --git a/src/syscalls/syscalls.h b/src/syscalls/syscalls.h new file mode 100644 index 00000000..1b2787cd --- /dev/null +++ b/src/syscalls/syscalls.h @@ -0,0 +1,53 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Syscall prototypes +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ +#ifndef SYSCALLS_H +#define SYSCALLS_H + +// Interrupt-safe memory routines +char *FTLstrdup(const char *src, const char *file, const char *func, const int line) __attribute__((malloc)); +void *FTLcalloc(size_t n, size_t size, const char *file, const char *func, const int line) __attribute__((malloc)) __attribute__((alloc_size(1,2))); +void *FTLrealloc(void *ptr_in, size_t size, const char *file, const char *func, const int line) __attribute__((alloc_size(2))); +void FTLfree(void *ptr, const char*file, const char *func, const int line); +int FTLfallocate(const int fd, const off_t offset, const off_t len, const char *file, const char *func, const int line); + + +// Interrupt-safe printing routines +// printf() is derived from fprintf(stdout, ...) +// vprintf() is derived from vfprintf(stdout, ...) +int FTLfprintf(FILE *stream, const char*file, const char *func, const int line, const char *format, ...) __attribute__ ((format (gnu_printf, 5, 6))); +int FTLvfprintf(FILE *stream, const char*file, const char *func, const int line, const char *format, va_list args) __attribute__ ((format (gnu_printf, 5, 0))); + +int FTLsprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const char *format, ...) __attribute__ ((format (gnu_printf, 5, 6))); +int FTLvsprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const char *format, va_list args) __attribute__ ((format (gnu_printf, 5, 0))); + +int FTLasprintf(const char *file, const char *func, const int line, char **buffer, const char *format, ...) __attribute__ ((format (gnu_printf, 5, 6))); +int FTLvasprintf(const char *file, const char *func, const int line, char **buffer, const char *format, va_list args) __attribute__ ((format (gnu_printf, 5, 0))); + +int FTLsnprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const size_t maxlen, const char *format, ...) __attribute__ ((format (gnu_printf, 6, 7))); +int FTLvsnprintf(const char *file, const char *func, const int line, char *__restrict__ buffer, const size_t maxlen, const char *format, va_list args) __attribute__ ((format (gnu_printf, 6, 0))); + +// Interrupt-safe socket routines +ssize_t FTLwrite(int fd, const void *buf, size_t total, const char *file, const char *func, const int line); +int FTLaccept(int sockfd, struct sockaddr *addr, socklen_t *addrlen, const char *file, const char *func, const int line); +ssize_t FTLrecv(int sockfd, void *buf, size_t len, int flags, const char *file, const char *func, const int line); +ssize_t FTLrecvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen, const char *file, const char *func, const int line); +int FTLselect(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout, const char *file, const char *func, const int line); +ssize_t FTLsendto(int sockfd, void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen, const char *file, const char *func, const int line); + +// Interrupt-safe thread routines +int FTLpthread_mutex_lock(pthread_mutex_t *__mutex, const char *file, const char *func, const int line); + +// Interrupt-safe file routines +FILE *FTLfopen(const char *pathname, const char *mode, const char *file, const char *func, const int line); + +// Syscall helpers +void syscalls_report_error(const char *error, FILE *stream, const int _errno, const char *format, const char *func, const char *file, const int line); + +#endif //SYSCALLS_H diff --git a/src/syscalls/vasprintf.c b/src/syscalls/vasprintf.c new file mode 100644 index 00000000..8300a8c5 --- /dev/null +++ b/src/syscalls/vasprintf.c @@ -0,0 +1,59 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for vasprintf +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +#undef vasprintf +int FTLvasprintf(const char *file, const char *func, const int line, char **buffer, const char *format, va_list args) +{ + // Sanity check + if(buffer == NULL) + { + syscalls_report_error("vasprintf() called with NULL buffer", + stdout, 0, format, func, file, line); + return 0; + } + // Print into dynamically allocated memory + int _errno, length = 0; + do + { + // The va_copy() macro copies the (previously initialized) variable + // argument list args to the local _args. The behavior is as if + // va_start() were applied to _args with the same last argument, + // followed by the same number of va_arg() invocations that was used to + // reach the current state of args. We do this to be able to reuse the + // arguments in args when we need to redo the string preparation + // procedure + va_list _args; + va_copy(_args, args); + // Reset errno before trying to get the string + errno = 0; + // Do the actual string transformation + length = vasprintf(buffer, format, _args); + // Copy errno into buffer before calling va_end() + _errno = errno; + va_end(_args); + } + // Try again to allocate memory if this failed due to an interruption by + // an incoming signal + while(length < 0 && _errno == EINTR); + + // Handle other errors than EINTR + if(length < 0) + { + syscalls_report_error("vasprintf() failed to print into buffer", + stdout, _errno, format, func, file, line); + } + + // Return number of written bytes + return length; +} diff --git a/src/syscalls/vfprintf.c b/src/syscalls/vfprintf.c new file mode 100644 index 00000000..f2e65353 --- /dev/null +++ b/src/syscalls/vfprintf.c @@ -0,0 +1,168 @@ +/* Pi-hole: A black hole for Internet advertisements +* (c) 2020 Pi-hole, LLC (https://pi-hole.net) +* Network-wide ad blocking via your own hardware. +* +* FTL Engine +* Pi-hole syscall implementation for vfprintf +* +* This file is copyright under the latest version of the EUPL. +* Please see LICENSE file for your rights under this license. */ + +#include "../FTL.h" +//#include "syscalls.h" is implicitly done in FTL.h +#include "../log.h" + +// itoa implementation using only static memory +// taken from Kernighan and Ritchie's "The C Programming Language" +// see https://clc-wiki.net/wiki/K&R2_solutions:Chapter_3:Exercise_4 +// This implementation has its drawbacks, however, we only use it for +// automated conversion of code line numbers to strings so we're not +// interested in its performance outside the range of [1, 10'000] +static void itoa(int n, char s[]) +{ + int i = 0, sign = n; + + // Make n positive if negative + if (sign < 0) + n = -n; + + // Generate digits in reverse order + do + { + s[i++] = n % 10 + '0'; /* get next digit */ + } while ((n /= 10) > 0); /* delete it */ + + // Add sign (if needed) + if (sign < 0) + s[i++] = '-'; + + // Rero-terminate string + s[i] = '\0'; + + // Reverse string s in place + int j; + char c; + int len = strlen(s); + for (i = 0, j = len-1; i 0) + written += ret; + } + // Try to write the remaining content into the stream if + // (a) we haven't written all the data, however, there was no other error + // (b) the last write() call failed due to an interruption by an incoming signal + while((written < total && errno == 0) || (ret < 0 && errno == EINTR)); + + // Final error checking (may have faild for some other reason then an + // EINTR = interrupted system call) + if(written < total) + logg("WARN: Could not write() everything in %s() [%s:%i]: %s", + func, file, line, strerror(errno)); + + return written; +} \ No newline at end of file diff --git a/src/timers.c b/src/timers.c index 56cfc125..691e16ba 100644 --- a/src/timers.c +++ b/src/timers.c @@ -10,7 +10,6 @@ #include "FTL.h" #include "timers.h" -#include "memory.h" #include "log.h" struct timespec t0[NUMTIMERS];