From f42ddfb107187c97249bb74070949ba931265905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?William=20B=C3=A9rub=C3=A9?= Date: Thu, 29 Aug 2024 15:20:22 -0400 Subject: [PATCH] Cleaning up the code again --- src/Makefile | 2 +- src/hal/hisi/v1_common.h | 2 -- src/hal/hisi/v1_hal.h | 1 + src/hal/hisi/v2_common.h | 2 -- src/hal/hisi/v2_hal.h | 1 + src/hal/hisi/v3_common.h | 2 -- src/hal/hisi/v3_hal.h | 1 + src/hal/hisi/v4_common.h | 2 -- src/hal/hisi/v4_hal.h | 1 + src/hal/inge/t31_common.h | 2 -- src/hal/inge/t31_hal.h | 2 ++ src/hal/plus/cvi_common.h | 2 -- src/hal/plus/cvi_hal.h | 1 + src/hal/plus/gm_common.h | 2 -- src/hal/plus/rk_common.h | 2 -- src/hal/plus/rk_hal.h | 1 + src/hal/star/i3_common.h | 2 -- src/hal/star/i6_common.h | 2 -- src/hal/star/i6_hal.h | 2 ++ src/hal/star/i6c_common.h | 2 -- src/hal/star/i6c_hal.h | 2 ++ src/hal/star/i6f_common.h | 2 -- src/hal/star/i6f_hal.h | 2 ++ src/main.c | 37 ++++++++++++++++++++++++++++++++++--- src/server.c | 29 +++-------------------------- src/stack.c | 4 ++++ 26 files changed, 56 insertions(+), 54 deletions(-) diff --git a/src/Makefile b/src/Makefile index a2e6308..cc911f4 100644 --- a/src/Makefile +++ b/src/Makefile @@ -5,4 +5,4 @@ OBJ := $(shell find ./ -name '*.c' | sed -e "s/\.c/\.o/") $(CC) $< -I src $(OPT) -c -o ../obj/$@ divinus: $(OBJ) - $(CC) $(addprefix ../obj/,$^) -I src -rdynamic $(OPT) -o ../$@ + $(CC) $(addprefix ../obj/,$^) -rdynamic $(OPT) -o ../$@ \ No newline at end of file diff --git a/src/hal/hisi/v1_common.h b/src/hal/hisi/v1_common.h index 293c76e..721fe99 100644 --- a/src/hal/hisi/v1_common.h +++ b/src/hal/hisi/v1_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/hisi/v1_hal.h b/src/hal/hisi/v1_hal.h index 50399c8..acf02cb 100644 --- a/src/hal/hisi/v1_hal.h +++ b/src/hal/hisi/v1_hal.h @@ -15,6 +15,7 @@ #include #include #include +#include #include extern char keepRunning; diff --git a/src/hal/hisi/v2_common.h b/src/hal/hisi/v2_common.h index 6569839..e3adf4f 100644 --- a/src/hal/hisi/v2_common.h +++ b/src/hal/hisi/v2_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/hisi/v2_hal.h b/src/hal/hisi/v2_hal.h index 4bc8e89..f0e4dcc 100644 --- a/src/hal/hisi/v2_hal.h +++ b/src/hal/hisi/v2_hal.h @@ -15,6 +15,7 @@ #include #include #include +#include #include extern char keepRunning; diff --git a/src/hal/hisi/v3_common.h b/src/hal/hisi/v3_common.h index 01be933..805f177 100644 --- a/src/hal/hisi/v3_common.h +++ b/src/hal/hisi/v3_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/hisi/v3_hal.h b/src/hal/hisi/v3_hal.h index 18efa0f..46872ab 100644 --- a/src/hal/hisi/v3_hal.h +++ b/src/hal/hisi/v3_hal.h @@ -15,6 +15,7 @@ #include #include #include +#include #include extern char keepRunning; diff --git a/src/hal/hisi/v4_common.h b/src/hal/hisi/v4_common.h index be6aa7b..fe27b77 100644 --- a/src/hal/hisi/v4_common.h +++ b/src/hal/hisi/v4_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/hisi/v4_hal.h b/src/hal/hisi/v4_hal.h index e7863bd..459a447 100644 --- a/src/hal/hisi/v4_hal.h +++ b/src/hal/hisi/v4_hal.h @@ -15,6 +15,7 @@ #include #include #include +#include #include extern char keepRunning; diff --git a/src/hal/inge/t31_common.h b/src/hal/inge/t31_common.h index 7de7557..5c25904 100644 --- a/src/hal/inge/t31_common.h +++ b/src/hal/inge/t31_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/inge/t31_hal.h b/src/hal/inge/t31_hal.h index 4638c18..18a4c90 100644 --- a/src/hal/inge/t31_hal.h +++ b/src/hal/inge/t31_hal.h @@ -10,6 +10,8 @@ #include "../config.h" +#include + extern char keepRunning; extern hal_chnstate t31_state[T31_VENC_CHN_NUM]; diff --git a/src/hal/plus/cvi_common.h b/src/hal/plus/cvi_common.h index b87df5c..9dbf09e 100644 --- a/src/hal/plus/cvi_common.h +++ b/src/hal/plus/cvi_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/plus/cvi_hal.h b/src/hal/plus/cvi_hal.h index 391b04a..e7810da 100644 --- a/src/hal/plus/cvi_hal.h +++ b/src/hal/plus/cvi_hal.h @@ -15,6 +15,7 @@ #include #include #include +#include #include extern char keepRunning; diff --git a/src/hal/plus/gm_common.h b/src/hal/plus/gm_common.h index cd88e12..914ee80 100644 --- a/src/hal/plus/gm_common.h +++ b/src/hal/plus/gm_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/plus/rk_common.h b/src/hal/plus/rk_common.h index c6164f4..2c4b889 100644 --- a/src/hal/plus/rk_common.h +++ b/src/hal/plus/rk_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/plus/rk_hal.h b/src/hal/plus/rk_hal.h index 19c3b86..dea1d77 100644 --- a/src/hal/plus/rk_hal.h +++ b/src/hal/plus/rk_hal.h @@ -12,6 +12,7 @@ #include #include #include +#include #include extern char keepRunning; diff --git a/src/hal/star/i3_common.h b/src/hal/star/i3_common.h index 65ff5ff..d8e9af7 100644 --- a/src/hal/star/i3_common.h +++ b/src/hal/star/i3_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/star/i6_common.h b/src/hal/star/i6_common.h index e6847ba..01cd57f 100644 --- a/src/hal/star/i6_common.h +++ b/src/hal/star/i6_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/star/i6_hal.h b/src/hal/star/i6_hal.h index 8266ac1..841ea59 100644 --- a/src/hal/star/i6_hal.h +++ b/src/hal/star/i6_hal.h @@ -12,6 +12,8 @@ #include "../support.h" +#include + extern char keepRunning; extern int (*i6_aud_cb)(hal_audframe*); diff --git a/src/hal/star/i6c_common.h b/src/hal/star/i6c_common.h index dc1c0a5..fb088d4 100644 --- a/src/hal/star/i6c_common.h +++ b/src/hal/star/i6c_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/star/i6c_hal.h b/src/hal/star/i6c_hal.h index e89ebbc..76e73e9 100644 --- a/src/hal/star/i6c_hal.h +++ b/src/hal/star/i6c_hal.h @@ -10,6 +10,8 @@ #include "i6c_venc.h" #include "i6c_vif.h" +#include + extern char keepRunning; extern int (*i6c_aud_cb)(hal_audframe*); diff --git a/src/hal/star/i6f_common.h b/src/hal/star/i6f_common.h index 10d473f..9ec4567 100644 --- a/src/hal/star/i6f_common.h +++ b/src/hal/star/i6f_common.h @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include "../symbols.h" #include "../types.h" diff --git a/src/hal/star/i6f_hal.h b/src/hal/star/i6f_hal.h index f6dab1d..bc804ad 100644 --- a/src/hal/star/i6f_hal.h +++ b/src/hal/star/i6f_hal.h @@ -8,6 +8,8 @@ #include "i6f_venc.h" #include "i6f_vif.h" +#include + extern char keepRunning; extern int (*i6f_aud_cb)(hal_audframe*); diff --git a/src/main.c b/src/main.c index 61bdbc1..8ec1441 100644 --- a/src/main.c +++ b/src/main.c @@ -16,20 +16,48 @@ #include rtsp_handle rtspHandle; -char graceful = 0; +char graceful = 0, keepRunning = 1; + +void handle_error(int signo) { + write(STDERR_FILENO, "Error occurred! Quitting...\n", 28); + keepRunning = 0; + exit(EXIT_FAILURE); +} + +void handle_exit(int signo) { + write(STDERR_FILENO, "Graceful shutdown...\n", 21); + keepRunning = 0; + graceful = 1; +} int main(int argc, char *argv[]) { + { + char signal_error[] = {SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV}; + char signal_exit[] = {SIGINT, SIGQUIT, SIGTERM}; + char signal_null[] = {EPIPE, SIGPIPE}; + + for (char *s = signal_error; s < (&signal_error)[1]; s++) + signal(*s, handle_error); + for (char *s = signal_exit; s < (&signal_exit)[1]; s++) + signal(*s, handle_exit); + for (char *s = signal_null; s < (&signal_null)[1]; s++) + signal(*s, NULL); + } + hal_identify(); if (!*family) HAL_ERROR("hal", "Unsupported chip family! Quitting...\n"); - fprintf(stderr, "Divinus for %s\n", family); + fprintf(stderr, "\033[7m Divinus for %s \033[0m\n", family); fprintf(stderr, "Chip ID: %s\n", chip); if (parse_app_config() != CONFIG_OK) HAL_ERROR("hal", "Can't load app config 'divinus.yaml'\n"); + if (app_config.watchdog) + watchdog_start(app_config.watchdog); + if (app_config.mdns_enable) start_mdns(); @@ -75,9 +103,12 @@ int main(int argc, char *argv[]) { if (app_config.mdns_enable) stop_mdns(); + if (app_config.watchdog) + watchdog_stop(); + if (!graceful) restore_app_config(); fprintf(stderr, "Main thread is shutting down...\n"); return EXIT_SUCCESS; -} +} \ No newline at end of file diff --git a/src/server.c b/src/server.c index e58e668..3939df8 100644 --- a/src/server.c +++ b/src/server.c @@ -6,8 +6,6 @@ IMPORT_STR(.rodata, "../res/index.html", indexhtml); extern const char indexhtml[]; -char keepRunning = 1; - enum StreamType { STREAM_H26X, STREAM_JPEG, @@ -1124,11 +1122,11 @@ void *server_thread(void *vargp) { short result = strtol(value, &remain, 10); if (remain == value) continue; t.tv_sec = result; - clock_settime(CLOCK_REALTIME, &t); + clock_settime(0, &t); } } } - clock_gettime(CLOCK_REALTIME, &t); + clock_gettime(0, &t); int respLen = sprintf(response, "HTTP/1.1 200 OK\r\n" \ "Content-Type: application/json;charset=UTF-8\r\n" \ @@ -1153,28 +1151,10 @@ void *server_thread(void *vargp) { return NULL; } -void sig_handler(int signo) { - HAL_INFO("server", "Graceful shutdown...\n"); - keepRunning = 0; - graceful = 1; -} -void epipe_handler(int signo) { printf("EPIPE\n"); } -void spipe_handler(int signo) { printf("SIGPIPE\n"); } - int server_fd = -1; pthread_t server_thread_id; int start_server() { - signal(SIGINT, sig_handler); - signal(SIGQUIT, sig_handler); - signal(SIGTERM, sig_handler); - - signal(SIGPIPE, spipe_handler); - signal(EPIPE, epipe_handler); - - if (app_config.watchdog) - watchdog_start(app_config.watchdog); - for (uint32_t i = 0; i < MAX_CLIENTS; ++i) { client_fds[i].socket_fd = -1; client_fds[i].type = -1; @@ -1213,8 +1193,5 @@ int stop_server() { pthread_mutex_destroy(&client_fds_mutex); HAL_INFO("server", "Shutting down server...\n"); - if (app_config.watchdog) - watchdog_stop(); - return EXIT_SUCCESS; -} +} \ No newline at end of file diff --git a/src/stack.c b/src/stack.c index 584aaa1..555ae51 100644 --- a/src/stack.c +++ b/src/stack.c @@ -16,3 +16,7 @@ __attribute__((noreturn)) void __stack_chk_fail(void) { panic("Stack smashing detected"); #endif } + +void __stack_chk_fail_local(void) { + __stack_chk_fail(); +} \ No newline at end of file