automotive-dlt
dlt_shm.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 
28 /*******************************************************************************
29 ** **
30 ** SRC-MODULE: dlt_shm.c **
31 ** **
32 ** TARGET : linux **
33 ** **
34 ** PROJECT : DLT **
35 ** **
36 ** AUTHOR : 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 ** aw Alexander Wenzel BMW **
55 *******************************************************************************/
56 
57 #include <sys/types.h>
58 #include <sys/ipc.h>
59 #include <sys/shm.h>
60 #include <sys/sem.h>
61 #include <sys/stat.h>
62 #include <stdio.h>
63 #include <string.h>
64 
65 #if !defined(_MSC_VER)
66 #include <unistd.h>
67 #include <syslog.h>
68 #endif
69 
70 #include <dlt_shm.h>
71 #include <dlt_common.h>
72 
73 void dlt_shm_print_hex(char *ptr,int size)
74 {
75  int num;
76 
77  for (num=0;num<size;num++)
78  {
79  if((num%16)==15)
80  printf("%.2x\n",((unsigned char*)ptr)[num]);
81  else
82  printf("%.2x ",((unsigned char*)ptr)[num]);
83  }
84  printf("\n");
85 }
86 
87 void dlt_shm_pv(int id,int operation)
88 {
89  static struct sembuf semaphor;
90 
91  semaphor.sem_op = operation;
92  semaphor.sem_flg = SEM_UNDO;
93 
94  if(semop(id, &semaphor,1) == -1) {
95  dlt_log(LOG_WARNING,"SHM: semop() failed");
96  }
97 }
98 
99 int dlt_shm_init_server(DltShm *buf,int key,int size) {
100  struct shmid_ds shm_buf;
101  unsigned char *ptr;
102 
103  // Init parameters
104  buf->shmid = 0;
105  buf->semid = 0;
106 
107  // Create the segment.
108  if ((buf->shmid = shmget(key, size, IPC_CREAT | 0666)) < 0) {
109  dlt_log(LOG_WARNING,"SHM: shmget() failed");
110  return DLT_RETURN_ERROR; /* ERROR */
111  }
112 
113  // get the size of shm
114  if (shmctl(buf->shmid, IPC_STAT, &shm_buf))
115  {
116  dlt_log(LOG_WARNING,"SHM: shmctl() failed");
117  return DLT_RETURN_ERROR; /* ERROR */
118  }
119 
120  // Now we attach the segment to our data space.
121  if ((ptr = shmat(buf->shmid, NULL, 0)) == (unsigned char *) -1) {
122  dlt_log(LOG_WARNING,"SHM: shmat() failed");
123  return DLT_RETURN_ERROR; /* ERROR */
124  }
125 
126  // Init semaphore
127  if( (buf->semid = semget(DLT_SHM_SEM,1,S_IRWXU|S_IRWXG|S_IRWXO|IPC_CREAT|IPC_EXCL)) == -1 ) {
128  if( (buf->semid = semget(DLT_SHM_SEM,1,S_IRWXU|S_IRWXG|S_IRWXO|IPC_EXCL)) == -1 ) {
129  dlt_log(LOG_WARNING,"SHM: semget() failed");
130  return DLT_RETURN_ERROR; /* ERROR */
131  }
132  }
133  if( semctl(buf->semid,0,SETVAL,(int)1) == -1 ) {
134  dlt_log(LOG_WARNING,"SHM: semctl() failed");
135  return DLT_RETURN_ERROR; /* ERROR */
136  }
137 
138  // init buffer
139  dlt_buffer_init_static_server(&(buf->buffer),ptr,shm_buf.shm_segsz);
140 
141  return DLT_RETURN_OK; /* OK */
142 }
143 
145  struct shmid_ds shm_buf;
146  unsigned char *ptr;
147 
148  // init parameters
149  buf->shmid = 0;
150  buf->semid = 0;
151 
152  // Create the segment.
153  if ((buf->shmid = shmget(key, 0, 0666)) < 0) {
154  dlt_log(LOG_WARNING,"SHM: shmget() failed");
155  return DLT_RETURN_ERROR; /* ERROR */
156  }
157 
158  // get the size of shm
159  if (shmctl(buf->shmid, IPC_STAT, &shm_buf))
160  {
161  dlt_log(LOG_WARNING,"SHM: shmctl() failed");
162  return DLT_RETURN_ERROR; /* ERROR */
163  }
164 
165  // Now we attach the segment to our data space.
166  if ((ptr = shmat(buf->shmid, NULL, 0)) == (unsigned char *) -1) {
167  dlt_log(LOG_WARNING,"shmat() failed");
168  return DLT_RETURN_ERROR; /* ERROR */
169  }
170 
171  // Init semaphore
172  if( (buf->semid = semget(DLT_SHM_SEM,0,0)) == -1 ) {
173  dlt_log(LOG_WARNING,"SHM: semget() failed");
174  return DLT_RETURN_ERROR; /* ERROR */
175  }
176 
177  // init buffer
178  dlt_buffer_init_static_client(&(buf->buffer),ptr,shm_buf.shm_segsz);
179 
180  return DLT_RETURN_OK; /* OK */
181 }
182 
184 {
185  dlt_buffer_info(&(buf->buffer));
186 }
187 
189 {
190  dlt_buffer_status(&(buf->buffer));
191 }
192 
194 {
195  return dlt_buffer_get_total_size(&(buf->buffer));
196 }
197 
199 {
200  int ret;
201 
202  /* check if buffer available */
203  if(!buf->buffer.mem)
204  return -1;
205 
206  DLT_SHM_SEM_GET(buf->semid);
207  ret = dlt_buffer_get_used_size(&(buf->buffer));
208  DLT_SHM_SEM_FREE(buf->semid);
209 
210  return ret;
211 }
212 
214 {
215  return dlt_buffer_get_message_count(&(buf->buffer));
216 }
217 
218 int dlt_shm_push(DltShm *buf,const unsigned char *data1,unsigned int size1,const unsigned char *data2,unsigned int size2,const unsigned char *data3,unsigned int size3)
219 {
220  int ret;
221 
222  /* check if buffer available */
223  if(!buf->buffer.mem)
224  return -1;
225 
226  DLT_SHM_SEM_GET(buf->semid);
227  ret = dlt_buffer_push3(&(buf->buffer),data1,size1,data2,size2,data3,size3);
228  DLT_SHM_SEM_FREE(buf->semid);
229 
230  return ret;
231 }
232 
233 int dlt_shm_pull(DltShm *buf,unsigned char *data, int max_size)
234 {
235  int ret;
236 
237  /* check if buffer available */
238  if(!buf->buffer.mem)
239  return -1;
240 
241  DLT_SHM_SEM_GET(buf->semid);
242  ret = dlt_buffer_pull(&(buf->buffer),data,max_size);
243  DLT_SHM_SEM_FREE(buf->semid);
244 
245  return ret;
246 }
247 
248 int dlt_shm_copy(DltShm *buf,unsigned char *data, int max_size)
249 {
250  int ret;
251 
252  /* check if buffer available */
253  if(!buf->buffer.mem)
254  return -1;
255 
256  DLT_SHM_SEM_GET(buf->semid);
257  ret = dlt_buffer_copy(&(buf->buffer),data,max_size);
258  DLT_SHM_SEM_FREE(buf->semid);
259 
260  return ret;
261 }
262 
264 {
265  int ret;
266 
267  /* check if buffer available */
268  if(!buf->buffer.mem)
269  return -1;
270 
271  DLT_SHM_SEM_GET(buf->semid);
272  ret = dlt_buffer_remove(&(buf->buffer));
273  DLT_SHM_SEM_FREE(buf->semid);
274 
275  return ret;
276 }
277 
279 
280  if(shmdt(buf->buffer.shm)) {
281  dlt_log(LOG_WARNING,"SHM: shmdt() failed");
282  return DLT_RETURN_ERROR; /* ERROR */
283  }
284 
285  if(shmctl(buf->shmid,IPC_RMID,NULL) == -1) {
286  dlt_log(LOG_WARNING,"SHM: shmdt() failed");
287  return DLT_RETURN_ERROR; /* ERROR */
288  }
289 
290  if(semctl(buf->semid,0,IPC_RMID,(int)0) == -1) {
291  dlt_log(LOG_WARNING,"SHM: shmdt() failed");
292  return DLT_RETURN_ERROR; /* ERROR */
293  }
294 
295  // Reset parameters
296  buf->shmid = 0;
297  buf->semid = 0;
298 
299  return dlt_buffer_free_static(&(buf->buffer));
300 
301 }
302 
304 
305  if(shmdt(buf->buffer.shm)) {
306  dlt_log(LOG_WARNING,"SHM: shmdt() failed");
307  return DLT_RETURN_ERROR; /* ERROR */
308  }
309 
310  // Reset parameters
311  buf->shmid = 0;
312  buf->semid = 0;
313 
314  return dlt_buffer_free_static(&(buf->buffer));
315 }
#define DLT_SHM_SEM_GET(id)
Definition: dlt_shm.h:92
int dlt_shm_copy(DltShm *buf, unsigned char *data, int max_size)
Definition: dlt_shm.c:248
uint32_t dlt_buffer_get_total_size(DltBuffer *buf)
Definition: dlt_common.c:2943
DltReturnValue
Definition: dlt_types.h:86
int dlt_shm_get_total_size(DltShm *buf)
Definition: dlt_shm.c:193
void dlt_shm_pv(int id, int operation)
Definition: dlt_shm.c:87
void dlt_buffer_info(DltBuffer *buf)
Definition: dlt_common.c:2899
DltReturnValue dlt_buffer_free_static(DltBuffer *buf)
Definition: dlt_common.c:2479
DltReturnValue dlt_shm_free_server(DltShm *buf)
Definition: dlt_shm.c:278
Definition: dlt_shm.h:78
DltReturnValue dlt_log(int prio, char *s)
Definition: dlt_common.c:2029
DltReturnValue dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
Definition: dlt_common.c:2376
int dlt_shm_push(DltShm *buf, const unsigned char *data1, unsigned int size1, const unsigned char *data2, unsigned int size2, const unsigned char *data3, unsigned int size3)
Definition: dlt_shm.c:218
unsigned char * mem
Definition: dlt_common.h:687
#define DLT_SHM_SEM_FREE(id)
Definition: dlt_shm.h:93
DltReturnValue dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size)
Definition: dlt_common.c:2407
int dlt_buffer_copy(DltBuffer *buf, unsigned char *data, int max_size)
Definition: dlt_common.c:2889
int dlt_shm_pull(DltShm *buf, unsigned char *data, int max_size)
Definition: dlt_shm.c:233
int dlt_buffer_get_used_size(DltBuffer *buf)
Definition: dlt_common.c:2952
void dlt_shm_status(DltShm *buf)
Definition: dlt_shm.c:188
static char data[kDataSize]
Definition: city-test.cc:40
#define DLT_SHM_SEM
Definition: dlt_shm.h:74
DltReturnValue dlt_shm_init_client(DltShm *buf, int key)
Definition: dlt_shm.c:144
unsigned char * shm
Definition: dlt_common.h:685
int dlt_shm_get_used_size(DltShm *buf)
Definition: dlt_shm.c:198
void dlt_buffer_status(DltBuffer *buf)
Definition: dlt_common.c:2916
void dlt_shm_info(DltShm *buf)
Definition: dlt_shm.c:183
DltBuffer buffer
Definition: dlt_shm.h:82
int dlt_buffer_remove(DltBuffer *buf)
Definition: dlt_common.c:2894
int dlt_buffer_get_message_count(DltBuffer *buf)
Definition: dlt_common.c:2977
void dlt_shm_print_hex(char *ptr, int size)
Definition: dlt_shm.c:73
int dlt_shm_init_server(DltShm *buf, int key, int size)
Definition: dlt_shm.c:99
int dlt_shm_get_message_count(DltShm *buf)
Definition: dlt_shm.c:213
DltReturnValue dlt_shm_free_client(DltShm *buf)
Definition: dlt_shm.c:303
int dlt_buffer_push3(DltBuffer *buf, const unsigned char *data1, unsigned int size1, const unsigned char *data2, unsigned int size2, const unsigned char *data3, unsigned int size3)
Definition: dlt_common.c:2691
int dlt_buffer_pull(DltBuffer *buf, unsigned char *data, int max_size)
Definition: dlt_common.c:2884
int dlt_shm_remove(DltShm *buf)
Definition: dlt_shm.c:263
int shmid
Definition: dlt_shm.h:80
#define NULL
Definition: dlt_common.h:232
int semid
Definition: dlt_shm.h:81