]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/LibC/Signal/Signal.c
Standard Libraries for EDK II.
[mirror_edk2.git] / StdLib / LibC / Signal / Signal.c
CommitLineData
2aa62f2b 1/** @file\r
2 Implementation of the signal and raise functions as declared in <signal.h>.\r
3\r
4 Copyright (c) 2010, Intel Corporation. All rights reserved.<BR>\r
5 This program and the accompanying materials are licensed and made available under\r
6 the terms and conditions of the BSD License that accompanies this distribution.\r
7 The full text of the license may be found at\r
8 http://opensource.org/licenses/bsd-license.php.\r
9\r
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14#include <Uefi.h>\r
15#include <Library/UefiLib.h>\r
16\r
17#include <LibConfig.h>\r
18#include <errno.h>\r
19#include <signal.h>\r
20#include <MainData.h>\r
21\r
22/** The signal function associates a "signal handler" with a signal number.\r
23\r
24 The signal function chooses one of three ways in which receipt of the\r
25 signal number, sig, is to be subsequently handled. If the value of func\r
26 is SIG_DFL, default handling for that signal will occur. If the value of\r
27 func is SIG_IGN, the signal will be ignored. Otherwise, func shall point\r
28 to a function to be called when that signal occurs. An invocation of such a\r
29 function because of a signal, or (recursively) of any further functions\r
30 called by that invocation (other than functions in the standard library),\r
31 is called a signal handler.\r
32\r
33 At program startup, the equivalent of signal(sig, SIG_IGN); may be executed\r
34 for some signals selected in an implementation-defined manner; the\r
35 equivalent of signal(sig, SIG_DFL); is executed for all other signals\r
36 defined by the implementation.\r
37\r
38 @return If the request can be honored, the signal function returns the\r
39 value of func for the most recent successful call to signal for\r
40 the specified signal sig. Otherwise, a value of SIG_ERR is\r
41 returned and a positive value is stored in errno.\r
42 */\r
43__sighandler_t *\r
44signal(int sig, __sighandler_t *func)\r
45{\r
46 __sighandler_t *OldHandler;\r
47\r
48 if (sig < 0 || sig >= SIG_LAST) {\r
49 errno = EINVAL;\r
50 return SIG_ERR;\r
51 }\r
52 OldHandler = gMD->sigarray[sig];\r
53 gMD->sigarray[sig] = func;\r
54\r
55 return OldHandler;\r
56}\r
57\r
58static\r
59void\r
60_defaultSignalHandler( int sig )\r
61{\r
62 Print(L"\nCaught signal %d.\n", sig);\r
63}\r
64\r
65/** Send a signal.\r
66\r
67 The raise function carries out the actions described for signal, above,\r
68 for the signal sig.\r
69\r
70 If a signal handler is called, the raise function shall not return until\r
71 after the signal handler does.\r
72\r
73 @return The raise function returns zero if successful,\r
74 nonzero if unsuccessful.\r
75**/\r
76int\r
77raise( int sig)\r
78{\r
79 __sighandler_t *Handler;\r
80\r
81 if (sig < 0 || sig >= SIG_LAST) {\r
82 return EINVAL;\r
83 }\r
84 Handler = gMD->sigarray[sig];\r
85\r
86 if(Handler == SIG_DFL) {\r
87 _defaultSignalHandler( sig );\r
88 }\r
89 else if( Handler != SIG_IGN) {\r
90 Handler( sig );\r
91 }\r
92 return 0;\r
93}\r