joyent/libuv — pull request #934 by jacobgroundwater (2013-09-21 04:29:25+00:00)
Title: Add Flag to Spawn Child Process with New Process Group ID but the Same Session ID

I am working on a primitive shell in node that has full-fledged job-control. Proper job control requires being able to spawn child processes with the same session id, but in a new process group. Currently libuv only supports flagging a child for complete detachment via `UV_PROCESS_DETACHED`.

A bit on TTYs and why this is necessary. I apologize if this is all familiar.

## Background

Whenever you log into a shell, such as bash, there is always a controlling TTY for the session. That controlling TTY is just a file descriptor for a character device e.g. `/dev/console`. The reason you see the output from a command like `ls` is because after bash forks, it keeps STDOUT/STDERR attached to the controlling TTY before calling `exec` on `ls`. Thus the TTY is being shared by almost every process that is a direct or indirect child of your login session.

In order to calm the madness, the TTY drive has the concept of a foreground process group. Only processes in the foreground process group are allowed to read and write to the TTYs character device. If a background process tries to read or write to the TTY it will be sent `SIGTTIN` or `SIGTTOUT`. The `SIGTTOUT` signal can be ignored, but ignoring `SIGTTIN` will result in an `EIO` read error.

The foreground process group also lets the line-discipline decide which processes should be sent control signals like `SIGINT` whenever it processes a `^C` character.

## Problem

The problem is that the system call ([tcsetpgrp][1]) for setting foreground process groups can *only* be done on process groups in the same session. Thus using `UV_PROCESS_DETACHED` will cause `tcsetpgrp` to error.

[1]: http://linux.die.net/man/3/tcsetpgrp