Improved main code

This commit is contained in:
Denis 2023-12-13 17:28:20 +01:00
parent 98e19476c5
commit 2808be0412
No known key found for this signature in database
GPG key ID: DD9B63F805CF5C03
2 changed files with 47 additions and 16 deletions

View file

@ -86,7 +86,7 @@ int main(int argc, char ** argv) {
} }
// Create a signalfd for the specified signals // Create a signalfd for the specified signals
int sfd = signalfd(-1, &mask, 0); const int sfd = signalfd(-1, &mask, 0);
if (sfd == -1) { if (sfd == -1) {
perror("signalfd"); perror("signalfd");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);

View file

@ -36,6 +36,25 @@ int main(int argc, char ** argv) {
init_config(&settings); init_config(&settings);
fill_config(&settings, configuration_file); fill_config(&settings, configuration_file);
// Create a signal set containing only SIGTERM
sigset_t mask;
sigemptyset(&mask);
sigaddset(&mask, SIGTERM);
sigaddset(&mask, SIGINT);
// Block SIGTERM for the current thread
if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1) {
perror("sigprocmask");
exit(EXIT_FAILURE);
}
// Create a signalfd for the specified signals
const int sfd = signalfd(-1, &mask, 0);
if (sfd == -1) {
perror("signalfd");
exit(EXIT_FAILURE);
}
// populate the output device thread data // populate the output device thread data
dev_out_data_t dev_out_thread_data = { dev_out_data_t dev_out_thread_data = {
.communication = { .communication = {
@ -62,15 +81,12 @@ int main(int argc, char ** argv) {
const uint64_t timeout_ms = 1500; const uint64_t timeout_ms = 1500;
struct pollfd poll_fds[2]; struct pollfd poll_fds[2];
poll_fds[0].fd = -1 /*signalfd(-1, )*/; poll_fds[0].fd = sfd;
poll_fds[0].events = POLL_IN; poll_fds[0].events = POLL_IN;
int sd=-1; int sd=-1;
struct sockaddr_un serveraddr; struct sockaddr_un serveraddr;
do { do {
poll_fds[0].revents = 0;
poll_fds[1].revents = 0;
sd = socket(AF_UNIX, SOCK_SEQPACKET, 0); sd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (sd < 0) if (sd < 0)
{ {
@ -80,7 +96,7 @@ int main(int argc, char ** argv) {
memset(&serveraddr, 0, sizeof(serveraddr)); memset(&serveraddr, 0, sizeof(serveraddr));
serveraddr.sun_family = AF_UNIX; serveraddr.sun_family = AF_UNIX;
strcpy(serveraddr.sun_path, SERVER_PATH); strncpy(serveraddr.sun_path, SERVER_PATH, sizeof(serveraddr.sun_path) - 1);
int rc = bind(sd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr)); int rc = bind(sd, (struct sockaddr *)&serveraddr, SUN_LEN(&serveraddr));
if (rc < 0) if (rc < 0)
@ -100,10 +116,31 @@ int main(int argc, char ** argv) {
poll_fds[1].events = POLL_IN; poll_fds[1].events = POLL_IN;
while (true) { while (true) {
poll_fds[0].revents = 0;
poll_fds[1].revents = 0;
const int poll_ret = poll(poll_fds, sizeof(poll_fds) / sizeof(poll_fds[0]), timeout_ms); const int poll_ret = poll(poll_fds, sizeof(poll_fds) / sizeof(poll_fds[0]), timeout_ms);
if (poll_fds[0].revents & POLLIN) { if (poll_fds[0].revents & POLLIN) {
// TODO: read for signals // Read signals from the signalfd
struct signalfd_siginfo si;
ssize_t s = read(sfd, &si, sizeof(struct signalfd_siginfo));
if (s != sizeof(struct signalfd_siginfo)) {
perror("Error reading signalfd\n");
exit(EXIT_FAILURE);
}
// Check the signal received
if (si.ssi_signo == SIGTERM) {
printf("Received SIGTERM -- propagating signal\n");
dev_out_thread_data.flags |= DEV_OUT_FLAG_EXIT;
goto main_exit;
} else if (si.ssi_signo == SIGINT) {
printf("Received SIGINT -- propagating signal\n");
dev_out_thread_data.flags |= DEV_OUT_FLAG_EXIT;
goto main_exit;
}
} else if (poll_fds[1].revents & POLLIN) { } else if (poll_fds[1].revents & POLLIN) {
const int client_fd = accept(sd, NULL, NULL); const int client_fd = accept(sd, NULL, NULL);
if (client_fd < 0) { if (client_fd < 0) {
@ -116,6 +153,7 @@ int main(int argc, char ** argv) {
bool found = false; bool found = false;
for (size_t i = 0; i < MAX_CONNECTED_CLIENTS; ++i) { for (size_t i = 0; i < MAX_CONNECTED_CLIENTS; ++i) {
if (dev_out_thread_data.communication.endpoint.ssocket.clients[i] < 0) { if (dev_out_thread_data.communication.endpoint.ssocket.clients[i] < 0) {
printf("Accepted new incoming connection on slot %zu: %d\n", i, client_fd);
dev_out_thread_data.communication.endpoint.ssocket.clients[i] = client_fd; dev_out_thread_data.communication.endpoint.ssocket.clients[i] = client_fd;
found = true; found = true;
break; break;
@ -139,18 +177,11 @@ int main(int argc, char ** argv) {
unlink(SERVER_PATH); unlink(SERVER_PATH);
/* main_exit:
// TODO: once the application is able to exit de-comment this
__sighandler_t sigint_hndl = signal(SIGINT, sig_handler);
if (sigint_hndl == SIG_ERR) {
fprintf(stderr, "Error registering SIGINT handler\n");
return EXIT_FAILURE;
}
*/
main_err: main_err:
if (dev_out_thread_creation == 0) { if (dev_out_thread_creation == 0) {
pthread_join(dev_out_thread, NULL); pthread_join(dev_out_thread, NULL);
printf("dev_out_thread terminated\n");
} }
return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE; return ret == 0 ? EXIT_SUCCESS : EXIT_FAILURE;