automotive-dlt
dlt-system-options.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-system-options.c **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Lassi Marttala <lassi.lm.marttala@partner.bmw.de> **
36 ** Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
37 ** **
38 ** PURPOSE : **
39 ** **
40 ** REMARKS : **
41 ** **
42 ** PLATFORM DEPENDANT [yes/no]: yes **
43 ** **
44 ** TO BE CHANGED BY USER [yes/no]: no **
45 ** **
46 *******************************************************************************/
47 
48 /*******************************************************************************
49 ** Author Identity **
50 ********************************************************************************
51 ** **
52 ** Initials Name Company **
53 ** -------- ------------------------- ---------------------------------- **
54 ** lm Lassi Marttala BMW **
55 ** aw Alexander Wenzel BMW **
56 *******************************************************************************/
57 
58 #include "dlt-system.h"
59 
60 #include <stdlib.h>
61 #include <string.h>
62 
66 void usage(char *prog_name)
67 {
68  char version[255];
69  dlt_get_version(version,255);
70 
71  printf("Usage: %s [options]\n", prog_name);
72  printf("Application to forward syslog messages to DLT, transfer system information, logs and files.\n");
73  printf("%s\n", version);
74  printf("Options:\n");
75  printf(" -d Daemonize. Detach from terminal and run in background.\n");
76  printf(" -c filename Use configuration file. \n");
77  printf(" Default: %s\n", DEFAULT_CONF_FILE);
78  printf(" -h This help message.\n");
79 }
80 
85 {
87  options->Daemonize = 0;
88 }
89 
93 int read_command_line(DltSystemCliOptions *options, int argc, char *argv[])
94 {
95  init_cli_options(options);
96  int opt;
97 
98  while((opt = getopt(argc, argv, "c:hd")) != -1)
99  {
100  switch(opt) {
101  case 'd':
102  {
103  options->Daemonize = 1;
104  break;
105  }
106  case 'c':
107  {
108  options->ConfigurationFileName = malloc(strlen(optarg)+1);
110  strcpy(options->ConfigurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */
111  break;
112  }
113  case 'h':
114  {
115  usage(argv[0]);
116  exit(0);
117  return -1;//for parasoft
118  }
119  default:
120  {
121  fprintf(stderr, "Unknown option '%c'\n", optopt);
122  usage(argv[0]);
123  return -1;
124  }
125  }
126  }
127  return 0;
128 }
129 
134 {
135  int i = 0;
136 
137  // Common
138  config->ApplicationId = "SYS";
139 
140  // Shell
141  config->Shell.Enable = 0;
142 
143  // Syslog
144  config->Syslog.Enable = 0;
145  config->Syslog.ContextId = "SYSL";
146  config->Syslog.Port = 47111;
147 
148  // Journal
149  config->Journal.Enable = 0;
150  config->Journal.ContextId = "JOUR";
151  config->Journal.CurrentBoot = 1;
152  config->Journal.Follow = 0;
153  config->Journal.MapLogLevels = 1;
154 
155  // File transfer
156  config->Filetransfer.Enable = 0;
157  config->Filetransfer.ContextId = "FILE";
158  config->Filetransfer.TimeDelay = 10;
159  config->Filetransfer.TimeStartup = 30;
160  config->Filetransfer.TimeoutBetweenLogs = 10;
161  config->Filetransfer.Count = 0;
162  for(i = 0;i < DLT_SYSTEM_LOG_DIRS_MAX;i++)
163  {
164  config->Filetransfer.Directory[i] = NULL;
165  config->Filetransfer.Compression[i] = 0;
166  config->Filetransfer.CompressionLevel[i] = 5;
167  }
168 
169  // Log file
170  config->LogFile.Enable = 0;
171  config->LogFile.Count = 0;
172  for(i = 0;i < DLT_SYSTEM_LOG_FILE_MAX;i++)
173  {
174  config->LogFile.ContextId[i] = NULL;
175  config->LogFile.Filename[i] = NULL;
176  config->LogFile.Mode[i] = 0;
177  config->LogFile.TimeDelay[i] = 0;
178  }
179 
180  // Log process
181  config->LogProcesses.Enable = 0;
182  config->LogProcesses.ContextId = "PROC";
183  config->LogProcesses.Count = 0;
184  for(i = 0;i < DLT_SYSTEM_LOG_PROCESSES_MAX;i++)
185  {
186  config->LogProcesses.Name[i] = NULL;
187  config->LogProcesses.Filename[i] = NULL;
188  config->LogProcesses.Mode[i] = 0;
189  config->LogProcesses.TimeDelay[i] = 0;
190  }
191 }
192 
197 {
198  FILE *file;
199  char *line, *token, *value, *pch;
200  int ret = 0;
201 
202  init_configuration(config);
203 
204  file = fopen(file_name, "r");
205 
206  if(file == NULL)
207  {
208  fprintf(stderr, "dlt-system-options, could not open configuration file.\n");
209  return -1;
210  }
211 
212  line = malloc(MAX_LINE);
213  token = malloc(MAX_LINE);
214  value = malloc(MAX_LINE);
215 
216  MALLOC_ASSERT(line);
217  MALLOC_ASSERT(token);
218  MALLOC_ASSERT(value);
219 
220  while(fgets(line, MAX_LINE, file) != NULL)
221  {
222  token[0] = 0;
223  value[0] = 0;
224 
225  pch = strtok (line, " =\r\n");
226  while(pch != NULL)
227  {
228  if(pch[0] == '#')
229  break;
230 
231  if(token[0] == 0)
232  {
233  strncpy(token, pch, MAX_LINE-1);
234  token[MAX_LINE-1]=0;
235  }
236  else
237  {
238  strncpy(value, pch, MAX_LINE);
239  value[MAX_LINE-1]=0;
240  break;
241  }
242 
243  pch = strtok (NULL, " =\r\n");
244  }
245 
246  if(token[0] && value[0])
247  {
248  // Common
249  if(strcmp(token, "ApplicationId") == 0)
250  {
251  config->ApplicationId = malloc(strlen(value)+1);
252  MALLOC_ASSERT(config->ApplicationId);
253  strcpy(config->ApplicationId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
254  }
255 
256  // Shell
257  else if(strcmp(token, "ShellEnable") == 0)
258  {
259  config->Shell.Enable = atoi(value);
260  }
261 
262  // Syslog
263  else if(strcmp(token, "SyslogEnable") == 0)
264  {
265  config->Syslog.Enable = atoi(value);
266  }
267  else if(strcmp(token, "SyslogContextId") == 0)
268  {
269  config->Syslog.ContextId = malloc(strlen(value)+1);
270  MALLOC_ASSERT(config->Syslog.ContextId);
271  strcpy(config->Syslog.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
272  }
273  else if(strcmp(token, "SyslogPort") == 0)
274  {
275  config->Syslog.Port = atoi(value);
276  }
277 
278  // Journal
279  else if(strcmp(token, "JournalEnable") == 0)
280  {
281  config->Journal.Enable = atoi(value);
282  }
283  else if(strcmp(token, "JournalContextId") == 0)
284  {
285  config->Journal.ContextId = malloc(strlen(value)+1);
287  strcpy(config->Journal.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
288  }
289  else if(strcmp(token, "JournalCurrentBoot") == 0)
290  {
291  config->Journal.CurrentBoot = atoi(value);
292  }
293  else if(strcmp(token, "JournalFollow") == 0)
294  {
295  config->Journal.Follow = atoi(value);
296  }
297  else if(strcmp(token, "JournalMapLogLevels") == 0)
298  {
299  config->Journal.MapLogLevels = atoi(value);
300  }
301 
302  // File transfer
303  else if(strcmp(token, "FiletransferEnable") == 0)
304  {
305  config->Filetransfer.Enable = atoi(value);
306  }
307  else if(strcmp(token, "FiletransferContextId") == 0)
308  {
309  config->Filetransfer.ContextId = malloc(strlen(value)+1);
311  strcpy(config->Filetransfer.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
312  }
313  else if(strcmp(token, "FiletransferTimeStartup") == 0)
314  {
315  config->Filetransfer.TimeStartup = atoi(value);
316  }
317  else if(strcmp(token, "FiletransferTimeDelay") == 0)
318  {
319  config->Filetransfer.TimeDelay = atoi(value);
320  }
321  else if(strcmp(token, "FiletransferTimeoutBetweenLogs") == 0)
322  {
323  config->Filetransfer.TimeoutBetweenLogs = atoi(value);
324  }
325  else if(strcmp(token, "FiletransferTempDir") == 0)
326  {
327  config->Filetransfer.TempDir = malloc(strlen(value)+1);
329  strcpy(config->Filetransfer.TempDir, value); /* strcpy unritical here, because size matches exactly the size to be copied */
330  }
331  else if(strcmp(token, "FiletransferDirectory") == 0)
332  {
333  config->Filetransfer.Directory[config->Filetransfer.Count] = malloc(strlen(value)+1);
335  strcpy(config->Filetransfer.Directory[config->Filetransfer.Count], value); /* strcpy unritical here, because size matches exactly the size to be copied */
336  }
337  else if(strcmp(token, "FiletransferCompression") == 0)
338  {
339  config->Filetransfer.Compression[config->Filetransfer.Count] = atoi(value);
340  }
341  else if(strcmp(token, "FiletransferCompressionLevel") == 0)
342  {
343  config->Filetransfer.CompressionLevel[config->Filetransfer.Count] = atoi(value);
344  if(config->Filetransfer.Count < (DLT_SYSTEM_LOG_DIRS_MAX - 1))
345  {
346  config->Filetransfer.Count++;
347  }
348  else
349  {
350  fprintf(stderr,
351  "Too many file transfer directories configured. Maximum: %d\n",
353  ret = -1;
354  break;
355  }
356  }
357 
358  // Log files
359  else if(strcmp(token, "LogFileEnable") == 0)
360  {
361  config->LogFile.Enable = atoi(value);
362  }
363  else if(strcmp(token, "LogFileFilename") == 0)
364  {
365  config->LogFile.Filename[config->LogFile.Count] = malloc(strlen(value)+1);
366  MALLOC_ASSERT(config->LogFile.Filename[config->LogFile.Count]);
367  strcpy(config->LogFile.Filename[config->LogFile.Count], value); /* strcpy unritical here, because size matches exactly the size to be copied */
368  }
369  else if(strcmp(token, "LogFileMode") == 0)
370  {
371  config->LogFile.Mode[config->LogFile.Count] = atoi(value);
372  }
373  else if(strcmp(token, "LogFileTimeDelay") == 0)
374  {
375  config->LogFile.TimeDelay[config->LogFile.Count] = atoi(value);
376  }
377  else if(strcmp(token, "LogFileContextId") == 0)
378  {
379  config->LogFile.ContextId[config->LogFile.Count] = malloc(strlen(value)+1);
380  MALLOC_ASSERT(config->LogFile.ContextId[config->LogFile.Count]);
381  strcpy(config->LogFile.ContextId[config->LogFile.Count], value); /* strcpy unritical here, because size matches exactly the size to be copied */
382  if(config->LogFile.Count < (DLT_SYSTEM_LOG_FILE_MAX - 1))
383  {
384  config->LogFile.Count++;
385  }
386  else
387  {
388  fprintf(stderr,
389  "Too many log files configured. Maximum: %d\n",
391  ret = -1;
392  break;
393  }
394 
395  }
396 
397  // Log Processes
398  else if(strcmp(token, "LogProcessesEnable") == 0)
399  {
400  config->LogProcesses.Enable = atoi(value);
401  }
402  else if(strcmp(token, "LogProcessesContextId") == 0)
403  {
404  config->LogProcesses.ContextId = malloc(strlen(value)+1);
406  strcpy(config->LogProcesses.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
407  }
408  else if(strcmp(token, "LogProcessName") == 0)
409  {
410  config->LogProcesses.Name[config->LogProcesses.Count] = malloc(strlen(value)+1);
412  strcpy(config->LogProcesses.Name[config->LogProcesses.Count], value); /* strcpy unritical here, because size matches exactly the size to be copied */
413  }
414  else if(strcmp(token, "LogProcessFilename") == 0)
415  {
416  config->LogProcesses.Filename[config->LogProcesses.Count] = malloc(strlen(value)+1);
418  strcpy(config->LogProcesses.Filename[config->LogProcesses.Count], value); /* strcpy unritical here, because size matches exactly the size to be copied */
419  }
420  else if(strcmp(token, "LogProcessMode") == 0)
421  {
422  config->LogProcesses.Mode[config->LogProcesses.Count] = atoi(value);
423  }
424  else if(strcmp(token, "LogProcessTimeDelay") == 0)
425  {
426  config->LogProcesses.TimeDelay[config->LogProcesses.Count] = atoi(value);
428  {
429  config->LogProcesses.Count++;
430  }
431  else
432  {
433  fprintf(stderr,
434  "Too many processes to log configured. Maximum: %d\n",
436  ret = -1;
437  break;
438  }
439 
440  }
441  }
442  }
443  fclose(file);
444  free(value);
445  free(token);
446  free(line);
447  return ret;
448 }
void init_cli_options(DltSystemCliOptions *options)
#define DEFAULT_CONF_FILE
Definition: dlt-dbus.h:34
int Mode[DLT_SYSTEM_LOG_FILE_MAX]
Definition: dlt-system.h:136
DltKpiConfig config
Definition: dlt-kpi.c:37
SyslogOptions Syslog
Definition: dlt-system.h:155
int read_configuration_file(DltSystemConfiguration *config, char *file_name)
#define DLT_SYSTEM_LOG_PROCESSES_MAX
Definition: dlt-system.h:67
char * ContextId
Definition: dlt-system.h:108
int CompressionLevel[DLT_SYSTEM_LOG_DIRS_MAX]
Definition: dlt-system.h:125
#define DLT_SYSTEM_LOG_DIRS_MAX
Definition: dlt-system.h:66
ShellOptions Shell
Definition: dlt-system.h:154
char * Directory[DLT_SYSTEM_LOG_DIRS_MAX]
Definition: dlt-system.h:126
void init_configuration(DltSystemConfiguration *config)
FiletransferOptions Filetransfer
Definition: dlt-system.h:157
LogFileOptions LogFile
Definition: dlt-system.h:158
int Mode[DLT_SYSTEM_LOG_PROCESSES_MAX]
Definition: dlt-system.h:148
int Compression[DLT_SYSTEM_LOG_DIRS_MAX]
Definition: dlt-system.h:124
JournalOptions Journal
Definition: dlt-system.h:156
int read_command_line(DltSystemCliOptions *options, int argc, char *argv[])
int TimeDelay[DLT_SYSTEM_LOG_PROCESSES_MAX]
Definition: dlt-system.h:149
char * ContextId
Definition: dlt-system.h:101
char * ContextId[DLT_SYSTEM_LOG_FILE_MAX]
Definition: dlt-system.h:134
char * Filename[DLT_SYSTEM_LOG_FILE_MAX]
Definition: dlt-system.h:135
#define MALLOC_ASSERT(x)
Definition: dlt-dbus.h:40
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
int TimeDelay[DLT_SYSTEM_LOG_FILE_MAX]
Definition: dlt-system.h:137
#define MAX_LINE
Definition: dlt-dbus.h:44
char * ConfigurationFileName
Definition: dlt-system.h:89
LogProcessOptions LogProcesses
Definition: dlt-system.h:159
char * Filename[DLT_SYSTEM_LOG_PROCESSES_MAX]
Definition: dlt-system.h:147
void usage(char *prog_name)
char * Name[DLT_SYSTEM_LOG_PROCESSES_MAX]
Definition: dlt-system.h:146
#define NULL
Definition: dlt_common.h:232
#define DLT_SYSTEM_LOG_FILE_MAX
Definition: dlt-system.h:65