linux/bootstrap: use Forbid/Permit only when thread is main AROS thread
[AROS.git] / compiler / stdc / __signal.c
blob32204621d8aad5870c9f1a2a3737bcf0c2121e1a
1 /*
2 Copyright © 2012-2013, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/exec.h>
7 #include <proto/intuition.h>
8 #include <libraries/stdc.h>
10 /* We include the signal.h of posixc.library for getting min/max signal number
11 TODO: Implement more elegant way to get maximum and minimum signal number
13 #include <aros/posixc/signal.h>
14 #include <errno.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <assert.h>
19 #include "__stdc_intbase.h"
20 #include "__signal.h"
21 #include "__optionallibs.h"
23 #define DEBUG 0
24 #include <aros/debug.h>
26 struct signal_func_data *__sig_getfuncdata(int signum)
28 struct StdCIntBase *StdCBase =
29 (struct StdCIntBase *)__aros_getbase_StdCBase();
30 int i;
32 if (signum < SIGHUP || signum > _SIGMAX)
34 errno = EINVAL;
35 return NULL;
38 if (StdCBase->sigfunc_array == NULL)
40 StdCBase->sigfunc_array = malloc(_SIGMAX*sizeof(struct signal_func_data));
42 if (!StdCBase->sigfunc_array)
44 errno = ENOMEM;
45 return NULL;
48 for (i = 0; i < _SIGMAX; i++)
50 StdCBase->sigfunc_array[i].sigfunc = SIG_DFL;
51 StdCBase->sigfunc_array[i].flags = 0;
55 return &StdCBase->sigfunc_array[signum-1];
58 /* Handler for SIG_DFL */
59 /* TODO: Maybe a mechanism has to be implemented so that default signal handler
60 can be overloaded by libraries. For example by stdcio.library or posixc.library
61 so they can use stdio for presenting the caught signal
63 void __sig_default(int signum)
65 char s[50];
66 char *taskname = FindTask(NULL)->tc_Node.ln_Name;
68 switch (signum)
70 case SIGABRT:
71 sprintf(s, "Program '%s' aborted.", taskname);
72 break;
74 case SIGTERM:
75 sprintf(s, "Program '%s' terminated.", taskname);
76 break;
78 default:
79 sprintf(s, "Program '%s' caught signal %d\naborting...",
80 taskname, signum
82 break;
85 /* Open requester if IntuitionBase is available otherwise use kprintf() */
86 if (__intuition_available())
88 struct EasyStruct es =
90 sizeof(struct EasyStruct),
92 "Caught Signal",
93 "%s",
94 "OK"
97 EasyRequest(NULL, &es, NULL, s);
99 else
100 kprintf("%s\n", s);
102 __stdc_jmp2exit(0, 20);
104 assert(0); /* Should not be reached */