Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensips/status_report.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2022 OpenSIPS Solutions
3
 *
4
 * This file is part of opensips, a free SIP server.
5
 *
6
 * opensips is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 2 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * opensips is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,USA
19
 */
20
21
#include "dprint.h"
22
#include "locking.h"
23
#include "rw_locking.h"
24
#include "str.h"
25
#include "ut.h"
26
#include "evi/evi.h"
27
#include "mi/fmt.h"
28
#include "status_report.h"
29
30
struct report_rec {
31
  str log;
32
  time_t ts;
33
};
34
35
/* Implementation details
36
 * The list of group is static, groups are to be registered only at startup and
37
 * it does not change at runtime; so it is safe to grap and hold referances
38
 * to groups (as they will not change or be deleted).
39
 * The list of identifiers under a group may change; identifiers may be added
40
 * or deleted at runtime; so, the framework does not expose any reference to
41
 * the identifiers. The identifiers will be looked up each time.
42
 * Both types of lists are protected by a RW lock. WRITE is used only when the
43
 * lists are to changed as elements (adding / removing), otherwise READ is
44
 * used.
45
 * For changes within an identity (like changing status or reports), the
46
 * per-identity lock is used.
47
 */
48
49
typedef struct _sr_identifier {
50
  /* name of the indentifier, allocated in the same mem chunk */
51
  str name;
52
  /* some text detailing / explaining the reason (usually for "KO" reason)
53
   * allocated as a separated mem chunk, to be freed
54
   * it is optional*/
55
  str status_txt;
56
  /* value of the status "> 0" ok; "< 0" not ok ; 0 not allowed */
57
  enum sr_core_states status;
58
  /* size of the "reports" array, pre-allocated */
59
  short max_reports;
60
  /* indexes of first and last used reports
61
   * starts as -1/-1 meaning no report at all */
62
  short first_report;
63
  short last_report;
64
  /* lock for protexting access to "status" and "reports" */
65
  gen_lock_t lock;
66
  /* array of max_reports reports, allocated in the same mem chunk */
67
  struct report_rec *reports;
68
  /* next identifier in the group */
69
  struct _sr_identifier *next;
70
} sr_identifier;
71
72
73
typedef struct _sr_group {
74
  /* name of the group, the buffer is allocated in the same mem chunk */
75
  str name;
76
  /* if public, no script or external function (like MI) will be allowed
77
   * to change the status or report here */
78
  int is_public;
79
  /* identifiers as a simple linked list (the list SHOULD not be empty) */
80
  sr_identifier *identifiers;
81
  /* link to the next group (simple linked list) */
82
  struct _sr_group *next;
83
} sr_group;
84
85
86
/* lock to protect the list of sr_groups and changes on their field */
87
static rw_lock_t *sr_lock = NULL;
88
89
/* global list of 'status_report' groups */
90
static sr_group *sr_groups = NULL;
91
92
/* name to be used as default (or nameless) identifier */
93
static str main_identifier = str_init("main");
94
95
/* event to be raised when the status of an identifier changed */
96
static str sr_event = str_init("E_CORE_SR_STATUS_CHANGED");
97
static event_id_t sr_evi_id;
98
99
/* the SR group of the core */
100
static sr_group *srg_core = NULL;
101
static sr_identifier *sri_core = NULL;
102
103
104
/****************** Internal functions  **********************/
105
106
/* Allocates, initializes and links a new group.
107
 * NOTE 1 - is expects the sr_lock to be already taken !!
108
 */
109
static sr_group* _create_new_group(str *name)
110
0
{
111
0
  sr_group *srg;
112
113
0
  LM_DBG("adding new group [%.*s]\n", name->len, name->s);
114
115
0
  srg=(sr_group*)shm_malloc( sizeof(sr_group) + name->len );
116
0
  if ( srg==NULL ) {
117
0
    LM_ERR("shm allocing for a new group failed\n");
118
0
    return NULL;
119
0
  }
120
121
0
  memset( srg, 0,  sizeof(sr_group));
122
123
  /* set the name*/
124
0
  srg->name.s = (char*)(srg+1);
125
0
  srg->name.len = name->len;
126
0
  memcpy( srg->name.s, name->s, name->len );
127
128
  /* link it first */
129
0
  srg->next = sr_groups;
130
0
  sr_groups = srg;
131
132
0
  return srg;
133
0
}
134
135
136
/* Searches a SR group by its name and returns the pointer to it
137
 * NOTE 1: the function assumes that the sr lock is already taken !!
138
 */
139
static sr_group* _get_group_by_name(str *name)
140
0
{
141
0
  sr_group *srg;
142
143
  /* locate the group */
144
0
  for ( srg=sr_groups ; srg ; srg=srg->next ) {
145
0
    if ( (srg->name.len == name->len) &&
146
0
    strncasecmp( srg->name.s, name->s, name->len)==0 )
147
0
      return srg;
148
0
  }
149
150
0
  return NULL;
151
0
}
152
153
154
/* Searches a SR identifier by its group and name and returns the pointer to it
155
 * NOTE 1: the function assumes that the sr lock is already taken !!
156
 */
157
static sr_identifier* _get_identifier_by_name(sr_group *srg, str *id_name)
158
0
{
159
0
  sr_identifier *sri;
160
161
0
  for( sri=srg->identifiers ; sri ; sri=sri->next )
162
0
    if (sri->name.len==id_name->len && 
163
0
    strncasecmp( sri->name.s, id_name->s, id_name->len)==0)
164
0
      return sri;
165
166
0
  return NULL;
167
0
}
168
169
170
171
/****************** Module (to be used) functions  **********************/
172
173
/* Registers a new SR group by its name
174
 * To be done at startup only
175
 * The modules should typically use their names as groups
176
 */
177
void *sr_register_group( char* name_s, int name_len, int is_public)
178
0
{
179
0
  sr_group *srg = NULL;
180
0
  str name = {name_s, name_len};
181
182
0
  lock_start_write( sr_lock );
183
184
  /* locate the group, for duplicates */
185
0
  srg = _get_group_by_name(&name);
186
0
  if (srg) {
187
0
    LM_ERR("duplicated group already existing [%.*s]\n",
188
0
      name.len, name.s);
189
0
    srg = NULL;
190
0
  } else {
191
0
    srg = _create_new_group( &name );
192
0
    if ( srg==NULL ) {
193
0
      LM_ERR("failed to register group [%.*s]\n",
194
0
        name.len, name.s);
195
0
    } else {
196
0
      srg->is_public = is_public;
197
0
    }
198
0
  }
199
200
0
  lock_stop_write( sr_lock );
201
202
0
  return srg;
203
0
}
204
205
206
/* Search a SR group by name and and returns its pointer
207
 */
208
void *sr_get_group_by_name(  char *name_s, int name_len)
209
0
{
210
0
  sr_group *srg = NULL;
211
0
  str name = {name_s, name_len};
212
213
0
  lock_start_read( sr_lock );
214
215
  /* locate the group, for duplicates */
216
0
  srg = _get_group_by_name( &name );
217
218
0
  lock_stop_read( sr_lock );
219
220
0
  return srg;
221
0
}
222
223
224
/* Registers a new SR identifier into a existing group.
225
 * - The group is mandatory
226
 * - The identifier name is optional; if missing, the default one (per entire
227
 *   group) will be used.
228
 * - The initial status of the identity; 0 is not accepted, it will be
229
 *   converted to 1
230
 * - The status text / details - optional string to detail the status code
231
 * - The max_reports defines how many logs should be kept (last logs) for the
232
 *   identifier; if 0, no logs/reports will be stored.
233
 */
234
int sr_register_identifier( void *group, 
235
    char *identifier_s, int identifier_len,
236
    int init_status, char *status_txt_s, int status_txt_len,
237
    int max_reports)
238
0
{
239
0
  sr_group *srg = (sr_group*)group;
240
0
  str identifier = {identifier_s, identifier_len};
241
0
  char *err_txt = "";
242
0
  sr_identifier *sri = NULL;
243
244
245
0
  if (identifier.s==NULL)
246
0
    identifier = main_identifier;
247
248
0
  LM_DBG("adding new identifier [%.*s] to group [%.*s]\n",
249
0
    identifier.len, identifier.s, srg->name.len, srg->name.s);
250
251
0
  lock_start_write( sr_lock );
252
253
  /* double check if we already have such identifier, just to be sure */
254
0
  sri = _get_identifier_by_name( srg, &identifier);
255
0
  if (sri) {
256
0
    err_txt = "add identifier (already existing)";
257
0
    sri = NULL; /* just to avoid its freeing on error handling */
258
0
    goto error;
259
0
  }
260
261
  /* allocate a new identifier */
262
0
  sri = (sr_identifier*)shm_malloc( sizeof(sr_identifier) + identifier.len
263
0
    + max_reports*sizeof(struct report_rec) );
264
0
  if (sri==NULL) {
265
0
    err_txt = "shm allocate new identifier";
266
0
    goto error;
267
0
  }
268
0
  memset(sri, 0, sizeof(sr_identifier) + identifier.len
269
0
    + max_reports*sizeof(struct report_rec) );
270
271
  /* initialize the new identifier */
272
0
  sri->reports = (struct report_rec*)(sri+1);
273
0
  sri->name.s = (char*)(sri->reports + max_reports);
274
0
  sri->name.len = identifier.len;
275
0
  memcpy( sri->name.s, identifier.s, identifier.len);
276
277
0
  sri->status = (init_status==0) ? 1: init_status;
278
0
  if (status_txt_s) {
279
0
    sri->status_txt.s = shm_malloc(status_txt_len);
280
0
    if (sri->status_txt.s) {
281
0
      memcpy( sri->status_txt.s, status_txt_s, status_txt_len);
282
0
      sri->status_txt.len = status_txt_len;
283
0
    } else
284
0
      sri->status_txt.len = 0;
285
0
  }
286
287
0
  if (max_reports==0)
288
0
    sri->reports = NULL;
289
0
  sri->max_reports = max_reports;
290
0
  sri->first_report = sri->last_report = -1;
291
292
0
  if (lock_init(&sri->lock)==NULL) {
293
0
    err_txt = "init identifier's lock";
294
0
    goto error;
295
0
  }
296
297
  /* add the new identifier to the group */
298
0
  sri->next = srg->identifiers;
299
0
  srg->identifiers = sri;
300
301
0
  lock_stop_write( sr_lock );
302
303
0
  return 0;
304
305
0
error:
306
0
  if (sri)
307
0
    shm_free(sri);
308
0
  lock_stop_write( sr_lock );
309
0
  LM_ERR("failed to %s when registering the [%.*s] identifier "
310
0
    "in [%.*s] group\n", err_txt, identifier.len, identifier.s,
311
0
    srg->name.len, srg->name.s);
312
0
  return -1;
313
0
}
314
315
316
/* Just a simple bundle of register_group and register_identifier for
317
 * cases where you need to register both in a single shot
318
 */
319
void* sr_register_group_with_identifier( char *group_s, int group_len,
320
    int grp_is_public,
321
    char *identifier_s, int identifier_len,
322
    int init_status, char *status_txt_s, int status_txt_len,
323
    int max_reports)
324
0
{
325
0
  void *srg;
326
327
0
  if ( (srg=sr_register_group( group_s, group_len, grp_is_public))==NULL ) {
328
0
    LM_ERR("failed to register 'status_report' group [%.*s]\n",
329
0
      group_len, group_s);
330
0
    return NULL;
331
0
  }
332
0
  if (sr_register_identifier( srg, identifier_s, identifier_len,
333
0
  init_status, status_txt_s, status_txt_len, max_reports)<0 ) {
334
0
    LM_ERR("Failed to create 'core' group and identifier\n'\n");
335
0
    return NULL;
336
0
  }
337
338
0
  return srg;
339
0
}
340
341
342
/* Unregisters / removes an identifier from its group
343
 */
344
int sr_unregister_identifier( void *group,
345
    char *identifier_s, int identifier_len)
346
0
{
347
0
  sr_group *srg = (sr_group*)group;
348
0
  str identifier = {identifier_s, identifier_len};
349
0
  sr_identifier *sri, *prev_sri;
350
0
  int i;
351
352
0
  if (identifier.s==NULL)
353
0
    identifier = main_identifier;
354
355
  /* search for the identifier, but remember
356
   * the prev as group and identifier as we will have to unlink it */
357
0
  lock_start_write( sr_lock );
358
359
  /* locate the identifier */
360
0
  for (sri=srg->identifiers, prev_sri=NULL ; sri ;
361
0
  prev_sri=sri,sri=sri->next ) {
362
0
    if ( sri->name.len==identifier.len &&
363
0
    strncasecmp( sri->name.s, identifier.s, identifier.len)==0 ) {
364
0
      LM_DBG("identity [%.*s] found in group [%.*s] , removing...\n",
365
0
        sri->name.len, sri->name.s,
366
0
        srg->name.len, srg->name.s);
367
      /* we found the identifier, first remove it from the 
368
       * list of identifiers within the group */
369
0
      if (prev_sri)
370
0
        prev_sri->next = sri->next;
371
0
      else
372
0
        srg->identifiers = sri->next;
373
374
      /* purge the identity - it is safe to remove it as we have
375
       * the WRITE access to the lists, so no one else may have 
376
       * a referece to the identity */
377
0
      for( i=0 ; i<sri->max_reports ; i++ )
378
0
        if (sri->reports[i].log.s)
379
0
          shm_free(sri->reports[i].log.s);
380
0
      if (sri->status_txt.s)
381
0
        shm_free(sri->status_txt.s);
382
0
      shm_free( sri );
383
384
      /* done */
385
0
      lock_stop_write( sr_lock );
386
0
      return 0;
387
0
    }
388
0
  }
389
390
  /* not found */
391
0
  LM_BUG("asking to remove an identity which was not found\n");
392
0
  lock_stop_write( sr_lock );
393
0
  return -1;
394
0
}
395
396
/* event parameters */
397
static str evi_group_str = str_init("group");
398
static str evi_identifier_str = str_init("identifier");
399
static str evi_status_str = str_init("status");
400
static str evi_details_str = str_init("details");
401
static str evi_old_status_str = str_init("old_status");
402
403
404
/* Sets a new status for an existing identifier (within a group).
405
 * - The "group" must a pointer to a registered SR group, mandatory
406
 * - The "identifier" name is optional if this is a default (per entire group
407
 *    identifier).
408
 * - Optionally a status text/detail may be provided (set status_txt_s NULL
409
 *    to avoid using it)
410
 * - Set "is_public" if this operation was triggered from script or external
411
      sources (like MI). If the identity does not accept public ops, this
412
 *    operation will be forbidden.
413
 */
414
int sr_set_status( void *group,
415
    char *identifier_s, int identifier_len,
416
    int status, char *status_txt_s, int status_txt_len,
417
    int is_public)
418
0
{
419
0
  sr_group *srg = (sr_group*)group;
420
0
  str identifier = {identifier_s, identifier_len};
421
0
  sr_identifier *sri;
422
0
  evi_params_p list = NULL;
423
0
  int old_status;
424
0
  str s;
425
426
0
  if (identifier.s==NULL)
427
0
    identifier = main_identifier;
428
429
0
  if ( (is_public) ^ (srg->is_public) ) {
430
0
    LM_ERR("forbidden setting status for identifier [%.*s] group [%.*s]\n",
431
0
      identifier.len, identifier.s, srg->name.len, srg->name.s);
432
0
    return -1;
433
0
  }
434
435
0
  lock_start_read( sr_lock );
436
437
0
  sri = _get_identifier_by_name( srg, &identifier);
438
0
  if (sri==NULL) {
439
0
    lock_stop_read( sr_lock );
440
0
    LM_ERR("setting status for unknow identifier [%.*s] group [%.*s]\n",
441
0
      identifier.len, identifier.s, srg->name.len, srg->name.s);
442
0
    return -1;
443
0
  }
444
445
  /* we do no accept 0 status values, as we cannot return 0 to script */
446
0
  if (status==0)
447
0
    status = 1;
448
449
0
  lock_get( &sri->lock );
450
0
  old_status = sri->status;
451
0
  sri->status = status;
452
0
  if (sri->status_txt.s) shm_free(sri->status_txt.s);
453
0
  if (status_txt_s) {
454
0
    sri->status_txt.s = shm_malloc(status_txt_len);
455
0
    if (sri->status_txt.s) {
456
0
      memcpy( sri->status_txt.s, status_txt_s, status_txt_len);
457
0
      sri->status_txt.len = status_txt_len;
458
0
    } else
459
0
      sri->status_txt.len = 0;
460
0
  } else {
461
0
    sri->status_txt.s = NULL;
462
0
    sri->status_txt.len = 0;
463
0
  }
464
0
  lock_release( &sri->lock );
465
466
  /* raise event if status changed */
467
0
  if (old_status != status && evi_probe_event(sr_evi_id)) {
468
0
    if (!(list = evi_get_params()))
469
0
      goto done;
470
0
    if (evi_param_add_str( list, &evi_group_str, &srg->name)) {
471
0
      LM_ERR("unable to add group EVI parameter\n");
472
0
      evi_free_params(list);
473
0
      goto done;
474
0
    }
475
0
    if (evi_param_add_str( list, &evi_identifier_str, &sri->name)) {
476
0
      LM_ERR("unable to add identifier EVI parameter\n");
477
0
      evi_free_params(list);
478
0
      goto done;
479
0
    }
480
0
    if (evi_param_add_int( list, &evi_status_str, &status)) {
481
0
      LM_ERR("unable to add status EVI parameter\n");
482
0
      evi_free_params(list);
483
0
      goto done;
484
0
    }
485
0
    if (status_txt_s) {
486
0
      s.s = status_txt_s;
487
0
      s.len = status_txt_len;
488
0
      if (evi_param_add_str( list, &evi_details_str, &s)) {
489
0
        LM_ERR("unable to add identifier parameter\n");
490
0
        evi_free_params(list);
491
0
        goto done;
492
0
      }
493
0
    }
494
0
    if (evi_param_add_int( list, &evi_old_status_str, &old_status)) {
495
0
      LM_ERR("unable to add old_status EVI parameter\n");
496
0
      evi_free_params(list);
497
0
      goto done;
498
0
    }
499
500
0
    if (evi_raise_event( sr_evi_id, list)) {
501
0
      LM_ERR("unable to raise status_changed event\n");
502
0
    }
503
504
0
  }
505
506
0
  lock_stop_read( sr_lock );
507
508
0
done:
509
0
  return 0;
510
0
}
511
512
513
/* Adds a new report log to an identifier.
514
 * - The "group" must a pointer to a registered SR group, mandatory
515
 * - The "identifier" name is optional if this is the default, per entire group
516
 *    identifier.
517
 * - The "report" is mandatory too, it will be internally duplicated
518
 * - Set "is_public" if this operation was triggered from script or external
519
 *    sources (like MI). If the identity does not accept public ops, this
520
 *    operation will be forbidden.
521
 */
522
int sr_add_report(void *group,
523
    char *identifier_s, int identifier_len,
524
    char *report_s, int report_len,
525
    int is_public)
526
0
{
527
0
  sr_group *srg = (sr_group*)group;
528
0
  str identifier = {identifier_s, identifier_len};
529
0
  sr_identifier *sri;
530
0
  short idx;
531
0
  char *s;
532
533
0
  if (group==NULL || report_s==NULL) {
534
0
    LM_BUG("bogus call wtih group %p, report %p\n",
535
0
      group, report_s);
536
0
    return -1;
537
0
  }
538
539
0
  if (identifier.s==NULL)
540
0
    identifier = main_identifier;
541
542
0
  if ( (is_public) ^ (srg->is_public) ) {
543
0
    LM_ERR("forbidden adding report to identifier [%.*s] group [%.*s]\n",
544
0
      identifier.len, identifier.s, srg->name.len, srg->name.s);
545
0
    return -1;
546
0
  }
547
548
0
  lock_start_read( sr_lock );
549
550
0
  sri = _get_identifier_by_name( srg, &identifier);
551
0
  if (sri==NULL) {
552
0
    lock_stop_read( sr_lock );
553
0
    LM_ERR("adding report for unknow identifier [%.*s] group [%.*s]\n",
554
0
      identifier.len, identifier.s, srg->name.len, srg->name.s);
555
0
    return -1;
556
0
  }
557
558
0
  if (sri->max_reports==0)  {
559
0
    lock_stop_read( sr_lock );
560
0
    LM_ERR("identifier [%.*s] group [%.*s] does not accept reports\n",
561
0
      identifier.len, identifier.s, srg->name.len, srg->name.s);
562
0
    return -1;
563
0
  }
564
565
0
  s = shm_malloc( report_len );
566
0
  if (s==NULL) {
567
0
    LM_ERR("failed to sh malloc for cloning report\n");
568
0
    lock_stop_read( sr_lock );
569
0
    return -1;
570
0
  }
571
572
0
  lock_get( &sri->lock );
573
574
  /* compute the index where to add the new report */
575
0
  if (sri->first_report==-1 && sri->last_report==-1) {
576
0
    sri->first_report = 0;
577
0
    idx = 0;
578
0
  } else {
579
0
    idx = (sri->last_report+1) % sri->max_reports;
580
0
  }
581
582
0
  if (idx==sri->first_report && -1!=sri->last_report) {
583
    /* overflow, free the oldest report */
584
0
    shm_free( sri->reports[idx].log.s );
585
0
    sri->reports[idx].log.s = NULL;
586
0
    sri->reports[idx].log.len = 0;
587
0
    sri->first_report = (sri->first_report+1) % sri->max_reports;
588
0
  }
589
590
0
  sri->last_report = idx;
591
592
0
  LM_DBG("adding report to identifier [%.*s] group [%.*s] on idx %d "
593
0
    "[%d,%d]\n", identifier.len, identifier.s,
594
0
    srg->name.len, srg->name.s, idx, sri->first_report, sri->last_report);
595
596
  /* copy the report here */
597
0
  sri->reports[idx].log.s = s;
598
0
  memcpy( s, report_s, report_len);
599
0
  sri->reports[idx].log.len = report_len;
600
0
  sri->reports[idx].ts = time(NULL);
601
602
0
  lock_release( &sri->lock );
603
604
0
  lock_stop_read( sr_lock );
605
606
0
  return sri?0:-1;
607
0
}
608
609
610
int sr_add_report_fmt(void *group,
611
    char *identifier_s, int identifier_len,
612
    int is_public,
613
    char *fmt_val, ...)
614
0
{
615
0
  va_list ap;
616
0
  char *report_s;
617
0
  int report_len;
618
619
0
  va_start(ap, fmt_val);
620
0
  report_s = mi_print_fmt(fmt_val, ap, &report_len);
621
0
  va_end(ap);
622
0
  if (!report_s)
623
0
    return -1;
624
625
0
  return sr_add_report( group, identifier_s, identifier_len,
626
0
    report_s, report_len, is_public);
627
0
}
628
629
630
#define _add_mi_readiness( _mi_item, _status) \
631
0
  add_mi_bool(_mi_item, CHAR_INT("Readiness"), (_status<0)?0:1)
632
633
/* Checks the status of an identifier
634
 * NOTE1 : for internal usage only, it does its own locking
635
 */
636
static int _check_status(sr_group *srg, str *identifier, mi_item_t *id_item)
637
0
{
638
0
  sr_identifier *sri;
639
0
  int status;
640
641
0
  if (identifier==NULL) {
642
0
    identifier = &main_identifier;
643
0
  } else if (identifier->len==3 && strncasecmp(identifier->s, "all", 3)==0){
644
0
    identifier = (void*)-1;
645
0
  }
646
647
0
  lock_start_read( sr_lock );
648
649
0
  if ( identifier != (void*)-1 ) {
650
651
    /* only one identifier */
652
0
    sri = _get_identifier_by_name( srg, identifier);
653
0
    if (sri==NULL) {
654
0
      LM_DBG("identifier [%.*s] not found in group [%.*s]\n",
655
0
        identifier->len, identifier->s,
656
0
        srg->name.len, srg->name.s);
657
0
      status = SR_STATUS_NOT_FOUND;
658
0
    } else {
659
0
      lock_get( &sri->lock );
660
0
      status = sri->status ;
661
0
      if (id_item) {
662
0
        _add_mi_readiness( id_item, status);
663
0
        add_mi_number( id_item, CHAR_INT("Status"), status);
664
0
        if (sri->status_txt.s)
665
0
          add_mi_string(id_item, CHAR_INT("Details"),
666
0
            sri->status_txt.s, sri->status_txt.len);
667
0
      }
668
0
      lock_release( &sri->lock );
669
0
    }
670
671
0
  } else {
672
673
    /* aggregate the status of all indetifiers in the group */
674
0
    status = SR_STATUS_READY;
675
0
    for( sri=srg->identifiers ; sri ; sri=sri->next) {
676
0
      lock_get( &sri->lock );
677
0
      if (sri->status < 0)
678
0
        status = SR_STATUS_NOT_READY;
679
0
      lock_release( &sri->lock );
680
0
    }
681
682
0
    if (id_item) {
683
0
      _add_mi_readiness( id_item, status);
684
0
      add_mi_number( id_item, CHAR_INT("Status"), status);
685
0
      add_mi_string(id_item, CHAR_INT("Details"),
686
0
        CHAR_INT("aggregated"));
687
0
    }
688
689
0
  }
690
691
0
  lock_stop_read( sr_lock );
692
693
0
  return status;
694
0
}
695
696
697
/****************** SR status of the OpenSIPS core  **************/
698
699
int sr_set_core_status(enum sr_core_states status, char *txt_s, int txt_len)
700
0
{
701
0
  if (status == STATE_RUNNING) {
702
0
    ready_time = time(NULL);
703
0
    ready_delay = ready_time - startup_time;
704
0
  }
705
706
0
  return sr_set_status( srg_core, CHAR_INT_NULL /*main*/, status,
707
0
    txt_s, txt_len, 0);
708
0
}
709
710
711
/* light version of set_core_status, doing no shm, locking or event
712
 * operations - such ops are potentially dangerous during shutdonw */
713
void sr_set_core_status_terminating( void )
714
0
{
715
0
  sri_core->status = STATE_TERMINATING;
716
  /* note: the below assigment will produce a small mem leak in shm
717
   * (for the previously allocated status), but we do not really care
718
   * as we do this only once, at shutdown */
719
0
  sri_core->status_txt.s = "shutting down";
720
0
  sri_core->status_txt.len = 13;
721
0
}
722
723
724
enum sr_core_states sr_get_core_status(void)
725
0
{
726
0
  return sri_core->status;
727
0
}
728
729
730
int sr_add_core_report(char *report_s, int report_len)
731
0
{
732
0
  return sr_add_report( srg_core, CHAR_INT_NULL /*main*/,
733
0
    report_s, report_len, 0);
734
0
}
735
736
/****************** Core (to be used) functions  **********************/
737
738
int init_status_report(void)
739
0
{
740
0
  sr_lock = lock_init_rw();
741
0
  if (sr_lock==NULL) {
742
0
    LM_ERR("Failed to create the global RW lock, abording\n");
743
0
    return -1;
744
0
  }
745
746
0
  sr_evi_id = evi_publish_event(sr_event);
747
0
  if (sr_evi_id == EVI_ERROR) {
748
0
    LM_ERR("cannot register 'statis_changed' event\n");
749
0
    return -1;
750
0
  }
751
752
0
  srg_core = (sr_group*)sr_register_group_with_identifier(
753
0
    CHAR_INT("core"), 0/*not public*/, CHAR_INT_NULL /*main*/,
754
0
    STATE_NONE, CHAR_INT_NULL/*report*/, 10);
755
0
  if (srg_core==NULL) {
756
0
    LM_ERR("Failed to register 'status_report' group and identifier for "
757
0
      "'core'\n");
758
0
    return -1;
759
0
  }
760
761
  /* this is a bit hackish, but 100% safe as time as it is done right
762
   * after the above sr_register_group_with_identifier() - the "main"
763
   * identifier will be the only one in the group, so the first one too */
764
0
  sri_core = (srg_core)->identifiers;
765
  /* also it is safe to keep a reference here as we will never delete 
766
   * this identifer - but be CAREFULL on this !!! */
767
768
0
  return 0;
769
0
}
770
771
772
/****************** Core Scripting functions  **********************/
773
774
int fixup_sr_group(void **param)
775
0
{
776
0
  str *name = (str*)*param;
777
0
  sr_group *srg = NULL;
778
779
0
  if ( (srg=sr_get_group_by_name( name->s, name->len )) == NULL ) {
780
0
    LM_ERR("SR group [%.*s] not registered\n",
781
0
      name->len, name->s);
782
0
    return -1;
783
0
  }
784
0
  *param = (void*)srg;
785
786
0
  return 0;
787
0
}
788
789
790
/* Checks the status of an identifier
791
 * - The "msg" is useless, just for compatibility with script functions
792
 * - The "group" must a pointer to a registered SR group, mandatory
793
 * - The "identifier" may be: (1) name of an identifier, (2) NULL, to refer to
794
 *    the default (per group) identifier or (3) "all" to refer to all the
795
 *    identifiers in the group
796
 * Returns the status (as non zero value) of the requested identifier(s). If
797
 *  multiple indetifiers are to be checked - case (3) - the returned 
798
 *  status is an aggregated one as 1 or -1 (over all identifiers).
799
 */
800
int w_sr_check_status(struct sip_msg *msg, void *group, str *identifier)
801
0
{
802
0
  return _check_status((sr_group *)group, identifier, NULL);
803
0
}
804
805
806
/****************** Core MI  functions  **********************/
807
808
mi_response_t *mi_sr_get_status(const mi_params_t *params,
809
                      struct mi_handler *async_hdl)
810
0
{
811
0
  str group;
812
0
  str identifier;
813
0
  int status;
814
0
  mi_response_t *resp;
815
0
  mi_item_t *resp_obj;
816
0
  sr_group *srg = NULL;
817
818
0
  if (get_mi_string_param(params, "group", &group.s, &group.len) < 0)
819
0
    return init_mi_param_error();
820
821
0
  if ( (srg=sr_get_group_by_name( group.s, group.len )) == NULL ) {
822
0
    LM_DBG("SR group [%.*s] not found as registered\n",
823
0
      group.len, group.s);
824
0
    return init_mi_error(404, CHAR_INT("Group not found"));
825
0
  }
826
827
0
  resp = init_mi_result_object(&resp_obj);
828
0
  if (!resp)
829
0
    return 0;
830
831
0
  if (try_get_mi_string_param(params, "identifier",
832
0
  &identifier.s, &identifier.len)!=0) {
833
    /* no identifier passed */
834
0
    status = _check_status( srg, NULL, resp_obj);
835
0
  } else {
836
0
    status = _check_status( srg, &identifier, resp_obj);
837
0
  }
838
839
0
  if (status == SR_STATUS_NOT_FOUND) {
840
0
    free_mi_response(resp);
841
0
    return init_mi_error(404, CHAR_INT("Identity not found"));
842
0
  }
843
844
0
  return resp;
845
0
}
846
847
848
static int _mi_list_status_group(sr_group *srg, mi_item_t *id_arr)
849
0
{
850
0
  sr_identifier *sri;
851
0
  mi_item_t *id_item;
852
853
0
  for ( sri=srg->identifiers ; sri ; sri=sri->next ) {
854
855
0
    id_item = add_mi_object(id_arr, 0, 0);
856
0
    if (!id_item)
857
0
      return -1;
858
859
0
    lock_get( &sri->lock );
860
861
0
    if (add_mi_string(id_item, CHAR_INT("Name"),
862
0
    sri->name.s, sri->name.len ) < 0 ) {
863
0
      lock_release( &sri->lock );
864
0
      return -1;
865
0
    }
866
867
0
    if (_add_mi_readiness( id_item, sri->status)<0 ) {
868
0
      lock_release( &sri->lock );
869
0
      return -1;
870
0
    }
871
872
0
    if (add_mi_number( id_item, CHAR_INT("Status"), sri->status)<0) {
873
0
      lock_release( &sri->lock );
874
0
      return -1;
875
0
    }
876
877
0
    if (sri->status_txt.s && add_mi_string(id_item, CHAR_INT("Details"),
878
0
    sri->status_txt.s, sri->status_txt.len) < 0) {
879
0
      lock_release( &sri->lock );
880
0
      return -1;
881
0
    }
882
883
0
    lock_release( &sri->lock );
884
885
0
  }
886
887
0
  return 0;
888
0
}
889
890
891
mi_response_t *mi_sr_list_status(const mi_params_t *params,
892
                      struct mi_handler *async_hdl)
893
0
{
894
0
  str group;
895
0
  mi_response_t *resp;
896
0
  mi_item_t *grp_arr, *grp_item, *id_arr;
897
0
  sr_group *srg = NULL;
898
899
0
  if (try_get_mi_string_param(params, "group", &group.s, &group.len)!=0) {
900
    /* no group passed */
901
0
    srg = NULL;
902
0
  } else {
903
0
    if ( (srg=sr_get_group_by_name( group.s, group.len )) == NULL ) {
904
0
      LM_DBG("SR group [%.*s] not found as registered\n",
905
0
        group.len, group.s);
906
0
      return init_mi_error(404, CHAR_INT("Group not found"));
907
0
    }
908
0
  }
909
910
0
  lock_start_read( sr_lock );
911
912
  /* list the readiness of all identifiers from the group(s) */
913
0
  if (srg) {
914
915
0
    resp = init_mi_result_array( &id_arr);
916
0
    if (!resp)
917
0
      goto error;
918
919
0
    if (_mi_list_status_group( srg, id_arr)<0) {
920
0
      LM_ERR("failed to inser group, mem failure\n");
921
0
      goto error;
922
0
    }
923
924
0
  } else {
925
926
0
    resp = init_mi_result_array( &grp_arr);
927
0
    if (!resp)
928
0
      goto error;
929
930
0
    for ( srg=sr_groups ; srg ; srg=srg->next ) {
931
      
932
0
      grp_item = add_mi_object( grp_arr, 0, 0);
933
0
      if (!grp_item)
934
0
        goto error;
935
936
0
      if (add_mi_string( grp_item, CHAR_INT("Name"),
937
0
      srg->name.s, srg->name.len)<0)
938
0
        goto error;
939
940
0
      id_arr = add_mi_array( grp_item, CHAR_INT("Identifiers"));
941
0
      if (!id_arr)
942
0
        goto error;
943
944
0
      if (_mi_list_status_group( srg, id_arr)<0) {
945
0
        LM_ERR("failed to inser group, mem failure\n");
946
0
        goto error;
947
0
      }
948
0
    }
949
950
0
  }
951
952
0
  lock_stop_read( sr_lock );
953
954
0
  return resp;
955
0
error:
956
0
  lock_stop_read( sr_lock );
957
0
  free_mi_response(resp);
958
0
  return 0;
959
960
0
}
961
962
963
static int _mi_list_reports(sr_identifier *sri, mi_item_t *log_arr)
964
0
{
965
0
  mi_item_t *log_item;
966
0
  short i, cnt;
967
0
  str date;
968
969
0
  lock_get( &sri->lock );
970
971
0
  if (sri->first_report==-1 || sri->max_reports==0) {
972
    /* no reports at all */
973
0
    lock_release( &sri->lock );
974
0
    return 0;
975
0
  }
976
977
978
0
  cnt = sri->last_report - sri->first_report + 1;
979
0
  if (cnt<=0)
980
0
    cnt += sri->max_reports;
981
982
0
  LM_DBG("idxes: first=%d, last=%d, cnt=%d\n",
983
0
    sri->first_report,sri->last_report,cnt);
984
985
0
  for ( i=sri->first_report ; cnt ; i=(i+1)%sri->max_reports,cnt-- ) {
986
987
0
    log_item = add_mi_object( log_arr, 0, 0);
988
0
    if (log_item==NULL)
989
0
      goto error;
990
991
0
    if (add_mi_number( log_item, CHAR_INT("Timestamp"), sri->reports[i].ts) < 0)
992
0
      goto error;
993
994
0
    date.s = ctime( &sri->reports[i].ts );
995
0
    date.len = strlen(date.s) - 1 /* get rid of the trailing \n */;
996
0
    if (add_mi_string( log_item, CHAR_INT("Date"), date.s, date.len) < 0)
997
0
      goto error;
998
999
0
    if (add_mi_string( log_item, CHAR_INT("Log"),
1000
0
    sri->reports[i].log.s, sri->reports[i].log.len ) < 0)
1001
0
      goto error;
1002
0
  }
1003
1004
0
  lock_release( &sri->lock );
1005
0
  return 0;
1006
1007
0
error:
1008
0
  lock_release( &sri->lock );
1009
0
  return -1;
1010
0
}
1011
1012
1013
static int _mi_list_reports_group(sr_group *srg, mi_item_t *id_arr)
1014
0
{
1015
0
  sr_identifier *sri;
1016
0
  mi_item_t *id_item, *log_arr;
1017
1018
0
  for ( sri=srg->identifiers ; sri ; sri=sri->next ) {
1019
1020
0
    id_item = add_mi_object( id_arr, 0, 0);
1021
0
    if (!id_item)
1022
0
      return -1;
1023
1024
0
    if (add_mi_string( id_item, CHAR_INT("Name"),
1025
0
    sri->name.s, sri->name.len ) < 0 )
1026
0
      return -1;
1027
1028
0
    log_arr = add_mi_array( id_item, CHAR_INT("Reports"));
1029
0
    if (log_arr==NULL)
1030
0
      return -1;
1031
1032
0
    if ( _mi_list_reports( sri, log_arr)!=0 )
1033
0
      return -1;
1034
1035
0
  }
1036
1037
0
  return 0;
1038
0
}
1039
1040
1041
mi_response_t *mi_sr_list_reports(const mi_params_t *params,
1042
                      struct mi_handler *async_hdl)
1043
0
{
1044
0
  str group, identifier;
1045
0
  mi_response_t *resp;
1046
0
  mi_item_t *log_arr, *id_arr, *grp_arr, *grp_item;
1047
0
  sr_group *srg = NULL;
1048
0
  sr_identifier *sri = NULL;
1049
1050
0
  if (try_get_mi_string_param(params, "group", &group.s, &group.len)==0) {
1051
1052
    /* group provide */
1053
0
    if ( (srg=sr_get_group_by_name( group.s, group.len )) == NULL ) {
1054
0
      LM_DBG("SR group [%.*s] not found as registered\n",
1055
0
        group.len, group.s);
1056
0
      return init_mi_error(404, CHAR_INT("Group not found"));
1057
0
    }
1058
1059
0
    lock_start_read( sr_lock );
1060
1061
0
    if (try_get_mi_string_param(params, "identifier",
1062
0
    &identifier.s, &identifier.len)==0) {
1063
0
      if ( (sri=_get_identifier_by_name( srg, &identifier )) == NULL ) {
1064
0
        lock_stop_read( sr_lock );
1065
0
        LM_DBG("SR identifier [%.*s] group [%.*s] not found as "
1066
0
          "registered\n", identifier.len, identifier.s,
1067
0
          group.len, group.s);
1068
0
        return init_mi_error(404, CHAR_INT("Identifier not found"));
1069
0
      }
1070
1071
0
    }
1072
1073
0
  } else {
1074
1075
0
    lock_start_read( sr_lock );
1076
1077
0
  }
1078
1079
0
  if (sri) {
1080
1081
0
    resp = init_mi_result_array( &log_arr);
1082
0
    if (!log_arr)
1083
0
      goto error;
1084
1085
0
    if (_mi_list_reports( sri, log_arr)<0) {
1086
0
      LM_ERR("failed to inser identity, mem failure\n");
1087
0
      goto error;
1088
0
    }
1089
1090
0
  } else if (srg) {
1091
1092
0
    resp = init_mi_result_array( &id_arr);
1093
0
    if (!id_arr)
1094
0
      goto error;
1095
1096
0
    if (_mi_list_reports_group( srg, id_arr)<0) {
1097
0
      LM_ERR("failed to inser group, mem failure\n");
1098
0
      goto error;
1099
0
    }
1100
1101
0
  } else {
1102
1103
0
    resp = init_mi_result_array( &grp_arr);
1104
0
    if (!grp_arr)
1105
0
      goto error;
1106
1107
0
    for ( srg=sr_groups ; srg ; srg=srg->next ) {
1108
1109
0
      grp_item = add_mi_object(grp_arr, 0, 0);
1110
0
      if (!grp_item)
1111
0
        goto error;
1112
1113
0
      if (add_mi_string( grp_item, CHAR_INT("Name"),
1114
0
      srg->name.s, srg->name.len)<0)
1115
0
        goto error;
1116
1117
0
      id_arr = add_mi_array(grp_item, CHAR_INT("Identifiers"));
1118
0
      if (!id_arr)
1119
0
        goto error;
1120
1121
0
      if (_mi_list_reports_group( srg, id_arr)<0) {
1122
0
        LM_ERR("failed to inser group, mem failure\n");
1123
0
        goto error;
1124
0
      }
1125
0
    }
1126
0
  }
1127
1128
0
  lock_stop_read( sr_lock );
1129
1130
0
  return resp;
1131
0
error:
1132
0
  lock_stop_read( sr_lock );
1133
0
  if (resp)
1134
0
    free_mi_response(resp);
1135
0
  return 0;
1136
0
}
1137
1138
1139
mi_response_t *mi_sr_list_identifiers(const mi_params_t *params,
1140
                      struct mi_handler *async_hdl)
1141
0
{
1142
0
  str group;
1143
0
  mi_response_t *resp;
1144
0
  mi_item_t *id_arr, *grp_arr, *grp_item;
1145
0
  sr_group *srg = NULL;
1146
0
  sr_identifier *sri = NULL;
1147
1148
0
  if (try_get_mi_string_param(params, "group", &group.s, &group.len)==0) {
1149
1150
    /* group provide */
1151
0
    if ( (srg=sr_get_group_by_name( group.s, group.len )) == NULL ) {
1152
0
      LM_DBG("SR group [%.*s] not found as registered\n",
1153
0
        group.len, group.s);
1154
0
      return init_mi_error(404, CHAR_INT("Group not found"));
1155
0
    }
1156
1157
0
  }
1158
1159
0
  lock_start_read( sr_lock );
1160
1161
0
  if (srg) {
1162
1163
0
    resp = init_mi_result_object( &grp_item);
1164
0
    if (!grp_item)
1165
0
      goto error;
1166
1167
0
    if (add_mi_string( grp_item, CHAR_INT("Group"),
1168
0
    srg->name.s, srg->name.len)<0)
1169
0
      goto error;
1170
1171
0
    id_arr = add_mi_array( grp_item,  CHAR_INT("Identifiers"));
1172
0
    if (!id_arr)
1173
0
      goto error;
1174
1175
0
    for ( sri=srg->identifiers ; sri ; sri=sri->next ) {
1176
0
      if (add_mi_string( id_arr, CHAR_INT("Name"),
1177
0
      sri->name.s, sri->name.len ) < 0 )
1178
0
        goto error;
1179
0
    }
1180
1181
0
  } else {
1182
1183
0
    resp = init_mi_result_array( &grp_arr);
1184
0
    if (!grp_arr)
1185
0
      goto error;
1186
1187
0
    for ( srg=sr_groups ; srg ; srg=srg->next ) {
1188
1189
0
      grp_item = add_mi_object(grp_arr, 0, 0);
1190
0
      if (!grp_item)
1191
0
        goto error;
1192
1193
0
      if (add_mi_string( grp_item, CHAR_INT("Group"),
1194
0
      srg->name.s, srg->name.len)<0)
1195
0
        goto error;
1196
1197
0
      id_arr = add_mi_array( grp_item,  CHAR_INT("Identifiers"));
1198
0
      if (!id_arr)
1199
0
        goto error;
1200
1201
0
      for ( sri=srg->identifiers ; sri ; sri=sri->next ) {
1202
0
        if (add_mi_string( id_arr, CHAR_INT("Name"),
1203
0
        sri->name.s, sri->name.len ) < 0 )
1204
0
          goto error;
1205
0
      }
1206
1207
0
    }
1208
0
  }
1209
1210
0
  lock_stop_read( sr_lock );
1211
1212
0
  return resp;
1213
0
error:
1214
0
  lock_stop_read( sr_lock );
1215
0
  if (resp)
1216
0
    free_mi_response(resp);
1217
0
  return 0;
1218
1219
0
}