forked from SiliconLabs/cpc-daemon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
333 lines (267 loc) · 8.53 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/***************************************************************************//**
* @file
* @brief Co-Processor Communication Protocol(CPC) - Main
*******************************************************************************
* # License
* <b>Copyright 2022 Silicon Laboratories Inc. www.silabs.com</b>
*******************************************************************************
*
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* www.silabs.com/about-us/legal/master-software-license-agreement. This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
*
******************************************************************************/
#define _GNU_SOURCE
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/signalfd.h>
#include <sys/epoll.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>
#include "version.h"
#include "misc/config.h"
#include "misc/logging.h"
#include "misc/sleep.h"
#include "modes/normal.h"
#include "modes/binding.h"
#include "modes/firmware_update.h"
#include "modes/uart_validation.h"
#include "driver/driver_kill.h"
#include "security/security.h"
#include "server_core/server_core.h"
#include "server_core/epoll/epoll.h"
#if defined(HAVE_BACKTRACE)
#include "backtrace.h"
#endif
pthread_t main_thread = 0;
pthread_t driver_thread = 0;
pthread_t server_core_thread = 0;
pthread_t security_thread = 0;
static int main_crash_eventfd;
static int main_graceful_exit_eventfd;
static int main_graceful_exit_signalfd;
static int main_wait_crash_or_graceful_exit_epoll;
static int exit_status = EXIT_SUCCESS;
/* Global copy of argv to be able to restart the daemon with the same arguments. */
char **argv_g = 0;
int argc_g = 0;
#define BT_BUF_SIZE 100
__attribute__((noreturn)) void software_graceful_exit(void);
void main_wait_crash_or_graceful_exit(void);
#if defined(HAVE_BACKTRACE)
static void segv_handler(int sig)
{
(void) sig;
int nptrs;
void *buffer[BT_BUF_SIZE];
char **strings;
fprintf(stderr, "SEGFAULT :\n");
nptrs = backtrace(buffer, BT_BUF_SIZE);
fprintf(stderr, "backtrace() returned %d addresses\n", nptrs);
// print out all the frames to stderr
strings = backtrace_symbols(buffer, nptrs);
if (strings == NULL) {
perror("backtrace_symbols");
exit(EXIT_FAILURE);
}
for (int j = 0; j < nptrs; j++) {
fprintf(stderr, "%s\n", strings[j]);
}
exit(EXIT_FAILURE);
}
#endif
int main(int argc, char *argv[])
{
argc_g = argc;
argv_g = argv;
main_thread = pthread_self();
pthread_setname_np(main_thread, "cpcd");
#if defined(HAVE_BACKTRACE)
/* Setup signaling segfault */
{
struct sigaction sa = { 0 };
sa.sa_handler = segv_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, NULL);
}
#endif
/* Setup crash and gracefull exit signaling */
{
sigset_t mask;
int ret;
sigemptyset(&mask);
sigaddset(&mask, SIGINT);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGQUIT);
/* Block signals so that they aren't handled
according to their default dispositions. */
ret = sigprocmask(SIG_BLOCK, &mask, NULL);
FATAL_ON(ret == -1);
/* Create crash fd and signal fd */
{
main_crash_eventfd = eventfd(0, //Start with 0 value
EFD_CLOEXEC);
FATAL_ON(main_crash_eventfd == -1);
main_graceful_exit_eventfd = eventfd(0, //Start with 0 value
EFD_CLOEXEC);
FATAL_ON(main_graceful_exit_eventfd == -1);
main_graceful_exit_signalfd = signalfd(-1, &mask, SFD_CLOEXEC);
FATAL_ON(main_graceful_exit_signalfd == -1);
}
/* Setup epoll for those fds */
{
struct epoll_event event = { .events = EPOLLIN };
main_wait_crash_or_graceful_exit_epoll = epoll_create1(EPOLL_CLOEXEC);
FATAL_SYSCALL_ON(main_wait_crash_or_graceful_exit_epoll < 0);
ret = epoll_ctl(main_wait_crash_or_graceful_exit_epoll,
EPOLL_CTL_ADD,
main_crash_eventfd,
&event);
FATAL_SYSCALL_ON(ret < 0);
ret = epoll_ctl(main_wait_crash_or_graceful_exit_epoll,
EPOLL_CTL_ADD,
main_graceful_exit_eventfd,
&event);
FATAL_SYSCALL_ON(ret < 0);
ret = epoll_ctl(main_wait_crash_or_graceful_exit_epoll,
EPOLL_CTL_ADD,
main_graceful_exit_signalfd,
&event);
FATAL_SYSCALL_ON(ret < 0);
}
}
epoll_init();
logging_init();
PRINT_INFO("[CPCd v%s] [Library API v%d] [RCP Protocol v%d]", PROJECT_VER, LIBRARY_API_VERSION, PROTOCOL_VERSION);
PRINT_INFO("Git commit: %s / branch: %s", GIT_SHA1, GIT_REFSPEC);
PRINT_INFO("Sources hash: %s", SOURCES_HASH);
if (geteuid() == 0) {
WARN("Running CPCd as 'root' is not recommended. Proceed at your own risk.");
}
config_init(argc, argv);
#if !defined(ENABLE_ENCRYPTION)
PRINT_INFO("\033[31;1mENCRYPTION IS DISABLED \033[0m");
#else
if (config.use_encryption == false) {
PRINT_INFO("\033[31;1mENCRYPTION IS DISABLED \033[0m");
}
#endif
switch (config.operation_mode) {
case MODE_NORMAL:
PRINT_INFO("Starting daemon in normal mode");
run_normal_mode();
break;
case MODE_BINDING_PLAIN_TEXT:
#if defined(ENABLE_ENCRYPTION)
PRINT_INFO("Starting daemon in plain text binding mode");
run_binding_mode();
#else
FATAL("Tried to initiate binding mode with encryption disabled");
#endif
break;
case MODE_BINDING_ECDH:
#if defined(ENABLE_ENCRYPTION)
PRINT_INFO("Starting daemon in ECDH binding mode");
run_binding_mode();
#else
FATAL("Tried to initiate binding mode with encryption disabled");
#endif
break;
case MODE_BINDING_UNBIND:
#if defined(ENABLE_ENCRYPTION)
PRINT_INFO("Starting daemon in unbind mode");
run_binding_mode();
#else
FATAL("Tried to unbind with encryption disabled");
#endif
break;
case MODE_FIRMWARE_UPDATE:
PRINT_INFO("Starting daemon in firmware update mode");
run_firmware_update();
break;
case MODE_UART_VALIDATION:
PRINT_INFO("Starting daemon in UART validation mode");
run_uart_validation();
break;
default:
BUG();
break;
}
return 0;
}
/* Meant to be called by the main thread only */
__attribute__((noreturn)) static void exit_daemon(void)
{
driver_kill_signal();
pthread_join(driver_thread, NULL);
#if defined(ENABLE_ENCRYPTION)
if (config.use_encryption && security_thread != 0) {
security_kill_signal();
pthread_join(security_thread, NULL);
}
#endif
server_core_kill_signal();
pthread_join(server_core_thread, NULL);
PRINT_INFO("Daemon exiting with status %s", (exit_status == 0) ? "EXIT_SUCCESS" : "EXIT_FAILURE");
/* Block until all logging data is flushed */
logging_kill();
exit(exit_status);
}
void main_wait_crash_or_graceful_exit(void)
{
int event_count;
struct epoll_event events;
do {
event_count = epoll_wait(main_wait_crash_or_graceful_exit_epoll,
&events,
1, //only one event
-1); //no timeout
} while (errno == EINTR && event_count < 0); // Ignore SIGSTOP
FATAL_SYSCALL_ON(event_count <= 0);
exit_daemon();
}
__attribute__((noreturn)) void signal_crash(void)
{
const uint64_t event_value = 1; //doesn't matter what it is
exit_status = EXIT_FAILURE;
sleep_s(1); // Wait for logs to be flushed to the output
if (pthread_self() == main_thread) {
exit_daemon();
} else {
if (pthread_self() == security_thread) {
security_thread = 0;
#if defined(ENABLE_ENCRYPTION)
security_unblock_post_command();
#endif
}
write(main_crash_eventfd, &event_value, sizeof(event_value));
}
pthread_exit(NULL);
}
__attribute__((noreturn)) void software_graceful_exit(void)
{
const uint64_t event_value = 1; //doesn't matter what it is
exit_status = EXIT_SUCCESS;
sleep_s(1); // Wait for logs to be flushed to the output
if (pthread_self() == main_thread) {
exit_daemon();
} else {
if (pthread_self() == security_thread) {
security_thread = 0;
}
write(main_graceful_exit_eventfd, &event_value, sizeof(event_value));
}
pthread_exit(NULL);
}