-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.c
164 lines (148 loc) · 4.05 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
/*
* JMPXRDS, an FM MPX signal generator with RDS support on
* top of Jack Audio Connection Kit - Main loop
*
* Copyright (C) 2015 Nick Kossifidis <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fmmod.h"
#include "utils.h"
#include "config.h"
#include <stdlib.h> /* For NULL */
#include <unistd.h> /* For sleep() */
#include <stdio.h> /* For printf */
#include <sched.h> /* For sched_setscheduler etc */
#include <signal.h> /* For signal handling / sig_atomic_t */
#include <string.h> /* For memset() */
#ifdef SYSTEMD_NOTIFY
#include <systemd/sd-daemon.h> /* For sd_notify() */
#endif
#ifdef DEBUG
#include <execinfo.h> /* For backtrace() etc */
#include <ucontext.h>
extern void __gcov_dump();
#endif
static volatile sig_atomic_t active;
static void
signal_handler(int sig, siginfo_t * info,
__attribute__((unused)) void *context)
{
#ifdef DEBUG
void *bt[16] = {0};
int bt_size = 0;
char **messages = NULL;
int i = 0;
#endif
switch (sig) {
case SIGPIPE:
return;
case SIGUSR1:
rtp_server_add_receiver(info->si_value.sival_int);
break;
case SIGUSR2:
rtp_server_remove_receiver(info->si_value.sival_int);
break;
case SIGABRT:
utils_err("[MAIN] Got abort at %p \n",
info->si_addr);
#ifdef DEBUG
if(info->si_addr) {
bt_size = backtrace(bt, 16);
messages = backtrace_symbols(bt, bt_size);
utils_trace("Backtrace:\n");
for(i= 1; i < bt_size; ++i)
utils_trace("\t%s\n", messages[i]);
}
#endif
utils_shm_unlink_all();
raise(SIGKILL);
break;
case SIGSEGV:
utils_err("[MAIN] Got segfault at %p \n",
info->si_addr);
#ifdef DEBUG
if(info->si_addr) {
bt_size = backtrace(bt, 16);
messages = backtrace_symbols(bt, bt_size);
utils_trace("Backtrace:\n");
for(i= 1; i < bt_size; ++i)
utils_trace("\t%s\n", messages[i]);
}
#endif
utils_shm_unlink_all();
raise(SIGKILL);
break;
case SIGQUIT:
#ifdef DEBUG
__gcov_dump();
#endif
default:
/* We haven't finished fmmod_initialize yet, not much
* we can do for graceful exit, just remove shm mappings
* and kill the process */
if (!active) {
utils_shm_unlink_all();
raise(SIGKILL);
}
/* Set active back to 0 and let running threads terminate */
active = 0;
break;
}
return;
}
int
main()
{
int ret = 0;
struct sched_param sched;
struct fmmod_instance fmmod_instance;
struct sigaction sa;
memset(&sched, 0, sizeof(struct sched_param));
memset(&sa, 0, sizeof(struct sigaction));
sched_getparam(0, &sched);
sched.sched_priority = 99;
if (sched_setscheduler(0, SCHED_FIFO, &sched) != 0)
utils_perr("[MAIN] Unable to set real time scheduling:");
ret = fmmod_initialize(&fmmod_instance);
if (ret < 0)
exit(ret);
active = 1;
/* Install a signal handler for graceful exit
* and for handling SIGPIPE */
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = signal_handler;
sigaction(SIGQUIT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGUSR2, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGABRT, &sa, NULL);
utils_ann("JMPXRDS Started\n");
#ifdef SYSTEMD_NOTIFY
/* Let systemd know the service has started */
sd_notify(0, "READY=1");
#endif
/* Keep running until the transport stops
* or in case we are interrupted */
while (active && fmmod_instance.active)
sleep(1);
if (fmmod_instance.active)
fmmod_destroy(&fmmod_instance, 0);
exit(0);
}