automotive-dlt
dlt-system-process-handling.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 /*******************************************************************************
28 ** **
29 ** SRC-MODULE: dlt-systemprocess-handling.c **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Lassi Marttala <lassi.lm.marttala@partner.bmw.de> **
36 ** **
37 ** PURPOSE : **
38 ** **
39 ** REMARKS : **
40 ** **
41 ** PLATFORM DEPENDANT [yes/no]: yes **
42 ** **
43 ** TO BE CHANGED BY USER [yes/no]: no **
44 ** **
45 *******************************************************************************/
46 #include "dlt.h"
47 
48 #include "dlt-system.h"
49 
50 #include <unistd.h>
51 #include <stdlib.h>
52 #include <fcntl.h>
53 #include <signal.h>
54 #include <sys/wait.h>
55 #include <pthread.h>
56 #include <limits.h>
57 
59 
61 
62 int daemonize()
63 {
64  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
65  DLT_STRING("dlt-system-process-handling, daemonize"));
66 
67  // Fork new process
68  int f = fork();
69 
70  if(f < 0)
71  return f;
72  if(f > 0)
73  exit(0);
74 
75  // Create a new process group
76  if(setsid() < 0)
77  return -1;
78 
83  int i;
84  for(i = getdtablesize(); i >= 0; i--)
85  close(i);
86 
87  int fd = open("/dev/null",O_RDWR);
88  if(fd < 0)
89  {
90  return -1;
91  }
92 
93  if(dup(fd) < 0 || dup(fd) < 0)
94  {
95  close(fd);
96  return -1;
97  }
98 
103  signal(SIGCHLD, SIG_IGN);
104  signal(SIGTSTP, SIG_IGN);
105  signal(SIGTTOU, SIG_IGN);
106  signal(SIGTTIN, SIG_IGN);
107  //no close(fd); - we just intentionally pointed stdx to null! tbd: set ignore for coverity
108  return 0;
109 }
110 
112 {
113  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
114  DLT_STRING("dlt-system-process-handling, start threads"));
115 
116  int i;
117  threads.count = 0;
118  threads.shutdown = 0;
119  for(i = 0;i < MAX_THREADS; i++)
120  {
121  threads.threads[i] = 0;
122  }
123 
124 #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE)
125  start_systemd_watchdog(config);
126 #endif
127 
128  if(config->Shell.Enable)
129  init_shell();
130 
131  if(config->LogFile.Enable)
132  start_logfile(config);
133 
134  if(config->Filetransfer.Enable)
135  start_filetransfer(config);
136 
137  if(config->LogProcesses.Enable)
138  start_logprocess(config);
139 
140  if(config->Syslog.Enable)
141  start_syslog(config);
142 
143 #if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
144  if(config->Journal.Enable)
145  start_systemd_journal(config);
146 #endif
147 }
148 
155 {
156  int i;
157  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
158  DLT_STRING("dlt-system-process-handling, waiting for threads to exit."));
159 
160  if(threads.count < 1)
161  {
162  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
163  DLT_STRING("dlt-system-process-handling, no threads, waiting for signal."));
164  sleep(UINT_MAX);
165  }
166  else
167  {
168  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
169  DLT_STRING("dlt-system-process-handling, thread count: "),
170  DLT_INT(threads.count));
171 
172  for (i = 0; i < threads.count; i++)
173  {
174  pthread_join(threads.threads[i], NULL);
175  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
176  DLT_STRING("dlt-system-process-handling, thread exit: "),
177  DLT_INT(threads.threads[i]));
178  }
179  }
180 }
181 
183 {
184  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
185  DLT_STRING("dlt-system-process-handling, signal handler"));
186 
187  switch (sig)
188  {
189  case SIGHUP:
190  case SIGTERM:
191  case SIGINT:
192  case SIGQUIT:
193  DLT_LOG(dltsystem,DLT_LOG_DEBUG,
194  DLT_STRING("dlt-system-process-handling, exit, signal: "),
195  DLT_INT(sig));
196  exit(0);
197  break;
198  default:
199  DLT_LOG(dltsystem,DLT_LOG_WARN,
200  DLT_STRING("dlt-system-process-handling, unknown signal!"));
201  break;
202  }
203 }
204 
void start_threads(DltSystemConfiguration *config)
DLT_IMPORT_CONTEXT(dltsystem)
int daemonize()
DltKpiConfig config
Definition: dlt-kpi.c:37
volatile DltSystemThreads threads
void start_logfile(DltSystemConfiguration *conf)
SyslogOptions Syslog
Definition: dlt-system.h:155
#define DLT_INT(INT_VAR)
void dlt_system_signal_handler(int sig)
void start_filetransfer(DltSystemConfiguration *conf)
void join_threads()
ShellOptions Shell
Definition: dlt-system.h:154
FiletransferOptions Filetransfer
Definition: dlt-system.h:157
#define MAX_THREADS
Definition: dlt-system.h:75
LogFileOptions LogFile
Definition: dlt-system.h:158
JournalOptions Journal
Definition: dlt-system.h:156
#define DLT_STRING(TEXT)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
void start_logprocess(DltSystemConfiguration *conf)
void start_syslog(DltSystemConfiguration *conf)
LogProcessOptions LogProcesses
Definition: dlt-system.h:159
void init_shell()
pthread_t threads[MAX_THREADS]
Definition: dlt-system.h:163
#define NULL
Definition: dlt_common.h:232