Visit Our UNIX and Linux User Community


Problem on capturing system Shutdown


 
Thread Tools Search this Thread
Top Forums Programming Problem on capturing system Shutdown
# 1  
Old 08-27-2013
Ubuntu Problem on capturing system Shutdown

I am having exactly the same problem with Application Cleanup during Linux Shutdown but the thread is old and closed. The only difference is that I use sigaction() instead of signal(), which is recommended, as far as I know.

This is my code:

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

void handler(int signal)
{
    FILE *out=fopen("test.txt","at");
    if (out)
    {
        fprintf(out,"got %d\n",signal);
        fclose(out);
    }
}

int main()
{
    struct sigaction sigIntHandler;

    sigIntHandler.sa_handler = handler;
    sigemptyset(&sigIntHandler.sa_mask);
    sigIntHandler.sa_flags = 0;

    sigaction(SIGINT, &sigIntHandler, NULL);
    sigaction(SIGTERM, &sigIntHandler, NULL);
    sigaction(SIGHUP, &sigIntHandler, NULL);
    sigaction(SIGQUIT, &sigIntHandler, NULL);
    while(1)
        sleep(30);
}

And this is some usage that shows that it is working:

Code:
$ rm -rf test.txt
$ ./signal_handling_test &
[1] 3599
$ kill -s SIGTERM 3599
$ cat test.txt
got 15

If I normally shutdown or log out then the process is killed without the clean up taking place.

I am running under Ubuntu 13.04.
# 2  
Old 08-27-2013
Read this:

signal(7) - Linux manual page

Pay particular attention to the section "Async-signal-safe functions". Bluntly, you can't call async-signal-unsafe functions from a signal handler and expect proper results.

If you want to see what's happening, you can run your program under strace and see for yourself.
This User Gave Thanks to achenle For This Post:
# 3  
Old 08-27-2013
Thanks a lot for your answer. I cannot understand anything from the phrase
"POSIX.1-2004 (also known as POSIX.1-2001 Technical Corrigendum 2) requires an implementation to guarantee that the following functions can be safely called inside a signal handler:
...
list of functions
...
"
POSIZ.1-2004 requires an implementation? What does that mean? After I implement POSIX.1-2004 then I can safely call only the following functions that almost none of them have to do with writing to files?

Also, is there any way to exactly simulate shutdown for my process only so as not to shutdown my system all the time for testing purposes?

EDIT: Also, how come and the signal handler works each time if I send the SIGTERM? What different does the system do? I thought it sends SIGTERM through kill and waits a small timeout for the processes to end.

Last edited by hakermania; 08-27-2013 at 03:03 PM..
# 4  
Old 08-27-2013
It means, the list of signal-safe functions are safe to use inside a signal handler. The POSIX stuff means that this is what's expected of a UNIX system.

How would you simulate shutdown without being shutdown? Killing all processes but not turning off the computer?
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 08-27-2013
I just downloaded the source code of ettercap-graphical (a well know application) and it doesn't use async-safe functions in the code (fprintf is being used, user interface is being normally shut down, the log file is flushed and closed). So how "strict" is it to make async-safe functions?

I also have this simple question: Why not use async-unsafe functions after all? The worse thing that could happen would be the application to crash or to be in a loop easily terminated with SIGKILL. The application was going to forcefully terminate after all. At least give it a chance to do a proper cleanup.

As for simulating shutdown: Now I am simulating shutdown with simply sending SIGTERM to my process. As I said before, this is not the only thing that really happens, because when I send SIGTERM it runs the handler without any problem. So, the system does something else that I am unaware of (I highly doubt that it really spends time on checking whether the function that is to be called is async-safe or not). So, I simply asked whether there is a better way to simulate the shutdown for only one process, because simply sending SIGTERM is not the best way. I don't believe I am being irrational.
# 6  
Old 08-27-2013
Quote:
I also have this simple question: Why not use async-unsafe functions after all? The worse thing that could happen would be the application to crash or to be in a loop easily terminated with SIGKILL.
I think you just answered your own question.
Quote:
The application was going to forcefully terminate after all. At least give it a chance to do a proper cleanup.
You can give it more than a chance -- you can actually do so. Async-safe system calls include open(), close(), read(), and write() among many other things. All you need.

fopen, printf() and its relatives in particular are not signal safe because they allocate memory. You might get away with it by accident but it's system-dependent, compiler-dependent, library-dependent, and luck-dependent. Your code may work all the time on your system and crash all the time on someone else's.

To write code that'll work both here and there, use signal-safe things. That's what they're for.
# 7  
Old 08-27-2013
Quote:
Originally Posted by Corona688
I think you just answered your own question. You can give it more than a chance -- you can actually do so. Async-safe calls include open(), close(), read(), and write() among many other things. All you need.
So you claim that if I make a sync-safe function using the above sync-safe functions, then the handler will be executed? And if this is true then how does the system determine:

a) Where in the memory the handler sits for each application
b) If the handler is sync-safe or sync-unsafe

Also, this is still a question:

What different does the system do instead of sending SIGTERM, waiting for the process to exit, and, after timing out, sending SIGKILL? Because it does something different, it not only sends SIGTERM, because if I send it, it just works.

Previous Thread | Next Thread
Test Your Knowledge in Computers #748
Difficulty: Medium
The ZX81 was a home computer that was produced by Sinclair Research and manufactured in Dundee, Scotland.
True or False?

8 More Discussions You Might Find Interesting

1. Red Hat

shutdown system/myeclipse

Hi All, I have one situation to shut-down the system through shell script.I need script command to shut-down the system and process should end(safe-mode) the MyEclipse. (0 Replies)
Discussion started by: stsivaraj
0 Replies

2. Solaris

Shutdown system option

I am working on sunos solaris. I want to know which is good for system shut-down ? (3 Replies)
Discussion started by: Jitesh Varshney
3 Replies

3. Shell Programming and Scripting

Capturing awk's system(cmd) output

Hi everybody, I am working on a bigger awk script in which one part is comparing the size of two files. I want to evaluate which file is bigger and then just save the bigger one. I got it all working except for the part where I want to figure out which file is bigger; the one awk is currently... (2 Replies)
Discussion started by: iMeal
2 Replies

4. Windows & DOS: Issues & Discussions

system restarts after shutdown!!

Hello, from last few days my laptop is not whutting down properly.. when ever i ty to shutdown it restarts again.. what may be problem?? antivirus is updated till date.. and i use windows xp sp2.... regards, deepak. (5 Replies)
Discussion started by: smarty86
5 Replies

5. Shell Programming and Scripting

Sending mail on system shutdown

I want to only send a mail before my system goes for a shutdown or reboot. I don't want a mail when it comes up after a reboot or is normally started.. How can i achieve this? (2 Replies)
Discussion started by: proactiveaditya
2 Replies

6. Solaris

logs for system shutdown

I am working on a SUN T2000 machine with Solaris 10 running on it. When I checked the system this morning, I found it to be turned off. The lastreboot command showed that the system had been shut down the previous night. I want to find out how the system was shut down. I have run hardware health... (2 Replies)
Discussion started by: batman727
2 Replies

7. Shell Programming and Scripting

Shutdown a system by MAC?

Ok here is the problem we have 2 v440 with same IP address running solars 9. one remains on the other remains off. They are both configured exactly the same for redundant purposes for the software we use. This was the best/worst idea. Great because down time is only a mere minutes. The bad is the... (7 Replies)
Discussion started by: deaconf19
7 Replies

8. UNIX for Advanced & Expert Users

Capturing Oracle Shutdown error

Hi, Iam calling 3 sql scripts through one shell script 'rmia.sh'. Till now this was working fine, but last time while calling the SQL scripts, Oracle was down. But amazingly the exit status was '0' (success)!!! Below is the shell code: #!/usr/bin/ksh -x assign_file asql a.sql 1... (15 Replies)
Discussion started by: ganapati
15 Replies

Featured Tech Videos