lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On Thu, Sep 13, 2018 at 11:59 AM William Ahern <william@25thandclement.com> wrote:
On Wed, Sep 12, 2018 at 12:30:58PM -0700, Russell Haley wrote:
<snip>
> What I don't know/understand yet if it's possible to run/control a process
> from Lua so that I can grab stdin/stdout? I am hoping there is something in
> cqueues to do this, but once again thought I'd send out a general request
> for ideas.

cqueues can be used to concurrently wait on raw descriptor events and
signals. But for low-level access to Unix process control APIs you need
something like luaposix or my lunix module.

The following regression script in lunix tests the behavior of PTYs and
process groups.

  https://github.com/wahern/lunix/blob/master/regress/0-ctty-sighup.lua

Specifically, it checks that SIGHUP is automatically sent to all the child
processes when the session leader dies, which is what should happen when the
controlling terminal is properly configured. (TTYs and process management
are intricately related because the evolution of job control in Unix made
the TTY driver responsible for sending various signals. See
http://www.linusakesson.net/programming/tty/)

The script shows how to use fork(2), pipe(2), and various process management
APIs exposed by lunix. Translating the I/O, signal, and timeout handling to
cqueues should [hopefully] be fairly straight-forward.

Normally you don't need to worry about PTYs and sessions if you're simply
trying to invoke a helper program and process stdin/stdout. But if you're
running as a daemon then its useful so that child processes are
automatically killed if the daemon crashes or terminates, especially if the
child process isn't in a simple loop reading stdin and writing to stdout
such that it would quickly receive EOF or SIGPIPE when the parent dies.

Thanks Bill. One of the things I thought of doing with cqueues was create an init/processes monitor, or augment one with Lua (https://github.com/mheily/jobd) so I appreciate this information. Not that I have any business writing init systems. :-/

Russ