Fixed: Security Issue by Command Injection in DLT System.
[profile/ivi/dlt-daemon.git] / src / system / dlt-system-process-handling.c
1 /**
2  * @licence app begin@
3  * Copyright (C) 2012  BMW AG
4  *
5  * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
6  *
7  * Contributions are licensed to the GENIVI Alliance under one or more
8  * Contribution License Agreements.
9  *
10  * \copyright
11  * This Source Code Form is subject to the terms of the
12  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
13  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14  *
15  *
16  * \author Lassi Marttala <lassi.lm.marttala@partner.bmw.de> BMW 2012
17  *
18  * \file dlt-system-process-handling.c
19  * For further information see http://www.genivi.org/.
20  * @licence end@
21  */
22
23 /*******************************************************************************
24 **                                                                            **
25 **  SRC-MODULE: dlt-systemprocess-handling.c                                                  **
26 **                                                                            **
27 **  TARGET    : linux                                                         **
28 **                                                                            **
29 **  PROJECT   : DLT                                                           **
30 **                                                                            **
31 **  AUTHOR    : Lassi Marttala <lassi.lm.marttala@partner.bmw.de>             **
32 **                                                                            **
33 **  PURPOSE   :                                                               **
34 **                                                                            **
35 **  REMARKS   :                                                               **
36 **                                                                            **
37 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
38 **                                                                            **
39 **  TO BE CHANGED BY USER [yes/no]: no                                        **
40 **                                                                            **
41 *******************************************************************************/
42 #include "dlt.h"
43
44 #include "dlt-system.h"
45
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <fcntl.h>
49 #include <signal.h>
50 #include <sys/wait.h>
51 #include <pthread.h>
52 #include <limits.h>
53
54 volatile DltSystemThreads threads;
55
56 DLT_IMPORT_CONTEXT(dltsystem);
57
58 int daemonize()
59 {
60         DLT_LOG(dltsystem,DLT_LOG_DEBUG,
61                         DLT_STRING("dlt-system-process-handling, daemonize"));
62
63         // Fork new process
64         int f = fork();
65
66         if(f < 0)
67                 return f;
68         if(f > 0)
69                 exit(0);
70
71         // Create a new process group
72         if(setsid() < 0)
73                 return -1;
74
75         /**
76          *  Close all file descriptors and point
77          *  stdin, stdout and stderr to /dev/null
78          */
79         int i;
80     for(i = getdtablesize(); i >= 0; i--)
81         close(i);
82
83         int fd = open("/dev/null",O_RDWR);
84     if(fd < 0)
85     {
86         return -1;
87     }
88
89     if(dup(fd) < 0 || dup(fd) < 0)
90     {
91         close(fd);
92                 return -1;
93     }
94
95         /**
96          * Ignore signals related to child processes and
97          * terminal handling.
98          */
99     signal(SIGCHLD, SIG_IGN);
100     signal(SIGTSTP, SIG_IGN);
101     signal(SIGTTOU, SIG_IGN);
102     signal(SIGTTIN, SIG_IGN);
103     //no close(fd); - we just intentionally pointed stdx to null! tbd: set ignore for coverity
104         return 0;
105 }
106
107 void start_threads(DltSystemConfiguration *config)
108 {
109         DLT_LOG(dltsystem,DLT_LOG_DEBUG,
110                         DLT_STRING("dlt-system-process-handling, start threads"));
111
112         int i;
113         threads.count = 0;
114         threads.shutdown = 0;
115         for(i = 0;i < MAX_THREADS; i++)
116         {
117                 threads.threads[i] = 0;
118         }
119
120 #if defined(DLT_SYSTEMD_WATCHDOG_ENABLE)
121         start_systemd_watchdog(config);
122 #endif
123
124         if(config->Shell.Enable)
125                 init_shell();
126
127         if(config->LogFile.Enable)
128                 start_logfile(config);
129
130         if(config->Filetransfer.Enable)
131                 start_filetransfer(config);
132
133         if(config->LogProcesses.Enable)
134                 start_logprocess(config);
135
136         if(config->Syslog.Enable)
137                 start_syslog(config);
138
139 #if defined(DLT_SYSTEMD_JOURNAL_ENABLE)
140         if(config->Journal.Enable)
141                 start_systemd_journal(config);
142 #endif
143 }
144
145 /**
146  * Wait for threads to exit.
147  * There's not actually a condition currently
148  * to bail out of file transfer without a signal.
149  */
150 void join_threads()
151 {
152         int i;
153         DLT_LOG(dltsystem, DLT_LOG_DEBUG,
154                         DLT_STRING("dlt-system-process-handling, waiting for threads to exit."));
155
156         if(threads.count < 1)
157         {
158                 DLT_LOG(dltsystem, DLT_LOG_DEBUG,
159                                 DLT_STRING("dlt-system-process-handling, no threads, waiting for signal."));
160                 sleep(UINT_MAX);
161         }
162         else
163         {
164                 DLT_LOG(dltsystem,DLT_LOG_DEBUG,
165                                 DLT_STRING("dlt-system-process-handling, thread count: "),
166                                 DLT_INT(threads.count));
167
168                 for (i = 0; i < threads.count; i++)
169                 {
170                         pthread_join(threads.threads[i], NULL);
171                         DLT_LOG(dltsystem,DLT_LOG_DEBUG,
172                                         DLT_STRING("dlt-system-process-handling, thread exit: "),
173                                         DLT_INT(threads.threads[i]));
174                 }
175         }
176 }
177
178 void dlt_system_signal_handler(int sig)
179 {
180         DLT_LOG(dltsystem,DLT_LOG_DEBUG,
181                         DLT_STRING("dlt-system-process-handling, signal handler"));
182
183     switch (sig)
184     {
185         case SIGHUP:
186         case SIGTERM:
187         case SIGINT:
188         case SIGQUIT:
189                 DLT_LOG(dltsystem,DLT_LOG_DEBUG,
190                                 DLT_STRING("dlt-system-process-handling, exit, signal: "),
191                                 DLT_INT(sig));
192                 exit(0);
193                 break;
194         default:
195                 DLT_LOG(dltsystem,DLT_LOG_WARN,
196                                 DLT_STRING("dlt-system-process-handling, unknown signal!"));
197                 break;
198         }
199 }
200