Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

conn_pty.c 11.4 KB
Newer Older
deuce's avatar
deuce committed
1 2
/* Copyright (C), 2007 by Stephen Hurd */

3 4 5 6
/* $Id$ */

#ifdef __unix__

7
#include <unistd.h>		/* _POSIX_VDISABLE - needed when termios.h is broken */
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
#include <signal.h>		// kill()
#include <sys/wait.h>	// WEXITSTATUS

#define TTYDEFCHARS		// needed for ttydefchars definition

#if defined(__FreeBSD__)
	#include <libutil.h>	// forkpty()
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DARWIN__)
	#include <util.h>
#elif defined(__linux__)
	#include <pty.h>
#elif defined(__QNX__)
#if 0
	#include <unix.h>
#else
	#define NEEDS_FORKPTY
#endif
#endif

	#ifdef NEEDS_FORKPTY
	#include <grp.h>
	#endif

	#include <termios.h>

/*
 * Control Character Defaults
 */
#ifndef CTRL
	#define CTRL(x)	(x&037)
#endif
#ifndef CEOF
	#define	CEOF		CTRL('d')
#endif
#ifndef CEOL
	#define	CEOL		0xff		/* XXX avoid _POSIX_VDISABLE */
#endif
#ifndef CERASE
	#define	CERASE		0177
#endif
#ifndef CERASE2
	#define	CERASE2		CTRL('h')
#endif
#ifndef CINTR
	#define	CINTR		CTRL('c')
#endif
#ifndef CSTATUS
	#define	CSTATUS		CTRL('t')
#endif
#ifndef CKILL
	#define	CKILL		CTRL('u')
#endif
#ifndef CMIN
	#define	CMIN		1
#endif
#ifndef CQUIT
	#define	CQUIT		034		/* FS, ^\ */
#endif
#ifndef CSUSP
	#define	CSUSP		CTRL('z')
#endif
#ifndef CTIME
	#define	CTIME		0
#endif
#ifndef CDSUSP
	#define	CDSUSP		CTRL('y')
#endif
#ifndef CSTART
	#define	CSTART		CTRL('q')
#endif
#ifndef CSTOP
	#define	CSTOP		CTRL('s')
#endif
#ifndef CLNEXT
	#define	CLNEXT		CTRL('v')
#endif
#ifndef CDISCARD
	#define	CDISCARD 	CTRL('o')
#endif
#ifndef CWERASE
	#define	CWERASE 	CTRL('w')
#endif
#ifndef CREPRINT
	#define	CREPRINT 	CTRL('r')
#endif
#ifndef CEOT
	#define	CEOT		CEOF
#endif
/* compat */
#ifndef CBRK
	#define	CBRK		CEOL
#endif
#ifndef CRPRNT
	#define CRPRNT		CREPRINT
#endif
#ifndef CFLUSH
	#define	CFLUSH		CDISCARD
#endif

#ifndef TTYDEF_IFLAG
deuce's avatar
deuce committed
108 109 110 111 112
	#ifndef IMAXBEL
		#define TTYDEF_IFLAG    (BRKINT | ICRNL | IXON | IXANY)
	#else
		#define TTYDEF_IFLAG    (BRKINT | ICRNL | IMAXBEL | IXON | IXANY)
	#endif
113 114 115 116 117 118 119 120 121 122
#endif
#ifndef TTYDEF_OFLAG
	#define TTYDEF_OFLAG    (OPOST | ONLCR)
#endif
#ifndef TTYDEF_LFLAG
	#define TTYDEF_LFLAG    (ECHO | ICANON | ISIG | IEXTEN | ECHOE|ECHOKE|ECHOCTL)
#endif
#ifndef TTYDEF_CFLAG
	#define TTYDEF_CFLAG    (CREAD | CS8 | HUPCL)
#endif
deuce's avatar
deuce committed
123
#if defined(__QNX__) || defined(__solaris__) || defined(__NetBSD__) || defined(__HAIKU__)
124 125 126 127 128 129 130 131 132 133 134
	static cc_t     ttydefchars[NCCS] = {
        CEOF,   CEOL,   CEOL,   CERASE, CWERASE, CKILL, CREPRINT,
        CERASE2, CINTR, CQUIT,  CSUSP,  CDSUSP, CSTART, CSTOP,  CLNEXT,
        CDISCARD, CMIN, CTIME,  CSTATUS
#ifndef __solaris__
	, _POSIX_VDISABLE
#endif
	};
#endif

#include <stdlib.h>
135
#include <xpprintf.h>
136 137 138 139 140

#include "bbslist.h"
#include "conn.h"
#include "uifcinit.h"
#include "ciolib.h"
deuce's avatar
deuce committed
141 142
#include "syncterm.h"
#include "fonts.h"
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
extern int default_font;

#ifdef NEEDS_CFMAKERAW
void
cfmakeraw(struct termios *t)
{
	t->c_iflag &= ~(IMAXBEL|IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
	t->c_oflag &= ~OPOST;
	t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
	t->c_cflag &= ~(CSIZE|PARENB);
	t->c_cflag |= CS8;
}
#endif

#ifdef NEEDS_FORKPTY
static int login_tty(int fd)
{
	(void) setsid();
	if (!isatty(fd))
		return (-1);
	(void) dup2(fd, 0);
	(void) dup2(fd, 1);
	(void) dup2(fd, 2);
	if (fd > 2)
		(void) close(fd);
	return (0);
}

#ifdef NEEDS_DAEMON
/****************************************************************************/
/* Daemonizes the process                                                   */
/****************************************************************************/
int
daemon(int nochdir, int noclose)
{
    int fd;

    switch (fork()) {
    case -1:
        return (-1);
    case 0:
        break;
    default:
        _exit(0);
    }

    if (setsid() == -1)
        return (-1);

    if (!nochdir)
        (void)chdir("/");

    if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
        (void)dup2(fd, STDIN_FILENO);
        (void)dup2(fd, STDOUT_FILENO);
        (void)dup2(fd, STDERR_FILENO);
        if (fd > 2)
            (void)close(fd);
    }
    return (0);
}
#endif

deuce's avatar
deuce committed
206
static int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize *winp)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
{
	char line[] = "/dev/ptyXX";
	const char *cp1, *cp2;
	int master, slave, ttygid;
	struct group *gr;

	if ((gr = getgrnam("tty")) != NULL)
		ttygid = gr->gr_gid;
	else
		ttygid = -1;

	for (cp1 = "pqrsPQRS"; *cp1; cp1++) {
		line[8] = *cp1;
		for (cp2 = "0123456789abcdefghijklmnopqrstuv"; *cp2; cp2++) {
			line[5] = 'p';
			line[9] = *cp2;
			if ((master = open(line, O_RDWR, 0)) == -1) {
				if (errno == ENOENT)
					break; /* try the next pty group */
			} else {
				line[5] = 't';
				(void) chown(line, getuid(), ttygid);
				(void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
				/* Hrm... SunOS doesn't seem to have revoke
				(void) revoke(line); */
				if ((slave = open(line, O_RDWR, 0)) != -1) {
					*amaster = master;
					*aslave = slave;
					if (name)
						strcpy(name, line);
					if (termp)
						(void) tcsetattr(slave,
							TCSAFLUSH, termp);
					if (winp)
						(void) ioctl(slave, TIOCSWINSZ,
							(char *)winp);
					return (0);
				}
				(void) close(master);
			}
		}
	}
	errno = ENOENT;	/* out of ptys */
	return (-1);
}

deuce's avatar
deuce committed
253
static int forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
{
	int master, slave, pid;

	if (openpty(&master, &slave, name, termp, winp) == -1)
		return (-1);
	switch (pid = FORK()) {
	case -1:
		return (-1);
	case 0:
		/*
		 * child
		 */
		(void) close(master);
		login_tty(slave);
		return (0);
	}
	/*
	 * parent
	 */
	*amaster = master;
	(void) close(slave);
	return (pid);
}
#endif /* NEED_FORKPTY */

int master;
int child_pid;
static int status;

#ifdef __BORLANDC__
#pragma argsused
#endif
void pty_input_thread(void *args)
{
	fd_set	rds;
	int		rd;
	int	buffered;
	size_t	buffer;
deuce's avatar
deuce committed
292
	int i;
293

deuce's avatar
deuce committed
294
	SetThreadName("PTY Input");
295 296
	conn_api.input_thread_running=1;
	while(master != -1 && !conn_api.terminate) {
deuce's avatar
deuce committed
297
		if((i=waitpid(child_pid, &status, WNOHANG)))
298 299 300
			break;
		FD_ZERO(&rds);
		FD_SET(master, &rds);
301 302 303 304 305 306 307 308
#ifdef __linux__
		{
			struct timeval tv;
			tv.tv_sec=0;
			tv.tv_usec=500000;
			rd=select(master+1, &rds, NULL, NULL, &tv);
		}
#else
309
		rd=select(master+1, &rds, NULL, NULL, NULL);
310
#endif
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
		if(rd==-1) {
			if(errno==EBADF)
				break;
			rd=0;
		}
		if(rd==1) {
			rd=read(master, conn_api.rd_buf, conn_api.rd_buf_size);
			if(rd < 0)
				continue;
		}
		buffered=0;
		while(buffered < rd) {
			pthread_mutex_lock(&(conn_inbuf.mutex));
			buffer=conn_buf_wait_free(&conn_inbuf, rd-buffered, 100);
			buffered+=conn_buf_put(&conn_inbuf, conn_api.rd_buf+buffered, buffer);
			pthread_mutex_unlock(&(conn_inbuf.mutex));
		}
	}
	conn_api.input_thread_running=0;
}

#ifdef __BORLANDC__
#pragma argsused
#endif
void pty_output_thread(void *args)
{
	fd_set	wds;
	int		wr;
	int		ret;
	int	sent;

deuce's avatar
deuce committed
342
	SetThreadName("PTY Output");
343 344 345 346 347
	conn_api.output_thread_running=1;
	while(master != -1 && !conn_api.terminate) {
		if(waitpid(child_pid, &status, WNOHANG))
			break;
		pthread_mutex_lock(&(conn_outbuf.mutex));
348
		ret=0;
349 350 351 352 353 354 355 356
		wr=conn_buf_wait_bytes(&conn_outbuf, 1, 100);
		if(wr) {
			wr=conn_buf_get(&conn_outbuf, conn_api.wr_buf, conn_api.wr_buf_size);
			pthread_mutex_unlock(&(conn_outbuf.mutex));
			sent=0;
			while(sent < wr) {
				FD_ZERO(&wds);
				FD_SET(master, &wds);
357 358 359 360 361 362 363 364
#ifdef __linux__
				{
					struct timeval tv;
					tv.tv_sec=0;
					tv.tv_usec=500000;
					ret=select(master+1, NULL, &wds, NULL, &tv);
				}
#else
365
				ret=select(master+1, NULL, &wds, NULL, NULL);
366
#endif
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
				if(ret==-1) {
					if(errno==EBADF)
						break;
					ret=0;
				}
				if(ret==1) {
					ret=write(master, conn_api.wr_buf+sent, wr-sent);
					if(ret==-1)
						continue;
					sent+=ret;
				}
			}
		}
		else
			pthread_mutex_unlock(&(conn_outbuf.mutex));
		if(ret==-1)
			break;
	}
	conn_api.output_thread_running=0;
}

int pty_connect(struct bbslist *bbs)
{
	struct winsize ws;
	struct termios ts;
	struct text_info ti;
393
	char	*termcap;
394 395 396 397 398 399 400 401 402

	/* Init ti */
	ts.c_iflag = TTYDEF_IFLAG;
	ts.c_oflag = TTYDEF_OFLAG;
	ts.c_lflag = TTYDEF_LFLAG;
	ts.c_cflag = TTYDEF_CFLAG;
	memcpy(ts.c_cc,ttydefchars,sizeof(ts.c_cc));

	/* Horrible way to determine the screen size */
deuce's avatar
deuce committed
403
	textmode(screen_to_ciolib(bbs->screen_mode));
404 405 406 407

	gettextinfo(&ti);
	if(ti.screenwidth < 80)
		ws.ws_col=40;
deuce's avatar
deuce committed
408 409 410 411 412 413
	else {
		if(ti.screenwidth < 132)
			ws.ws_col=80;
		else
			ws.ws_col=132;
	}
414 415 416 417 418 419 420 421 422 423 424 425 426 427
	ws.ws_row=ti.screenheight;
	if(!bbs->nostatus)
		ws.ws_row--;
	if(ws.ws_row<24)
		ws.ws_row=24;

	child_pid = forkpty(&master, NULL, &ts, &ws);
	switch(child_pid) {
	case -1:
		load_font_files();
		textmode(ti.currmode);
		settitle("SyncTERM");
		return(-1);
	case 0:		/* Child */
428
		setenv("TERM",settings.TERM,1);
429
		termcap=xp_asprintf("syncterm|SyncTERM"
430
				":am:mi:ms:ut"
431 432 433 434 435
				":Co#8:co#%d:it#8:li#%d:pa#64"
				":@7=\\E[K:AB=\\E[4%%dm:AF=\\E[3%%dm:AL=\\E[%%dL:DC=\\E[%%dP"
				":DL=\\E[%%dM:DO=\\E[%%dB:F1=\\E[23~:F2=\\E[24~:IC=\\E[%%d@"
				":LE=\\E[%%dD:RA=\\E[7l:RI=\\E[%%dC:SA=\\E[?7h:SF=\\E[%%dS"
				":SR=\\E[%%dT:UP=\\E[%%dA"
436 437
				":ac=-\\030.^Y0\\333`\\004a\\260f\\370g\\361h\\261i\\025j\\331k\\277l\\332m\\300n\\305q\\304t\\303u\\264v\\301w\\302x\\263y\\363z\\362~\\371"
				":al=\\E[L:bl=^G:bt=\\E[Z:cb=\\E[1K:cd=\\E[J:ce=\\E[K:cl=\\E[2J"
438
				":cm=\\E[%%i%%d;%%dH:cr=^M:dc=\\E[P:dl=\\E[M:do=^J:ec=\\E[%%dX:ei="
439 440 441 442 443 444 445 446
				":ho=\\E[H:ic=\\E[@:im="
				":is=\\E[?7h\\E[?25h\\E[?31l\\E[?32l\\E[?33l\\E[*r\\E[ D\\E[0m\\E[?s"
				":k1=\\EOP:k2=\\EOQ:k3=\\EOR:k4=\\EOS:k5=\\EOt:k6=\\E[17~"
				":k7=\\E[18~:k8=\\E[19~:k9=\\E[20~:k;=\\E[21~:kD=\\177:kI=\\E[@"
				":kd=\\E[B:kN=\\E[U:kP=\\E[V:kb=^H:kh=\\E[H:kl=\\E[D:kr=\\E[C"
				":ku=\\E[A:le=\\E[D:mb=\\E[5m:md=\\E[1m:me=\\E[0m:nd=\\E[C"
				":nw=^M^J:rc=\\E[u"
				":sc=\\E[s:sf=\\E[S:so=\\E[0;1;7m:sr=\\E[T:up=\\E[A:ve=\\E[?25h"
447 448 449
				":vi=\\E[?25l:",ws.ws_col,ws.ws_row);
		setenv("TERMCAP",termcap,1);
		xp_asprintf_free(termcap);
deuce's avatar
deuce committed
450 451 452 453 454 455
		termcap=xp_asprintf("%d",ws.ws_col);
		setenv("COLUMNS",termcap,1);
		xp_asprintf_free(termcap);
		termcap=xp_asprintf("%d",ws.ws_row);
		setenv("LINES",termcap,1);
		xp_asprintf_free(termcap);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
		if(bbs->addr[0])
			execl("/bin/sh", "/bin/sh", "-c", bbs->addr, (char *)0);
		else
			execl(getenv("SHELL"), getenv("SHELL"), (char *)0);
		exit(1);
	}

	if(!create_conn_buf(&conn_inbuf, BUFFER_SIZE)) {
		load_font_files();
		textmode(ti.currmode);
		settitle("SyncTERM");
		return(-1);
	}
	if(!create_conn_buf(&conn_outbuf, BUFFER_SIZE)) {
		destroy_conn_buf(&conn_inbuf);
		load_font_files();
		textmode(ti.currmode);
		settitle("SyncTERM");
		return(-1);
	}
	if(!(conn_api.rd_buf=(unsigned char *)malloc(BUFFER_SIZE))) {
		destroy_conn_buf(&conn_inbuf);
		destroy_conn_buf(&conn_outbuf);
		load_font_files();
		textmode(ti.currmode);
		settitle("SyncTERM");
		return(-1);
	}
	conn_api.rd_buf_size=BUFFER_SIZE;
	if(!(conn_api.wr_buf=(unsigned char *)malloc(BUFFER_SIZE))) {
		free(conn_api.rd_buf);
		destroy_conn_buf(&conn_inbuf);
		destroy_conn_buf(&conn_outbuf);
		load_font_files();
		textmode(ti.currmode);
		settitle("SyncTERM");
		return(-1);
	}
	conn_api.wr_buf_size=BUFFER_SIZE;

	_beginthread(pty_output_thread, 0, NULL);
	_beginthread(pty_input_thread, 0, NULL);

	return(0);
}

int pty_close(void)
{
	time_t	start;

	conn_api.terminate=1;
	start=time(NULL);
	kill(child_pid, SIGHUP);
	while(waitpid(child_pid, &status, WNOHANG)==0) {
		/* Wait for 10 seconds */
		if(time(NULL)-start >= 10)
			break;
		SLEEP(1);
	}
	kill(child_pid, SIGKILL);
	waitpid(child_pid, &status, 0);

	while(conn_api.input_thread_running || conn_api.output_thread_running)
		SLEEP(1);
	destroy_conn_buf(&conn_inbuf);
	destroy_conn_buf(&conn_outbuf);
	FREE_AND_NULL(conn_api.rd_buf);
	FREE_AND_NULL(conn_api.wr_buf);
	return(0);
}

#endif	/* __unix__ */