|
|
1.1 ! root 1: /* ! 2: * File: recorder.c ! 3: * ! 4: * Purpose: recording proces ! 5: * ! 6: * $Log: recorder.c,v $ ! 7: * Revision 1.3 93/02/04 12:02:49 bin ! 8: * hal: changed from sys/fcntl.h to fcntl.h in getps* , changed from execvp ! 9: * to execvpe in recorder.c ! 10: * ! 11: */ ! 12: ! 13: /* ! 14: * Includes. ! 15: */ ! 16: #include <stdio.h> ! 17: #include <errno.h> ! 18: #include <signal.h> ! 19: ! 20: /* ! 21: * Definitions. ! 22: * Constants. ! 23: * Macros with argument lists. ! 24: * Typedefs. ! 25: * Enums. ! 26: */ ! 27: #define BSIZE 512 ! 28: ! 29: typedef unsigned char uchar; ! 30: typedef unsigned int uint; ! 31: typedef unsigned long ulong; ! 32: ! 33: /* ! 34: * Functions. ! 35: * Import Functions. ! 36: * Export Functions. ! 37: * Local Functions. ! 38: */ ! 39: void exec_shell(); ! 40: void pass_all(); ! 41: static void sig_term(); ! 42: ! 43: /* ! 44: * Global Data. ! 45: * Import Variables. ! 46: * Export Variables. ! 47: * Local Variables. ! 48: */ ! 49: static int sigcaught; ! 50: ! 51: /* ! 52: * exec_shell() ! 53: */ ! 54: void ! 55: exec_shell(fd, argv, envp) ! 56: int fd; ! 57: char ** argv, ** envp; ! 58: { ! 59: char * shell; ! 60: char * genenv(), * rindex(); ! 61: ! 62: close(0); ! 63: close(1); ! 64: close(2); ! 65: if (dup(fd) != 0 || dup(fd) != 1 || dup(fd) != 2) { ! 66: /* dup failed */ ! 67: exit (1); ! 68: } ! 69: ! 70: if (*argv == NULL) { ! 71: char * args[2]; ! 72: args[1] = NULL; ! 73: if ( (shell = getenv("SHELL")) == NULL) ! 74: shell = "/bin/sh"; ! 75: args[0] = shell; ! 76: execvpe(shell, args, envp); ! 77: #if 0 ! 78: if ( (shell = getenv("SHELL")) == NULL) ! 79: shell = "/bin/sh"; ! 80: if ( (argv[0] = rindex(shell, '/')) != NULL) ! 81: argv[0]++; ! 82: else ! 83: argv[0] = shell; ! 84: execvpe(shell, argv, envp); ! 85: #endif ! 86: } else ! 87: execvpe(argv[0], argv, envp); ! 88: ! 89: /* execvpe failed */ ! 90: exit (1); ! 91: } ! 92: ! 93: /* ! 94: * pass_all() ! 95: */ ! 96: void ! 97: pass_all(fd, lf) ! 98: int fd; ! 99: char * lf; ! 100: { ! 101: int newpid, nread; ! 102: char buff[BSIZE]; ! 103: int ppid = getpid(); ! 104: ! 105: if ( (newpid = fork()) < 0) { ! 106: /* pass_all can't fork */ ! 107: exit (1); ! 108: } else if (newpid == 0) { ! 109: /* ! 110: * Child process. ! 111: * Copy terminal stdin to fd for pty master. ! 112: */ ! 113: for (;;){ ! 114: nread = read(0, buff, BSIZE); ! 115: if (nread < 0) { ! 116: /* pass_all read err on stdin */ ! 117: exit (1); ! 118: } ! 119: if (nread == 0) ! 120: break; /* EOF */ ! 121: if (write(fd, buff, nread) != nread) { ! 122: /* pass_all write err to fd */ ! 123: exit(1); ! 124: } ! 125: } ! 126: kill(ppid, SIGTERM); ! 127: exit(0); ! 128: } ! 129: ! 130: /* ! 131: * Parent process. ! 132: * Copy from pty master fd to terminal stdout. ! 133: */ ! 134: { ! 135: int lfd; ! 136: ! 137: if((lfd = creat(lf, 0666)) == -1) { ! 138: printf("Can't open log file \"%s\"\n", lf); ! 139: goto bye; ! 140: } ! 141: sigcaught = 0; ! 142: signal(SIGTERM, sig_term); ! 143: for (;;) { ! 144: nread = read(fd, buff, BSIZE); ! 145: if (nread <= 0) ! 146: break; /* error or EOF */ ! 147: write(lfd, buff, nread); ! 148: if (write(1, buff, nread) != nread) { ! 149: /* pass_all write error to stdout */ ! 150: close(lfd); ! 151: exit (1); ! 152: } ! 153: } ! 154: close(lfd); ! 155: bye: ! 156: if (sigcaught == 0) { ! 157: kill(newpid, SIGTERM); ! 158: } ! 159: } ! 160: } ! 161: ! 162: void ! 163: sig_term() ! 164: { ! 165: sigcaught = 1; ! 166: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.