1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 3*0Sstevel@tonic-gate * Use is subject to license terms. 4*0Sstevel@tonic-gate */ 5*0Sstevel@tonic-gate 6*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 7*0Sstevel@tonic-gate /* All Rights Reserved */ 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate /* 11*0Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 12*0Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 13*0Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 14*0Sstevel@tonic-gate */ 15*0Sstevel@tonic-gate 16*0Sstevel@tonic-gate /* Portions Copyright(c) 1988, Sun Microsystems, Inc. */ 17*0Sstevel@tonic-gate /* All Rights Reserved. */ 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gate /* 22*0Sstevel@tonic-gate * script 23*0Sstevel@tonic-gate */ 24*0Sstevel@tonic-gate #include <stdio.h> 25*0Sstevel@tonic-gate #include <signal.h> 26*0Sstevel@tonic-gate #include <fcntl.h> 27*0Sstevel@tonic-gate #include <locale.h> 28*0Sstevel@tonic-gate #include <time.h> 29*0Sstevel@tonic-gate #include <sys/stropts.h> 30*0Sstevel@tonic-gate #include <sys/types.h> 31*0Sstevel@tonic-gate #include <sys/stat.h> 32*0Sstevel@tonic-gate #include <sys/termios.h> 33*0Sstevel@tonic-gate #include <sys/file.h> 34*0Sstevel@tonic-gate #include <errno.h> 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate int grantpt(); 37*0Sstevel@tonic-gate int unlockpt(); 38*0Sstevel@tonic-gate char *ptsname(); 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate char *getenv(); 41*0Sstevel@tonic-gate struct tm *localtime(); 42*0Sstevel@tonic-gate char *shell; 43*0Sstevel@tonic-gate FILE *fscript; 44*0Sstevel@tonic-gate int master; /* file descriptor for master pseudo-tty */ 45*0Sstevel@tonic-gate int slave; /* file descriptor for slave pseudo-tty */ 46*0Sstevel@tonic-gate int child; 47*0Sstevel@tonic-gate int subchild; 48*0Sstevel@tonic-gate char *fname = "typescript"; 49*0Sstevel@tonic-gate void sigwinch(); 50*0Sstevel@tonic-gate void finish(); 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate struct termios b; 53*0Sstevel@tonic-gate struct winsize size; 54*0Sstevel@tonic-gate int lb; 55*0Sstevel@tonic-gate int l; 56*0Sstevel@tonic-gate char *mptname = "/dev/ptmx"; /* master pseudo-tty device */ 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate int aflg; 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate main(argc, argv) 61*0Sstevel@tonic-gate int argc; 62*0Sstevel@tonic-gate char *argv[]; 63*0Sstevel@tonic-gate { 64*0Sstevel@tonic-gate uid_t ruidt; 65*0Sstevel@tonic-gate gid_t gidt; 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 68*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) 69*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" 70*0Sstevel@tonic-gate #endif 71*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 72*0Sstevel@tonic-gate 73*0Sstevel@tonic-gate shell = getenv("SHELL"); 74*0Sstevel@tonic-gate if (shell == 0) 75*0Sstevel@tonic-gate shell = "/bin/sh"; 76*0Sstevel@tonic-gate argc--, argv++; 77*0Sstevel@tonic-gate while (argc > 0 && argv[0][0] == '-') { 78*0Sstevel@tonic-gate switch (argv[0][1]) { 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate case 'a': 81*0Sstevel@tonic-gate aflg++; 82*0Sstevel@tonic-gate break; 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate default: 85*0Sstevel@tonic-gate fprintf(stderr, 86*0Sstevel@tonic-gate gettext("usage: script [ -a ] [ typescript ]\n")); 87*0Sstevel@tonic-gate exit(1); 88*0Sstevel@tonic-gate } 89*0Sstevel@tonic-gate argc--, argv++; 90*0Sstevel@tonic-gate } 91*0Sstevel@tonic-gate if (argc > 0) 92*0Sstevel@tonic-gate fname = argv[0]; 93*0Sstevel@tonic-gate ruidt = getuid(); 94*0Sstevel@tonic-gate gidt = getgid(); 95*0Sstevel@tonic-gate if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) { 96*0Sstevel@tonic-gate perror(fname); 97*0Sstevel@tonic-gate fail(); 98*0Sstevel@tonic-gate } 99*0Sstevel@tonic-gate chown(fname, ruidt, gidt); 100*0Sstevel@tonic-gate getmaster(); 101*0Sstevel@tonic-gate printf(gettext("Script started, file is %s\n"), fname); 102*0Sstevel@tonic-gate fixtty(); 103*0Sstevel@tonic-gate 104*0Sstevel@tonic-gate (void) signal(SIGCHLD, finish); 105*0Sstevel@tonic-gate child = fork(); 106*0Sstevel@tonic-gate if (child < 0) { 107*0Sstevel@tonic-gate perror("fork"); 108*0Sstevel@tonic-gate fail(); 109*0Sstevel@tonic-gate } 110*0Sstevel@tonic-gate if (child == 0) { 111*0Sstevel@tonic-gate subchild = child = fork(); 112*0Sstevel@tonic-gate if (child < 0) { 113*0Sstevel@tonic-gate perror("fork"); 114*0Sstevel@tonic-gate fail(); 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate if (child) 117*0Sstevel@tonic-gate dooutput(); 118*0Sstevel@tonic-gate else 119*0Sstevel@tonic-gate doshell(); 120*0Sstevel@tonic-gate } 121*0Sstevel@tonic-gate doinput(); 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate doinput() 125*0Sstevel@tonic-gate { 126*0Sstevel@tonic-gate char ibuf[BUFSIZ]; 127*0Sstevel@tonic-gate int cc; 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate (void) fclose(fscript); 130*0Sstevel@tonic-gate sigset(SIGWINCH, sigwinch); 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate while ((cc = read(0, ibuf, BUFSIZ)) != 0) { 133*0Sstevel@tonic-gate if (cc == -1) { 134*0Sstevel@tonic-gate if (errno == EINTR) { /* SIGWINCH probably */ 135*0Sstevel@tonic-gate continue; 136*0Sstevel@tonic-gate } else { 137*0Sstevel@tonic-gate break; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate } 140*0Sstevel@tonic-gate (void) write(master, ibuf, cc); 141*0Sstevel@tonic-gate } 142*0Sstevel@tonic-gate done(); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate void 146*0Sstevel@tonic-gate sigwinch() 147*0Sstevel@tonic-gate { 148*0Sstevel@tonic-gate struct winsize ws; 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate if (ioctl(0, TIOCGWINSZ, &ws) == 0) 151*0Sstevel@tonic-gate (void) ioctl(master, TIOCSWINSZ, &ws); 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate #include <sys/wait.h> 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate void 157*0Sstevel@tonic-gate finish() 158*0Sstevel@tonic-gate { 159*0Sstevel@tonic-gate int status; 160*0Sstevel@tonic-gate register int pid; 161*0Sstevel@tonic-gate register int die = 0; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate while ((pid = wait(&status)) > 0) 164*0Sstevel@tonic-gate if (pid == child) 165*0Sstevel@tonic-gate die = 1; 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gate if (die) 168*0Sstevel@tonic-gate done(); 169*0Sstevel@tonic-gate } 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate dooutput() 172*0Sstevel@tonic-gate { 173*0Sstevel@tonic-gate time_t tvec; 174*0Sstevel@tonic-gate char obuf[BUFSIZ]; 175*0Sstevel@tonic-gate char tbuf[BUFSIZ]; 176*0Sstevel@tonic-gate int cc; 177*0Sstevel@tonic-gate 178*0Sstevel@tonic-gate (void) close(0); 179*0Sstevel@tonic-gate tvec = time((time_t *)0); 180*0Sstevel@tonic-gate strftime(tbuf, BUFSIZ, "%c", localtime(&tvec)); 181*0Sstevel@tonic-gate fprintf(fscript, gettext("Script started on %s\n"), tbuf); 182*0Sstevel@tonic-gate for (;;) { 183*0Sstevel@tonic-gate cc = read(master, obuf, sizeof (obuf)); 184*0Sstevel@tonic-gate if (cc <= 0) 185*0Sstevel@tonic-gate break; 186*0Sstevel@tonic-gate (void) write(1, obuf, cc); 187*0Sstevel@tonic-gate (void) fwrite(obuf, 1, cc, fscript); 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate done(); 190*0Sstevel@tonic-gate } 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gate doshell() 193*0Sstevel@tonic-gate { 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate setpgrp(); /* relinquish control terminal */ 196*0Sstevel@tonic-gate getslave(); 197*0Sstevel@tonic-gate (void) close(master); 198*0Sstevel@tonic-gate (void) fclose(fscript); 199*0Sstevel@tonic-gate (void) dup2(slave, 0); 200*0Sstevel@tonic-gate (void) dup2(slave, 1); 201*0Sstevel@tonic-gate (void) dup2(slave, 2); 202*0Sstevel@tonic-gate (void) close(slave); 203*0Sstevel@tonic-gate execl(shell, shell, "-i", (char *)0); 204*0Sstevel@tonic-gate perror(shell); 205*0Sstevel@tonic-gate fail(); 206*0Sstevel@tonic-gate } 207*0Sstevel@tonic-gate 208*0Sstevel@tonic-gate fixtty() 209*0Sstevel@tonic-gate { 210*0Sstevel@tonic-gate struct termios sbuf; 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate sbuf = b; 213*0Sstevel@tonic-gate sbuf.c_iflag &= ~(INLCR|IGNCR|ICRNL|IUCLC|IXON); 214*0Sstevel@tonic-gate sbuf.c_oflag &= ~OPOST; 215*0Sstevel@tonic-gate sbuf.c_lflag &= ~(ICANON|ISIG|ECHO); 216*0Sstevel@tonic-gate sbuf.c_cc[VMIN] = 1; 217*0Sstevel@tonic-gate sbuf.c_cc[VTIME] = 0; 218*0Sstevel@tonic-gate (void) ioctl(0, TCSETSF, (char *)&sbuf); 219*0Sstevel@tonic-gate } 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate fail() 222*0Sstevel@tonic-gate { 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gate (void) kill(0, SIGTERM); 225*0Sstevel@tonic-gate done(); 226*0Sstevel@tonic-gate } 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate done() 229*0Sstevel@tonic-gate { 230*0Sstevel@tonic-gate time_t tvec; 231*0Sstevel@tonic-gate char tbuf[BUFSIZ]; 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate if (subchild) { 234*0Sstevel@tonic-gate tvec = time((time_t *)0); 235*0Sstevel@tonic-gate strftime(tbuf, BUFSIZ, "%c", localtime(&tvec)); 236*0Sstevel@tonic-gate fprintf(fscript, gettext("\nscript done on %s\n"), tbuf); 237*0Sstevel@tonic-gate (void) fclose(fscript); 238*0Sstevel@tonic-gate (void) close(master); 239*0Sstevel@tonic-gate } else { 240*0Sstevel@tonic-gate (void) ioctl(0, TCSETSW, (char *)&b); 241*0Sstevel@tonic-gate printf(gettext("Script done, file is %s\n"), fname); 242*0Sstevel@tonic-gate } 243*0Sstevel@tonic-gate exit(0); 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate getmaster() 247*0Sstevel@tonic-gate { 248*0Sstevel@tonic-gate struct stat stb; 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate if ((master = open(mptname, O_RDWR)) >= 0) { /* a pseudo-tty is free */ 251*0Sstevel@tonic-gate (void) ioctl(0, TCGETS, (char *)&b); 252*0Sstevel@tonic-gate (void) ioctl(0, TIOCGWINSZ, (char *)&size); 253*0Sstevel@tonic-gate return; 254*0Sstevel@tonic-gate } else { /* out of pseudo-tty's */ 255*0Sstevel@tonic-gate perror(mptname); 256*0Sstevel@tonic-gate fprintf(stderr, gettext("Out of pseudo-tty's\n")); 257*0Sstevel@tonic-gate fail(); 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate } 260*0Sstevel@tonic-gate 261*0Sstevel@tonic-gate getslave() 262*0Sstevel@tonic-gate { 263*0Sstevel@tonic-gate char *slavename; /* name of slave pseudo-tty */ 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate grantpt(master); /* change permissions of slave */ 266*0Sstevel@tonic-gate unlockpt(master); /* unlock slave */ 267*0Sstevel@tonic-gate slavename = ptsname(master); /* get name of slave */ 268*0Sstevel@tonic-gate slave = open(slavename, O_RDWR); /* open slave */ 269*0Sstevel@tonic-gate if (slave < 0) { /* error on opening slave */ 270*0Sstevel@tonic-gate perror(slavename); 271*0Sstevel@tonic-gate fail(); 272*0Sstevel@tonic-gate } 273*0Sstevel@tonic-gate ioctl(slave, I_PUSH, "ptem"); /* push pt hw emulation module */ 274*0Sstevel@tonic-gate ioctl(slave, I_PUSH, "ldterm"); /* push line discipline */ 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate (void) ioctl(slave, TCSETSF, (char *)&b); 277*0Sstevel@tonic-gate (void) ioctl(slave, TIOCSWINSZ, (char *)&size); 278*0Sstevel@tonic-gate } 279