Shut Pi Down From Xojo APP

Hello Everyone,

Usual issues with linux, nothing seems easy or simple to achieve.

my project has one function, to shutdown the Pi from an Xojo created application.

at the terminal i would use:-

sudo shutdown -h now

this works as expected and the Pi goes into shutdown.

does anyone know how i might use a GUI app (that normally runs on the Pi as Kiosk) and receives serial 232 data messages from a hardware controller to launch the terminal and perform what seems a very simple task of shutting down.

i have a console app that i can launch when the GUI app commands but i can not get it to shut the Pi down.

does anyone have any ideas as to what i might do, thank you in advance.

Would issuing a “shutdown” command from a shell do the job? The app would presumably have to run with elevated priviledges. Essentially issuing the same command as you use in the terminal, but through a Xojo shell.

I tried to make a couple of declares, and I get the following error:

An exception of class FunctionNotFoundException was not handled. The application mus shut down.

Can a declare be created on the Raspberry Pi? Here is my code that doesn’t work:

[code]Sub Action() Handles Action
Const LINUX_REBOOT_MAGIC1 = &Hfee1dead
Const LINUX_REBOOT_MAGIC2 = 672274793
Const LINUX_REBOOT_CMD_POWER_OFF = &H4321fedc

'Declare Function REBOOT Lib “libc” (magic as Integer, magic2 as Integer, cmd as Integer, arg as Ptr) as Integer
Declare Function REBOOT Lib “libc” (cmd as Integer) as Integer

Dim P as Ptr

'call REBOOT(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_POWER_OFF,p)
call REBOOT(LINUX_REBOOT_CMD_POWER_OFF)
End Sub
[/code]

This was placed in a Pushbutton action event.

Hello Louis, i have a working ‘shell’ application that when run from a terminal shows me the PRINT statement containing the phrase ‘sudo shutdown -h now’ text in the terminal at the prompt.
nothing happens except that text is echoed in the terminal, if i double click on the application nothing happens.

quite obviously i am doing a whole heap of wrong and have no idea what is going on, sadly.

Eugene, what you suggest is way beyond anything i have even thought of, i am stumped as to why this is so difficult.

i am quite happy to call a python script (somehow) if that works, i don’t care if its xojo code, just that it does this seemingly extremely simple job…

You must find a way to actually elevate to root permissions in your shell. Using ‚sudo‘ requires the user to type in a password…

A quick search brought up this post by Tim Jones:

[quote]here’s the way I get around the
hack potential for passing the password to the shell process:

theShell.Execute “echo " + thePW + " | sudo -S /usr/bin/true”
theShell.Execute "sudo " + theCommand
theShell.Execute “sudo -K”

Note that the call to /usr/bin/bin/true simply sets up the sudo clock
so that the next call doesn’t require the password. Also, the call
to /usr/bin/true happens so quickly that it doesn’t stay around long
enough for someone to grab the password from the process list. The
last command kills the sudo clock.

The only limitation with that mechanism is that the current user must
be an admin user.[/quote]

hello Oliver,

thanks for that, i did all sorts of searches and came up with absolutely nothing with the search terms i used, i even started to go through every raspberry post but gave up too soon obviously.

i will test this tonight and report back if it does what i hope so the next person who comes along finds it more easily.

the project is never connected to a network and will never be needing any form of write to the SD card, i am going to make it read only when i get the image finished, its literally just being used as a screen driver in an embedded system so security is no issue at all.

With thanks to all replies i ended up with this easy solution, largely based upon the example in ‘Shell’ from the language reference.
I was confusing ‘Console’ and ‘Shell’ when searching for information in both the forum and language reference.

the following is called when the Pi application receives on its serial port the phrase ‘pishutdown’ which is produced when a button on the external hardware is pressed:-

  if data="pishutdown" then
    
    dim s as new Shell
    dim cmd as string
    
    cmd="sudo shutdown -h now"
    
    s.Execute(cmd)
    if s.ErrorCode<>0 then
      MsgBox("Error:- " + Str(s.ErrorCode))
    end
    
  end