signals.c
author viric@llimona
Mon, 12 Nov 2007 09:57:46 +0100
changeset 140 0b99d94818d1
parent 139 e5c0fccdb233
child 146 5e689cb593aa
permissions -rw-r--r--
Added the web page.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
102
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 95
diff changeset
     1
/*
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 95
diff changeset
     2
    Task Spooler - a task queue system for the unix user
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 95
diff changeset
     3
    Copyright (C) 2007  LluĂ­s Batlle i Rossell
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 95
diff changeset
     4
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 95
diff changeset
     5
    Please find the license in the provided COPYING file.
27b0f0e44de8 Added copyright headers to the new files. I always forget that.
viric@llimona
parents: 95
diff changeset
     6
*/
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
     7
#include <signal.h>
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
     8
#include <stdio.h>
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
     9
#include <stdlib.h> /* for NULL */
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    10
#include "main.h"
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    11
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    12
/* Some externs refer to this variable */
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    13
static sigset_t normal_sigmask;
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    14
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    15
/* as extern in execute.c */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    16
int signals_child_pid; /* 0, not set. otherwise, set. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    17
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    18
void ignore_sigpipe()
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    19
{
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    20
    sigset_t set;
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    21
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    22
    sigemptyset(&set);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    23
    sigaddset(&set, SIGPIPE);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    24
    sigprocmask(SIG_BLOCK, &set, &normal_sigmask);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    25
}
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    26
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    27
void restore_sigmask()
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    28
{
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    29
    sigprocmask(SIG_SETMASK, &normal_sigmask, NULL);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    30
}
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    31
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    32
void sigint_handler(int s)
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    33
{
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    34
    if (signals_child_pid)
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    35
    {
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    36
        kill(signals_child_pid, SIGINT);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    37
    } else
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    38
    {
139
e5c0fccdb233 Removing warning on SIGINT.
viric@llimona
parents: 138
diff changeset
    39
        /* ts client killed by SIGINT */
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    40
        exit(1);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    41
    }
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    42
}
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    43
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    44
void block_sigint()
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    45
{
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    46
    sigset_t set;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    47
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    48
    sigemptyset(&set);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    49
    sigaddset(&set, SIGINT);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    50
    /* ignore_sigpipe() will always be called first, and
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    51
     * only that sets the normal_sigmask. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    52
    sigprocmask(SIG_BLOCK, &set, 0);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    53
}
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    54
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    55
void unblock_sigint_and_install_handler()
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    56
{
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    57
    sigset_t set;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    58
    struct sigaction act;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    59
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    60
    /* Install the handler */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    61
    act.sa_handler = sigint_handler;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    62
    sigemptyset(&act.sa_mask);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    63
    act.sa_flags = 0;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    64
    sigaction(SIGINT, &act, 0);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    65
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    66
    /* Unblock the signal */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    67
    sigemptyset(&set);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    68
    sigaddset(&set, SIGINT);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    69
    /* ignore_sigpipe() will always be called first, and
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    70
     * only that sets the normal_sigmask. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    71
    sigprocmask(SIG_UNBLOCK, &set, 0);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    72
}