Page 1 of 1

Handling POSIX Signals

Posted: Sat Jan 16, 2021 5:31 pm
by jdoolin
Is it possible to handle POSIX signals in Love2D? Such as SIGSTOP, SIGCONT, SIGTERM, etc?

I've seen a luaposix library, but I'm still not sure how I'd incorporate it into Love2D's callbacks.

Re: Handling POSIX Signals

Posted: Sat Jan 16, 2021 6:18 pm
by pgimeno
You can install and require luaposix, then just call its functions. Distribution is going to be a problem, though, as binary libraries need to be included for all POSIX platforms you intend to support. But if it's for your own use, no problem.

Re: Handling POSIX Signals

Posted: Sat Jan 16, 2021 8:37 pm
by jdoolin
pgimeno wrote: Sat Jan 16, 2021 6:18 pm You can install and require luaposix, then just call its functions. Distribution is going to be a problem, though, as binary libraries need to be included for all POSIX platforms you intend to support. But if it's for your own use, no problem.
Technically, the POSIX requirement is only for one platform, so I'd only need to build one binary library. How does one include and distribute those compiled libraries in the love project?

I'm also not clear on how I would get the game to receive the signal or register the handle. I try the code below and send the STOP signal with the kill command, but nothing happens. It just... well, stops just like it would when pressing ctrl-z.

Code: Select all

function pause(signo)
    print("Pause")
end

function love.load()
    M = require 'posix.signal'
    M.signal(SIGSTOP, pause)
end

Re: Handling POSIX Signals

Posted: Sun Jan 17, 2021 2:59 am
by pgimeno
jdoolin wrote: Sat Jan 16, 2021 8:37 pm Technically, the POSIX requirement is only for one platform, so I'd only need to build one binary library.
For which platform? Linux? Android? MacOS? iOS? All four can handle POSIX signals. Basically, all the platforms Löve supports except Windows.

jdoolin wrote: Sat Jan 16, 2021 8:37 pm How does one include and distribute those compiled libraries in the love project?
For Linux, they need to be available somewhere in package.cpath, and if they are inside a .love file, be copied to the save directory then loaded from there. For the other systems, I don't know.

jdoolin wrote: Sat Jan 16, 2021 8:37 pm I'm also not clear on how I would get the game to receive the signal or register the handle. I try the code below and send the STOP signal with the kill command, but nothing happens. It just... well, stops just like it would when pressing ctrl-z.
From signal(7): "The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored". If I trap SIGCONT and interrupt, then resume, it prints "Pause".

Re: Handling POSIX Signals

Posted: Sun Jan 17, 2021 5:35 am
by slime
What do you plan to use it for? Does the love.quit callback at least partially cover your use case?

Re: Handling POSIX Signals

Posted: Sun Jan 17, 2021 5:21 pm
by jdoolin
I'm under NDA and not sure if I can go into detail other than to say that it has been recommended, though not explicitly required, that my project be able to handle SIGSTOP, SIGCONT and SIGTERM.

However, I think in my case I may be able to go without handling those.
pgimeno wrote: Sun Jan 17, 2021 2:59 am For which platform? Linux? Android? MacOS? iOS? All four can handle POSIX signals. Basically, all the platforms Löve supports except Windows.
This is a Linux platform.
jdoolin wrote: Sat Jan 16, 2021 8:37 pm For Linux, they need to be available somewhere in package.cpath, and if they are inside a .love file, be copied to the save directory then loaded from there.
By save directory, you mean what would normally be ~/.love/game_id/ ?
jdoolin wrote: Sat Jan 16, 2021 8:37 pm From signal(7): "The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored". If I trap SIGCONT and interrupt, then resume, it prints "Pause".
Are you saying you tried the code sample with SIGCONT and it worked for you? I'll give that a try. Even though I may not need to worry abou this now, I'm still pretty curious.

Thanks for the help folks!

Re: Handling POSIX Signals

Posted: Sun Jan 17, 2021 6:05 pm
by pgimeno
jdoolin wrote: Sun Jan 17, 2021 5:21 pm By save directory, you mean what would normally be ~/.love/game_id/ ?
I mean whatever love.filesystem.getSaveDirectory() returns, or any subdirectory inside that. I think it defaults to ~/.local/share/love/game_id/ on Linux.

Note I haven't tried this, I've just seen it mentioned in this forum at some point.

jdoolin wrote: Sun Jan 17, 2021 5:21 pm Are you saying you tried the code sample with SIGCONT and it worked for you? I'll give that a try. Even though I may not need to worry abou this now, I'm still pretty curious.
Indeed, just changing your example to use SIGCONT instead of SIGSTOP:

Code: Select all

$ love11 .
^Z
[1]+  Stopped                 love11 .
$ fg
love11 .
Pause
$ 
"Pause" is printed by the handler.

Note SIGTSTP can also be handled, and that's the one received when you issue a ^Z. I've also tried handling it and it works.