Find the answer to your Linux question:
Results 1 to 2 of 2
I wrote the test code for blocking the shutdown for some time using the Signal handler. But unable to catch the SIGTERM signal if I do shutdown, as man pages ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    5

    Call function before shutdown the system

    I wrote the test code for blocking the shutdown for some time using the Signal handler.
    But unable to catch the SIGTERM signal if I do shutdown, as man pages says shutdown genrates the SIGTERM and SIGKILL signals, but we cant handle the SIGKILL signal.

    My code is working fine if I genrate the SIGTERM signal by Kill command, and also for
    SIGINT signgal genrated from the CTRL+c key.

    Thanks in advance for your ideas:

    Here is my code:

    /* Example of using sigaction() to setup a signal handler with 3 arguments
    * including siginfo_t.
    */
    #include <stdio.h>
    #include <unistd.h>
    #include <signal.h>
    #include <string.h>

    static void signal_hdl (int sig, siginfo_t *siginfo, void *context);
    static void process_signal (void);

    static void signal_hdl (int sig, siginfo_t *siginfo, void *context)
    {
    printf("Entering in singnal handler Caught the %d Signal Please wait\n",sig);
    process_signal();
    printf("Exiting from the signal handler\n");
    }

    static void process_signal (void)
    {
    int i, j, k;
    /*Stay here for some time Do something*/
    for (i = 0; i < 10000; i++)
    {
    for (j = 0; j < 100000; j++);
    {
    for (k = 0; k < 100000; k++);
    }

    }

    return;
    }

    int main (int argc, char *argv[])
    {
    struct sigaction act, old_act;

    memset (&act, '\0', sizeof(act));

    /* Use the sa_sigaction field because the handles has two additional parameters */
    act.sa_sigaction = &signal_hdl;

    /* The SA_SIGINFO flag tells sigaction() to use the sa_sigaction field, not sa_handler. */
    act.sa_flags = SA_SIGINFO;

    if (sigaction(SIGTERM, &act, &old_act) < 0) {
    perror ("sigaction failed for SIGTERM signal\n");
    return 1;
    }
    if (sigaction(SIGINT, &act, NULL) < 0) {
    perror ("sigaction failed for SIGINT signal\n");
    return 1;
    }
    printf("PID of this program is %d.\n", getpid());
    while (1)
    sleep (10);

    return 0;
    }

  2. #2
    Just Joined!
    Join Date
    Apr 2011
    Posts
    19

    Might try catching SIGPWR

    I think SIGPWR is used by some systems on shutdown. You cannot catch sigkill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...