How come I can't re-acquire the controlling terminal here
Code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
void reterm(void)
{
pid_t pid;
if((pid = fork()) < 0) {
fprintf(stderr, "can't fork\n");
exit(1);
}
if(pid !=0)
exit(1);
if(setsid() < 0) {
fprintf(stderr, "setsid error\n");
}
}
int main(void)
{
int fd;
reterm();
if((fd = open("/dev/null", O_RDONLY | O_NOCTTY)) < 0) {
fprintf(stderr, "can't open tty\n");
exit(1);
}
sleep(5000);
close(fd);
return 0;
}
[cd@localhost oakland]$ gcc -Wall reterm.c -o reterm
[cd@localhost oakland]$ ./reterm
[cd@localhost oakland]$ ps waux | grep reterm
cd 25021 0.0 0.0 1484 156 ? Ss 17:36 0:00 ./
reterm
cd 25031 0.0 0.1 3880 676 pts/3 R+ 17:36 0:00 grep
reterm
[cd@localhost oakland]$