Search code examples
clinuxsignalshttpserver

Signal Handler Not working On my C http Server


Problem: I want my single handler to work as intended and print out "EXITED NICELY" when I press ctrl C. This is a assignment and we must use signal handlers. As you can see I also experimented with sigaction but to the same results.

Current Behaviour: "work" is printing out suggesting the signal handler is working, however it must be getting stuck somewhere because it doesn't cancel the program. Although if I press ctrl c and then send a http request to the server such as curl http:/localhost:port/file.name it will then exit gracefully and print out my desired message. However I would like it to do that without me having to send a request.

Edit through further research. I put a print after and before my accept call. print before will print once and then the accept will just hold it until it receives a connection. So that where the problem is but how do we fix that?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<signal.h>
#include<fcntl.h>
#include "http_common.h"
#define CONNMAX 1000
#define BYTES 1024

char *ROOT;
int verbose;
int signalReceived = 1;
int listenfd, clients[CONNMAX];
void error(char *);

static void clean(int arg)
{
        if(arg == SIGINT) {
                printf("work\n");
                signalReceived = 0;
                //signal(SIGINT, clean);
        }
        else if(arg == SIGHUP) {
                signalReceived = 0;
        }
}

int main(int argc, char *argv[])
{
        //struct sigaction act;
        //memset (&act, '\0', sizeof(act));
        //act.sa_handler = clean;
        //sigemptyset(&act.sa_mask);
        //act.sa_flags = SA_RESTART;
        signal(SIGINT, clean);
        //signal(SIGHUP, clean);
        //if (sigaction(SIGINT, &act, NULL) == -1)
        //      printf("doing something\n");
        struct sockaddr_in clientaddr;
        socklen_t addrlen;
        char c;
        char PORT[6];
        ROOT = getenv("PWD");
        strcpy(PORT, "8888");
        int slot;
        while ((c = getopt (argc, argv, "p:v")) != -1)

                switch (c) {
                case'v':
                        verbose = 1;
                        break;
                case'p':
                        strcpy(PORT, optarg);
                        break;
                case'?':
                        fprintf(stderr, "Wrong arguments given\n");
                        exit(1);
                default:
                        exit(1);
                }
        printf("Listening on port %s%s%s, root is %s%s%s\n", "\033[92m", PORT, "\033[0m", "\033[92m", ROOT, "\033[0m");
        int i = 0;
        for (i = 0; i < CONNMAX; i++)
                clients[i] = -1;
        startServer(PORT, &listenfd);
        while (signalReceived == 1) {
                addrlen = sizeof(clientaddr);
                clients[slot] = accept (listenfd, (struct sockaddr *) &clientaddr, &addrlen);
                if (clients[slot] < 0)
                        exit(0);
                else {
                        if (fork() == 0) {
                                respond(slot, verbose, ROOT, clients);
                             exit(0);
                        }
                }
                while (clients[slot] != -1)
                        slot = (slot + 1) % CONNMAX;

        }
        printf("EXITED NICLEY\n");//ients[slot] = accept (listenfd, (struct sockaddr *) &clientaddr, &addrlen);
        return 0;
}

Solution

  • You have to register the signal with sigaction() and without the SA_RESTART flag.

    When you register a signal handler with signal(), it will set the SA_RESTART flag. See the glibc manual:

    In the GNU C Library, establishing a handler with signal sets all the flags to zero except for SA_RESTART, whose value depends on the settings you have made with siginterrupt. See Interrupted Primitives, to see what this is about.

    When SA_RESTART is set, the signal will not interrupt (most) system calls, but it will instead restart them. See the signal man page:

    If a signal handler is invoked while a system call or library function call is blocked, then either:

    • the call is automatically restarted after the signal handler returns; or
    • the call fails with the error EINTR.

    Which of these two behaviors occurs depends on the interface and whether or not the signal handler was established using the SA_RESTART flag (see sigaction(2)).