lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [day] [month] [year] [list]
Message-ID: <CAFf+5zg91=Ei-L-XxroiTQKqVwpG+hVSU5ahOkEuWB0_A1WqbA@mail.gmail.com>
Date: Thu, 28 Aug 2025 13:52:27 +0530
From: Amit <amitchoudhary0523@...il.com>
To: linux-kernel@...r.kernel.org
Subject: Signal handler example (Three files: signal_handler_example.c,
 compile_signal_handler_example.sh, and ReadMe.txt).

Signal handler example (Three files: signal_handler_example.c,
compile_signal_handler_example.sh, and ReadMe.txt).

------------------------------------
signal_handler_example.c
------------------------------------

/*
 * License:
 *
 * This file has been released under "unlicense" license
 * (https://unlicense.org).
 *
 * This is free and unencumbered software released into the public domain.
 *
 * Anyone is free to copy, modify, publish, use, compile, sell, or distribute
 * this software, either in source code form or as a compiled binary, for any
 * purpose, commercial or non-commercial, and by any means.
 *
 * For more information about this license, please visit - https://unlicense.org
 */

/*
 * Author: Amit Choudhary
 * Email: amitchoudhary0523 AT gmail DOT com
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>

// Function prototype for gcc flag -Werror-implicit-function-declaration.
void signal_handler_function(int signum);

void signal_handler_function(int signum)
{

    static long count = 0;

    count = count + 1;

    printf("\n");
    printf("--------------------------------------------------------------\n");
    printf("%s(): \"\"SIGNAL HANDLER FUNCTION CALLED\"\".\n", __FUNCTION__);
    printf("This process has woken up because the signal \"SIGUSR1\" (signal"
           " number %d) was received.\n", signum);
    printf("Now, this process will again go back to sleep.\n");
    printf("This signal handler has been called \"%ld\" %s till now.\n",
           count, (count > 1) ? "times" : "time");
    printf("--------------------------------------------------------------\n");

    return;

} // end of function signal_handler_function()

int main(void)
{

    struct sigaction sa;
    pid_t mypid = -1;
    int retval = -1;

    memset(&sa, 0, sizeof(sa));

    sa.sa_handler = signal_handler_function;

    retval = sigaction(SIGUSR1, &sa, NULL);

    if (retval < 0) {
        printf("\n%s(): Error: sigaction() returned error. Signal handler was"
               " not installed. Exiting...\n", __FUNCTION__);
        exit(EXIT_FAILURE);
    }

    printf("\n%s(): Signal handler installed successfully.\n", __FUNCTION__);

    mypid = getpid();

    while(1) {
        printf("\n%s(): The pid of this process is %d.\n", __FUNCTION__, mypid);
        printf("\n%s(): This process is going to sleep. This process will wake"
               " up when the signal \"SIGUSR1\" (signal number 10) will be"
               " received. If any other signal is received then the results are"
               " undefined.\n", __FUNCTION__);
        pause();
    }

    return 0;

} // end of function main()

-------------------------------------------------
compile_signal_handler_example.sh
-------------------------------------------------

#!/bin/bash

set -x

rm -f signal_handler_example.out

gcc -Wall -Werror -Wextra -Wundef -Wunreachable-code -Winit-self
-Wparentheses -Wconversion -Wsign-conversion -Wsign-compare
-Werror-implicit-function-declaration -Wmissing-prototypes
-Wmissing-declarations -Wformat-security signal_handler_example.c -o
signal_handler_example.out

----------------
ReadMe.txt
----------------

File "signal_handler_example.c" gives an example of how to install a user
defined signal handler that will override the signal handler provided by the
system for that signal.

In this file, we install a signal handler for the signal "SIGUSR1" (signal
number 10).

This program, on startup, goes to sleep waiting for the signal "SIGUSR1". When
the signal "SIGUSR1" is sent to this program, this program wakes up and the
user's signal handler function is called. After the signal handler function
returns, this program again goes back to sleep waiting for the signal "SIGUSR1".
This goes on in an endless loop until the user terminates the program.

If any other signal is sent to this program then the results are undefined.

You can send the signal "SIGUSR1" to this program by running the following
command in a terminal shell (bash, etc.):

            kill -10 pid_of_this_program

So, if the pid of this program is 14753, then you can send "SIGUSR1" signal to
this program as follows:

            kill -10 14753

This program prints its pid on the output screen, so you can use that pid
directly, instead of grepping the output of "ps -aef".

---- End of ReadMe ----

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ