signals.c
author viric <viriketo@gmail.com>
Mon, 18 Jul 2011 22:48:54 +0200
branchqueuelimit
changeset 293 bb87d5e7c466
parent 282 835551fcecb7
permissions -rw-r--r--
Closing this branch, where I wrote bad code about adding the proper handling of queue limits by blocking clients.
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
267
11631dd11ff8 Updating copyright years in the source.
viric@mandarina
parents: 265
diff changeset
     3
    Copyright (C) 2007-2009  LluĂ­s Batlle i Rossell
102
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 */
146
5e689cb593aa Bones of the "-i" parameter, job info.
viric@llimona
parents: 139
diff changeset
    10
#include <sys/time.h> /* for NULL */
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    11
#include "main.h"
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    12
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    13
/* Some externs refer to this variable */
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    14
static sigset_t normal_sigmask;
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    15
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    16
/* as extern in execute.c */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    17
int signals_child_pid; /* 0, not set. otherwise, set. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    18
95
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    19
void ignore_sigpipe()
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    20
{
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    21
    sigset_t set;
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    22
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    23
    sigemptyset(&set);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    24
    sigaddset(&set, SIGPIPE);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    25
    sigprocmask(SIG_BLOCK, &set, &normal_sigmask);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    26
}
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    27
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    28
void restore_sigmask()
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    29
{
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    30
    sigprocmask(SIG_SETMASK, &normal_sigmask, NULL);
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    31
}
d31aaee661d1 Protection against SIGPIPE. Block it.
viric@llimona
parents:
diff changeset
    32
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    33
void sigint_handler(int s)
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    34
{
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    35
    if (signals_child_pid)
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    36
    {
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    37
        kill(signals_child_pid, SIGINT);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    38
    } else
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    39
    {
139
e5c0fccdb233 Removing warning on SIGINT.
viric@llimona
parents: 138
diff changeset
    40
        /* ts client killed by SIGINT */
138
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    41
        exit(1);
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
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    45
void block_sigint()
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    46
{
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    47
    sigset_t set;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    48
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    49
    sigemptyset(&set);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    50
    sigaddset(&set, SIGINT);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    51
    /* ignore_sigpipe() will always be called first, and
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    52
     * only that sets the normal_sigmask. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    53
    sigprocmask(SIG_BLOCK, &set, 0);
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
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    56
void unblock_sigint_and_install_handler()
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    57
{
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    58
    sigset_t set;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    59
    struct sigaction act;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    60
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    61
    /* Install the handler */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    62
    act.sa_handler = sigint_handler;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    63
    sigemptyset(&act.sa_mask);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    64
    act.sa_flags = 0;
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    65
    sigaction(SIGINT, &act, 0);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    66
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    67
    /* Unblock the signal */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    68
    sigemptyset(&set);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    69
    sigaddset(&set, SIGINT);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    70
    /* ignore_sigpipe() will always be called first, and
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    71
     * only that sets the normal_sigmask. */
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    72
    sigprocmask(SIG_UNBLOCK, &set, 0);
00461b7bdf4b Passing SIGINT to the child, if it started.
viric@llimona
parents: 102
diff changeset
    73
}