13

On Linux, is it possible to somehow disable signaling for programs externally... that is, without modifying their source code?

Context:

I'm calling a C (and also a Java) program from within a bash script on Linux. I don't want any interruptions for my bash script, and for the other programs that the script launches (as foreground processes).

While I can use a...

trap '' INT

... in my bash script to disable the Ctrl C signal, this works only when the program control happens to be in the bash code. That is, if I press Ctrl C while the C program is running, the C program gets interrupted and it exits! This C program is doing some critical operation because of which I don't want it be interrupted. I don't have access to the source code of this C program, so signal handling inside the C program is out of question.

#!/bin/bash

trap 'echo You pressed Ctrl C' INT 

# A C program to emulate a real-world, long-running program,
# which I don't want to be interrupted, and for which I 
# don't have the source code!
#
# File: y.c
# To build: gcc -o y y.c
#
# #include <stdio.h>
# int main(int argc, char *argv[]) {
#  printf("Performing a critical operation...\n");
#    for(;;); // Do nothing forever.
#  printf("Performing a critical operation... done.\n");
# }

./y

Regards,

/HS

Jolta
  • 2,620
  • 1
  • 29
  • 42
Harry
  • 3,684
  • 6
  • 39
  • 48
  • can you use the & at the end? so you send the prog to background. e.g. cat file & – Eric Fortis Dec 23 '10 at 02:11
  • You should also be aware that user's who fail at stopping a program by pressing ctrl-c often have a nasty tendacy to get brutal and start using the kill command. So do make sure you deal with the possibility of the whole thing getting killed off in spite of your efforts. – Michael Kohne Dec 23 '10 at 02:21
  • @Eric Fortis I need to carry out the steps in my bash script sequentially, so cannot afford to send an interim step in the background. At first, I jumped at your suggestion... thought, yeah why not send it to background and then call the 'wait' builtin. But & + wait doesn't appear to help: the infinite loop C program does manage to get interrupted, and the bash script also exits shortly thereafter. – Harry Dec 23 '10 at 03:06
  • @Michael Kohne That is a good advice. The audience of this program I think will comprise of users who know 'kill means bad' but are not necessarily aware of 'Ctrl C can also be bad in some cases'. Though I'll be documenting the 'no-Ctrl-C's-or-else-dire-consequences!" clause, I thought why not also attempt to prevent it via a trap and a friendly message to the effect. But if all this turns out to be not possible, then I may be forced to rely only on documentation. – Harry Dec 23 '10 at 03:09

5 Answers5

13

The process signal mask is inherited across exec, so you can simply write a small wrapper program that blocks SIGINT and executes the target:

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

int main(int argc, char *argv[])
{
        sigset_t sigs;

        sigemptyset(&sigs);
        sigaddset(&sigs, SIGINT);
        sigprocmask(SIG_BLOCK, &sigs, 0);

        if (argc > 1) {
                execvp(argv[1], argv + 1);
                perror("execv");
        } else {
                fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
        }
        return 1;
}

If you compile this program to noint, you would just execute ./noint ./y.

As ephemient notes in comments, the signal disposition is also inherited, so you can have the wrapper ignore the signal instead of blocking it:

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

int main(int argc, char *argv[])
{
        struct sigaction sa = { 0 };

        sa.sa_handler = SIG_IGN;
        sigaction(SIGINT, &sa, 0);

        if (argc > 1) {
                execvp(argv[1], argv + 1);
                perror("execv");
        } else {
                fprintf(stderr, "Usage: %s <command> [args...]\n", argv[0]);
        }
        return 1;
}

(and of course for a belt-and-braces approach, you could do both).

caf
  • 233,326
  • 40
  • 323
  • 462
  • 1
    +1: I upvoted your answer but have a question. What if the child explicitly cleared it's own process signal mask? Wouldn't that invalidate the signal masks the parent process set? – sashang Dec 23 '10 at 04:14
  • 4
    It might make more sense to ignore the signal than to block it: `sigaction(SIGINT, &(struct sigaction){.sa_handler = SIG_IGN}, NULL);` This is what the shell does for `SIGTTOU` to its children, after all. – ephemient Dec 23 '10 at 04:18
  • @caf Boy, was that one hell of an answer. It appears to work. +1 -ing you while I evaluate it further (esp in light of ephemient's comment). Just fyi, I did try to think of some solution based on wrapper but, instead of execvp, I was thinking of doing system("...") inside and so couldn't have gotten it to work. Thanks much. – Harry Dec 23 '10 at 04:40
  • To stop the child process changing its own signal mask, you could use a LD_PRELOAD library and put in your own signal functions to intercept the ones in the standard library. – camh Dec 23 '10 at 09:35
  • @ephemient: Good point, have updated answer with that option. – caf Dec 23 '10 at 12:55
  • @sashang: Yes, anything done in the wrapper can be undone in the child process. Most processes are not so wilful, though. – caf Dec 23 '10 at 12:56
  • For cow sake, why you need to re-invent the wheel if there is already `nohup`? –  Dec 23 '10 at 13:28
  • 1
    @Vlad: `nohup` only ignores `SIGHUP`, not `SIGINT`. – ephemient Dec 23 '10 at 15:01
  • @Vlad Lazarenko: This isn't the same as `nohup`. For example, this allows the target process to continue reading input from the terminal, whereas `nohup` works by redirecting standard input to `/dev/null`. – caf Dec 23 '10 at 20:52
3

The "trap" command is local to this process, never applies to children.

To really trap the signal, you have to hack it using a LD_PRELOAD hook. This is non-trival task (you have to compile a loadable with _init(), sigaction() inside), so I won't include the full code here. You can find an example for SIGSEGV on Phack Volume 0x0b, Issue 0x3a, Phile #0x03.

Alternativlly, try the nohup and tail trick.

nohup  your_command &
tail -F nohup.out
J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
  • Appreciate your telling me (with clarity) about trap being local to the process, so +1. But caf's solution is really what I was looking for. Thanks. – Harry Dec 23 '10 at 07:31
  • 1
    @Harry, No worries. You should accept Caf's answer as correct so that other people can find the info more easily :) - ED this was meant ot go on my post. Oh well, point stands. – richo Dec 23 '10 at 21:37
0

The solutions explained above are not working for me, even by chaining the both commands proposed by Caf.

However, I finally succeeded in getting the expected behavior this way :

#!/bin/zsh
setopt MONITOR
TRAPINT() { print AAA }

print 1
( ./child & ; wait)
print 2

If I press Ctrl-C while child is running, it will wait that it exits, then will print AAA and 2. child will not receive any signals.

The subshell is used to prevent the PID from being shown.

And sorry... this is for zsh though the question is for bash, but I do not know bash enough to provide an equivalent script.

calandoa
  • 5,668
  • 2
  • 28
  • 25
0

This is example code of enabling signals like Ctrl+C for programs which block it.

fixControlC.c

#include <stdio.h>
#include <signal.h>
int sigaddset(sigset_t *set, int signo) {
    printf("int sigaddset(sigset_t *set=%p, int signo=%d)\n", set, signo);
    return 0;
}

Compile it:

gcc -fPIC -shared -o fixControlC.so fixControlC.c

Run it:

LD_LIBRARY_PATH=. LD_PRELOAD=fixControlC.so mysqld
kungfooman
  • 4,473
  • 1
  • 44
  • 33
0

I would suggest that your C (and Java) application needs rewriting so that it can handle an exception, what happens if it really does need to be interrupted, power fails, etc...

I that fails, J-16 is right on the money. Does the user need to interract with the process, or just see the output (do they even need to see the output?)

richo
  • 8,717
  • 3
  • 29
  • 47
  • Richo, rewriting is out of question... not because I don't want to but because of lack of availability of source. I believe caf's solution above should work for me. Thanks. – Harry Dec 23 '10 at 07:29