[pisa-src] r1694 - trunk/community-operator/co_server.c

  • From: Mircea Gherzan <mircea.gherzan@xxxxxxxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Mon, 16 Nov 2009 19:32:21 +0100

Author: gherzan
Date: Mon Nov 16 19:32:20 2009
New Revision: 1694

Log:
co: upgrade signal handling in the server

Switch from signal(2) to sigaction(2) in setting up the signal
handlers. Once again, the latter has the advantage of providing more 
detail about (the context of) the incoming signal.

Modified:
   trunk/community-operator/co_server.c

Modified: trunk/community-operator/co_server.c
==============================================================================
--- trunk/community-operator/co_server.c        Mon Nov 16 16:23:15 2009        
(r1693)
+++ trunk/community-operator/co_server.c        Mon Nov 16 19:32:20 2009        
(r1694)
@@ -3,12 +3,15 @@
  * All rights reserved.
  */
 
+/* we need sa_sigaction... */
+#define _POSIX_C_SOURCE 200112
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <signal.h>
 #include <sys/socket.h>
 #include <sys/select.h>
-#include <signal.h>
 
 #include "config.h"
 #include "libpisa/global.h"
@@ -28,7 +31,7 @@
 
 static int create_server_socket(void);
 static int answer_certificate_query(struct sockaddr_in6 *cl_addr);
-static void co_quit(int quitcode);
+static void co_quit(int signum, siginfo_t *info, void *ctx);
 static char *get_certificate_for_hit(struct in6_addr *hit, int *reason);
 static int handle_packet(struct sockaddr_in6 *socket_addr,
                co_packet *co_packet);
@@ -70,11 +73,10 @@
 
 /**
  * Terminate community-operator server by receiving signal
- * @param signal signal quit code
  */
-static void co_quit(int quitcode)
+static void co_quit(int signum, UNUSED siginfo_t *info, UNUSED void *ctx)
 {
-       switch (quitcode) {
+       switch (signum) {
        case SIGTERM:
        case SIGINT:
        case SIGQUIT:
@@ -186,27 +188,36 @@
 
 int main(void)
 {
-       signal(SIGTERM, co_quit);
-       signal(SIGINT, co_quit);
-       signal(SIGQUIT, co_quit);
-       signal(SIGILL, co_quit);
-       signal(SIGPIPE, SIG_IGN);
-       signal(SIGBUS, co_quit);
-
        int received;
        socklen_t addrlen;
        char *addr_string;
        co_packet *packet;
        struct sockaddr_in6 cl_addr;
+       struct sigaction sigact;
+
+       sigemptyset(&sigact.sa_mask);
+       sigact.sa_flags = SA_SIGINFO;
+       
+       sigact.sa_sigaction = co_quit;  
+       sigaction(SIGTERM, &sigact, NULL);
+       sigaction(SIGINT, &sigact, NULL);
+       sigaction(SIGQUIT, &sigact, NULL);
+       sigaction(SIGILL, &sigact, NULL);
+       sigaction(SIGBUS, &sigact, NULL);
+       
+       sigact.sa_flags &= ~SA_SIGINFO;
+       sigact.sa_handler = SIG_IGN;
+       sigaction(SIGPIPE, &sigact, NULL);
+
 
        if (getuid() != 0) {
                PISA_ERROR("You're not root. HIPD won't sign certificates.\n");
-               return -1;
+               return EXIT_FAILURE;
        }
 
        running = 1;
 
-       packet = malloc(sizeof(co_packet));
+       packet = malloc(sizeof(*packet));
        addr_string = malloc(INET6_ADDRSTRLEN);
 
        pisa_cfg_authorized_hosts_setup_file("co_server.cfg");
@@ -215,7 +226,7 @@
 
        sock = create_server_socket();
        if (get_default_hit(&issuer_hit) != 0)
-               return 1;
+               return EXIT_FAILURE;
 
        inet_ntop(AF_INET6, &issuer_hit, addr_string, INET6_ADDRSTRLEN);
        PISA_INFO("community operator server ready, HIT: %s\n", addr_string);
@@ -271,7 +282,7 @@
        free(addr_string);
        close(sock);
 
-       return 0;
+       return EXIT_SUCCESS;
 }
 
 /**

Other related posts:

  • » [pisa-src] r1694 - trunk/community-operator/co_server.c - Mircea Gherzan