Skip to content

Instantly share code, notes, and snippets.

@rhpk
Created January 21, 2015 15:35
Show Gist options
  • Save rhpk/861e5970044d7ade997a to your computer and use it in GitHub Desktop.
Save rhpk/861e5970044d7ade997a to your computer and use it in GitHub Desktop.
Controlling tty stealing toy code
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
int main(int argc, char *argv[]) {
int status;
int consoledev_fd;
if(fork()) {
printf("Waiting for child to terminate\n");
wait(&status);
printf("Exiting.\n");
return 0;
}
else {
printf("I'm child. Closing std{in|out|err} and trying to steal the terminal!\n");
close(0); close(1); close(2);
setsid();
consoledev_fd = open("/dev/pts/17", O_RDWR|O_NOCTTY);
ioctl(consoledev_fd, TIOCSCTTY, 1);
sleep(45);
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment