njsss
Posts: 35
Joined: Fri May 20, 2016 9:36 pm

How to safely exit my program before KEY_POWER shutdown?

Mon Feb 25, 2019 4:10 pm

The gpio-shutdown overlay shuts down the system, but how can I monitor this KEY_POWER event and safely shuts down my own program (save import files) before the system shutdown?

User avatar
thagrol
Posts: 4038
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How to safely exit my program before KEY_POWER shutdown?

Mon Feb 25, 2019 7:14 pm

Given that AIUI all the overlay does is send a KEY_POWER event you'd do it the same way you'd trap a clean shutdown by any other means: trap a few signals and act on them.

The exact way you do that depends on which language your programme is written in.
Arguing with strangers on the internet since 1993.

njsss
Posts: 35
Joined: Fri May 20, 2016 9:36 pm

Re: How to safely exit my program before KEY_POWER shutdown?

Mon Feb 25, 2019 7:28 pm

thagrol wrote: Given that AIUI all the overlay does is send a KEY_POWER event you'd do it the same way you'd trap a clean shutdown by any other means: trap a few signals and act on them.

The exact way you do that depends on which language your programme is written in.
Does it mean I won't be able to act upon the KEY_POWER event? If so can I register a system event and act on it, so I don't have to constantly monitor the gpio pin. I use c++ but has limited knowledge about Linux.

User avatar
thagrol
Posts: 4038
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: How to safely exit my program before KEY_POWER shutdown?

Mon Feb 25, 2019 8:37 pm

njsss wrote:
Mon Feb 25, 2019 7:28 pm
thagrol wrote: Given that AIUI all the overlay does is send a KEY_POWER event you'd do it the same way you'd trap a clean shutdown by any other means: trap a few signals and act on them.

The exact way you do that depends on which language your programme is written in.
Does it mean I won't be able to act upon the KEY_POWER event? If so can I register a system event and act on it, so I don't have to constantly monitor the gpio pin. I use c++ but has limited knowledge about Linux.
Can't help you with C++, I don't know it. My understanding is that when the switch is pressed the kernel sees a KEY_POWER event which triggers the normal shutdown process. You shouldn't need to monitor the gpio yourself, just have your programme trap and respond to a SIGTERM (I think it's SIGTERM, google will probably know).
Arguing with strangers on the internet since 1993.

User avatar
DougieLawson
Posts: 40567
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: How to safely exit my program before KEY_POWER shutdown?

Tue Feb 26, 2019 2:54 am

Normal shutdown sends SIGTERM to every process. If the process doesn't end normally then 90s later it sends SIGKILL.

You can trap SIGTERM with a C++ signal handler. You can't trap SIGKILL.

https://www.tutorialspoint.com/cplusplu ... ndling.htm gives you an example of trapping signals.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

ghans
Posts: 7883
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: How to safely exit my program before KEY_POWER shutdown?

Tue Feb 26, 2019 6:16 am

You need to write a signal handler for your program. SIGTERM is the one you should take care of first.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

njsss
Posts: 35
Joined: Fri May 20, 2016 9:36 pm

Re: How to safely exit my program before KEY_POWER shutdown?

Tue Feb 26, 2019 7:35 pm

I tried to implement a simple test in both c++ and python, but couldn't trap SIGTERM. Shutdown happens immediately either by pull the shutdown-pin down or just type "sudo shutdown now".

Return to “Device Tree”