git.y1.nz

fbui

Framebuffer-based graphical environment
download: https://git.y1.nz/archives/fbui.tar.gz
README | Files | Log | Refs

libfbui/Term/forkpty.c

      1 
      2 #include "fbterm.h"
      3 
      4 #ifndef HAVE_FORKPTY
      5 
      6 #include <stdio.h>
      7 #include <sys/types.h>
      8 #include <sys/stat.h>
      9 #include <fcntl.h>
     10 #include <unistd.h>
     11 #include <termios.h>
     12 /*#if GWINSZ_IN_SYS_IOCTL*/
     13 #include <sys/ioctl.h>
     14 /*#endif*/
     15 
     16 
     17 int forkpty_findtty (int* master_fd, int* slave_fd, char *name)
     18 {
     19 	int n;
     20 	char master_name[16];
     21 	char slave_name[16];
     22 
     23 	for (n=0; n<16; n++)
     24 	{
     25 		sprintf (master_name, "/dev/ptyp%x", n);
     26 		*master_fd = open (master_name, O_RDWR);
     27 		if (*master_fd >= 0) break;
     28 	}
     29 
     30 	if (n==16) return -1;
     31 
     32 	sprintf (slave_name, "/dev/ttyp%x", n);
     33 	sprintf (name, "/dev/ttyp%x", n);
     34 	*slave_fd = open (slave_name, O_RDWR);
     35 	if (*slave_fd < 0)
     36 	{
     37 		perror (slave_name);
     38 		close (*master_fd);
     39 		return -1;
     40 	}
     41 	
     42 	return 0;
     43 }
     44 
     45 
     46 
     47 
     48 pid_t forkpty (int *master_fd, char *name, struct termios *termp, struct winsize *winp)
     49 {
     50 	int slave_fd;
     51 	pid_t pid;
     52 	
     53 	debug (DEBUG_INFO, "entering custom forkpty\n");
     54 	if (forkpty_findtty (master_fd, &slave_fd, name) < 0)
     55 	{
     56 		fbtermError ("Couldn't open any tty.\n");
     57 		return -1;
     58 	}
     59 	
     60 	if (ioctl (*master_fd, TIOCSWINSZ, winp) < 0)
     61 	{
     62 		fbtermError ("Error setting window size.\n");
     63 	}
     64 	
     65 	debug (DEBUG_DETAIL, "Forking\n");
     66 	if ((pid = fork ()) == 0)
     67 	{
     68 		close (*master_fd);
     69 		
     70 		seteuid (getuid ());
     71 		setegid (getgid ());
     72 		setsid ();
     73 		
     74 		/* If we have TIOCSCTTY we use it to set our controlling tty */
     75 #ifdef TIOCSCTTY
     76 		ioctl (slave_fd, TIOCSCTTY, 0);
     77 #else
     78 		/* If not this should do: the first tty a process opens becomes
     79 		   its controlling tty */
     80 		debug (DEBUG_INFO, "Child: Closing %s\n", name);
     81 		close (slave_fd);
     82 		debug (DEBUG_INFO, "Child: Reopening %s\n", name);
     83 		slave_fd = open (name, O_RDWR);
     84 		if (slave_fd < 0)
     85 		{
     86 			perror (name);
     87 			fbtermError ("Child: Couldn't reopen %s\nExiting\n", name);
     88 			/* if it didn't work, exit noisily! */
     89 			exit (1);
     90 		}
     91 		else
     92 			debug (DEBUG_INFO, "Child: Reopened %s\n", name);
     93 #endif /* TIOCSCTTY */
     94 		
     95 		close (0);
     96 		close (1);
     97 		close (2);
     98 		
     99 		dup2 (slave_fd, 0);
    100 		dup2 (slave_fd, 1);
    101 		dup2 (slave_fd, 2);
    102 	}
    103 	else
    104 	{
    105 		close (slave_fd);
    106 		debug (DEBUG_INFO, "leaving custom forkpty\n");
    107 	}
    108 	return pid;
    109 }
    110 
    111 #endif /* HAVE_FORKPTY */

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.