Signal handlers to deploy as an init.d service. Makefile install routine
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sendmysql.h"
|
||||
#include "calculate.h"
|
||||
@@ -25,9 +26,13 @@ void exitFunction(void) {
|
||||
printf("Exiting HumidityServer.\n");
|
||||
ulfius_stop_framework(&instance);
|
||||
ulfius_clean_instance(&instance);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
void sig_handler(int signo) {
|
||||
if (signo == SIGINT || signo == SIGTERM || signo == SIGKILL) {
|
||||
exit(0); //executes the exitFunction
|
||||
}
|
||||
}
|
||||
|
||||
int callback_welcome (const struct _u_request * request, struct _u_response * response, void * user_data) {
|
||||
ulfius_set_string_body_response(response, 200, "Welcome to the HumidityPi rest service!");
|
||||
@@ -114,6 +119,17 @@ int callback_absTempHum(const struct _u_request *request, struct _u_response *re
|
||||
int main(void) {
|
||||
|
||||
atexit(exitFunction);
|
||||
|
||||
if (signal(SIGINT, sig_handler) == SIG_ERR) {
|
||||
printf("\ncan't catch SIGINT\n");
|
||||
}
|
||||
if (signal(SIGTERM, sig_handler) == SIG_ERR) {
|
||||
printf("\ncan't catch SIGTERM\n");
|
||||
}
|
||||
if (signal(SIGKILL, sig_handler) == SIG_ERR) {
|
||||
printf("\ncan't catch SIGTERM\n");
|
||||
}
|
||||
|
||||
// Initialize instance with the port number
|
||||
if (ulfius_init_instance(&instance, PORT, NULL, NULL) != U_OK) {
|
||||
fprintf(stderr, "Error ulfius_init_instance, abort\n");
|
||||
@@ -128,8 +144,8 @@ int main(void) {
|
||||
if (ulfius_start_framework(&instance) == U_OK) {
|
||||
printf("Start framework on port %d\n", instance.port);
|
||||
|
||||
// Wait for the user to press <enter> on the console to quit the application
|
||||
getchar();
|
||||
// Wait for a signal to stop the program.
|
||||
pause();
|
||||
} else {
|
||||
fprintf(stderr, "Error starting framework\n");
|
||||
}
|
||||
|
Reference in New Issue
Block a user