From e5888a1ffdae813d7575f5fb02275c6bb07e5199 Mon Sep 17 00:00:00 2001 From: Taru Karttunen Date: Wed, 30 Mar 2011 15:46:40 +0300 Subject: Import sources from 2011-03-30 iso image --- sys/src/ape/lib/bsd/pty.c | 111 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100755 sys/src/ape/lib/bsd/pty.c (limited to 'sys/src/ape/lib/bsd/pty.c') diff --git a/sys/src/ape/lib/bsd/pty.c b/sys/src/ape/lib/bsd/pty.c new file mode 100755 index 000000000..3189d24e1 --- /dev/null +++ b/sys/src/ape/lib/bsd/pty.c @@ -0,0 +1,111 @@ +/* posix */ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include "lib.h" +#include "sys9.h" +#include "dir.h" + +/* + * return the name of the slave + */ +char* +ptsname(int fd) +{ + Dir *d; + static char buf[32]; + + if((d = _dirfstat(fd)) == nil || strlen(d->name) < 4){ + free(d); + _syserrno(); + return 0; + } + snprintf(buf, sizeof buf, "/dev/ptty%d", atoi(d->name+4)); + free(d); + return buf; +} + +/* + * return the name of the master + */ +char* +ptmname(int fd) +{ + Dir *d; + static char buf[32]; + + if((d = _dirfstat(fd)) == nil || strlen(d->name) < 4){ + free(d); + _syserrno(); + return 0; + } + + snprintf(buf, sizeof buf, "/dev/ttym%d", atoi(d->name+4)); + return buf; +} + +static char ptycl[] = "/dev/ptyclone"; +static char fssrv[] = "/srv/ptyfs"; + +static void +mkserver(void) +{ + int fd, i; + char *argv[3]; + + fd = _OPEN(fssrv, O_RDWR); + if(_MOUNT(fd, -1, "/dev", MAFTER, "") < 0) { + /* + * remove fssrv here, if it exists, to avoid a race + * between the loop in the default case below and the + * new ptyfs removing fssrv when it starts. + * we otherwise might be unlucky enough to open the old + * (hung channel) fssrv before ptyfs removes it and break + * out of the loop with an open fd to a hung channel? + */ + _CLOSE(fd); + _REMOVE(fssrv); + switch(_RFORK(RFPROC|RFFDG)) { + case -1: + return; + case 0: + argv[0] = "ptyfs"; + argv[1] = 0; + _EXEC("/bin/ape/ptyfs", argv); + _EXITS(0); + default: + for(i = 0; i < 3; i++) { + fd = _OPEN(fssrv, O_RDWR); + if(fd >= 0) + break; + _SLEEP(1000); + } + } + if(fd < 0) + return; + if(_MOUNT(fd, -1, "/dev", MAFTER, "") < 0) + _CLOSE(fd); + } + /* successful _MOUNT closes fd */ +} + +/* + * allocate a new pty + */ +int +_getpty(void) +{ + struct stat sb; + + if(stat(ptycl, &sb) < 0) + mkserver(); + + return open(ptycl, O_RDWR); +} -- cgit v1.2.3