summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBiastoch, Darian (ADITG/ESM) <dbiastoch@de.adit-jv.com>2021-07-16 00:29:10 +0000
committerSaya Sugiura <39760799+ssugiura@users.noreply.github.com>2021-10-05 11:19:26 +0900
commit50a385e6afea50922569828438194477537a768b (patch)
tree120995ed2095145b31caf2958ddd259ced88786d
parent0ee7fdff3d6bb2f2055e7083fa48728f4d274661 (diff)
downloadDLT-daemon-50a385e6afea50922569828438194477537a768b.tar.gz
system: use signalfd for dlt-system
1: Move the call of pthread_sigmask in dlt-qnx-systems main before DLT_REGISTER_APP, because this already spawns a thread, what was done without applying pthread_sigmask before. 2: Replaced all threads in dlt-system, that are spawned for each feature, through one poll. This poll waits for a file descriptor event, that triggers the feature routine, that was executed before in the thread. Done this for following processes: * reading syslog and journal * frequently reading logfile and logprocess * triggering filetransfer, if a file was added/changed in watched directory * checking watchdog timer of systemd service file with defined intervall Signed-off-by: dbiastoch <dbiastoch@de.adit-jv.com>
-rw-r--r--doc/dlt-system.conf.5.md6
-rw-r--r--src/dlt-qnx-system/dlt-qnx-system.c25
-rw-r--r--src/system/dlt-system-filetransfer.c49
-rw-r--r--src/system/dlt-system-journal.c289
-rw-r--r--src/system/dlt-system-logfile.c40
-rw-r--r--src/system/dlt-system-options.c5
-rw-r--r--src/system/dlt-system-process-handling.c263
-rw-r--r--src/system/dlt-system-processes.c40
-rw-r--r--src/system/dlt-system-syslog.c42
-rw-r--r--src/system/dlt-system-watchdog.c166
-rw-r--r--src/system/dlt-system.c5
-rw-r--r--src/system/dlt-system.conf5
-rw-r--r--src/system/dlt-system.h73
13 files changed, 512 insertions, 496 deletions
diff --git a/doc/dlt-system.conf.5.md b/doc/dlt-system.conf.5.md
index 7020281..9fc65b2 100644
--- a/doc/dlt-system.conf.5.md
+++ b/doc/dlt-system.conf.5.md
@@ -114,12 +114,6 @@ Time in seconds after startup of dlt-system when first file is transfered.
Default: 0
-## FiletransferTimeDelay
-
-Time in seconds to wait between deletion of transferred file and start of next file transfer.
-
- Default: 10
-
## FiletransferTimeoutBetweenLogs
Time in seconds to wait between two file transfer logs of a single file to DLT.
diff --git a/src/dlt-qnx-system/dlt-qnx-system.c b/src/dlt-qnx-system/dlt-qnx-system.c
index afd34b4..03ad0fb 100644
--- a/src/dlt-qnx-system/dlt-qnx-system.c
+++ b/src/dlt-qnx-system/dlt-qnx-system.c
@@ -70,28 +70,27 @@ int main(int argc, char* argv[])
daemonize();
}
- DLT_REGISTER_APP(config.applicationId, "DLT QNX System");
- DLT_REGISTER_CONTEXT(dltQnxSystem, config.applicationContextId,
- "Context of main dlt qnx system manager");
-
- DLT_LOG(dltQnxSystem, DLT_LOG_DEBUG,
- DLT_STRING("Setting signals wait for abnormal exit"));
-
- g_threads.mainThread = pthread_self();
-
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGHUP);
sigaddset(&mask, SIGQUIT);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGALRM);
- if (pthread_sigmask(SIG_BLOCK, &mask, NULL) == -1) {
- DLT_LOG(dltQnxSystem, DLT_LOG_WARN,
- DLT_STRING("Failed to block signals!"));
- DLT_UNREGISTER_APP();
+ if (pthread_sigmask(SIG_BLOCK, &mask, NULL) != 0)
+ {
+ fprintf(stderr, "Couldn't set mask for potential future threads.\n");
return -1;
}
+ DLT_REGISTER_APP(config.applicationId, "DLT QNX System");
+ DLT_REGISTER_CONTEXT(dltQnxSystem, config.applicationContextId,
+ "Context of main dlt qnx system manager");
+
+ DLT_LOG(dltQnxSystem, DLT_LOG_DEBUG,
+ DLT_STRING("Setting signals wait for abnormal exit"));
+
+ g_threads.mainThread = pthread_self();
+
DLT_LOG(dltQnxSystem, DLT_LOG_DEBUG, DLT_STRING("Launching threads."));
start_threads(&config);
diff --git a/src/system/dlt-system-filetransfer.c b/src/system/dlt-system-filetransfer.c
index dbc5431..a06e512 100644
--- a/src/system/dlt-system-filetransfer.c
+++ b/src/system/dlt-system-filetransfer.c
@@ -49,7 +49,6 @@
*******************************************************************************/
-#include <pthread.h>
#include <unistd.h>
#ifdef linux
# include <sys/inotify.h>
@@ -61,6 +60,7 @@
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
+#include <poll.h>
#include "dlt-system.h"
#include "dlt.h"
@@ -76,8 +76,6 @@
#define SUBDIR_TOSEND ".tosend"
-
-extern DltSystemThreads threads;
/* From dlt_filetransfer */
extern uint32_t getFileSerialNumber(const char *file, int *ok);
@@ -85,11 +83,6 @@ DLT_IMPORT_CONTEXT(dltsystem)
DLT_DECLARE_CONTEXT(filetransferContext)
#ifdef linux
-typedef struct {
- int handle;
- int fd[DLT_SYSTEM_LOG_DIRS_MAX];
-} s_ft_inotify;
-
s_ft_inotify ino;
#endif
@@ -260,7 +253,6 @@ int send_one(char *src, FiletransferOptions const *opts, int which)
{
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
DLT_STRING("dlt-system-filetransfer, sending a file."));
- sleep(opts->TimeDelay);
/* Prepare all needed file names */
char *fn = basename(src);
@@ -374,7 +366,6 @@ int send_one(char *src, FiletransferOptions const *opts, int which)
int flush_dir_send(FiletransferOptions const *opts, const char *compress_dir, const char *send_dir)
{
-
struct dirent *dp;
DIR *dir;
dir = opendir(send_dir);
@@ -568,8 +559,6 @@ int flush_dir_original(FiletransferOptions const *opts, int which)
*/
int flush_dir(FiletransferOptions const *opts, int which)
{
-
-
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
DLT_STRING("dlt-system-filetransfer, flush directory of old files."));
@@ -611,11 +600,13 @@ int flush_dir(FiletransferOptions const *opts, int which)
/*!Initializes the surveyed directories */
/**On startup, the inotifiy handlers are created, and existing files shall be sent into DLT stream
- * @param opts FiletransferOptions
+ * @param config DltSystemConfiguration
* @return Returns 0 if everything was okay. If there was a failure a value < 0 will be returned.
*/
-int init_filetransfer_dirs(FiletransferOptions const *opts)
+int init_filetransfer_dirs(DltSystemConfiguration *config)
{
+ FiletransferOptions const *opts = &(config->Filetransfer);
+ DLT_REGISTER_CONTEXT(filetransferContext, config->Filetransfer.ContextId, "Filetransfer Adapter");
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
DLT_STRING("dlt-system-filetransfer, initializing inotify on directories."));
int i;
@@ -690,20 +681,19 @@ int init_filetransfer_dirs(FiletransferOptions const *opts)
flush_dir(opts, i);
}
-
return 0;
}
-int wait_for_files(FiletransferOptions const *opts)
+int process_files(FiletransferOptions const *opts)
{
#ifdef linux
- DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, waiting for files."));
+ DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, processing files."));
static char buf[INOTIFY_LEN];
ssize_t len = read(ino.handle, buf, INOTIFY_LEN);
if (len < 0) {
DLT_LOG(filetransferContext, DLT_LOG_ERROR,
- DLT_STRING("Error while waiting for files in dlt-system file transfer."));
+ DLT_STRING("Error while reading events for files in dlt-system file transfer."));
return -1;
}
@@ -748,24 +738,7 @@ int wait_for_files(FiletransferOptions const *opts)
return 0;
}
-void filetransfer_thread(void *v_conf)
+void filetransfer_fd_handler(DltSystemConfiguration *config)
{
- DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("dlt-system-filetransfer, in thread."));
- DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
- DLT_REGISTER_CONTEXT(filetransferContext, conf->Filetransfer.ContextId,
- "File transfer manager.");
-
- sleep(conf->Filetransfer.TimeStartup);
-
- if (init_filetransfer_dirs(&(conf->Filetransfer)) < 0)
- return;
-
- while (!threads.shutdown) {
- if (wait_for_files(&(conf->Filetransfer)) < 0) {
- DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while waiting files. File transfer shutdown."));
- return;
- }
-
- sleep(conf->Filetransfer.TimeDelay);
- }
-}
+ process_files(&(config->Filetransfer));
+} \ No newline at end of file
diff --git a/src/system/dlt-system-journal.c b/src/system/dlt-system-journal.c
index aa9ba52..c56e58a 100644
--- a/src/system/dlt-system-journal.c
+++ b/src/system/dlt-system-journal.c
@@ -43,7 +43,7 @@
*******************************************************************************/
#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
-#include <pthread.h>
+#include <poll.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
@@ -56,7 +56,6 @@
#include <systemd/sd-id128.h>
#include <inttypes.h> /* for PRI formatting macro */
-extern DltSystemThreads threads;
#define DLT_SYSTEM_JOURNAL_BUFFER_SIZE 256
#define DLT_SYSTEM_JOURNAL_BUFFER_SIZE_BIG 2048
@@ -199,13 +198,10 @@ void dlt_system_journal_get_timestamp(sd_journal *journal, MessageTimestamp *tim
time_usecs % 1000000);
}
-void journal_thread(void *v_conf)
-{
- int r;
- sd_journal *j;
- char match[DLT_SYSTEM_JOURNAL_BOOT_ID_MAX_LENGTH] = "_BOOT_ID=";
- sd_id128_t boot_id;
+void get_journal_msg(sd_journal *j, DltSystemConfiguration *config)
+{
uint32_t ts;
+ int r;
char buffer_process[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
buffer_priority[DLT_SYSTEM_JOURNAL_BUFFER_SIZE] = { 0 },
@@ -220,183 +216,178 @@ void journal_thread(void *v_conf)
char *systemd_log_levels[] =
{ "Emergency", "Alert", "Critical", "Error", "Warning", "Notice", "Informational", "Debug" };
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-journal, in thread."));
+ for(;;)
+ {
+ r = sd_journal_next(j);
+ if (r < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR,
+ DLT_STRING("dlt-system-journal failed to get next entry:"), DLT_STRING(strerror(-r)));
+ sd_journal_close(j);
+ return;
+ }
+ else if (r == 0) {
+ return;
+ }
- DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
- DLT_REGISTER_CONTEXT(journalContext, conf->Journal.ContextId, "Journal Adapter");
+ /* get all data from current journal entry */
+ dlt_system_journal_get_timestamp(j, &timestamp);
+
+ /* get data from journal entry, empty string if invalid fields */
+ dlt_system_journal_get(j, buffer_comm, "_COMM", sizeof(buffer_comm));
+ dlt_system_journal_get(j, buffer_pid, "_PID", sizeof(buffer_pid));
+ dlt_system_journal_get(j, buffer_priority, "PRIORITY", sizeof(buffer_priority));
+ dlt_system_journal_get(j, buffer_message, "MESSAGE", sizeof(buffer_message));
+ dlt_system_journal_get(j, buffer_transport, "_TRANSPORT", sizeof(buffer_transport));
+
+ /* prepare process string */
+ if (strcmp(buffer_transport, "kernel") == 0)
+ snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "kernel:");
+ else
+ snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s[%s]:", buffer_comm, buffer_pid);
+
+ /* map log level on demand */
+ loglevel = DLT_LOG_INFO;
+ systemd_loglevel = atoi(buffer_priority);
+
+ if (config->Journal.MapLogLevels) {
+ /* Map log levels from journal to DLT */
+ switch (systemd_loglevel) {
+ case 0: /* Emergency */
+ case 1: /* Alert */
+ case 2: /* Critical */
+ loglevel = DLT_LOG_FATAL;
+ break;
+ case 3: /* Error */
+ loglevel = DLT_LOG_ERROR;
+ break;
+ case 4: /* Warning */
+ loglevel = DLT_LOG_WARN;
+ break;
+ case 5: /* Notice */
+ case 6: /* Informational */
+ loglevel = DLT_LOG_INFO;
+ break;
+ case 7: /* Debug */
+ loglevel = DLT_LOG_DEBUG;
+ break;
+ default:
+ loglevel = DLT_LOG_INFO;
+ break;
+ }
+ }
- r = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY /*SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY*/);
- printf("journal open return %d\n", r);
+ if ((systemd_loglevel >= 0) && (systemd_loglevel <= 7))
+ snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s:", systemd_log_levels[systemd_loglevel]);
+ else
+ snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "prio_unknown:");
+
+ /* write log entry */
+ if (config->Journal.UseOriginalTimestamp == 0) {
+ DLT_LOG(journalContext, loglevel,
+ DLT_STRING(timestamp.real),
+ DLT_STRING(timestamp.monotonic),
+ DLT_STRING(buffer_process),
+ DLT_STRING(buffer_priority),
+ DLT_STRING(buffer_message)
+ );
+ }
+ else {
+ /* since we are talking about points in time, I'd prefer truncating over arithmetic rounding */
+ ts = (uint32_t)(atof(timestamp.monotonic) * 10000);
+ DLT_LOG_TS(journalContext, loglevel, ts,
+ DLT_STRING(timestamp.real),
+ DLT_STRING(buffer_process),
+ DLT_STRING(buffer_priority),
+ DLT_STRING(buffer_message)
+ );
+ }
+
+ if (journal_checkUserBufferForFreeSpace() == -1) {
+ /* buffer is nearly full */
+ /* wait 500ms for writing next entry */
+ struct timespec t;
+ t.tv_sec = 0;
+ t.tv_nsec = 1000000ul * 500;
+ nanosleep(&t, NULL);
+ }
+ }
+}
+
+void register_journal_fd(sd_journal **j, struct pollfd *pollfd, int i, DltSystemConfiguration *config)
+{
+ DLT_REGISTER_CONTEXT(journalContext, config->Journal.ContextId, "Journal Adapter");
+ sd_journal *j_tmp;
+ char match[DLT_SYSTEM_JOURNAL_BOOT_ID_MAX_LENGTH] = "_BOOT_ID=";
+ sd_id128_t boot_id;
+ int r;
+
+ r = sd_journal_open(&j_tmp, SD_JOURNAL_LOCAL_ONLY /*SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY*/);
+ printf("journal open return %d\n", r);
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal, cannot open journal:"), DLT_STRING(strerror(-r)));
printf("journal open failed: %s\n", strerror(-r));
- return;
+ j_tmp = NULL;
}
- if (conf->Journal.CurrentBoot) {
+ if (config->Journal.CurrentBoot) {
/* show only current boot entries */
r = sd_id128_get_boot(&boot_id);
-
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to get boot id:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
sd_id128_to_string(boot_id, match + 9);
- r = sd_journal_add_match(j, match, strlen(match));
-
+ r = sd_journal_add_match(j_tmp, match, strlen(match));
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to get match:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
}
- if (conf->Journal.Follow) {
+ if (config->Journal.Follow) {
/* show only last 10 entries and follow */
- r = sd_journal_seek_tail(j);
-
+ r = sd_journal_seek_tail(j_tmp);
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to seek to tail:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
- r = sd_journal_previous_skip(j, 10);
-
+ r = sd_journal_previous_skip(j_tmp, 10);
if (r < 0) {
DLT_LOG(dltsystem, DLT_LOG_ERROR,
DLT_STRING("dlt-system-journal failed to seek back 10 entries:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
+ sd_journal_close(j_tmp);
+ j_tmp = NULL;
}
}
- while (!threads.shutdown) {
-
- r = sd_journal_next(j);
-
- if (r < 0) {
- DLT_LOG(dltsystem, DLT_LOG_ERROR,
- DLT_STRING("dlt-system-journal failed to get next entry:"), DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
- }
- else if (r > 0)
- {
- /* get all data from current journal entry */
- dlt_system_journal_get_timestamp(j, &timestamp);
-
- /* get data from journal entry, empty string if invalid fields */
- dlt_system_journal_get(j, buffer_comm, "_COMM", sizeof(buffer_comm));
- dlt_system_journal_get(j, buffer_pid, "_PID", sizeof(buffer_pid));
- dlt_system_journal_get(j, buffer_priority, "PRIORITY", sizeof(buffer_priority));
- dlt_system_journal_get(j, buffer_message, "MESSAGE", sizeof(buffer_message));
- dlt_system_journal_get(j, buffer_transport, "_TRANSPORT", sizeof(buffer_transport));
-
- /* prepare process string */
- if (strcmp(buffer_transport, "kernel") == 0)
- snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "kernel:");
- else
- snprintf(buffer_process, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s[%s]:", buffer_comm, buffer_pid);
-
- /* map log level on demand */
- loglevel = DLT_LOG_INFO;
- systemd_loglevel = atoi(buffer_priority);
-
- if (conf->Journal.MapLogLevels) {
- /* Map log levels from journal to DLT */
- switch (systemd_loglevel) {
- case 0: /* Emergency */
- case 1: /* Alert */
- case 2: /* Critical */
- loglevel = DLT_LOG_FATAL;
- break;
- case 3: /* Error */
- loglevel = DLT_LOG_ERROR;
- break;
- case 4: /* Warning */
- loglevel = DLT_LOG_WARN;
- break;
- case 5: /* Notice */
- case 6: /* Informational */
- loglevel = DLT_LOG_INFO;
- break;
- case 7: /* Debug */
- loglevel = DLT_LOG_DEBUG;
- break;
- default:
- loglevel = DLT_LOG_INFO;
- break;
- }
- }
-
- if ((systemd_loglevel >= 0) && (systemd_loglevel <= 7))
- snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "%s:", systemd_log_levels[systemd_loglevel]);
- else
- snprintf(buffer_priority, DLT_SYSTEM_JOURNAL_BUFFER_SIZE, "prio_unknown:");
-
- /* write log entry */
- if (conf->Journal.UseOriginalTimestamp == 0) {
- DLT_LOG(journalContext, loglevel,
- DLT_STRING(timestamp.real),
- DLT_STRING(timestamp.monotonic),
- DLT_STRING(buffer_process),
- DLT_STRING(buffer_priority),
- DLT_STRING(buffer_message)
- );
-
- }
- else {
- /* since we are talking about points in time, I'd prefer truncating over arithmetic rounding */
- ts = (uint32_t)(atof(timestamp.monotonic) * 10000);
- DLT_LOG_TS(journalContext, loglevel, ts,
- DLT_STRING(timestamp.real),
- DLT_STRING(buffer_process),
- DLT_STRING(buffer_priority),
- DLT_STRING(buffer_message)
- );
- }
- }
- else {
- r = sd_journal_wait(j, 1000000);
-
- if (r < 0) {
- DLT_LOG(dltsystem, DLT_LOG_ERROR,
- DLT_STRING("dlt-system-journal failed to call sd_journal_get_realtime_usec(): "),
- DLT_STRING(strerror(-r)));
- sd_journal_close(j);
- return;
-
- }
- }
-
- if (journal_checkUserBufferForFreeSpace() == -1) {
- /* buffer is nearly full */
- /* wait 500ms for writing next entry */
- struct timespec t;
- t.tv_sec = 0;
- t.tv_nsec = 1000000ul * 500;
- nanosleep(&t, NULL);
- }
+ pollfd[i].fd = sd_journal_get_fd(j_tmp);
+ if(pollfd[i].fd < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while getting journal fd: "),
+ DLT_STRING(strerror(pollfd[i].fd)));
+ j_tmp = NULL;
}
+ pollfd[i].events = sd_journal_get_events(j_tmp);
+ if(pollfd[i].events < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while getting journal events: "),
+ DLT_STRING(strerror(pollfd[i].events)));
+ j_tmp = NULL;
+ }
+ *j = j_tmp;
+}
- sd_journal_close(j);
-
- DLT_UNREGISTER_CONTEXT(journalContext);
-
+void journal_fd_handler(sd_journal *j, DltSystemConfiguration *config)
+{
+ get_journal_msg(j, config);
}
-#endif /* DLT_SYSTEMD_JOURNAL_ENABLE */
+#endif /* DLT_SYSTEMD_JOURNAL_ENABLE */ \ No newline at end of file
diff --git a/src/system/dlt-system-logfile.c b/src/system/dlt-system-logfile.c
index 6466172..5e916d4 100644
--- a/src/system/dlt-system-logfile.c
+++ b/src/system/dlt-system-logfile.c
@@ -44,7 +44,6 @@
*******************************************************************************/
-#include <pthread.h>
#include <unistd.h>
#include "dlt-system.h"
@@ -55,8 +54,8 @@
DLT_IMPORT_CONTEXT(dltsystem)
-extern DltSystemThreads threads;
DltContext logfileContext[DLT_SYSTEM_LOG_FILE_MAX];
+int logfile_delays[DLT_SYSTEM_LOG_FILE_MAX];
void send_file(LogFileOptions const *fileopt, int n)
{
@@ -108,7 +107,7 @@ void register_contexts(LogFileOptions const *fileopts)
(*fileopts).Filename[i]);
}
-void logfile_thread(void *v_conf)
+void logfile_init(void *v_conf)
{
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
DLT_STRING("dlt-system-logfile, in thread."));
@@ -116,29 +115,26 @@ void logfile_thread(void *v_conf)
register_contexts(&(conf->LogFile));
- int logfile_delays[DLT_SYSTEM_LOG_FILE_MAX];
- int i;
-
- for (i = 0; i < conf->LogFile.Count; i++)
+ for (int i = 0; i < conf->LogFile.Count; i++)
logfile_delays[i] = conf->LogFile.TimeDelay[i];
+}
- while (!threads.shutdown) {
- sleep(1);
-
- for (i = 0; i < conf->LogFile.Count; i++) {
- if (conf->LogFile.Mode[i] == SEND_MODE_OFF)
- continue;
+void logfile_fd_handler(void *v_conf)
+{
+ DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
+ for (int i = 0; i < conf->LogFile.Count; i++) {
+ if (conf->LogFile.Mode[i] == SEND_MODE_OFF)
+ continue;
- if (logfile_delays[i] <= 0) {
- send_file(&(conf->LogFile), i);
- logfile_delays[i] = conf->LogFile.TimeDelay[i];
+ if (logfile_delays[i] <= 0) {
+ send_file(&(conf->LogFile), i);
+ logfile_delays[i] = conf->LogFile.TimeDelay[i];
- if (conf->LogFile.Mode[i] == SEND_MODE_ONCE)
- conf->LogFile.Mode[i] = SEND_MODE_OFF;
- }
- else {
- logfile_delays[i]--;
- }
+ if (conf->LogFile.Mode[i] == SEND_MODE_ONCE)
+ conf->LogFile.Mode[i] = SEND_MODE_OFF;
+ }
+ else {
+ logfile_delays[i]--;
}
}
}
diff --git a/src/system/dlt-system-options.c b/src/system/dlt-system-options.c
index 5fa31cb..89a9196 100644
--- a/src/system/dlt-system-options.c
+++ b/src/system/dlt-system-options.c
@@ -153,7 +153,6 @@ void init_configuration(DltSystemConfiguration *config)
/* File transfer */
config->Filetransfer.Enable = 0;
config->Filetransfer.ContextId = "FILE";
- config->Filetransfer.TimeDelay = 10;
config->Filetransfer.TimeStartup = 30;
config->Filetransfer.TimeoutBetweenLogs = 10;
config->Filetransfer.Count = 0;
@@ -310,10 +309,6 @@ int read_configuration_file(DltSystemConfiguration *config, char *file_name)
{
config->Filetransfer.TimeStartup = atoi(value);
}
- else if (strcmp(token, "FiletransferTimeDelay") == 0)
- {
- config->Filetransfer.TimeDelay = atoi(value);
- }
else if (strcmp(token, "FiletransferTimeoutBetweenLogs") == 0)
{
config->Filetransfer.TimeoutBetweenLogs = atoi(value);
diff --git a/src/system/dlt-system-process-handling.c b/src/system/dlt-system-process-handling.c
index eb310b0..57bd774 100644
--- a/src/system/dlt-system-process-handling.c
+++ b/src/system/dlt-system-process-handling.c
@@ -50,12 +50,26 @@
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
-#include <pthread.h>
#include <limits.h>
-volatile DltSystemThreads threads;
+#include <systemd/sd-journal.h>
+#include <poll.h>
+#include <sys/timerfd.h>
+#include <time.h>
DLT_IMPORT_CONTEXT(dltsystem)
+DLT_IMPORT_CONTEXT(syslogContext)
+DLT_IMPORT_CONTEXT(journalContext)
+DLT_IMPORT_CONTEXT(watchdogContext)
+DLT_IMPORT_CONTEXT(procContext)
+DLT_IMPORT_CONTEXT(filetransferContext)
+extern DltContext logfileContext[DLT_SYSTEM_LOG_FILE_MAX];
+
+volatile uint8_t quit = 0;
+
+#if defined(DLT_FILETRANSFER_ENABLE)
+extern s_ft_inotify ino;
+#endif
int daemonize()
{
@@ -106,105 +120,195 @@ int daemonize()
return 0;
}
-void start_threads(DltSystemConfiguration *config)
+/* Unregisters all DLT Contexts and closes all file descriptors */
+void cleanup_processes(struct pollfd *pollfd, sd_journal *j, DltSystemConfiguration *config)
{
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-process-handling, start threads"));
+ //Syslog cleanup
+ if (config->Syslog.Enable)
+ DLT_UNREGISTER_CONTEXT(syslogContext);
+
+ //Journal cleanup
+#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
+ if (config->Journal.Enable)
+ DLT_UNREGISTER_CONTEXT(journalContext);
+ if(j != NULL)
+ sd_journal_close(j);
+#endif
- int i;
- threads.count = 0;
- threads.shutdown = 0;
+ //Logfile cleanup
+ if (config->LogFile.Enable) {
+ for (int i = 0; i < config->LogFile.Count; i++)
+ DLT_UNREGISTER_CONTEXT(logfileContext[i]);
+ }
- for (i = 0; i < MAX_THREADS; i++)
- threads.threads[i] = 0;
+ //LogProcess cleanup
+ if (config->LogProcesses.Enable) {
+ DLT_UNREGISTER_CONTEXT(procContext);
+ }
+ //Watchdog cleanup
#if defined(DLT_SYSTEMD_WATCHDOG_ENABLE)
- start_thread(config, watchdog_thread, "systemd watchdog");
+ DLT_UNREGISTER_CONTEXT(watchdogContext);
#endif
- if (config->Shell.Enable)
- init_shell();
-
- if (config->LogFile.Enable)
- start_thread(config, logfile_thread, "log file");
-
+ //FileTransfer cleanup
#if defined(DLT_FILETRANSFER_ENABLE)
- if (config->Filetransfer.Enable)
- start_thread(config, filetransfer_thread, "file transfer");
+ if (config->Filetransfer.Enable) {
+ DLT_UNREGISTER_CONTEXT(filetransferContext);
+ }
#endif
- if (config->LogProcesses.Enable)
- start_thread(config, logprocess_thread, "log process");
-
- if (config->Syslog.Enable)
- start_thread(config, syslog_thread, "syslog");
-
-#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
-
- if (config->Journal.Enable)
- start_thread(config, journal_thread, "systemd journal");
-
-#endif
+ for (int i = 0; i < MAX_FD_NUMBER; i++) {
+ if(pollfd[i].fd > 0)
+ close(pollfd[i].fd);
+ }
}
-/**
- * Start a thread and add it to the thread pool.
- */
-
-void start_thread(DltSystemConfiguration *conf,
- void (thread)(void *), const char *name)
+/* Creates timer for LogFile and LogProcess, that need to be called every second. */
+int register_timer_fd(struct pollfd *pollfd, int fdcnt)
{
- 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;
+ struct itimerspec timerValue;
+ memset(&timerValue, '\0', sizeof(timerValue));
+ timerValue.it_value.tv_sec = 1;
+ timerValue.it_value.tv_nsec = 0;
+ timerValue.it_interval.tv_sec = 1;
+ timerValue.it_interval.tv_nsec = 0;
+
+ int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
+ if (timerfd < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Failed to create timer fd"));
+ return -1;
}
+ pollfd[fdcnt].fd = timerfd;
+ pollfd[fdcnt].events = POLLIN;
- DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Creating thread for "),
- DLT_STRING(name),
- DLT_STRING("\n"));
-
- pthread_t pt;
+ if (timerfd_settime(timerfd, 0, &timerValue, NULL) < 0) { // init timer with 1 second
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Could not start timer"));
+ return -1;
+ }
+ return 0;
+}
- 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"));
+/* Routine for executing LogProcess and LogFile, when timer expires */
+void timer_fd_handler(int fd, DltSystemConfiguration *config)
+{
+ long int timersElapsed = 0;
+ int r = read(fd, &timersElapsed, 8); // only needed to reset fd event
+ if (r < 0)
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while reading timer fd: "),
+ DLT_STRING(strerror(r)));
+
+ if(config->LogProcesses.Enable)
+ logprocess_fd_handler(config);
+ if(config->LogFile.Enable)
+ logfile_fd_handler(config);
}
-/**
- * Wait for threads to exit.
- * There's not actually a condition currently
- * to bail out of file transfer without a signal.
- */
-void join_threads()
+void start_dlt_system_processes(DltSystemConfiguration *config)
{
- int i;
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-process-handling, waiting for threads to exit."));
+ DLT_STRING("dlt-system-process-handling, start threads"));
- if (threads.count < 1) {
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-process-handling, no threads, waiting for signal."));
- sleep(UINT_MAX);
+ if (config->Shell.Enable)
+ init_shell();
+
+ int fdcnt = 0;
+
+ /* Init FDs for all activated processes*/
+ struct pollfd pollfd[MAX_FD_NUMBER]; // Holds all FDs and events
+ uint8_t fdType[MAX_FD_NUMBER]; // Holds corresponding enum for process identification
+
+ for(int cnt = 0 ; cnt < MAX_FD_NUMBER ; cnt++) {
+ pollfd[cnt].fd = 0;
+ pollfd[cnt].events = 0;
}
- else {
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-process-handling, thread count: "),
- DLT_INT(threads.count));
-
- for (i = 0; i < threads.count; i++) {
- pthread_join(threads.threads[i], NULL);
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-process-handling, thread exit: "),
- DLT_INT(threads.threads[i]));
+
+ //init FD for LogFile and LogProcesses
+ if (config->LogProcesses.Enable || config->LogFile.Enable) {
+ fdType[fdcnt] = fdType_timer;
+ if (register_timer_fd(pollfd, fdcnt) == 0) {
+ if(config->LogProcesses.Enable)
+ logprocess_init(config);
+ if(config->LogFile.Enable)
+ logfile_init(config);
+ fdcnt++;
}
}
+
+ //init FD for Syslog
+ int syslogSock = 0;
+ if (config->Syslog.Enable) {
+ fdType[fdcnt] = fdType_syslog;
+ syslogSock = register_syslog_fd(pollfd, fdcnt, config);
+ fdcnt++;
+ }
+
+ //init FD for Journal
+ sd_journal *j = NULL;
+#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
+ if (config->Journal.Enable) {
+ register_journal_fd(&j, pollfd, fdcnt, config);
+ fdType[fdcnt] = fdType_journal;
+ fdcnt++;
+ }
+#endif
+
+ //init FD for FileTransfer
+#if defined(DLT_FILETRANSFER_ENABLE)
+ if (config->Filetransfer.Enable) {
+ init_filetransfer_dirs(config);
+ pollfd[fdcnt].fd = ino.handle;
+ pollfd[fdcnt].events = POLLIN;
+ fdType[fdcnt] = fdType_filetransfer;
+ fdcnt++;
+ }
+#endif
+
+ //init FD for Watchdog
+#if defined(DLT_SYSTEMD_WATCHDOG_ENABLE)
+ fdType[fdcnt] = fdType_watchdog;
+ register_watchdog_fd(pollfd, fdcnt);
+#endif
+
+ while (quit == 0)
+ {
+ int ready;
+ ready = poll(pollfd, MAX_FD_NUMBER, -1);
+ if (ready == -1 && quit == 0)
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Error while poll. Exit with: "),
+ DLT_STRING(strerror(ready)));
+
+ for (int i = 0; i < MAX_FD_NUMBER; i++) {
+ if(pollfd[i].revents & POLLIN){
+ if (fdType[i] == fdType_syslog && syslogSock > 0) {
+ syslog_fd_handler(syslogSock);
+ }
+ else if (fdType[i] == fdType_timer) {
+ timer_fd_handler(pollfd[i].fd, config);
+ }
+ #if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
+ else if((fdType[i] == fdType_journal) && (j != NULL)) {
+ if(sd_journal_process(j) == SD_JOURNAL_APPEND) {
+ journal_fd_handler(j, config);
+ }
+ }
+ #endif
+ #if defined(DLT_FILETRANSFER_ENABLE)
+ else if (fdType[i] == fdType_filetransfer) {
+ filetransfer_fd_handler(config);
+ }
+ #endif
+ #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE)
+ else if (fdType[i] == fdType_watchdog) {
+ watchdog_fd_handler(pollfd[i].fd);
+ }
+ #endif
+ }
+ }
+ }
+
+ cleanup_processes(pollfd, j, config);
+ exit(0);
}
void dlt_system_signal_handler(int sig)
@@ -220,12 +324,11 @@ void dlt_system_signal_handler(int sig)
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
DLT_STRING("dlt-system-process-handling, exit, signal: "),
DLT_INT(sig));
- exit(0);
+ quit = 1;
break;
default:
DLT_LOG(dltsystem, DLT_LOG_WARN,
DLT_STRING("dlt-system-process-handling, unknown signal!"));
break;
}
-}
-
+} \ No newline at end of file
diff --git a/src/system/dlt-system-processes.c b/src/system/dlt-system-processes.c
index 71f5a1d..a6e4918 100644
--- a/src/system/dlt-system-processes.c
+++ b/src/system/dlt-system-processes.c
@@ -44,7 +44,6 @@
*******************************************************************************/
-#include <pthread.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
@@ -60,7 +59,7 @@
#define SEND_MODE_ONCE 1
#define SEND_MODE_ON 2
-extern DltSystemThreads threads;
+int process_delays[DLT_SYSTEM_LOG_PROCESSES_MAX];
DLT_IMPORT_CONTEXT(dltsystem)
DLT_DECLARE_CONTEXT(procContext)
@@ -125,7 +124,7 @@ void send_process(LogProcessOptions const *popts, int n)
DLT_STRING("not running!"));
}
-void logprocess_thread(void *v_conf)
+void logprocess_init(void *v_conf)
{
DLT_LOG(dltsystem, DLT_LOG_DEBUG,
DLT_STRING("dlt-system-processes, in thread."));
@@ -133,29 +132,26 @@ void logprocess_thread(void *v_conf)
DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
DLT_REGISTER_CONTEXT(procContext, conf->LogProcesses.ContextId, "Log Processes");
- int process_delays[DLT_SYSTEM_LOG_PROCESSES_MAX];
- int i;
-
- for (i = 0; i < conf->LogProcesses.Count; i++)
+ for (int i = 0; i < conf->LogProcesses.Count; i++)
process_delays[i] = conf->LogProcesses.TimeDelay[i];
+}
- while (!threads.shutdown) {
- sleep(1);
-
- for (i = 0; i < conf->LogProcesses.Count; i++) {
- if (conf->LogProcesses.Mode[i] == SEND_MODE_OFF)
- continue;
+void logprocess_fd_handler(void *v_conf)
+{
+ DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
+ for (int i = 0; i < conf->LogProcesses.Count; i++) {
+ if (conf->LogProcesses.Mode[i] == SEND_MODE_OFF)
+ continue;
- if (process_delays[i] <= 0) {
- send_process(&(conf->LogProcesses), i);
- process_delays[i] = conf->LogProcesses.TimeDelay[i];
+ if (process_delays[i] <= 0) {
+ send_process(&(conf->LogProcesses), i);
+ process_delays[i] = conf->LogProcesses.TimeDelay[i];
- if (conf->LogProcesses.Mode[i] == SEND_MODE_ONCE)
- conf->LogProcesses.Mode[i] = SEND_MODE_OFF;
- }
- else {
- process_delays[i]--;
- }
+ if (conf->LogProcesses.Mode[i] == SEND_MODE_ONCE)
+ conf->LogProcesses.Mode[i] = SEND_MODE_OFF;
+ }
+ else {
+ process_delays[i]--;
}
}
}
diff --git a/src/system/dlt-system-syslog.c b/src/system/dlt-system-syslog.c
index 7808613..2995441 100644
--- a/src/system/dlt-system-syslog.c
+++ b/src/system/dlt-system-syslog.c
@@ -44,16 +44,16 @@
*******************************************************************************/
-#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
-#include "dlt-system.h"
+#include <systemd/sd-journal.h>
+#include <poll.h>
-extern DltSystemThreads threads;
+#include "dlt-system.h"
DLT_IMPORT_CONTEXT(dltsystem)
DLT_DECLARE_CONTEXT(syslogContext)
@@ -134,29 +134,27 @@ int read_socket(int sock)
recv_data[bytes_read] = '\0';
if (bytes_read != 0)
+ {
DLT_LOG(syslogContext, DLT_LOG_INFO, DLT_STRING(recv_data));
+ }
return bytes_read;
}
-void syslog_thread(void *v_conf)
+int register_syslog_fd(struct pollfd *pollfd, int i, DltSystemConfiguration *config)
{
- DLT_LOG(dltsystem, DLT_LOG_DEBUG,
- DLT_STRING("dlt-system-syslog, in thread."));
-
- DltSystemConfiguration *conf = (DltSystemConfiguration *)v_conf;
- DLT_REGISTER_CONTEXT(syslogContext, conf->Syslog.ContextId, "SYSLOG Adapter");
-
- int sock = init_socket(conf->Syslog);
-
- if (sock < 0)
- return;
-
- while (!threads.shutdown)
- if (read_socket(sock) < 0) {
- close(sock);
- return;
- }
-
- close (sock);
+ DLT_REGISTER_CONTEXT(syslogContext, config->Syslog.ContextId, "SYSLOG Adapter");
+ int syslogSock = init_socket(config->Syslog);
+ if (syslogSock < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Could not init syslog socket\n"));
+ return -1;
+ }
+ pollfd[i].fd = syslogSock;
+ pollfd[i].events = POLLIN;
+ return syslogSock;
}
+
+void syslog_fd_handler(int syslogSock)
+{
+ read_socket(syslogSock);
+} \ No newline at end of file
diff --git a/src/system/dlt-system-watchdog.c b/src/system/dlt-system-watchdog.c
index fd6bcac..a2b01de 100644
--- a/src/system/dlt-system-watchdog.c
+++ b/src/system/dlt-system-watchdog.c
@@ -29,135 +29,95 @@
#if defined(DLT_SYSTEMD_WATCHDOG_ENABLE)
#include <stdio.h>
#include <stdlib.h>
-#include <pthread.h>
+#include <string.h>
#include <sys/timerfd.h>
#include "dlt.h"
#include "dlt-system.h"
-
-#include "sd-daemon.h"
-
+#include <poll.h>
+#include <systemd/sd-daemon.h>
DLT_DECLARE_CONTEXT(watchdogContext)
DLT_IMPORT_CONTEXT(dltsystem)
-extern DltSystemThreads threads;
-
-typedef struct
-{
- int timer_fd;
- unsigned long long wakeups_missed;
-} PeriodicData;
-
-void wait_period (PeriodicData *info)
-{
- unsigned long long missed;
-
- if (read (info->timer_fd, &missed, sizeof (missed)) < 0)
- DLT_LOG(watchdogContext, DLT_LOG_ERROR,
- DLT_STRING("Could not read from timer file descriptor in watchdog.\n"));
-
- if (missed > 0)
- info->wakeups_missed += (missed - 1);
-}
-
-int make_periodic(unsigned int period, PeriodicData *info)
+int calculate_period(struct itimerspec *itval)
{
unsigned int ns;
unsigned int sec;
- int fd;
- struct itimerspec itval;
+ char str[512];
+ char *watchdogUSec;
+ unsigned int watchdogTimeoutSeconds;
+ unsigned int notifiyPeriodNSec;
+
+ watchdogUSec = getenv("WATCHDOG_USEC");
- if (info == 0) {
- DLT_LOG(watchdogContext, DLT_LOG_ERROR,
- DLT_STRING("Invalid function parameters used for function make_periodic.\n"));
+ if (watchdogUSec == NULL) {
+ DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("systemd watchdog timeout (WATCHDOG_USEC) is null\n"));
return -1;
}
+
+ DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING("watchdogusec: "), DLT_STRING(watchdogUSec));
+ watchdogTimeoutSeconds = atoi(watchdogUSec);
- /* Create the timer */
- fd = timerfd_create (CLOCK_MONOTONIC, 0);
-
- info->wakeups_missed = 0;
- info->timer_fd = fd;
-
- if (fd == -1) {
- DLT_LOG(watchdogContext, DLT_LOG_ERROR,
- DLT_STRING("Can't create timer filedescriptor.\n"));
+ if (watchdogTimeoutSeconds <= 0) {
+ snprintf(str, 512, "systemd watchdog timeout incorrect: %u\n", watchdogTimeoutSeconds);
+ DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING(str));
return -1;
}
+ /* Calculate half of WATCHDOG_USEC in ns for timer tick */
+ notifiyPeriodNSec = watchdogTimeoutSeconds / 2;
+
+ snprintf(str,
+ 512,
+ "systemd watchdog timeout: %u nsec - timer will be initialized: %u nsec\n",
+ watchdogTimeoutSeconds,
+ notifiyPeriodNSec);
+ DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING(str));
+
/* Make the timer periodic */
- sec = period / 1000000;
- ns = (period - (sec * 1000000)) * 1000;
- itval.it_interval.tv_sec = sec;
- itval.it_interval.tv_nsec = ns;
- itval.it_value.tv_sec = sec;
- itval.it_value.tv_nsec = ns;
-
- return timerfd_settime (fd, 0, &itval, NULL);
+ sec = notifiyPeriodNSec / 1000000;
+ ns = (notifiyPeriodNSec - (sec * 1000000)) * 1000;
+ itval->it_interval.tv_sec = sec;
+ itval->it_interval.tv_nsec = ns;
+ itval->it_value.tv_sec = sec;
+ itval->it_value.tv_nsec = ns;
+
+ return 0;
}
-
-void watchdog_thread(void *v_conf)
+int register_watchdog_fd(struct pollfd *pollfd, int fdcnt)
{
- char str[512];
- char *watchdogUSec;
- unsigned int watchdogTimeoutSeconds;
- unsigned int notifiyPeriodNSec;
- PeriodicData info;
-
DLT_REGISTER_CONTEXT(watchdogContext, "DOG", "dlt system watchdog context.");
+ struct itimerspec timerValue;
+ if (calculate_period(&timerValue) != 0)
+ return -1;
- sleep(1);
-
- DLT_LOG(watchdogContext, DLT_LOG_INFO, DLT_STRING("Watchdog thread started.\n"));
-
- if (v_conf == 0) {
- DLT_LOG(watchdogContext, DLT_LOG_ERROR,
- DLT_STRING("Invalid function parameters used for function watchdog_thread.\n"));
- return;
+ int timerfd = timerfd_create(CLOCK_MONOTONIC, 0);
+ if (timerfd < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Failed to create timer fd\n"));
+ return -1;
}
+ pollfd[fdcnt].fd = timerfd;
+ pollfd[fdcnt].events = POLLIN;
- watchdogUSec = getenv("WATCHDOG_USEC");
-
- if (watchdogUSec) {
- DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING("watchdogusec: "), DLT_STRING(watchdogUSec));
-
- watchdogTimeoutSeconds = atoi(watchdogUSec);
-
- if (watchdogTimeoutSeconds > 0) {
-
- /* Calculate half of WATCHDOG_USEC in ns for timer tick */
- notifiyPeriodNSec = watchdogTimeoutSeconds / 2;
-
- snprintf(str,
- 512,
- "systemd watchdog timeout: %u nsec - timer will be initialized: %u nsec\n",
- watchdogTimeoutSeconds,
- notifiyPeriodNSec);
- DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING(str));
-
- if (make_periodic (notifiyPeriodNSec, &info) < 0) {
- DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("Could not initialize systemd watchdog timer\n"));
- return;
- }
+ if (timerfd_settime(timerfd, 0, &timerValue, NULL) < 0) {
+ DLT_LOG(dltsystem, DLT_LOG_ERROR, DLT_STRING("Could not start timer\n"));
+ return -1;
+ }
+ return 0;
+}
- while (1) {
- if (sd_notify(0, "WATCHDOG=1") < 0)
- DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("Could not reset systemd watchdog\n"));
+void watchdog_fd_handler(int fd)
+{
+ long int timersElapsed = 0;
+ int r = read(fd, &timersElapsed, 8); // only needed to reset fd event
+ if(r < 0)
+ DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("Could not reset systemd watchdog. Exit with: "),
+ DLT_STRING(strerror(r)));
- DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING("systemd watchdog waited periodic\n"));
+ if (sd_notify(0, "WATCHDOG=1") < 0)
+ DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("Could not reset systemd watchdog\n"));
- /* Wait for next period */
- wait_period(&info);
- }
- }
- else {
- snprintf(str, 512, "systemd watchdog timeout incorrect: %u\n", watchdogTimeoutSeconds);
- DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING(str));
- }
- }
- else {
- DLT_LOG(watchdogContext, DLT_LOG_ERROR, DLT_STRING("systemd watchdog timeout (WATCHDOG_USEC) is null\n"));
- }
+ DLT_LOG(watchdogContext, DLT_LOG_DEBUG, DLT_STRING("systemd watchdog waited periodic\n"));
}
-#endif
+#endif \ No newline at end of file
diff --git a/src/system/dlt-system.c b/src/system/dlt-system.c
index 0b3cd0d..92ea466 100644
--- a/src/system/dlt-system.c
+++ b/src/system/dlt-system.c
@@ -112,11 +112,10 @@ int main(int argc, char *argv[])
signal(SIGQUIT, dlt_system_signal_handler);
signal(SIGINT, dlt_system_signal_handler);
- DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Launching threads."));
+ DLT_LOG(dltsystem, DLT_LOG_DEBUG, DLT_STRING("Initializing all processes and starting poll for events."));
- start_threads(&config);
- join_threads();
+ start_dlt_system_processes(&config);
return 0;
}
diff --git a/src/system/dlt-system.conf b/src/system/dlt-system.conf
index 4010b70..58f56fa 100644
--- a/src/system/dlt-system.conf
+++ b/src/system/dlt-system.conf
@@ -90,10 +90,6 @@ FiletransferContextId = FILE
# Time in seconds after startup of dlt-system when first file is transfered (Default: 0)
FiletransferTimeStartup = 0
-# Time to wait when transfered file is deleted and next file transfer starts (Default: 10)
-# Time in seconds
-FiletransferTimeDelay = 10
-
# Time in ms seconds to wait between two file transfer logs of a single file to DLT. (Default: 10)
FiletransferTimeoutBetweenLogs = 5
@@ -202,4 +198,3 @@ LogProcessTimeDelay = 5
# LogProcessFilename = stat
# LogProcessMode = 2
# LogProcessTimeDelay = 1
-
diff --git a/src/system/dlt-system.h b/src/system/dlt-system.h
index e60cb9b..9be1cf4 100644
--- a/src/system/dlt-system.h
+++ b/src/system/dlt-system.h
@@ -54,6 +54,9 @@
#ifndef DLT_SYSTEM_H_
#define DLT_SYSTEM_H_
+#include <systemd/sd-journal.h>
+#include <poll.h>
+
/* DLT related includes. */
#include "dlt.h"
#include "dlt_common.h"
@@ -70,13 +73,29 @@
#define MAX_LINE 1024
-#define MAX_THREADS 8
+/** Total number of file descriptors needed for processing all features:
+* - Journal file descriptor
+* - Syslog file descriptor
+* - Timer file descriptor for processing LogFile and LogProcesses every second
+* - Inotify file descriptor for FileTransfer
+* - Timer file descriptor for Watchdog
+*/
+#define MAX_FD_NUMBER 5
/* Macros */
#define MALLOC_ASSERT(x) if (x == NULL) { \
fprintf(stderr, "Out of memory\n"); \
abort(); }
+/* enum for classification of FD */
+enum fdType {
+ fdType_journal = 0,
+ fdType_syslog,
+ fdType_filetransfer,
+ fdType_timer,
+ fdType_watchdog,
+};
+
/**
* Configuration structures.
* Please see dlt-system.conf for explanation of all the options.
@@ -114,7 +133,6 @@ typedef struct {
int Enable;
char *ContextId;
int TimeStartup;
- int TimeDelay;
int TimeoutBetweenLogs;
char *TempDir;
@@ -126,6 +144,11 @@ typedef struct {
} FiletransferOptions;
typedef struct {
+ int handle;
+ int fd[DLT_SYSTEM_LOG_DIRS_MAX];
+} s_ft_inotify;
+
+typedef struct {
int Enable;
/* Variable number of files to transfer */
@@ -158,12 +181,6 @@ typedef struct {
LogProcessOptions LogProcesses;
} DltSystemConfiguration;
-typedef struct {
- pthread_t threads[MAX_THREADS];
- int count;
- int shutdown;
-} DltSystemThreads;
-
/**
* Forward declarations for the whole application
*/
@@ -172,28 +189,28 @@ typedef struct {
int read_command_line(DltSystemCliOptions *options, int argc, char *argv[]);
int read_configuration_file(DltSystemConfiguration *config, char *file_name);
-/* In dlt-process-handling.c */
+/* For 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 init_shell();
void dlt_system_signal_handler(int sig);
-void register_with_dlt(DltSystemConfiguration *config);
-/* Threads */
-void init_shell();
-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 watchdog_thread(void *v_conf);
-#endif
-
-#if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
-void journal_thread(void *v_conf);
-#endif
+/* Main function for creating/registering all needed file descriptors and starting the poll for all of them. */
+void start_dlt_system_processes(DltSystemConfiguration *config);
+
+/* Init process, create file descriptors and register them into main pollfd. */
+int register_watchdog_fd(struct pollfd *pollfd, int fdcnt);
+int init_filetransfer_dirs(DltSystemConfiguration *config);
+void logfile_init(void *v_conf);
+void logprocess_init(void *v_conf);
+void register_journal_fd(sd_journal **j, struct pollfd *pollfd, int i, DltSystemConfiguration *config);
+int register_syslog_fd(struct pollfd *pollfd, int i, DltSystemConfiguration *config);
+
+/* Routines that are called, when a fd event was raised. */
+void logfile_fd_handler(void *v_conf);
+void logprocess_fd_handler(void *v_conf);
+void filetransfer_fd_handler(DltSystemConfiguration *config);
+void watchdog_fd_handler(int fd);
+void journal_fd_handler(sd_journal *j, DltSystemConfiguration *config);
+void syslog_fd_handler(int syslogSock);
#endif /* DLT_SYSTEM_H_ */