source: roaraudio/libroar/trap.c @ 5248:0133acb5ae31

Last change on this file since 5248:0133acb5ae31 was 4784:7aa703c721af, checked in by phi, 13 years ago

added support for trap-policy:die and updated to a good message for policy:warn

File size: 4.0 KB
Line 
1//*.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38static struct _group {
39 unsigned int id;
40 const char * name;
41} _libroar_trap_groups[] = {
42 {ROAR_TRAP_GROUP_LIBROAR, "libroar"},
43 {ROAR_TRAP_GROUP_ROARD,   "roard"},
44 {ROAR_TRAP_GROUP_APP,     "app"},
45 {ROAR_TRAP_GROUP_LIB,     "lib"},
46 {ROAR_TRAP_GROUP_NETWORK, "network"},
47 {ROAR_TRAP_GROUP_PROTO,   "proto"},
48 {-1, NULL}
49};
50
51unsigned int roar_trap_register_group(const char * name) {
52 static unsigned int state = ROAR_TRAP_GROUP_USER_MIN;
53 unsigned int ret;
54 size_t i;
55
56 for (i = 0; i < (sizeof(_libroar_trap_groups)/sizeof(*_libroar_trap_groups)) && _libroar_trap_groups[i].id != -1; i++);
57 if ( i == (sizeof(_libroar_trap_groups)/sizeof(*_libroar_trap_groups)) )
58  return -1;
59
60 ret = state++;
61 _libroar_trap_groups[i].id   = ret;
62 _libroar_trap_groups[i].name = name;
63
64 return ret;
65}
66
67const char * roar_trap_get_groupname(const unsigned int group) {
68 size_t i;
69
70 for (i = 0; i < (sizeof(_libroar_trap_groups)/sizeof(*_libroar_trap_groups)) && _libroar_trap_groups[i].id != -1; i++) {
71  if ( _libroar_trap_groups[i].id == group )
72   return _libroar_trap_groups[i].name;
73 }
74
75 return NULL;
76}
77
78unsigned int roar_trap_get_groupid(const char * name) {
79 size_t i;
80
81 for (i = 0; i < (sizeof(_libroar_trap_groups)/sizeof(*_libroar_trap_groups)) && _libroar_trap_groups[i].id != -1; i++) {
82  if ( !strcasecmp(_libroar_trap_groups[i].name, name) )
83   return _libroar_trap_groups[i].id;
84 }
85
86 return -1;
87}
88
89void roar_strap_impl(const unsigned int group, const char * name, unsigned int line, const char * file, const char * prefix) {
90#ifdef ROAR_SUPPORT_TRAP
91 struct roar_libroar_config * config = roar_libroar_get_config();
92 enum roar_trap_policy policy = config->trap_policy;
93
94 // need to search for the correct policy for this specific trap.
95
96 switch (policy) {
97  case ROAR_TRAP_IGNORE:
98    return;
99   break;
100  case ROAR_TRAP_WARN:
101    ROAR_DBG("roar_strap_impl(group=%u(\"%s\"), name='%s', line=%u, file='%s') = (void)",
102               group, roar_trap_get_groupname(group), name, line, file);
103    roar_debug_msg(ROAR_DEBUG_TYPE_WARNING, line, file, prefix, "Trap %s of group %s.",
104                   name, roar_trap_get_groupname(group));
105   break;
106  case ROAR_TRAP_ABORT:
107    abort();
108   break;
109#ifdef SIGKILL
110  case ROAR_TRAP_KILL:
111    raise(SIGKILL);
112   break;
113#endif
114#ifdef SIGSTOP
115  case ROAR_TRAP_STOP:
116    raise(SIGSTOP);
117   break;
118#endif
119  case ROAR_TRAP_DIE:
120    roar_debug_msg(ROAR_DEBUG_TYPE_WARNING, line, file, prefix, "Trap %s of group %s, terminating program.",
121                   name, roar_trap_get_groupname(group));
122#ifdef SIGKILL
123    raise(SIGKILL);
124#else
125    abort();
126#endif
127   break;
128 }
129#else
130 return;
131#endif
132}
133
134//ll
Note: See TracBrowser for help on using the repository browser.