/*
 * Compile and run:

        gcc signal-bug.c -Wall -lutil -lpthread -o signal-bug; ./signal-bug

 * Under kernel 2.6.5 and previous, this produces the output 'Completed.'
 * Under kernel 2.6.6 and later, this produces the output 'Died.'
 */

#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <pty.h>

void* tfunc(void* arg) {
  sleep(1);
  return 0;
}

void die(int ignored) {
  printf("Died.\n");
  _exit(1);
}

int main(int argc, char** argv) {
  char out[128];
  pthread_attr_t attr;
  pthread_t thread;
  signal(SIGHUP, die);
  int masterfd;
  
  if (!forkpty(&masterfd, NULL, NULL, NULL)) {
    pthread_attr_init(&attr);
    pthread_create(&thread, &attr, tfunc, NULL);
    sleep(2);
    printf("Completed.\n");
    return 0; 
  } else {
    out[read(masterfd, out, 1024)] = '\0';
    printf(out);
    return 0;    
  }
}
