automotive-dlt
dlt-system-processes.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-processes.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 #include <pthread.h>
50 #include <unistd.h>
51 #include <ctype.h>
52 #include <sys/types.h>
53 #include <dirent.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <stdlib.h>
57 
58 #include "dlt-system.h"
59 
60 // Modes of sending
61 #define SEND_MODE_OFF 0
62 #define SEND_MODE_ONCE 1
63 #define SEND_MODE_ON 2
64 
66 
68 DLT_DECLARE_CONTEXT(procContext)
69 
70 void send_process(LogProcessOptions const *popts, int n)
71 {
72  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
73  DLT_STRING("dlt-system-processes, send process info."));
74  FILE * pFile;
75  struct dirent *dp;
76  char filename[PATH_MAX];
77  char buffer[1024];
78  int bytes;
79  int found = 0;
80 
81  /* go through all process files in directory */
82  DIR *dir = opendir("/proc");
83  if(dir != NULL)
84  {
85  while ((dp=readdir(dir)) != NULL)
86  {
87  if(isdigit(dp->d_name[0]))
88  {
89  buffer[0] = 0;
90  snprintf(filename,PATH_MAX, "/proc/%s/cmdline",dp->d_name);
91  pFile = fopen(filename, "r");
92  if(pFile != NULL)
93  {
94  bytes = fread(buffer, 1, sizeof(buffer)-1, pFile);
95  fclose(pFile);
96  }
97  if((strcmp((*popts).Name[n], "*")==0) ||
98  (strcmp(buffer, (*popts).Name[n])==0))
99  {
100  found = 1;
101  snprintf(filename,PATH_MAX, "/proc/%s/%s", dp->d_name,(*popts).Filename[n]);
102  pFile = fopen(filename, "r");
103  if(pFile != NULL)
104  {
105  bytes = fread(buffer, 1, sizeof(buffer)-1, pFile);
106  fclose(pFile);
107 
108  if(bytes>0)
109  {
110  buffer[bytes] = 0;
111  DLT_LOG(procContext, DLT_LOG_INFO, DLT_INT(atoi(dp->d_name)), DLT_STRING((*popts).Filename[n]), DLT_STRING(buffer));
112  }
113  }
114  if(strcmp((*popts).Name[n], "*") !=0)
115  break;
116  }
117  }
118  }
119  closedir(dir);
120  }
121  else
122  {
123  DLT_LOG(dltsystem, DLT_LOG_ERROR,
124  DLT_STRING("dlt-system-processes, failed to open /proc."));
125  }
126 
127  if(!found) {
128  DLT_LOG(procContext, DLT_LOG_INFO, DLT_STRING("Process"), DLT_STRING((*popts).Name[n]),DLT_STRING("not running!"));
129  }
130 }
131 
132 void logprocess_thread(void *v_conf)
133 {
134  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
135  DLT_STRING("dlt-system-processes, in thread."));
136 
138  DLT_REGISTER_CONTEXT(procContext, conf->LogProcesses.ContextId, "Log Processes");
139 
140  int process_delays[DLT_SYSTEM_LOG_PROCESSES_MAX];
141  int i;
142  for(i = 0;i < conf->LogProcesses.Count;i++)
143  process_delays[i] = conf->LogProcesses.TimeDelay[i];
144 
145  while(!threads.shutdown)
146  {
147  sleep(1);
148  for(i = 0;i < conf->LogProcesses.Count;i++)
149  {
150  if(conf->LogProcesses.Mode[i] == SEND_MODE_OFF)
151  continue;
152 
153  if(process_delays[i] <= 0)
154  {
155  send_process(&(conf->LogProcesses), i);
156  process_delays[i] = conf->LogProcesses.TimeDelay[i];
157  if(conf->LogProcesses.Mode[i] == SEND_MODE_ONCE)
158  conf->LogProcesses.Mode[i] = SEND_MODE_OFF;
159  }
160  else
161  {
162  process_delays[i]--;
163  }
164  }
165  }
166 }
167 
169 {
170  DLT_LOG(dltsystem, DLT_LOG_DEBUG,
171  DLT_STRING("dlt-system-processes, starting process log."));
172  static pthread_attr_t t_attr;
173  static pthread_t pt;
174  pthread_create(&pt, &t_attr, (void *)logprocess_thread, conf);
175  threads.threads[threads.count++] = pt;
176 }
#define DLT_SYSTEM_LOG_PROCESSES_MAX
Definition: dlt-system.h:67
#define DLT_INT(INT_VAR)
DLT_IMPORT_CONTEXT(dltsystem)
DltSystemThreads threads
#define SEND_MODE_OFF
int Mode[DLT_SYSTEM_LOG_PROCESSES_MAX]
Definition: dlt-system.h:148
#define DLT_DECLARE_CONTEXT(CONTEXT)
#define DLT_STRING(TEXT)
int TimeDelay[DLT_SYSTEM_LOG_PROCESSES_MAX]
Definition: dlt-system.h:149
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define SEND_MODE_ONCE
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
void start_logprocess(DltSystemConfiguration *conf)
void logprocess_thread(void *v_conf)
LogProcessOptions LogProcesses
Definition: dlt-system.h:159
pthread_t threads[MAX_THREADS]
Definition: dlt-system.h:163
#define NULL
Definition: dlt_common.h:232