signals

scanf loop, and signal handler

萝らか妹 提交于 2019-12-02 18:22:52
问题 Just learned about sigacation, also implemented another timer to make it more interesting. #include <stdio.h> #include <signal.h> #include <sys/time.h> volatile sig_atomic_t gotsignal; void handler(){ gotsignal = 1; } int main(){ struct sigaction sig; struct itimerval timer; timer.it_value.tv_sec = 5; timer.it_value.tv_usec = 0; timer.it_interval = timer.it_value; sig.sa_handler = handler; sig.sa_flags = 0; sigemptyset(&sig.sa_mask); setitimer(ITIMER_REAL, &timer, 0); sigaction(SIGALRM, &sig,

How to trap exit code in Bash script

不想你离开。 提交于 2019-12-02 17:53:13
There're many exit points in my bash code. I need to do some clean up work on exit, so I used trap to add a callback for exit like this: trap "mycleanup" EXIT The problem is there're different exit codes, I need to do corresponding cleanup works. Can I get exit code in mycleanup? I think you can use $? to get the exit code. The accepted answer is basically correct, I just want to clarify things. The following example works well: #!/bin/bash cleanup() { rv=$? rm -rf "$tmpdir" exit $rv } tmpdir="$(mktemp)" trap "cleanup" INT TERM EXIT # Do things... But you have to be more careful if doing

How to signal an application without killing it in Linux?

不问归期 提交于 2019-12-02 17:29:09
I have a watchdog application. It watches my main app which might crash for one reason or another (I know it is bad, but this is not the point). I programmed this watchdog to accept SIGUSR1 signals to stop monitoring my application presence. I signal it with kill -SIGUSR1 `pidof myapp` This works really well. My problem comes when I try to signal an older version of the watchdog which does not have this functionality built in. In this case, the kill signal kills the watchdog (terminates the process), which leads to further complications (rebooting of the device). Is there a way to signal my

How to modify EIP's tracee forked procee?

不打扰是莪最后的温柔 提交于 2019-12-02 16:58:59
问题 I'm working on a Linux application incorporating ptrace to observe another process which had been created by fork() system call. Strictly speaking: I want to implement a fault injection into forked process (chile process or "tracee"). As you can see in the figure below: the tracer gets the regs (struct_user_regs) structure from the tracee by using PTRACE_GETREGS request. after that, tracer modifies the EIP value of the tracee (when kernel switch into tracee, order execution will be violate so

Set and Oldset in sigprocmask()

元气小坏坏 提交于 2019-12-02 14:42:34
I haven't completely understood, how to use sigprocmask() . Particularly, how the set and oldset and its syntax work and how to use them. int sigprocmask(int how, const sigset_t *set, sigset_t *oldset); Please explain with an example, to block, say SIGUSR1 for a few seconds and then unblock and handle it. ­­­­­­­­­­­­­­­­­­­­­­­­­­­­­ The idea is that you provide a mask in set , effectively a list of signals. The how argument says what you should do with the mask in set . You can either use SIG_BLOCK to block the signals in the set list, or SIG_UNBLOCK to unblock them. Neither of these changes

How are asynchronous signal handlers executed on Linux?

我是研究僧i 提交于 2019-12-02 14:14:06
I would like to know exactly how the execution of asynchronous signal handlers works on Linux. First, I am unclear as to which thread executes the signal handler. Second, I would like to know the steps that are followed to make the thread execute the signal handler. On the first matter, I have read two different, seemingly conflicting, explanations: The Linux Kernel, by Andries Brouwer, §5.2 "Receiving signals" states : When a signal arrives, the process is interrupted, the current registers are saved, and the signal handler is invoked. When the signal handler returns, the interrupted activity

SIGCHLD is sent on SIGCONT on Linux but not on macOS

与世无争的帅哥 提交于 2019-12-02 13:35:17
问题 In the main process I listen to SIGCHLD: signal(SIGCHLD, &my_handler); Then I fork() , execv() and let it run in background (/bin/cat for example). When I try from terminal to send SIGSTOP to the child process, my_handler() gets called. But when I try to send SIGCONT to it, the the handler isn't called on macOS but it's executed on my Ubuntu. Man: SIGCHLD: child status has changed. Am I missing something? Is it an expected behaviour? I wrote my app on Ubuntu and expected it to work on mac as

How to use SIGFPE with signal?

拟墨画扇 提交于 2019-12-02 10:09:22
I just informed myselve about "signals" in C/C++ and played around. But i have a problem to understand the logic of SIGFPE . I wrote a little program which will run into a division by zero, if this happens then the signal should be triggered and the signal handler should be executed. But instead my program just crashes. So what is the purpose of the SIGFPE if it does not even work on division by zero? #include <stdio.h> #include <signal.h> #include <iostream> int signal_status = 0; void my_handler (int param) { signal_status = 1; printf ("DIVISION BY ZERO!"); } int main () { signal (SIGFPE, my

How to modify EIP's tracee forked procee?

女生的网名这么多〃 提交于 2019-12-02 09:06:43
I'm working on a Linux application incorporating ptrace to observe another process which had been created by fork() system call. Strictly speaking: I want to implement a fault injection into forked process (chile process or "tracee"). As you can see in the figure below: the tracer gets the regs (struct_user_regs) structure from the tracee by using PTRACE_GETREGS request. after that, tracer modifies the EIP value of the tracee (when kernel switch into tracee, order execution will be violate so-called control flow error CFE). then PTRAC E_CONT request will send to tracee to continue its

Why timer_create throw error for SIGEV_THREAD in solaris 10?

拥有回忆 提交于 2019-12-02 08:50:19
问题 I wrote a piece of by using timer_create for sets the timer to invoke a thread in which i set sigev_notify as SIGEV_THREAD, it is giving me error EINVAL(Invalid argument) but when i am setting sigev_notify as SIGEV_SIGNAL code is working fine. my this piece of code is working in all OS even in solaris 11 but for solaris 10 giving me error. code given below: { int status =0; struct itimerspec ts; struct sigevent se; se.sigev_notify = SIGEV_THREAD; se.sigev_value.sival_int = val; se.sigev