Skip to content

Commit 2d0a9f0

Browse files
isak-jakobssonjohan-hultberg-work
authored andcommitted
Use g_unix_signal_add for ACAPs with GMainLoop
1 parent 333795d commit 2d0a9f0

File tree

4 files changed

+45
-124
lines changed

4 files changed

+45
-124
lines changed

axoverlay/app/axoverlay.c

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
#include <axoverlay.h>
3232
#include <cairo/cairo.h>
3333
#include <errno.h>
34+
#include <glib-unix.h>
3435
#include <glib.h>
3536
#include <syslog.h>
3637

3738
#define PALETTE_VALUE_RANGE 255.0
3839

39-
static GMainLoop* main_loop = NULL;
4040
static gint animation_timer = -1;
4141
static gint overlay_id = -1;
4242
static gint overlay_id_text = -1;
@@ -327,47 +327,12 @@ static gboolean update_overlay_cb(gpointer user_data) {
327327
/**
328328
* brief Handles the signals.
329329
*
330-
* This function handles the signals when it is time to
331-
* quit the main loop.
332-
*
333-
* param signal_num Signal number.
334-
*/
335-
static void signal_handler(gint signal_num) {
336-
switch (signal_num) {
337-
case SIGTERM:
338-
case SIGABRT:
339-
case SIGINT:
340-
g_main_loop_quit(main_loop);
341-
break;
342-
default:
343-
break;
344-
}
345-
}
346-
347-
/**
348-
* brief Initialize the signal handler.
349-
*
350-
* This function handles the initialization of signals.
351-
*
352-
* return result as boolean.
330+
* param loop Loop to quit
353331
*/
354-
static gboolean signal_handler_init(void) {
355-
struct sigaction sa = {0};
356-
357-
if (sigemptyset(&sa.sa_mask) == -1) {
358-
syslog(LOG_ERR, "Failed to initialize signal handler: %s", strerror(errno));
359-
return FALSE;
360-
}
361-
362-
sa.sa_handler = signal_handler;
363-
364-
if ((sigaction(SIGTERM, &sa, NULL) < 0) || (sigaction(SIGABRT, &sa, NULL) < 0) ||
365-
(sigaction(SIGINT, &sa, NULL) < 0)) {
366-
syslog(LOG_ERR, "Failed to install signal handler: %s", strerror(errno));
367-
return FALSE;
368-
}
369-
370-
return TRUE;
332+
static gboolean signal_handler(gpointer loop) {
333+
g_main_loop_quit((GMainLoop*)loop);
334+
syslog(LOG_INFO, "Application was stopped by SIGTERM or SIGINT.");
335+
return G_SOURCE_REMOVE;
371336
}
372337

373338
/***** Main function *********************************************************/
@@ -386,20 +351,18 @@ int main(int argc, char** argv) {
386351
(void)argc;
387352
(void)argv;
388353

354+
GMainLoop* loop = NULL;
389355
GError* error = NULL;
390356
GError* error_text = NULL;
391357
gint camera_height = 0;
392358
gint camera_width = 0;
393359

394360
openlog(NULL, LOG_PID, LOG_USER);
395361

396-
if (!signal_handler_init()) {
397-
syslog(LOG_ERR, "Could not set up signal handler");
398-
return 1;
399-
}
400-
401362
// Create a glib main loop
402-
main_loop = g_main_loop_new(NULL, FALSE);
363+
loop = g_main_loop_new(NULL, FALSE);
364+
g_unix_signal_add(SIGTERM, signal_handler, loop);
365+
g_unix_signal_add(SIGINT, signal_handler, loop);
403366

404367
if (!axoverlay_is_backend_supported(AXOVERLAY_CAIRO_IMAGE_BACKEND)) {
405368
syslog(LOG_ERR, "AXOVERLAY_CAIRO_IMAGE_BACKEND is not supported");
@@ -486,7 +449,7 @@ int main(int argc, char** argv) {
486449
animation_timer = g_timeout_add_seconds(1, update_overlay_cb, NULL);
487450

488451
// Enter main loop
489-
g_main_loop_run(main_loop);
452+
g_main_loop_run(loop);
490453

491454
// Destroy the overlay
492455
axoverlay_destroy_overlay(overlay_id, &error);
@@ -509,7 +472,7 @@ int main(int argc, char** argv) {
509472
g_source_remove(animation_timer);
510473

511474
// Release main loop
512-
g_main_loop_unref(main_loop);
475+
g_main_loop_unref(loop);
513476

514477
return 0;
515478
}

axparameter/app/axparameter.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
* limitations they impose.
2323
*/
2424
#include <axsdk/axparameter.h>
25+
#include <glib-unix.h>
2526
#include <stdbool.h>
2627
#include <syslog.h>
2728

@@ -34,19 +35,10 @@ struct message {
3435
char* value;
3536
};
3637

37-
static GMainLoop* loop = NULL; // Must be global, so quit_main_loop() can use it
38-
39-
static void quit_main_loop(__attribute__((unused)) int signal_num) {
40-
g_main_loop_quit(loop);
41-
}
42-
43-
static void set_sigterm_and_sigint_handler(void (*handler)(int)) {
44-
struct sigaction sa = {0};
45-
46-
sigemptyset(&sa.sa_mask);
47-
sa.sa_handler = handler;
48-
sigaction(SIGTERM, &sa, NULL);
49-
sigaction(SIGINT, &sa, NULL);
38+
static gboolean signal_handler(gpointer loop) {
39+
g_main_loop_quit((GMainLoop*)loop);
40+
syslog(LOG_INFO, "Application was stopped by SIGTERM or SIGINT.");
41+
return G_SOURCE_REMOVE;
5042
}
5143

5244
// Print an error to syslog and exit the application if a fatal error occurs.
@@ -182,7 +174,8 @@ static void parameter_changed(const gchar* name, const gchar* value, gpointer ha
182174
}
183175

184176
int main(void) {
185-
GError* error = NULL;
177+
GError* error = NULL;
178+
GMainLoop* loop = NULL;
186179

187180
openlog(APP_NAME, LOG_PID, LOG_USER);
188181

@@ -209,9 +202,9 @@ int main(void) {
209202

210203
// Start listening to callbacks by launching a GLib main loop.
211204
loop = g_main_loop_new(NULL, FALSE);
212-
set_sigterm_and_sigint_handler(quit_main_loop);
205+
g_unix_signal_add(SIGTERM, signal_handler, loop);
206+
g_unix_signal_add(SIGINT, signal_handler, loop);
213207
g_main_loop_run(loop);
214-
syslog(LOG_INFO, "Application was stopped by SIGTERM or SIGINT.");
215208

216209
g_main_loop_unref(loop);
217210
ax_parameter_free(handle);

axserialport/app/axserialport.c

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
#include <glib-unix.h>
1718
#include <glib.h>
1819
#include <stdlib.h>
1920
#include <syslog.h>
@@ -34,36 +35,15 @@ typedef struct MyConfigAndData {
3435
gpointer data;
3536
} MyConfigAndData;
3637

37-
static GMainLoop* loop = NULL;
38-
39-
/**
40-
* @brief Signals handling
41-
*
42-
* @param signal_num Signal number
43-
*/
44-
static void handle_sigterm(int signo) {
45-
(void)signo;
46-
if (loop != NULL) {
47-
g_main_loop_quit(loop);
48-
} else {
49-
exit(EXIT_FAILURE);
50-
}
51-
}
52-
5338
/**
54-
* @brief Initialize signals
39+
* @brief Handles the signals.
5540
*
56-
* @param signo Signal id (not used)
41+
* @param loop Loop to quit
5742
*/
58-
static void init_signals(void) {
59-
struct sigaction sa;
60-
61-
sa.sa_flags = 0;
62-
63-
sigemptyset(&sa.sa_mask);
64-
sa.sa_handler = handle_sigterm;
65-
sigaction(SIGTERM, &sa, NULL);
66-
sigaction(SIGINT, &sa, NULL);
43+
static gboolean signal_handler(gpointer loop) {
44+
g_main_loop_quit((GMainLoop*)loop);
45+
syslog(LOG_INFO, "Application was stopped by SIGTERM or SIGINT.");
46+
return G_SOURCE_REMOVE;
6747
}
6848

6949
/**
@@ -187,12 +167,14 @@ int main(void) {
187167
guint port0 = 0;
188168
AXSerialConfig* config = NULL;
189169
GIOChannel* iochannel = NULL;
170+
GMainLoop* loop = NULL;
190171
GError* error = NULL;
191172
MyConfigAndData conf_data;
192173

193174
/* Initialization */
194-
init_signals();
195175
loop = g_main_loop_new(NULL, FALSE);
176+
g_unix_signal_add(SIGTERM, signal_handler, loop);
177+
g_unix_signal_add(SIGINT, signal_handler, loop);
196178

197179
/* Print some startup messages */
198180
openlog(APP_NAME, LOG_PID, LOG_LOCAL4);

axstorage/app/axstorage.c

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
#include <errno.h>
18+
#include <glib-unix.h>
1819
#include <glib.h>
1920
#include <glib/gstdio.h>
2021
#include <signal.h>
@@ -45,36 +46,16 @@ typedef struct {
4546
} disk_item_t;
4647

4748
static GList* disks_list = NULL;
48-
static GMainLoop* loop = NULL;
4949

5050
/**
51-
* @brief Signals handling
51+
* @brief Handles the signals.
5252
*
53-
* @param signal_num Signal number
53+
* @param loop Loop to quit
5454
*/
55-
static void handle_sigterm(int signo) {
56-
(void)signo;
57-
if (loop != NULL) {
58-
g_main_loop_quit(loop);
59-
} else {
60-
exit(EXIT_FAILURE);
61-
}
62-
}
63-
64-
/**
65-
* @brief Initialize signals
66-
*
67-
* @param signo Signal id (not used)
68-
*/
69-
static void init_signals(void) {
70-
struct sigaction sa;
71-
72-
sa.sa_flags = 0;
73-
74-
sigemptyset(&sa.sa_mask);
75-
sa.sa_handler = handle_sigterm;
76-
sigaction(SIGTERM, &sa, NULL);
77-
sigaction(SIGINT, &sa, NULL);
55+
static gboolean signal_handler(gpointer loop) {
56+
g_main_loop_quit((GMainLoop*)loop);
57+
syslog(LOG_INFO, "Application was stopped by SIGTERM or SIGINT.");
58+
return G_SOURCE_REMOVE;
7859
}
7960

8061
/**
@@ -393,12 +374,12 @@ static disk_item_t* new_disk_item_t(gchar* storage_id) {
393374
* @return Result
394375
*/
395376
gint main(void) {
396-
GList* disks = NULL;
397-
GList* node = NULL;
398-
GError* error = NULL;
399-
gint ret = EXIT_SUCCESS;
377+
GList* disks = NULL;
378+
GList* node = NULL;
379+
GError* error = NULL;
380+
GMainLoop* loop = NULL;
381+
gint ret = EXIT_SUCCESS;
400382

401-
init_signals();
402383
openlog(APP_NAME, LOG_PID, LOG_USER);
403384
syslog(LOG_INFO, "Start AXStorage application");
404385

@@ -412,6 +393,8 @@ gint main(void) {
412393
}
413394

414395
loop = g_main_loop_new(NULL, FALSE);
396+
g_unix_signal_add(SIGTERM, signal_handler, loop);
397+
g_unix_signal_add(SIGINT, signal_handler, loop);
415398

416399
/* Loop through the retrieved disks and subscribe to their events. */
417400
for (node = g_list_first(disks); node != NULL; node = g_list_next(node)) {

0 commit comments

Comments
 (0)