~donmcc/ood

1182412fd4ece0cb55c4b3ebefee78b22c8c15bd — Don McCaughey 1 year, 1 month ago 8ddb93d
Add `ood_halt()` function.

Unify points where we print an error and exit before it gets messy by
introducting the `ood_halt()` and `ood_halt_on_error()` functions.  The
former prints a caller-provided message and halts; the latter prints the
system error message for the current value of `errno` and exits, using
`errno` as the process exit status value.
M src/dood/main.c => src/dood/main.c +4 -8
@@ 121,8 121,7 @@ listener_on_read(struct ood_inet_socket *listener)
    while (server_can_accept_connections(server)) {
        struct ood_inet_socket *connection = NULL;
        if (!ood_inet_socket_accept(listener, &connection)) {
            perror("dood");
            exit(EXIT_FAILURE);
            ood_halt_on_error();
        }
        if (!connection) return;



@@ 149,13 148,11 @@ main(int argc, char *argv[]) {
    
    struct server *server = server_alloc(8);
    if (!server) {
        perror("dood");
        exit(EXIT_FAILURE);
        ood_halt_on_error();
    }

    if (!server_add_listener(server, INADDR_LOOPBACK, port, backlog, listener_on_read, listener_on_error)) {
        perror("dood");
        exit(EXIT_FAILURE);
        ood_halt_on_error();
    }

    printf("dood: ready\n");


@@ 166,8 163,7 @@ main(int argc, char *argv[]) {

        bool interrupted;
        if (!ood_select_sets_select(&select_sets, timeout_millis, &interrupted)) {
            perror("dood");
            exit(EXIT_FAILURE);
            ood_halt_on_error();
        }

        if (!interrupted) {

M src/dood/server.c => src/dood/server.c +2 -4
@@ 7,8 7,7 @@
void
server_add_socket(struct server *server, struct ood_inet_socket *socket) {
    if (!ood_ptr_array_add(server->sockets, socket)) {
        fprintf(stderr, "dood: unable to add socket\n");
        exit(EXIT_FAILURE);
        ood_halt("unable to add socket");
    }
}



@@ 97,8 96,7 @@ server_close_all_listeners(struct server *server)
void
server_close_socket(struct server *server, struct ood_inet_socket *socket) {
    if (!ood_ptr_array_clear(server->sockets, socket)) {
        fprintf(stderr, "dood: unable to close socket\n");
        exit(EXIT_FAILURE);
        ood_halt("unable to close socket");
    }
    ood_inet_socket_free(socket);
}

M src/libood/CMakeLists.txt => src/libood/CMakeLists.txt +2 -0
@@ 2,6 2,8 @@ add_library(libood STATIC
        ood.h
        ood.c

        ood/halt.c
        ood/halt.h
        ood/ptr_array.c
        ood/ptr_array.h
        ood/result.h

M src/libood/ood.h => src/libood/ood.h +1 -0
@@ 2,6 2,7 @@
#define OOD_H_INCLUDED


#include "ood/halt.h"
#include "ood/ptr_array.h"
#include "ood/result.h"
#include "ood/select_sets.h"

A src/libood/ood/halt.c => src/libood/ood/halt.c +37 -0
@@ 0,0 1,37 @@
#include "halt.h"

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>


static void
halt(char const *message, int status)
{
    fprintf(stderr, "ERROR: %s\n", message);
    fflush(stderr);
    exit(status);
}


void
ood_halt(char const *message)
{
    if (message && message[0]) {
        halt(message, EXIT_FAILURE);
    } else {
        halt("No message", EXIT_FAILURE);
    }
}


void
ood_halt_on_error(void)
{
    if (errno) {
        halt(strerror(errno), errno);
    } else {
        halt("No error number", EXIT_FAILURE);
    }
}

A src/libood/ood/halt.h => src/libood/ood/halt.h +12 -0
@@ 0,0 1,12 @@
#ifndef OOD_HALT_H_INCLUDED
#define OOD_HALT_H_INCLUDED


void
ood_halt(char const *message);

void
ood_halt_on_error(void);


#endif