[squeak-dev] Is there a way to quit/exit Squeak with a return code that can be tested in a .so file?

Louis LaBrunda Lou at Keystone-Software.com
Thu May 4 13:38:12 UTC 2017


Hi Eliot,

>Hi Louis,
>    why not run the Smalltalk image as root and use the FFI to invoke the reboot system call?

I had already done something similar where I invoke the shutdown command from within Squeak. It
worked but I didn't like the way it worked.  Seaside has its own "flow control" and I want to
let if have a chance to show the logoff screen, if I make the shutdown call without any delay,
that doesn't happen.  The GPIO interface has termination code that it wants to go through and
again the shutdown call without any delay doesn't allow time for the termination code to
execute.  That may not matter as everything should be reset on the next boot but it offends my
sensibilities.

The shutdown command does have a delay option.  The documentation I have seen claims the delay
interval to be in seconds but on Raspbian Jessie Lite it is in minutes.  I assume that is a bug
but that is the way it is.  I could live with a 1 minute delay before shutdown or reboot but I
wanted a better way.

I already had a simple Bash script to start the Squeak program for a few reasons.  I needed to
fire up the GPIO interface with:

sudo systemctl start pigpiod

I also needed to tell Squeak to run headless and to run a smalltalk script to start the Seaside
code up.  So, getting Squeak to exit with a return code and with what turned out to be a fairly
simple Bash script, I got everything I wanted.

I hesitate to bash (sorry I couldn't resist, Tim will probably like that) open source code like
Bash but it was the hardest part to get right.  I'm no Bash or Linux expert and I will rant
here a little in the hope it may help others.  There are a lot of ways to compare integers in
Bash, like =, == and -eq but you have to do it within the context of things like: [ $ret -eq 1
] or $(()) and others.  It is hard to get it all right.  Setting the variable without a $ and
having to reference it with a $ is also confusing.  Not (clearly and easily) knowing which
values are strings and which are integers is also confusing.  UNIX was developed on very tiny
machines, so I understand not wanting to do a lot to help the programmer back then.  We needed
to just get it right.  But today I don't see why comparing an integer and a string wouldn't
result in converting the string to an integer and then doing the comparison.  If they are
equal, fine if not or if the conversion fails, they are not equal, done.  I know, < or >
complicates it to.

Thanks, everyone for the help.

Lou

>Here's a snippet that /doesn't/ invoke the system call directly :-)
>
>https://www.raspberrypi.org/forums/viewtopic.php?t=85815&p=605954
>
>_,,,^..^,,,_ (phone)
>
>> On May 3, 2017, at 2:35 PM, Louis LaBrunda <Lou at Keystone-Software.com> wrote:
>> 
>> Hi Dave,
>> 
>> Thanks for checking this for me.  Sorry it took me so long to get back to this.  I had to set
>> up a new Raspberry and Samba and Apache2 gave me a lot of trouble, not to mentions the Bash
>> script.
>> 
>> I did know about Smalltalk>snapshot:andQuitWithExitCode: when I posted about
>> Smalltalk>snapshot:andQuit:withExitCode:embedded:, I just wanted to point everyone to the
>> method that did the work.
>> 
>> Big picture:  I'm working on a Squeak/Seaside program that controls four relays.  I gave my old
>> Raspberry Pi (model 1) to a friend to open and close his garage doors.  The Seaside parts
>> displays a web page (login, relay page and settings) in any web browser.  With a hole in his
>> router, he can get to it from anywhere, even with his iPhone.
>> 
>> You may remember me asking about how to shutdown or reboot from within Squeak.  I wanted to be
>> able to reboot to clean up a problem or shutdown if you wanted to turn off the power without
>> just pulling the plug while things were running.  I had some timing problems with that and had
>> to use a delay options in the shutdown call.  The problem with that is that you couldn't wait
>> less than a minute.  There is a call that says it could wait n number of seconds but it waited
>> n minutes.  Looks like a bug to me.  Anyway, waiting a minute was too long.  Since I was
>> already using a script to start pigpiod and Squeak, I decided to remove the shutdown/reboot
>> calls from within my program and move them out to the Bash script.  The final version of the
>> script is below.  I know Linux is free open source and I shouldn't complain about free stuff
>> but what a mess of a scripting language.  Too many ways to do the same thing.  All too easily
>> mixed together in ways that won't work.
>> 
>> This is the final script.  It works and allows the user to exit the Squeak program and
>> optionally shutdown or reboot.  The script is automatically on boot.
>> 
>> #!/bin/bash
>> sudo systemctl start pigpiod
>> 
>> squeak -vm-display-null /usr/share/RaspberryRelay/RasRelay.image RasRelay.St
>> ret=$?
>> 
>> if [ $ret -eq 1 ];
>> then
>> sudo reboot
>> elif [ $ret -eq 2 ];
>> then
>> sudo halt
>> fi
>> 
>> 
>> 
>> 
>> 
>>> On Fri, 28 Apr 2017 20:05:46 -0400, "David T. Lewis" <lewis at mail.msen.com> wrote:
>>> 
>>> Confirming: Yes it works with the latest Spur VM and Squeak trunk image.
>>> 
>>> From a console window, run Squeak in the foreground (no '&' at the end of the command):
>>> 
>>> $ spur64 my64BitImage
>>> 
>>> Then in the image, do e.g. "Smalltalk snapshot: false andQuitWithExitCode: 3"
>>> to quit without saving and return exit status 3.
>>> 
>>> After the image exits, test the exit status that was returned to the shell:
>>> 
>>> $ echo $?
>>> 3
>>> 
>>> 
>>> Dave
>>> 
>>> 
>>>> On Fri, Apr 28, 2017 at 01:13:38PM -0400, Louis LaBrunda wrote:
>>>> Hi Guys,
>>>> 
>>>> Thanks for the reply's.  I found:
>>>> 
>>>> snapshot: save andQuit: quit withExitCode: exitCode embedded: embeddedFlag
>>>> 
>>>> which looks like it will do what I want.  I will give it a try and report back.
>>>> 
>>>> Lou
>>>> 
>>>> 
>>>>> On Fri, 28 Apr 2017 18:43:59 +0200, Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com> wrote:
>>>>> 
>>>>> 2017-04-28 18:36 GMT+02:00 Nicolas Cellier <
>>>>> nicolas.cellier.aka.nice at gmail.com>:
>>>>> 
>>>>>> 
>>>>>> 
>>>>>> 2017-04-28 17:48 GMT+02:00 David T. Lewis <lewis at mail.msen.com>:
>>>>>> 
>>>>>>>> Hi Squeakers,
>>>>>>>> 
>>>>>>>> Is there a way to quit/exit Squeak with a return code that can be tested
>>>>>>>> in a .so file?
>>>>>>>> 
>>>>>>> 
>>>>>>> I think I remember implementing this a long time ago in the Unix VM. I
>>>>>>> can't really look further right now but I'll check later and try to give a
>>>>>>> better answer.
>>>>>>> 
>>>>>>> IIRC, primitiveQuit can take an optional argument that is passed back to
>>>>>>> the shell script that in turn gives you an exit code that you can test.
>>>>>>> This probably is working right now in the interpreter VM (sorry I can't
>>>>>>> check it at the moment). I'm not sure if it's in Cog/Spur, but if not it
>>>>>>> can be easily added.
>>>>>>> 
>>>>>>> Dave
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>>> 
>>>>>> Answer is yes, if you browse the code on github,
>>>>>> https://raw.githubusercontent.com/OpenSmalltalk/
>>>>>> opensmalltalk-vm/Cog/src/vm/cointerp.c
>>>>>> then you will see this:
>>>>>> 
>>>>>> /* 113 */ primitiveQuit,
>>>>>> 
>>>>>> ... snip...
>>>>>> 
>>>>>>    /* InterpreterPrimitives>>#primitiveQuit */
>>>>>> static void
>>>>>> primitiveQuit(void)
>>>>>> {   DECL_MAYBE_SQ_GLOBAL_STRUCT
>>>>>>    ioExitWithErrorCode((GIV(argumentCount) == 1
>>>>>>        ? ((longAt(GIV(stackPointer))) >> 1)
>>>>>>        : 0));
>>>>>> }
>>>>>> 
>>>>>> 
>>>>>> So you can pass a SmallInteger to the primitive 113
>>>>>> 
>>>>>> Then to know what the platform will do with this error code, just query it
>>>>>> thru github web iface:
>>>>>> 
>>>>>> https://github.com/OpenSmalltalk/opensmalltalk-vm/search?utf8=%E2%9C%93&q=
>>>>>> ioExitWithErrorCode&type=
>>>>>> 
>>>>>> Sorry, I wanted to post that all supported platforms would call
>>>>> exit(errorCode), but I'm bad with web iface shortcuts...
>>>>> <rant>
>>>>> The windows squeak spur VM/image fixture insist on having shortcuts thru
>>>>> ALT instead of CTRL which certainly DOES NOT HELP!!!
>>>>> </rant>
>>>> -- 
>>>> Louis LaBrunda
>>>> Keystone Software Corp.
>>>> SkypeMe callto://PhotonDemon
>>>> 
>>>> 
>>> 
>> -- 
>> Louis LaBrunda
>> Keystone Software Corp.
>> SkypeMe callto://PhotonDemon
>> 
>> 
-- 
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon



More information about the Squeak-dev mailing list