/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Copyright © 2023 Keith Packard
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#define _GNU_SOURCE
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>

#define HANDLER_NOT_CALLED  0
#define HANDLER_SUCCESS     1
#define HANDLER_FAILED      2
#define HANDLER_NOT_BLOCKED 3

#define HANDLER_RECURSE     4

static volatile sig_atomic_t handler_result;
static volatile sig_atomic_t handler_sig_expect;
static volatile sig_atomic_t handler_sig_received;
static volatile sig_atomic_t handler_sig_count;

static void
signal_handler(int sig)
{
    sig_atomic_t entry_sig_count;

    printf("            ******* caught %d *******\n", sig);
    handler_sig_received = sig;
    if (sig != (int)handler_sig_expect) {
        handler_result = HANDLER_FAILED;
        return;
    }
    handler_result = HANDLER_SUCCESS;

    ++handler_sig_count;
    entry_sig_count = handler_sig_count;
    if (handler_sig_count < HANDLER_RECURSE)
        raise(sig);
    printf("                    recurse %d %d *******\n", sig, handler_sig_count);
    if (handler_sig_count != entry_sig_count)
        handler_result = HANDLER_NOT_BLOCKED;
}

static const int test_signals[] = {
    SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM,
};

#define MODE_IGN   0
#define MODE_CATCH 1
#define MODE_MAX   2

#define NTEST_SIG  (sizeof(test_signals) / sizeof(test_signals[0]))

int
main(void)
{
    void     (*old_func)(int);
    void     (*new_func)(int);
    void     (*prev_func)(int);
    int      ret;
    unsigned u;
    int      fail = 0;
    int      mode;

    setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
    for (u = 0; u < NTEST_SIG; u++) {
        int sig = test_signals[u];
        prev_func = SIG_DFL;
        for (mode = 0; mode < MODE_MAX; mode++) {
            switch (mode) {
            case MODE_IGN:
                new_func = SIG_IGN;
                break;
            case MODE_CATCH:
                new_func = signal_handler;
                break;
            }

            /* Set up the signal handler */
            printf("call signal(%d, %p)\n", sig, new_func);
            old_func = signal(sig, new_func);
            printf("    got %p\n", old_func);
            if (old_func != SIG_DFL) {
                printf("signal %d: old_func was %p, not SIG_DFL\n", sig, old_func);
                if (old_func == (void (*)(int))-1 && errno == EPERM) {
                    printf("Assuming github seccomp filter is active, ignoring\n");
                    continue;
                }
                fail = 1;
            }

            /* Invoke the handler */
            prev_func = new_func;
            handler_result = HANDLER_NOT_CALLED;
            handler_sig_expect = (sig_atomic_t)sig;
            handler_sig_count = 0;

            printf("    call raise(%d)\n", sig);
            ret = raise(sig);
            printf("        got %d (errno %d)\n", ret, errno);
            if (ret != 0) {
                printf("signal %d: raise returned %d\n", sig, ret);
                fail = 1;
            }

            /* Validate the results */
            switch (mode) {
            case MODE_IGN:
                if (handler_result != HANDLER_NOT_CALLED) {
                    printf("signal %d: handler called with SIG_IGN\n", sig);
                    fail = 1;
                }
                break;
            case MODE_CATCH:
                switch (handler_result) {
                case HANDLER_NOT_CALLED:
                    printf("signal %d: handler not called\n", sig);
                    fail = 1;
                    break;
                case HANDLER_SUCCESS:
                    if (handler_sig_count != HANDLER_RECURSE) {
                        printf("signal %d: handler didn't recurse %d times (only %d)\n", sig,
                               HANDLER_RECURSE, (int)handler_sig_count);
                        fail = 1;
                    }
                    break;
                case HANDLER_FAILED:
                    printf("signal %d: handler failed, received signal %d\n", sig,
                           (int)handler_sig_received);
                    fail = 1;
                    break;
                case HANDLER_NOT_BLOCKED:
                    printf("signal %d: signal not blocked in handler\n", sig);
                    fail = 1;
                    break;
                }
                break;
            default:
                fail = 1;
                break;
            }

            printf("    call signal(%d, SIG_DFL)\n", sig);
            old_func = signal(sig, SIG_DFL);
            printf("        got %p\n", old_func);
            if (old_func != prev_func) {
                printf("signal %d: after test, signal is %p expected %p\n", sig, old_func,
                       prev_func);
                fail = 1;
            }
        }
    }
    exit(fail);
}
