summaryrefslogtreecommitdiffstats
path: root/nsframework/common_library/client/src/cl_monitor.c
blob: 26d8e2e5510383d24a4039b53d39aafec59b3f5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <semaphore.h>

#include "cl_error.h"
#include <native_service/cl_region.h>
#include <native_service/cl_monitor.h>

#define CL_MONITOR_SHM_NAME "/cl_monitor"

#define CL_MONITOR_ID_REAL_GENERIC_START (0)
#define CL_MONITOR_ID_REAL_GENERIC_END   (1023)
#define CL_MONITOR_ID_REAL_RPC_START     (49952)
#define CL_MONITOR_ID_REAL_RPC_END       (59999)

#define CL_MONITOR_ID_FLAT_GENERIC_START (0)
#define CL_MONITOR_ID_FLAT_GENERIC_END \
  (CL_MONITOR_ID_FLAT_GENERIC_START + (CL_MONITOR_ID_REAL_GENERIC_END - CL_MONITOR_ID_REAL_GENERIC_START))
#define CL_MONITOR_ID_FLAT_RPC_START (CL_MONITOR_ID_FLAT_GENERIC_END + 1)
#define CL_MONITOR_ID_FLAT_RPC_END \
  (CL_MONITOR_ID_FLAT_RPC_START + (CL_MONITOR_ID_REAL_RPC_END - CL_MONITOR_ID_REAL_RPC_START))
#define CL_MONITOR_ENTRY_NUM (CL_MONITOR_ID_FLAT_RPC_END + 1)

#define CL_MONITOR_BITMAP_LENGTH ((CL_MONITOR_ENTRY_NUM + 63) / 64)  // aligned

#define CL_ALIGN(x, a) (((x) + (a - 1)) / (a) * (a))

struct cl_monitor_header {
  char signature[4];
  uint64_t bitmap[CL_MONITOR_BITMAP_LENGTH];
  sem_t sem;
};

#define CL_MONITOR_OBJECT_SIZE \
  CL_ALIGN((sizeof(struct cl_monitor_header) + (sizeof(CL_MonitorEntry_t) * CL_MONITOR_ENTRY_NUM)), 4096)

#define CL_MONITOR_SET_BIT(a, i) (a[i / 64] |= (1ULL << (i % 64)))
#define CL_MONITOR_CLEAR_BIT(a, i) (a[i / 64] &= ~(1ULL << (i % 64)))

static struct cl_monitor_header *cl_monitor_obj;
static CL_MonitorEntry_t *cl_monitor_entry_head;

int CL_MonitorInit(CL_MonitorInit_t init_type) {
  int fd;
  int oflag;
  mode_t mode;

  if (init_type != CL_MONITOR_INIT_SYSTEM && init_type != CL_MONITOR_INIT_USER) {
    errno = EINVAL;
    return -1;
  }

  if (cl_monitor_obj != NULL) {
    return 0;
  }

  if (init_type == CL_MONITOR_INIT_SYSTEM) {
    oflag = O_RDWR | O_CREAT | O_EXCL;
    mode = 0666;
  } else {
    oflag = O_RDWR;
    mode = 0;
  }

  if ((fd = shm_open(CL_MONITOR_SHM_NAME, oflag, mode)) == -1) { // LCOV_EXCL_BR_LINE 5: fail safe for glibc function shm_open
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1;// LCOV_EXCL_LINE 5: fail safe for glibc function shm_open
  }

  if (init_type == CL_MONITOR_INIT_SYSTEM) {
    if (ftruncate(fd, CL_MONITOR_OBJECT_SIZE) == -1) { // LCOV_EXCL_BR_LINE 5:  fail safe for glibc function ftruncate
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

      return -1; // LCOV_EXCL_LINE 5:  fail safe for glibc function ftruncate
    }
  } else {
    struct stat st_buf;
    if (fstat(fd, &st_buf) == -1) { // LCOV_EXCL_BR_LINE 5:  fail safe for glibc function fstat
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

      return -1; // LCOV_EXCL_LINE 5:  fail safe for glibc function fstat
    }

    if (st_buf.st_size != CL_MONITOR_OBJECT_SIZE) {
      errno = EAGAIN;
      return -1;
    }
  }

  // LCOV_EXCL_BR_START 5:  fail safe for glibc function mmap
  if ((cl_monitor_obj = mmap(NULL, CL_MONITOR_OBJECT_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) {
    // LCOV_EXCL_BR_STOP
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1; // LCOV_EXCL_LINE 5:  fail safe for glibc function mmap
  }

  if (init_type == CL_MONITOR_INIT_SYSTEM) { // LCOV_EXCL_BR_LINE 11:  out branch
    memcpy(cl_monitor_obj->signature, "CLAM", 4);
    memset(cl_monitor_obj->bitmap, 0, sizeof(cl_monitor_obj->bitmap));
    sem_init(&cl_monitor_obj->sem, 1, 1);
  }
  cl_monitor_entry_head = (CL_MonitorEntry_t *)(((char *)cl_monitor_obj) + sizeof(struct cl_monitor_header));

  return 0;
}

static inline int cl_monitor_check_type_id(CL_MonitorType_t type, uint32_t id, int *offset) {
  if (type == CL_MONITOR_TYPE_GENERIC) {
    if (id > CL_MONITOR_ID_REAL_GENERIC_END) {
      errno = ENOENT;
      return -1;
    }
    *offset = (int)((id - CL_MONITOR_ID_REAL_GENERIC_START) + CL_MONITOR_ID_FLAT_GENERIC_START);
  } else if (type == CL_MONITOR_TYPE_RPC) {
    if (id < CL_MONITOR_ID_REAL_RPC_START || id > CL_MONITOR_ID_REAL_RPC_END) {
      errno = ENOENT;
      return -1;
    }
    *offset = (int)((id - CL_MONITOR_ID_REAL_RPC_START) + CL_MONITOR_ID_FLAT_RPC_START);
  } else {
    errno = ENOENT;
    return -1;
  }

  return 0;
}

static inline int cl_monitor_sem_wait(sem_t *sem) {
  int ret;

retry:
  ret = sem_wait(sem);
  if (ret == -1) { // LCOV_EXCL_BR_LINE 5: fail safe for libc   sem_wait
    // LCOV_EXCL_START 5: fail safe for libc   sem_wait

    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    if (errno == EINTR) {
      goto retry;
    }
    CL_PERROR("sem_wait");
  }
  // LCOV_EXCL_STOP

  return ret;
}

static inline int cl_monitor_sem_post(sem_t *sem) {
  int ret;

  if ((ret = sem_post(sem)) == -1) { // LCOV_EXCL_BR_LINE 5: fail safe for libc sem_post
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    CL_PERROR("sem_post");  // LCOV_EXCL_LINE 5: fail safe for libc sem_post
  }

  return ret;
}

int CL_MonitorSetEntry(CL_MonitorType_t type, uint32_t id, CL_MonitorState_t state, uint32_t timeout,
                       uint32_t user_data) {
  int offset;
  CL_MonitorEntry_t *e;
  struct timespec ts;

  if (cl_monitor_obj == NULL) {
    errno = ENOENT;
    return -1;
  }

  if (cl_monitor_check_type_id(type, id, &offset) == -1) {
    return -1;
  }

  if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) { // LCOV_EXCL_BR_LINE 5: fail safe for libc clock_gettime
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1; // LCOV_EXCL_LINE 5: fail safe for libc clock_gettime
  }

  e = cl_monitor_entry_head + offset;
  cl_monitor_sem_wait(&cl_monitor_obj->sem);

  if (state == CL_MONITOR_STATE_SLEEP) {
    memset(e, 0, sizeof(CL_MonitorEntry_t));
    CL_MONITOR_CLEAR_BIT(cl_monitor_obj->bitmap, offset);
  } else {
    e->pid = (uint16_t)getpid();
    e->type = type;
    e->state = state;

//    e->timeout = (uint32_t)ts.tv_sec + timeout;
    e->timeout = ts.tv_sec + timeout;
    e->id = id;
    e->user_data = user_data;
    CL_MONITOR_SET_BIT(cl_monitor_obj->bitmap, offset);
  }

  cl_monitor_sem_post(&cl_monitor_obj->sem);

  return 0;
}

int CL_MonitorGetEntry(CL_MonitorType_t type, uint32_t id, CL_MonitorEntry_t *entry) {
  int offset;
  CL_MonitorEntry_t *e;

  if (cl_monitor_obj == NULL) {
    errno = ENOENT;
    return -1;
  }

  if (entry == NULL) {
    errno = EINVAL;
    return -1;
  }

  if (cl_monitor_check_type_id(type, id, &offset) == -1) {
    return -1;
  }

  e = cl_monitor_entry_head + offset;
  cl_monitor_sem_wait(&cl_monitor_obj->sem);

  memcpy(entry, e, sizeof(CL_MonitorEntry_t));

  cl_monitor_sem_post(&cl_monitor_obj->sem);

  return 0;
}

int CL_MonitorSearchInit(CL_MonitorSearch_t *serch) {
  if (serch == NULL) {
    errno = EINVAL;
    return -1;
  }

  serch->entry_list = NULL;
  serch->entry_num = 0;
  return 0;
}

int CL_MonitorSearchDestroy(CL_MonitorSearch_t *serch) {
  if (serch == NULL) {
    errno = EINVAL;
    return -1;
  }

  free(serch->entry_list);
  serch->entry_num = 0;
  return 0;
}

static inline int cl_monitor_popcnt64(uint64_t x) {
  uint64_t n;

  n = (x >> 1) & 0x7777777777777777ULL;
  x = x - n;
  n = (n >> 1) & 0x7777777777777777ULL;
  x = x - n;
  n = (n >> 1) & 0x7777777777777777ULL;
  x = x - n;
  x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
  x = x * 0x0101010101010101ULL;
  return (int)(x >> 56);
}

typedef struct cl_monitor_offset_s cl_monitor_offset_t;
struct cl_monitor_offset_s {
  cl_monitor_offset_t *next;
  int offset;
};

int CL_MonitorSearchTimeout(CL_MonitorSearch_t *search) {
  int i;
  int offset;
  CL_MonitorEntry_t *e;
  struct timespec ts;
  int timeout_entry_num = 0;
  cl_region_t *r;
  cl_monitor_offset_t *o, *head = NULL, *tail = NULL;

  if (cl_monitor_obj == NULL) {
    errno = ENOENT;
    return -1;
  }

  if (search == NULL) {
    errno = EINVAL;
    return -1;
  }

  if ((r = CL_RegionCreate(CL_REGION_DEFAULT_SIZE)) == NULL) {
    errno = ENOMEM;
    return -1;
  }

  free(search->entry_list);
  search->entry_list = NULL;

  if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) {// LCOV_EXCL_BR_LINE 5: fail safe for libc clock_gettime
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    return -1;// LCOV_EXCL_LINE 5: fail safe for libc clock_gettime
  }

  cl_monitor_sem_wait(&cl_monitor_obj->sem);

  for (i = 0; i < CL_MONITOR_BITMAP_LENGTH; i++) {
    if (cl_monitor_obj->bitmap[i] != 0) {
      uint64_t bits, mrb1;  // most right bit 1

      bits = cl_monitor_obj->bitmap[i];
      while (bits) {
        mrb1 = bits & (-bits);
        offset = i * 64 + cl_monitor_popcnt64(mrb1 - 1);
        e = cl_monitor_entry_head + offset;

        if (e->timeout <= ts.tv_sec) {
          timeout_entry_num++;
          if ((o = CL_RegionAlloc(r, cl_monitor_offset_t, 1)) == NULL) {  // LCOV_EXCL_BR_LINE 5: fail safe for mmap
            // LCOV_EXCL_START 5: fail safe for libc mmap
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            errno = ENOMEM;
            timeout_entry_num = -1;
            goto exit;
            // LCOV_EXCL_STOP
          }

          o->offset = offset;
          o->next = NULL;

          if (head == NULL) {
            head = tail = o;
          } else {
            tail->next = o;
            tail = o;
          }
        }
        bits &= ~mrb1;
      }
    }
  }

  if (timeout_entry_num) {
    CL_MonitorEntry_t *src, *dst;
    dst = malloc(sizeof(CL_MonitorEntry_t) * (size_t)timeout_entry_num);
    if (dst == NULL) {  // LCOV_EXCL_LINE 5: fail safe for libc malloc
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

      errno = ENOMEM;  // LCOV_EXCL_LINE 5: fail safe for libc malloc
      timeout_entry_num = -1;  // LCOV_EXCL_LINE 5: fail safe for libc malloc
      goto exit;  // LCOV_EXCL_LINE 5: fail safe for libc malloc
    }

    search->entry_list = dst;
    o = head;
    for (i = 0; i < timeout_entry_num; i++) {
      src = cl_monitor_entry_head + o->offset;
      memcpy(dst, src, sizeof(CL_MonitorEntry_t));

      o = o->next;
      dst++;
    }
  }

exit:
  cl_monitor_sem_post(&cl_monitor_obj->sem);

  CL_RegionDestroy(r);
  search->entry_num = (timeout_entry_num == -1) ? 0 : timeout_entry_num;  // LCOV_EXCL_BR_LINE 11:  out branch
  return timeout_entry_num;
}

int cl_monitor_cleanup(int pid) {
  int ret = 0;
  int i;
  int offset;
  CL_MonitorEntry_t *e;

  if (cl_monitor_obj == NULL) {
    errno = ENOENT;
    return -1;
  }

  if (pid <= 0) { // LCOV_EXCL_LINE 5: fail safe for glibc function waitid
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert

    errno = EINVAL;// LCOV_EXCL_LINE 5: fail safe for glibc function waitid
    return -1;// LCOV_EXCL_LINE 5: fail safe for glibc function waitid
  }

  cl_monitor_sem_wait(&cl_monitor_obj->sem);

  for (i = 0; i < CL_MONITOR_BITMAP_LENGTH; i++) {
    if (cl_monitor_obj->bitmap[i] != 0) {
      uint64_t bits, mrb1;  // most right bit 1

      bits = cl_monitor_obj->bitmap[i];
      while (bits) {
        mrb1 = bits & (-bits);
        offset = i * 64 + cl_monitor_popcnt64(mrb1 - 1);

        e = cl_monitor_entry_head + offset;
        if (e->pid == pid) {
          memset(e, 0, sizeof(CL_MonitorEntry_t));
          CL_MONITOR_CLEAR_BIT(cl_monitor_obj->bitmap, offset);
          ret++;
        }

        bits &= ~mrb1;
      }
    }
  }

  cl_monitor_sem_post(&cl_monitor_obj->sem);

  return ret;
}