From 5f42ef3c92b3a1f5679e92ba3b2bb3a547c16da2 Mon Sep 17 00:00:00 2001 From: Sebastian Kloska Date: Tue, 10 Sep 2019 14:25:34 +0200 Subject: Proper setup and error checking of pthread_create * Replace all start_XXX functions with one start_thread * Don't make pthread_t static * Don't use pthread_attr_t. Simply pass NULL * Bail out when pthread_create() fails * Check if MAX_THREADS gets exceeded Signed-off-by: Sebastian Kloska --- src/system/dlt-system-filetransfer.c | 9 ------- src/system/dlt-system-journal.c | 16 +++--------- src/system/dlt-system-logfile.c | 10 -------- src/system/dlt-system-process-handling.c | 42 +++++++++++++++++++++++++++----- src/system/dlt-system-processes.c | 10 -------- src/system/dlt-system-syslog.c | 10 -------- src/system/dlt-system-watchdog.c | 13 ---------- src/system/dlt-system.h | 16 ++++++------ 8 files changed, 49 insertions(+), 77 deletions(-) (limited to 'src/system') diff --git a/src/system/dlt-system-filetransfer.c b/src/system/dlt-system-filetransfer.c index 9091b1a..85aa380 100644 --- a/src/system/dlt-system-filetransfer.c +++ b/src/system/dlt-system-filetransfer.c @@ -765,12 +765,3 @@ void filetransfer_thread(void *v_conf) sleep(conf->Filetransfer.TimeDelay); } } - -void start_filetransfer(DltSystemConfiguration *conf) -{ - DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, start.")); - static pthread_attr_t t_attr; - static pthread_t pt; - pthread_create(&pt, &t_attr, (void *)filetransfer_thread, conf); - threads.threads[threads.count++] = pt; -} diff --git a/src/system/dlt-system-journal.c b/src/system/dlt-system-journal.c index 2845406..ee3ddf0 100644 --- a/src/system/dlt-system-journal.c +++ b/src/system/dlt-system-journal.c @@ -154,7 +154,8 @@ void dlt_system_journal_get_timestamp(sd_journal *journal, MessageTimestamp *tim if (errno != 0) time_usecs = 0; } - else if ((ret = sd_journal_get_realtime_usec(journal, &time_usecs)) < 0) { + else if ((ret = sd_journal_get_realtime_usec(journal, &time_usecs)) < 0) + { DLT_LOG(dltsystem, DLT_LOG_WARN, DLT_STRING("dlt-system-journal failed to get realtime: "), DLT_STRING(strerror(-ret))); @@ -180,7 +181,8 @@ void dlt_system_journal_get_timestamp(sd_journal *journal, MessageTimestamp *tim if (errno != 0) time_usecs = 0; } - else if ((ret = sd_journal_get_monotonic_usec(journal, &time_usecs, NULL)) < 0) { + else if ((ret = sd_journal_get_monotonic_usec(journal, &time_usecs, NULL)) < 0) + { DLT_LOG(dltsystem, DLT_LOG_WARN, DLT_STRING("dlt-system-journal failed to get monotonic time: "), DLT_STRING(strerror(-ret))); @@ -396,14 +398,4 @@ void journal_thread(void *v_conf) } -void start_systemd_journal(DltSystemConfiguration *conf) -{ - DLT_LOG(dltsystem, DLT_LOG_DEBUG, - DLT_STRING("dlt-system-journal, start journal")); - static pthread_attr_t t_attr; - static pthread_t pt; - pthread_create(&pt, &t_attr, (void *)journal_thread, conf); - threads.threads[threads.count++] = pt; -} - #endif /* DLT_SYSTEMD_JOURNAL_ENABLE */ diff --git a/src/system/dlt-system-logfile.c b/src/system/dlt-system-logfile.c index b7ed38a..6466172 100644 --- a/src/system/dlt-system-logfile.c +++ b/src/system/dlt-system-logfile.c @@ -143,13 +143,3 @@ void logfile_thread(void *v_conf) } } -void start_logfile(DltSystemConfiguration *conf) -{ - DLT_LOG(dltsystem, DLT_LOG_DEBUG, - DLT_STRING("dlt-system-logfile, starting.")); - DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Starting thread for logfile")); - static pthread_attr_t t_attr; - static pthread_t pt; - pthread_create(&pt, &t_attr, (void *)logfile_thread, conf); - threads.threads[threads.count++] = pt; -} diff --git a/src/system/dlt-system-process-handling.c b/src/system/dlt-system-process-handling.c index 8b89c03..af6ba66 100644 --- a/src/system/dlt-system-process-handling.c +++ b/src/system/dlt-system-process-handling.c @@ -119,32 +119,62 @@ void start_threads(DltSystemConfiguration *config) threads.threads[i] = 0; #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE) - start_systemd_watchdog(config); + start_thread(config, watchdog_thread, "systemd watchdog"); #endif if (config->Shell.Enable) init_shell(); if (config->LogFile.Enable) - start_logfile(config); + start_thread(config, logfile_thread, "log file"); if (config->Filetransfer.Enable) - start_filetransfer(config); + start_thread(config, filetransfer_thread, "file transfer"); if (config->LogProcesses.Enable) - start_logprocess(config); + start_thread(config, logprocess_thread, "log process"); if (config->Syslog.Enable) - start_syslog(config); + start_thread(config, syslog_thread, "syslog"); #if defined(DLT_SYSTEMD_JOURNAL_ENABLE) if (config->Journal.Enable) - start_systemd_journal(config); + start_thread(config, journal_thread, "systemd journal"); #endif } +/** + * Start a thread and add it to the thread pool. + */ + +void start_thread(DltSystemConfiguration *conf, + void (thread)(void *), const char *name) +{ + if (threads.count == MAX_THREADS) { + DLT_LOG(dltsystem, DLT_LOG_ERROR, + DLT_STRING("Could not create thread for "), + DLT_STRING(name), + DLT_STRING("Out of thread slots.\n")); + return; + } + + DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Creating thread for "), + DLT_STRING(name), + DLT_STRING("\n")); + + pthread_t pt; + + if (pthread_create(&pt, NULL, (void *)thread, conf) == 0) + threads.threads[threads.count++] = pt; + else + DLT_LOG(dltsystem, DLT_LOG_ERROR, + DLT_STRING("Could not create thread for "), + DLT_STRING(name), + DLT_STRING("\n")); +} + /** * Wait for threads to exit. * There's not actually a condition currently diff --git a/src/system/dlt-system-processes.c b/src/system/dlt-system-processes.c index 24a6dcf..71f5a1d 100644 --- a/src/system/dlt-system-processes.c +++ b/src/system/dlt-system-processes.c @@ -159,13 +159,3 @@ void logprocess_thread(void *v_conf) } } } - -void start_logprocess(DltSystemConfiguration *conf) -{ - DLT_LOG(dltsystem, DLT_LOG_DEBUG, - DLT_STRING("dlt-system-processes, starting process log.")); - static pthread_attr_t t_attr; - static pthread_t pt; - pthread_create(&pt, &t_attr, (void *)logprocess_thread, conf); - threads.threads[threads.count++] = pt; -} diff --git a/src/system/dlt-system-syslog.c b/src/system/dlt-system-syslog.c index fc59774..0e2e2c4 100644 --- a/src/system/dlt-system-syslog.c +++ b/src/system/dlt-system-syslog.c @@ -151,13 +151,3 @@ void syslog_thread(void *v_conf) close (sock); } - -void start_syslog(DltSystemConfiguration *conf) -{ - DLT_LOG(dltsystem, DLT_LOG_DEBUG, - DLT_STRING("dlt-system-syslog, start syslog")); - static pthread_attr_t t_attr; - static pthread_t pt; - pthread_create(&pt, &t_attr, (void *)syslog_thread, conf); - threads.threads[threads.count++] = pt; -} diff --git a/src/system/dlt-system-watchdog.c b/src/system/dlt-system-watchdog.c index f85398b..fd6bcac 100644 --- a/src/system/dlt-system-watchdog.c +++ b/src/system/dlt-system-watchdog.c @@ -160,17 +160,4 @@ void watchdog_thread(void *v_conf) DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("systemd watchdog timeout (WATCHDOG_USEC) is null\n")); } } - -void start_systemd_watchdog(DltSystemConfiguration *conf) -{ - DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Creating thread for systemd watchdog\n")); - - static pthread_attr_t t_attr; - static pthread_t pt; - - if (pthread_create(&pt, &t_attr, (void *)watchdog_thread, conf) == 0) - threads.threads[threads.count++] = pt; - else - DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Could not create thread for systemd watchdog\n")); -} #endif diff --git a/src/system/dlt-system.h b/src/system/dlt-system.h index 46738b8..e60cb9b 100644 --- a/src/system/dlt-system.h +++ b/src/system/dlt-system.h @@ -175,23 +175,25 @@ int read_configuration_file(DltSystemConfiguration *config, char *file_name); /* In dlt-process-handling.c */ int daemonize(); void start_threads(DltSystemConfiguration *config); +void start_thread(DltSystemConfiguration *conf, + void (thread)(void *), const char *nam); void join_threads(); void dlt_system_signal_handler(int sig); void register_with_dlt(DltSystemConfiguration *config); -/* Thread initiators: */ +/* Threads */ void init_shell(); -void start_syslog(); -void start_filetransfer(DltSystemConfiguration *conf); -void start_logfile(DltSystemConfiguration *conf); -void start_logprocess(DltSystemConfiguration *conf); +void syslog_thread(void *v_conf); +void filetransfer_thread(void *v_conf); +void logfile_thread(void *v_conf); +void logprocess_thread(void *v_conf); #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE) -void start_systemd_watchdog(DltSystemConfiguration *conf); +void watchdog_thread(void *v_conf); #endif #if defined(DLT_SYSTEMD_JOURNAL_ENABLE) -void start_systemd_journal(DltSystemConfiguration *conf); +void journal_thread(void *v_conf); #endif #endif /* DLT_SYSTEM_H_ */ -- cgit v1.2.1