According to the POSIX specification, tcsetpgrp can cause SIGTTOU to be sent to the group of the calling process if it is a background process.

However I can't understand if in such case the foreground group is changed.

Also, if the foreground group is actually changed despite the signal generation, I wonder what happens to the session and to the terminal if the new foreground group is the one that is going to receive the SIGTTOU.

up vote 1 down vote accepted

TL:DR:

No the foreground group does not change. This makes sense since the signal is supposed to be sent when the process is changing a setting on the terminal -- an output operation. The signal would also not be delivered to the process (now the foreground group) if the change succeeded, because then it could get stuck without someone to send SIGCONT.

Longer answer:

A simple example:

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <signal.h>


void sig(int signo) {
    const char* msg = strsignal(signo); // XXX: Not async-signal-safe.
    write(STDOUT_FILENO, msg, strlen(msg));
    write(STDOUT_FILENO, "\n", 1);
}

int main() {
    char cntl_tty[L_ctermid];
    ctermid(cntl_tty);

    signal(SIGTTOU, sig);
    signal(SIGCONT, sig);

    int fd = open(cntl_tty, O_RDONLY);
    if (fd == -1) {
        perror("open");
        exit(1);
    }
    if (tcsetpgrp(fd, getpgrp()) == -1) {
        perror("tcsetpgrp");
    } else {
        puts("foregrounded");
    }
    return 0;
}

When this code is started as a background process and SIGTTOU is handled, this loops forever printing that the signal is received. The perror is never called, which implies that the kernel restarts the system call. Sending SIGCONT does not matter. The foregrounding never succeeds. However when foregrounding the code through the shell, "foregrounded" is printed as expected.

When the signal disposition for SIGTTOU is changed to SIG_IGN, "foregrounded" is printed immediately.

  • 1
    Thanks, clear and nice answer... However I wonder if the perror is not reached just because of the print int the signal handler: after all printing from a background process should produce SIGTTOU, which would cause the handler to restart, printing again, causing a new SITTOU... in an infinite loop. – Giacomo Tesio Dec 13 '17 at 22:09
  • Good point, I hadn't thought of that. I tried it again, removing the prints, leaving an empty signal handler body, and the behavior remains the same. It is kind of odd that the print occurred at all, since it should've failed with I suppose EINTR. – jared_schmitz Dec 15 '17 at 18:10
  • D'oh, that's the shell writing "Stopped (tty output)", nevermind. =) – jared_schmitz Dec 15 '17 at 18:17

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.