⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sys_term.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 3 页
字号:
}#if	defined (AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)	inttty_setraw(on){#  ifndef USE_TERMIO	if (on)		termbuf.sg.sg_flags |= RAW;	else		termbuf.sg.sg_flags &= ~RAW;#  else	if (on)		termbuf.c_lflag &= ~ICANON;	else		termbuf.c_lflag |= ICANON;#  endif}#endif	voidtty_binaryin(on)	int on;{#ifndef	USE_TERMIO	if (on)		termbuf.lflags |= LPASS8;	else		termbuf.lflags &= ~LPASS8;#else	if (on) {		termbuf.c_iflag &= ~ISTRIP;	} else {		termbuf.c_iflag |= ISTRIP;	}#endif}	voidtty_binaryout(on)	int on;{#ifndef	USE_TERMIO	if (on)		termbuf.lflags |= LLITOUT;	else		termbuf.lflags &= ~LLITOUT;#else	if (on) {		termbuf.c_cflag &= ~(CSIZE|PARENB);		termbuf.c_cflag |= CS8;		termbuf.c_oflag &= ~OPOST;	} else {		termbuf.c_cflag &= ~CSIZE;		termbuf.c_cflag |= CS7|PARENB;		termbuf.c_oflag |= OPOST;	}#endif}	inttty_isbinaryin(){#ifndef	USE_TERMIO	return(termbuf.lflags & LPASS8);#else	return(!(termbuf.c_iflag & ISTRIP));#endif}	inttty_isbinaryout(){#ifndef	USE_TERMIO	return(termbuf.lflags & LLITOUT);#else	return(!(termbuf.c_oflag&OPOST));#endif}#ifdef	LINEMODE	inttty_isediting(){#ifndef USE_TERMIO	return(!(termbuf.sg.sg_flags & (CBREAK|RAW)));#else	return(termbuf.c_lflag & ICANON);#endif}	inttty_istrapsig(){#ifndef USE_TERMIO	return(!(termbuf.sg.sg_flags&RAW));#else	return(termbuf.c_lflag & ISIG);#endif}	voidtty_setedit(on)	int on;{#ifndef USE_TERMIO	if (on)		termbuf.sg.sg_flags &= ~CBREAK;	else		termbuf.sg.sg_flags |= CBREAK;#else	if (on)		termbuf.c_lflag |= ICANON;	else		termbuf.c_lflag &= ~ICANON;#endif}	voidtty_setsig(on)	int on;{#ifndef	USE_TERMIO	if (on)		;#else	if (on)		termbuf.c_lflag |= ISIG;	else		termbuf.c_lflag &= ~ISIG;#endif}#endif	/* LINEMODE */	inttty_issofttab(){#ifndef	USE_TERMIO	return (termbuf.sg.sg_flags & XTABS);#else# ifdef	OXTABS	return (termbuf.c_oflag & OXTABS);# endif# ifdef	TABDLY	return ((termbuf.c_oflag & TABDLY) == TAB3);# endif#endif}	voidtty_setsofttab(on)	int on;{#ifndef	USE_TERMIO	if (on)		termbuf.sg.sg_flags |= XTABS;	else		termbuf.sg.sg_flags &= ~XTABS;#else	if (on) {# ifdef	OXTABS		termbuf.c_oflag |= OXTABS;# endif# ifdef	TABDLY		termbuf.c_oflag &= ~TABDLY;		termbuf.c_oflag |= TAB3;# endif	} else {# ifdef	OXTABS		termbuf.c_oflag &= ~OXTABS;# endif# ifdef	TABDLY		termbuf.c_oflag &= ~TABDLY;		termbuf.c_oflag |= TAB0;# endif	}#endif}	inttty_islitecho(){#ifndef	USE_TERMIO	return (!(termbuf.lflags & LCTLECH));#else# ifdef	ECHOCTL	return (!(termbuf.c_lflag & ECHOCTL));# endif# ifdef	TCTLECH	return (!(termbuf.c_lflag & TCTLECH));# endif# if	!defined(ECHOCTL) && !defined(TCTLECH)	return (0);	/* assumes ctl chars are echoed '^x' */# endif#endif}	voidtty_setlitecho(on)	int on;{#ifndef	USE_TERMIO	if (on)		termbuf.lflags &= ~LCTLECH;	else		termbuf.lflags |= LCTLECH;#else# ifdef	ECHOCTL	if (on)		termbuf.c_lflag &= ~ECHOCTL;	else		termbuf.c_lflag |= ECHOCTL;# endif# ifdef	TCTLECH	if (on)		termbuf.c_lflag &= ~TCTLECH;	else		termbuf.c_lflag |= TCTLECH;# endif#endif}	inttty_iscrnl(){#ifndef	USE_TERMIO	return (termbuf.sg.sg_flags & CRMOD);#else	return (termbuf.c_iflag & ICRNL);#endif}/* * A table of available terminal speeds */struct termspeeds {	int	speed;	int	value;} termspeeds[] = {	{ 0,     B0 },    { 50,    B50 },   { 75,    B75 },	{ 110,   B110 },  { 134,   B134 },  { 150,   B150 },	{ 200,   B200 },  { 300,   B300 },  { 600,   B600 },	{ 1200,  B1200 }, { 1800,  B1800 }, { 2400,  B2400 },	{ 4800,  B4800 }, { 9600,  B9600 }, { 19200, B9600 },	{ 38400, B9600 }, { -1,    B9600 }};	voidtty_tspeed(val)	int val;{	register struct termspeeds *tp;	for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)		;	cfsetospeed(&termbuf, tp->value);}	voidtty_rspeed(val)	int val;{	register struct termspeeds *tp;	for (tp = termspeeds; (tp->speed != -1) && (val > tp->speed); tp++)		;	cfsetispeed(&termbuf, tp->value);}#if	defined(CRAY2) && defined(UNICOS5)	inttty_isnewmap(){	return((termbuf.c_oflag & OPOST) && (termbuf.c_oflag & ONLCR) &&			!(termbuf.c_oflag & ONLRET));}#endif#ifdef PARENT_DOES_UTMP# ifndef NEWINITextern	struct utmp wtmp;extern char wtmpf[];# else	/* NEWINIT */int	gotalarm;	/* ARGSUSED */	voidnologinproc(sig)	int sig;{	gotalarm++;}# endif	/* NEWINIT */#endif /* PARENT_DOES_UTMP */#ifndef	NEWINIT# ifdef PARENT_DOES_UTMPextern void utmp_sig_init P((void));extern void utmp_sig_reset P((void));extern void utmp_sig_wait P((void));extern void utmp_sig_notify P((int));# endif /* PARENT_DOES_UTMP */#endif/* * getptyslave() * * Open the slave side of the pty, and do any initialization * that is necessary.  The return value is a file descriptor * for the slave side. */	intgetptyslave(){	register int t = -1;#if	!defined(CRAY) || !defined(NEWINIT)# ifdef	LINEMODE	int waslm;# endif# ifdef	TIOCGWINSZ	struct winsize ws;	extern int def_row, def_col;# endif	extern int def_tspeed, def_rspeed;	/*	 * Opening the slave side may cause initilization of the	 * kernel tty structure.  We need remember the state of	 * 	if linemode was turned on	 *	terminal window size	 *	terminal speed	 * so that we can re-set them if we need to.	 */# ifdef	LINEMODE	waslm = tty_linemode();# endif	/*	 * Make sure that we don't have a controlling tty, and	 * that we are the session (process group) leader.	 */# ifdef	TIOCNOTTY	t = open(_PATH_TTY, O_RDWR);	if (t >= 0) {		(void) ioctl(t, TIOCNOTTY, (char *)0);		(void) close(t);	}# endif# ifdef PARENT_DOES_UTMP	/*	 * Wait for our parent to get the utmp stuff to get done.	 */	utmp_sig_wait();# endif	t = cleanopen(line);	if (t < 0)		fatalperror(net, line);#ifdef  STREAMSPTY#ifdef	USE_TERMIO	ttyfd = t;#endif	if (ioctl(t, I_PUSH, "ptem") < 0) 		fatal(net, "I_PUSH ptem");	if (ioctl(t, I_PUSH, "ldterm") < 0)		fatal(net, "I_PUSH ldterm");	if (ioctl(t, I_PUSH, "ttcompat") < 0)		fatal(net, "I_PUSH ttcompat");	if (ioctl(pty, I_PUSH, "pckt") < 0)		fatal(net, "I_PUSH pckt");#endif	/*	 * set up the tty modes as we like them to be.	 */	init_termbuf();# ifdef	TIOCGWINSZ	if (def_row || def_col) {		bzero((char *)&ws, sizeof(ws));		ws.ws_col = def_col;		ws.ws_row = def_row;		(void)ioctl(t, TIOCSWINSZ, (char *)&ws);	}# endif	/*	 * Settings for sgtty based systems	 */# ifndef	USE_TERMIO	termbuf.sg.sg_flags |= CRMOD|ANYP|ECHO|XTABS;# endif	/* USE_TERMIO */	/*	 * Settings for UNICOS (and HPUX)	 */# if defined(CRAY) || defined(__hpux)	termbuf.c_oflag = OPOST|ONLCR|TAB3;	termbuf.c_iflag = IGNPAR|ISTRIP|ICRNL|IXON;	termbuf.c_lflag = ISIG|ICANON|ECHO|ECHOE|ECHOK;	termbuf.c_cflag = EXTB|HUPCL|CS8;# endif	/*	 * Settings for all other termios/termio based	 * systems, other than 4.4BSD.  In 4.4BSD the	 * kernel does the initial terminal setup.	 */# if defined(USE_TERMIO) && !(defined(CRAY) || defined(__hpux)) && (BSD <= 43)#  ifndef	OXTABS#   define OXTABS	0#  endif	termbuf.c_lflag |= ECHO;	termbuf.c_oflag |= ONLCR|OXTABS;	termbuf.c_iflag |= ICRNL;	termbuf.c_iflag &= ~IXOFF;# endif /* defined(USE_TERMIO) && !defined(CRAY) && (BSD <= 43) */	tty_rspeed((def_rspeed > 0) ? def_rspeed : 9600);	tty_tspeed((def_tspeed > 0) ? def_tspeed : 9600);# ifdef	LINEMODE	if (waslm)		tty_setlinemode(1);# endif	/* LINEMODE */	/*	 * Set the tty modes, and make this our controlling tty.	 */	set_termbuf();	if (login_tty(t) == -1)		fatalperror(net, "login_tty");#endif	/* !defined(CRAY) || !defined(NEWINIT) */	if (net > 2)		(void) close(net);#if	defined(AUTHENTICATION) && defined(NO_LOGIN_F) && defined(LOGIN_R)	/*	 * Leave the pty open so that we can write out the rlogin	 * protocol for /bin/login, if the authentication works.	 */#else	if (pty > 2) {		(void) close(pty);		pty = -1;	}#endif}#if	!defined(CRAY) || !defined(NEWINIT)#ifndef	O_NOCTTY#define	O_NOCTTY	0#endif/* * Open the specified slave side of the pty, * making sure that we have a clean tty. */	intcleanopen(line)	char *line;{	register int t;#if	defined(_SC_CRAY_SECURE_SYS)	struct secstat secbuf;#endif	/* _SC_CRAY_SECURE_SYS */#ifndef STREAMSPTY	/*	 * Make sure that other people can't open the	 * slave side of the connection.	 */	(void) chown(line, 0, 0);	(void) chmod(line, 0600);#endif# if !defined(CRAY) && (BSD > 43)	(void) revoke(line);# endif#if	defined(_SC_CRAY_SECURE_SYS)	if (secflag) {		if (secstat(line, &secbuf) < 0)			return(-1);		if (setulvl(secbuf.st_slevel) < 0)			return(-1);		if (setucmp(secbuf.st_compart) < 0)			return(-1);	}#endif	/* _SC_CRAY_SECURE_SYS */	t = open(line, O_RDWR|O_NOCTTY);#if	defined(_SC_CRAY_SECURE_SYS)	if (secflag) {		if (setulvl(sysv.sy_minlvl) < 0)			return(-1);		if (setucmp(0) < 0)			return(-1);	}#endif	/* _SC_CRAY_SECURE_SYS */	if (t < 0)		return(-1);	/*	 * Hangup anybody else using this ttyp, then reopen it for	 * ourselves.	 */# if !(defined(CRAY) || defined(__hpux)) && (BSD <= 43) && !defined(STREAMSPTY)	(void) signal(SIGHUP, SIG_IGN);	vhangup();	(void) signal(SIGHUP, SIG_DFL);	t = open(line, O_RDWR|O_NOCTTY);	if (t < 0)		return(-1);# endif# if	defined(CRAY) && defined(TCVHUP)	{		register int i;		(void) signal(SIGHUP, SIG_IGN);		(void) ioctl(t, TCVHUP, (char *)0);		(void) signal(SIGHUP, SIG_DFL);		setpgrp();#if		defined(_SC_CRAY_SECURE_SYS)		if (secflag) {			if (secstat(line, &secbuf) < 0)				return(-1);			if (setulvl(secbuf.st_slevel) < 0)				return(-1);			if (setucmp(secbuf.st_compart) < 0)				return(-1);		}#endif		/* _SC_CRAY_SECURE_SYS */		i = open(line, O_RDWR);#if		defined(_SC_CRAY_SECURE_SYS)		if (secflag) {			if (setulvl(sysv.sy_minlvl) < 0)				return(-1);			if (setucmp(0) < 0)				return(-1);		}#endif		/* _SC_CRAY_SECURE_SYS */		if (i < 0)			return(-1);		(void) close(t);		t = i;	}# endif	/* defined(CRAY) && defined(TCVHUP) */	return(t);}#endif	/* !defined(CRAY) || !defined(NEWINIT) */#if BSD <= 43	intlogin_tty(t)	int t;{	if (setsid() < 0) {#ifdef ultrix		/*		 * The setsid() may have failed because we		 * already have a pgrp == pid.  Zero out		 * our pgrp and try again...		 */		if ((setpgrp(0, 0) < 0) || (setsid() < 0))#endif			fatalperror(net, "setsid()");	}# ifdef	TIOCSCTTY	if (ioctl(t, TIOCSCTTY, (char *)0) < 0)		fatalperror(net, "ioctl(sctty)");#  if defined(CRAY)	/*	 * Close the hard fd to /dev/ttypXXX, and re-open through	 * the indirect /dev/tty interface.	 */	close(t);	if ((t = open("/dev/tty", O_RDWR)) < 0)		fatalperror(net, "open(/dev/tty)");#  endif# else	/*	 * We get our controlling tty assigned as a side-effect	 * of opening up a tty device.  But on BSD based systems,	 * this only happens if our process group is zero.  The	 * setsid() call above may have set our pgrp, so clear	 * it out before opening the tty...	 */	(void) setpgrp(0, 0);	close(open(line, O_RDWR));# endif	if (t != 0)		(void) dup2(t, 0);	if (t != 1)		(void) dup2(t, 1);	if (t != 2)		(void) dup2(t, 2);	if (t > 2)		close(t);	return(0);}#endif	/* BSD <= 43 */#ifdef	NEWINITchar *gen_id = "fe";#endif/* * startslave(host) * * Given a hostname, do whatever * is necessary to startup the login process on the slave side of the pty. *//* ARGSUSED */	voidstartslave(host, autologin, autoname)	char *host;	int autologin;	char *autoname;{	register int i;	long time();	char name[256];#ifdef	NEWINIT	extern char *ptyip;	struct init_request request;	void nologinproc();	register int n;#endif	/* NEWINIT */#if	defined(AUTHENTICATION)	if (!autoname || !autoname[0])		autologin = 0;	if (autologin < auth_level) {		fatal(net, "Authorization failed");		exit(1);	}#endif#ifndef	NEWINIT# ifdef	PARENT_DOES_UTMP	utmp_sig_init();# endif	/* PARENT_DOES_UTMP */	if ((i = fork()) < 0)		fatalperror(net, "fork");	if (i) {# ifdef PARENT_DOES_UTMP		/*		 * Cray parent will create utmp entry for child and send		 * signal to child to tell when done.  Child waits for signal		 * before doing anything important.		 */		register int pid = i;		void sigjob P((int));		setpgrp();		utmp_sig_reset();		/* reset handler to default */		/*		 * Create utmp entry for child		 */		(void) time(&wtmp.ut_time);		wtmp.ut_type = LOGIN_PROCESS;		wtmp.ut_pid = pid;		SCPYN(wtmp.ut_user, "LOGIN");		SCPYN(wtmp.ut_host, host);		SCPYN(wtmp.ut_line, line + sizeof("/dev/") - 1);#ifndef	__hpux		SCPYN(wtmp.ut_id, wtmp.ut_line+3);#else		SCPYN(wtmp.ut_id, wtmp.ut_line+7);#endif		pututline(&wtmp);		endutent();		if ((i = open(wtmpf, O_WRONLY|O_APPEND)) >= 0) {			(void) write(i, (char *)&wtmp, sizeof(struct utmp));			(void) close(i);		}#ifdef	CRAY		(void) signal(WJSIGNAL, sigjob);#endif		utmp_sig_notify(pid);# endif	/* PARENT_DOES_UTMP */	} else {		getptyslave(autologin);		start_login(host, autologin, autoname);		/*NOTREACHED*/	}#else	/* NEWINIT */	/*	 * Init will start up login process if we ask nicely.  We only wait	 * for it to start up and begin normal telnet operation.	 */	if ((i = open(INIT_FIFO, O_WRONLY)) < 0) {		char tbuf[128];		(void) sprintf(tbuf, "Can't open %s\n", INIT_FIFO);		fatalperror(net, tbuf);	}	memset((char *)&request, 0, sizeof(request));	request.magic = INIT_MAGIC;	SCPYN(request.gen_id, gen_id);	SCPYN(request.tty_id, &line[8]);	SCPYN(request.host, host);	SCPYN(request.term_type, terminaltype ? terminaltype : "network");#if	!defined(UNICOS5)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -