/src/FreeRDP/libfreerdp/utils/signal.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Signal handling |
4 | | * |
5 | | * Copyright 2011 Shea Levy <shea@shealevy.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <freerdp/config.h> |
21 | | |
22 | | #include <string.h> |
23 | | |
24 | | #include <winpr/crt.h> |
25 | | #include <winpr/debug.h> |
26 | | |
27 | | #include <freerdp/utils/signal.h> |
28 | | #include <freerdp/log.h> |
29 | | |
30 | | #include "platform_signal.h" |
31 | | |
32 | | #define TAG FREERDP_TAG("utils.signal") |
33 | | |
34 | | #if defined(_WIN32) |
35 | | const char* strsignal(int signum); |
36 | | #endif |
37 | | |
38 | | BOOL fsig_handlers_registered = FALSE; |
39 | | |
40 | | typedef struct |
41 | | { |
42 | | void* context; |
43 | | freerdp_signal_handler_t handler; |
44 | | } cleanup_handler_t; |
45 | | |
46 | | static size_t cleanup_handler_count = 0; |
47 | | static cleanup_handler_t cleanup_handlers[20] = WINPR_C_ARRAY_INIT; |
48 | | |
49 | | void fsig_term_handler(int signum) |
50 | 0 | { |
51 | 0 | static BOOL recursive = FALSE; |
52 | |
|
53 | 0 | if (!recursive) |
54 | 0 | { |
55 | 0 | recursive = TRUE; |
56 | | // NOLINTNEXTLINE(concurrency-mt-unsafe) |
57 | 0 | WLog_ERR(TAG, "Caught signal '%s' [%d]", strsignal(signum), signum); |
58 | 0 | } |
59 | |
|
60 | 0 | fsig_lock(); |
61 | 0 | for (size_t x = 0; x < cleanup_handler_count; x++) |
62 | 0 | { |
63 | 0 | const cleanup_handler_t empty = WINPR_C_ARRAY_INIT; |
64 | 0 | cleanup_handler_t* cur = &cleanup_handlers[x]; |
65 | 0 | if (cur->handler) |
66 | 0 | { |
67 | | // NOLINTNEXTLINE(concurrency-mt-unsafe) |
68 | 0 | cur->handler(signum, strsignal(signum), cur->context); |
69 | 0 | } |
70 | 0 | *cur = empty; |
71 | 0 | } |
72 | 0 | cleanup_handler_count = 0; |
73 | 0 | fsig_unlock(); |
74 | 0 | } |
75 | | |
76 | | BOOL freerdp_add_signal_cleanup_handler(void* context, freerdp_signal_handler_t handler) |
77 | 0 | { |
78 | 0 | BOOL rc = FALSE; |
79 | 0 | fsig_lock(); |
80 | 0 | if (fsig_handlers_registered) |
81 | 0 | { |
82 | 0 | if (cleanup_handler_count < ARRAYSIZE(cleanup_handlers)) |
83 | 0 | { |
84 | 0 | cleanup_handler_t* cur = &cleanup_handlers[cleanup_handler_count++]; |
85 | 0 | cur->context = context; |
86 | 0 | cur->handler = handler; |
87 | 0 | } |
88 | 0 | else |
89 | 0 | WLog_WARN(TAG, "Failed to register cleanup handler, only %" PRIuz " handlers supported", |
90 | 0 | ARRAYSIZE(cleanup_handlers)); |
91 | 0 | } |
92 | 0 | rc = TRUE; |
93 | 0 | fsig_unlock(); |
94 | 0 | return rc; |
95 | 0 | } |
96 | | |
97 | | BOOL freerdp_del_signal_cleanup_handler(void* context, freerdp_signal_handler_t handler) |
98 | 0 | { |
99 | 0 | BOOL rc = FALSE; |
100 | 0 | fsig_lock(); |
101 | 0 | if (fsig_handlers_registered) |
102 | 0 | { |
103 | 0 | for (size_t x = 0; x < cleanup_handler_count; x++) |
104 | 0 | { |
105 | 0 | cleanup_handler_t* cur = &cleanup_handlers[x]; |
106 | 0 | if ((cur->context == context) && (cur->handler == handler)) |
107 | 0 | { |
108 | 0 | const cleanup_handler_t empty = WINPR_C_ARRAY_INIT; |
109 | 0 | for (size_t y = x + 1; y < cleanup_handler_count - 1; y++) |
110 | 0 | { |
111 | 0 | *cur++ = cleanup_handlers[y]; |
112 | 0 | } |
113 | |
|
114 | 0 | *cur = empty; |
115 | 0 | cleanup_handler_count--; |
116 | 0 | break; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | } |
120 | 0 | rc = TRUE; |
121 | 0 | fsig_unlock(); |
122 | 0 | return rc; |
123 | 0 | } |