juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Shutting down safely in a C++ program

Mon Feb 22, 2016 12:31 pm

HI everybody,

I have an C++ program that I am going to use as embedded project in a raspberry. It read sensor data and saves data in csv file.

The problem is that I do not know how I can shut down the raspberry pi in a secure mode. I am programming in C++ language and the program has to be use in undetermined time, so I can not program the shutting down automatically. Right now I am shutting down leaving the power supply directly, but I know that it is not the best mode, and this can corrupt the SD card.


Any help would be very appreciate.

User avatar
buja
Posts: 458
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Shutting down safely in a C++ program

Mon Feb 22, 2016 12:33 pm

The simplest way is by using the system function:

Code: Select all

system("sudo poweroff");

User avatar
AndyD
Posts: 2326
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: Shutting down safely in a C++ program

Mon Feb 22, 2016 1:09 pm

You can call reboot, see

Code: Select all

man 2 reboot.

Code: Select all

#include <stdio.h>
#include <unistd.h>
#include <sys/reboot.h>

int main(void)
{
    if (reboot(RB_POWER_OFF) == -1)
    {
        perror("reboot");
    }

    return 0;
}

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

Re: Shutting down safely in a C++ program

Mon Feb 22, 2016 1:35 pm

The reboot syscall is very brutal and immediately reboots your machine.

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

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Shutting down safely in a C++ program

Mon Feb 22, 2016 1:36 pm

In order to shutdown the computer you need permission to do so. If sudo requires a password, then using it will ask for a password (of course). It may not be convienient for you to be sitting there waiting to be asked.

One alternative is to make your program run as administrator, either by using sudo to start it or by making the executable "setuid", but then everything it does it does with superuser permissions. So bugs or hackers may cause havoc.

The alternative is to use capabilities which is a rather advanced topic but I think you'll want to use it. See "man 8 setcap" for a starting point.

The manual entry for reboot says
man 2 reboot wrote:The calling process has insufficient privilege to call reboot(); the CAP_SYS_BOOT capability is required.
So that's the one you need to give your program.

Edit: ghans says that reboot is too brutal. If it doesn't nicely stop all the processes then you could theoretically end up with a corrupted SD card. The capability system should give you the ability to use

Code: Select all

system("poweroff");     // note: there is no sudo
but in case it doesn't...

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4247
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Shutting down safely in a C++ program

Mon Feb 22, 2016 2:10 pm

If in doubt...
Obiwan Kenobi wrote: Use the source, Luke.
I think this is as close as I can find to the Raspbian version online. Obviously the actual thing is out there, I just don't know where to look.

Here is the source code for the shutdown command:
http://util-linux.sourcearchive.com/doc ... ource.html
There is a lot of complexity in there.

User avatar
buja
Posts: 458
Joined: Wed Dec 31, 2014 8:21 am
Location: Netherlands

Re: Shutting down safely in a C++ program

Mon Feb 22, 2016 2:16 pm

Pretty useful info here :!:

User avatar
liudr
Posts: 666
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Shutting down safely in a C++ program

Wed Feb 24, 2016 4:07 am

From what I read, op wants to tell his c program to shut down. I guess you can catch ctrl c exception and gracefully exit the program.

Maybe something like this
http://stackoverflow.com/questions/1641 ... -c-event-c

Maybe OP wants to catch sigkill instead.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

fsr
Posts: 88
Joined: Wed Jan 13, 2016 2:29 am

Re: Shutting down safely in a C++ program

Wed Feb 24, 2016 5:58 am

rurwin wrote:In order to shutdown the computer you need permission to do so. If sudo requires a password, then using it will ask for a password (of course). It may not be convienient for you to be sitting there waiting to be asked.
as an extension to this, you can avoid permissions by having a root cronjob run every minute (say) that looks for s specific file in a specific place and if it exists removes it then does a shutdown. your program then only has to be able to create a file which can be done anywhere it has write access to (tmp is a good place as it starts blank and so won't cause an immediate shutdown on startup if your file is still there)

grafin
Posts: 1
Joined: Fri Aug 17, 2018 6:30 am

Re: Shutting down safely in a C++ program

Fri Aug 17, 2018 7:42 am

I join the question - how correctly to execute program shutdown rpi from c++ program?
So that, for example, the destructors in the class were executed or correct stop main program?
If I call system("poweroff"), or system("shutdown now") the program stops in this line.
Most likely the main program is blocked and then receives a kill signal.
In example code after system("poweroff") will never be called:

Code: Select all

#include <iostream>
#include <cstdio>
int main() {
        std::freopen("/home/user/poweroff.log", "w", stdout);
        std::cout << "Before poweroff" << std::endl;
        system("poweroff");
        //never called!
        std::cout << "After poweroff" << std::endl;
        return 0;
}

But, if i do a deferred shutdown, main program ends correctly:

Code: Select all

#include <iostream>
#include <cstdio>
int main() {
        std::freopen("/home/user/poweroff.log", "w", stdout);
        std::cout << "Before poweroff" << std::endl;
        system("shutdown +1");
        //called!
        std::cout << "After poweroff" << std::endl;
        return 0;
}

The problem is that 1 minute is a big interval for me.
I tried to make a call in a separate thread, but it too is blocked.

rln
Posts: 154
Joined: Wed Apr 09, 2014 1:43 pm
Location: Sweden
Contact: Website

Re: Shutting down safely in a C++ program

Mon Aug 20, 2018 9:36 am

Another solution is to change OS to one that doesn't require safe shutdown at all. My Nard SDK project will survive just about any dirty power cuts and is designed for embedded systems.
http://www.nard.se/
Author of the robust Nard distro http://www.nard.se

Return to “Advanced users”

Who is online

Users browsing this forum: No registered users and 12 guests