Qt application unable to catch sudo reboot / sudo poweroff from terminal
-
@SGaist I believe that may be the case here.
Currently I have the following theories for it not working- Have implemented a wrong logic -- ( But I did try couple of implementations even the one @Pablo-J-Rogina mentioned. Thanks @Pablo-J-Rogina for your suggestion ). Also would like to point out that I am able to catch all ctrl c, ctrl z, kill -15 pid etc
- It could be something to do with Qt or the version of Qt I am using.
- May be my application goes to halt state as soon as sudo reboot / sudo poweroff is executed. Thus leading it not to catch the signals.
- When sudo reboot / sudo poweroff is executed my Qt application is not notified with the signal. I am guessing this because when I execute kill -15 pid it works as the signal is directed to my application by mentioning the pid.
So I would like to know if someone in the forum has tested their signal handler code with sudo reboot / sudo poweroff ? Meanwhile I shall keep trying to get through this or atleast try to learn why I am unable to catch the signal.
@NarutoKun
Assuming you know how to do this, if it were me I'd write a small non-Qt C/C++ program (e.g. I'm thinking justsleep()
and do your signal handling), verify you can catch your SIGTERM like now, and then see how it behaves under shutdown. You need to know how/whether it works completely outside of Qt first. It may be this is purely a Linux (or code) issue, nothing to do with Qt. -
@NarutoKun
Assuming you know how to do this, if it were me I'd write a small non-Qt C/C++ program (e.g. I'm thinking justsleep()
and do your signal handling), verify you can catch your SIGTERM like now, and then see how it behaves under shutdown. You need to know how/whether it works completely outside of Qt first. It may be this is purely a Linux (or code) issue, nothing to do with Qt. -
@JNBarchan
Good thought.. May be I shall give it a try. Non Qt code to handle Signals.
Thanks
How do you judge that you haven't received/handled the SIGTERM at shutdown?
-
@kshegunov
I already asked him this question above:Also, how precisely do you judge that "my implementation doesn't work as expected when I execute sudo reboot / sudo poweroff"?
He replied:
@JNBarchan
application is able to catch sudo kill -15 pid, to confirm the reception of signal I am writing to syslog directly as LOG_ALERT and also to a file. ( just writing short phrases in both instances )I take him at his word, that he sees these from
kill
but not fromshutdown
...! -
There seems to be an assumption that when calling
sudo shutdown
orsudo reboot
the system will let the time to all application do stop properly at their own pace. That's wrong. Like explained in the command documentation, theSIGTERM
andSIGKILL
signals are sent one after the other with a possible period between the two if provided. Since this period is not provided in this case and the default value is unspecified, the processes are likely going to get killed "en masse" pretty quickly to allow the system to shutdown/reboot. -
There seems to be an assumption that when calling
sudo shutdown
orsudo reboot
the system will let the time to all application do stop properly at their own pace. That's wrong. Like explained in the command documentation, theSIGTERM
andSIGKILL
signals are sent one after the other with a possible period between the two if provided. Since this period is not provided in this case and the default value is unspecified, the processes are likely going to get killed "en masse" pretty quickly to allow the system to shutdown/reboot.@SGaist
I wondered about this too. One presumes processes would receive SIGTERM at least a touch before SIGKILL, and I advised the OP to put it something very quick & simple for SIGTERM handler. I believe he claims that syslog shows the SIGTERM signal being delivered to the process viakill -15
but not during shutdown (but not sure). In any case, that is why I advised him that he needs to discover the shutdown actual behaviour, preferably outside of Qt. He should also read around general Linux stuff to discover how you are supposed to gracefully handle shutdown, as again I assume (based on nothing) that it allows programs a quick "grace period" to clean up & exit. -
@kshegunov
I already asked him this question above:Also, how precisely do you judge that "my implementation doesn't work as expected when I execute sudo reboot / sudo poweroff"?
He replied:
@JNBarchan
application is able to catch sudo kill -15 pid, to confirm the reception of signal I am writing to syslog directly as LOG_ALERT and also to a file. ( just writing short phrases in both instances )I take him at his word, that he sees these from
kill
but not fromshutdown
...!@JNBarchan said in Qt application unable to catch sudo reboot / sudo poweroff from terminal:
I already asked him this question above
Missed that, sorry. Your suggestion is good, but as @SGaist pointed out there may be no time to really respond to the sequence of signals, and
SIGKILL
isn't something you can catch. -
@JNBarchan said in Qt application unable to catch sudo reboot / sudo poweroff from terminal:
I already asked him this question above
Missed that, sorry. Your suggestion is good, but as @SGaist pointed out there may be no time to really respond to the sequence of signals, and
SIGKILL
isn't something you can catch.@kshegunov
I do not know about this area, but I believed the OP was saying that the system logged in syslog the sent/delivered signals to processes and he was looking through that. I may have misunderstood, and he only means his application does the logging when signal received, and then indeed we could have race conditions or no such signal actually sent.This indicates even more that OP needs to read up elsewhere how others handle "graceful shutdown" --- assuming it is designed to allow brief code execution in response to SIGTERM before delievery of something like SIGKILL.... e.g. perhaps start from https://stackoverflow.com/questions/22009705/how-to-detect-linux-shutdown-reboot, which might be indicating the same issue as OP is reporting?
-
@kshegunov
I do not know about this area, but I believed the OP was saying that the system logged in syslog the sent/delivered signals to processes and he was looking through that. I may have misunderstood, and he only means his application does the logging when signal received, and then indeed we could have race conditions or no such signal actually sent.This indicates even more that OP needs to read up elsewhere how others handle "graceful shutdown" --- assuming it is designed to allow brief code execution in response to SIGTERM before delievery of something like SIGKILL.... e.g. perhaps start from https://stackoverflow.com/questions/22009705/how-to-detect-linux-shutdown-reboot, which might be indicating the same issue as OP is reporting?
I think the problem here is there's a very few functions that can be called from a signal handler. It may be that the process is actually segfaulting, or if the exact example from the Qt docs is used then the acceptance of the
SIGTERM
would mean the continuation of the shutdown sequence (and subsequently killing the process as it hasn't quit). I'd try the following (use only the allowed POSIX functions for the signal handler):- Create a semaphore on startup and acquire it
- When you get the
SIGTERM
write to the socket pair and acquire the semaphore again to prevent returning from the handler - Do the clean up from the Qt handler
- Subscribe to the
aboutToQuit
signal and release the semaphore from the slot - Only then return from the signal handler
The above you could also accomplish by
select
ing on the socket you opened and writing back a byte after your Qt handler has been called and the cleanup code has run. -
I think the problem here is there's a very few functions that can be called from a signal handler. It may be that the process is actually segfaulting, or if the exact example from the Qt docs is used then the acceptance of the
SIGTERM
would mean the continuation of the shutdown sequence (and subsequently killing the process as it hasn't quit). I'd try the following (use only the allowed POSIX functions for the signal handler):- Create a semaphore on startup and acquire it
- When you get the
SIGTERM
write to the socket pair and acquire the semaphore again to prevent returning from the handler - Do the clean up from the Qt handler
- Subscribe to the
aboutToQuit
signal and release the semaphore from the slot - Only then return from the signal handler
The above you could also accomplish by
select
ing on the socket you opened and writing back a byte after your Qt handler has been called and the cleanup code has run.@kshegunov
OP says of what happens when signal delivered:my implementation doesn't work as expected when I execute sudo reboot / sudo poweroff in a terminal, though internally SIGTERM signal is getting emitted. Have checked this in syslog.
I'm still unclear whether he means he writes to syslog from his handler, or that the OS automatically logs signal delivery itself. I'm thinking the latter, as he's saying:
yet my application doesn't hit my signal handler when sudo poweroff / sudo reboot is executed through terminal. But when I execute kill -15 pid ( pid of my application ) signal handler gets executed
It depends on whether during shutdown he is truly not receiving the initial SIGTERM which would initiate his clean up code, or whether actually he is receiving it but very soon afterwards he's getting a SIGKILL or similar, so that his clean up code doesn't get to do much. Even if he hasn't yet returned from the SIGTERM handler, I thought nothing would block the SIGKILL from immediate delivery and termination, surely it doesn't care that you are presently in a signal handler?
-
@kshegunov
OP says of what happens when signal delivered:my implementation doesn't work as expected when I execute sudo reboot / sudo poweroff in a terminal, though internally SIGTERM signal is getting emitted. Have checked this in syslog.
I'm still unclear whether he means he writes to syslog from his handler, or that the OS automatically logs signal delivery itself. I'm thinking the latter, as he's saying:
yet my application doesn't hit my signal handler when sudo poweroff / sudo reboot is executed through terminal. But when I execute kill -15 pid ( pid of my application ) signal handler gets executed
It depends on whether during shutdown he is truly not receiving the initial SIGTERM which would initiate his clean up code, or whether actually he is receiving it but very soon afterwards he's getting a SIGKILL or similar, so that his clean up code doesn't get to do much. Even if he hasn't yet returned from the SIGTERM handler, I thought nothing would block the SIGKILL from immediate delivery and termination, surely it doesn't care that you are presently in a signal handler?
I'd really like to see some code, though.
For one not everything can be done from a signal handler (as Qt's docs correctly note), and for two you need to register the handlers in such a way so you don't get interleaved signal handlers called, otherwise it's a big mess. A signal sent to the process is much like an interrupt, so it can get quite complicated if you're not requesting them to be queued. -
I'd really like to see some code, though.
For one not everything can be done from a signal handler (as Qt's docs correctly note), and for two you need to register the handlers in such a way so you don't get interleaved signal handlers called, otherwise it's a big mess. A signal sent to the process is much like an interrupt, so it can get quite complicated if you're not requesting them to be queued.@kshegunov @JNBarchan
Sorry was away hence couldn't respond.
Have written a C code for handling SIGTERM even here I am unable to catch sudo reboot / sudo poweroff, but my handler is able to catch kill -15 pid. I am creating a text file upon arrival of the signal.
Through this exercise atleast I am sure I am missing some concepts of how to handle Signals.
#include <stdio.h> #include <signal.h> #include <unistd.h> void signal_handler() { // Open a file on receiving the signal FILE *fp = fopen("naruto.txt", "ab+"); } void main() { pid_t app_pid; struct sigaction a; // PID of the application app_pid = getpid(); printf("process pid %d \n",app_pid); // assigning the signal handler a.sa_handler = signal_handler; // Restarting system calls a.sa_flags = SA_RESTART; if (sigaction(SIGTERM,&a, NULL) < 0) { printf("error"); } if (sigaction(SIGINT,&a , NULL) < 0) { printf("error"); } // Waiting for the signal pause(); printf("end\n"); }
I am also trying to understand how to use RUN_LVL parameter for graceful shutdown. Thanks for the link.
[Added code tags ~kshegunov]
-
@kshegunov @JNBarchan
Sorry was away hence couldn't respond.
Have written a C code for handling SIGTERM even here I am unable to catch sudo reboot / sudo poweroff, but my handler is able to catch kill -15 pid. I am creating a text file upon arrival of the signal.
Through this exercise atleast I am sure I am missing some concepts of how to handle Signals.
#include <stdio.h> #include <signal.h> #include <unistd.h> void signal_handler() { // Open a file on receiving the signal FILE *fp = fopen("naruto.txt", "ab+"); } void main() { pid_t app_pid; struct sigaction a; // PID of the application app_pid = getpid(); printf("process pid %d \n",app_pid); // assigning the signal handler a.sa_handler = signal_handler; // Restarting system calls a.sa_flags = SA_RESTART; if (sigaction(SIGTERM,&a, NULL) < 0) { printf("error"); } if (sigaction(SIGINT,&a , NULL) < 0) { printf("error"); } // Waiting for the signal pause(); printf("end\n"); }
I am also trying to understand how to use RUN_LVL parameter for graceful shutdown. Thanks for the link.
[Added code tags ~kshegunov]
Have written a C code for handling SIGTERM even here I am unable to catch sudo reboot / sudo poweroff, but my handler is able to catch kill -15 pid.
So what document/example are you working from for handling reboot/poweroff? Because there seems to be no evidence from your findings that it even sends a SIGTERM or whatever, or you aren't getting time to handle them, or other things are happening first! There must be posts on the web giving some suggested code for your situation?
-
Have written a C code for handling SIGTERM even here I am unable to catch sudo reboot / sudo poweroff, but my handler is able to catch kill -15 pid.
So what document/example are you working from for handling reboot/poweroff? Because there seems to be no evidence from your findings that it even sends a SIGTERM or whatever, or you aren't getting time to handle them, or other things are happening first! There must be posts on the web giving some suggested code for your situation?
@JNBarchan
Didn't get your question
So what document/example are you working from for handling reboot/poweroff?For evidence of which Signal I am receiving on sudo reboot / sudo poweroff the following message gets logged in syslog file
rosui rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="656" x-info="http://www.rsyslog.com"] exiting on signal 15.
-
@JNBarchan
Didn't get your question
So what document/example are you working from for handling reboot/poweroff?For evidence of which Signal I am receiving on sudo reboot / sudo poweroff the following message gets logged in syslog file
rosui rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="656" x-info="http://www.rsyslog.com"] exiting on signal 15.
@NarutoKun
I mean, IMHO, you need to be looking at some documentation/code which shows how to handle your shutdown in a C-type program. It's clear it's doing a lot more than just a simplekill -15
does, so you need something which explains what's going on and what your code needs to do to handle it. -
@JNBarchan
Didn't get your question
So what document/example are you working from for handling reboot/poweroff?For evidence of which Signal I am receiving on sudo reboot / sudo poweroff the following message gets logged in syslog file
rosui rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="656" x-info="http://www.rsyslog.com"] exiting on signal 15.
Excerpt from old code follows
void signal_handler(int sig) { // ... handle signal here, but have in mind a very limited number of functions are allowed } int main() { // set up the signal handler sigaction action; sigset_t blkMask; sigemptyset(&blkMask); sigaddset(&blkMask, SIGINT); sigaddset(&blkMask, SIGQUIT); sigaddset(&blkMask, SIGTERM); sigaddset(&blkMask, SIGCHLD); sigaddset(&blkMask, SIGALRM); signal(SIGPIPE, SIG_IGN); action.sa_handler = signal_handler; action.sa_mask = blkMask; action.sa_flags = 0; sigaction(SIGINT , &action, NULL); sigaction(SIGQUIT, &action, NULL); sigaction(SIGTERM, &action, NULL); sigaction(SIGCHLD, &action, NULL); // Terminates and main proc // ... rest of code ... }
My advice is to dedicate some time to read the POSIX documentation very carefully, it's paramount in this case.
For evidence of which Signal I am receiving on sudo reboot / sudo poweroff the following message gets logged in syslog file
rosui rsyslogd: [origin software="rsyslogd" swVersion="7.4.4" x-pid="656" x-info="http://www.rsyslog.com"] exiting on signal 15.This doesn't prove you handle that signal, only that it was sent to you. Open a file when starting the application and log the signal number when you enter the signal handler to your file. Again, be sure to use only the functions that are allowed in signal handlers.