home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / misc / conutils / ACS.c next >
Encoding:
C/C++ Source or Header  |  1999-06-07  |  5.2 KB  |  208 lines

  1. /* ACS - Auto Console Switching                                    */
  2. /* What this does is allows you to specify VTs (virtual terminals, */
  3. /* also known as consoles) to watch. When it detects new data on   */
  4. /* this tty it will automatically switch.                          */
  5. /*                                   */
  6. /* This allows you to do many things at once such as:              */
  7. /*   1.) IRC on one VT                                             */
  8. /*   2.) IRC on a different IRC network on another                 */
  9. /*   3.) wait for a web page to load up on another                 */
  10. /*   4.) wait for telnet to connect to a site on another           */
  11. /*                                   */
  12. /* Compile with -DBEEPONLY to cause a beep on new activity, rather */
  13. /* than switching to the console with new activity.                */
  14. /*                                   */
  15. /* Note: This should work on Linux and *BSD systems.               */
  16. /*                                   */
  17. /* So you can see the uses of this. Enjoy.                         */
  18. /* Shok (Matt Conover), shok@dataforce.net                         */
  19.  
  20. #include <stdio.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <sys/stat.h>
  24. #include <linux/vt.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/types.h>
  27.  
  28. #define ERROR -1
  29. #define MAXVTNUM 63
  30.           
  31. struct vcs {
  32.   int fd;
  33.   char curbuf[6666], prevbuf[6666];
  34. } vcs[MAXVTNUM];
  35.  
  36. int fd;
  37.  
  38. void clean(int val);
  39. void syntax(char **argv);
  40. void parse(int argc, char **argv);
  41.  
  42. void sighandler(int signum);
  43.  
  44. void main(int argc, char **argv)
  45. {
  46.    char buf[512];
  47.    register int i;
  48.    register int tty;
  49.  
  50.    memset(buf, 0, sizeof(buf));
  51.  
  52.    for (i = 0 ; i < MAXVTNUM; i++) 
  53.    {
  54.       vcs[i].fd = -1, vcs[i].curbuf[0] = 0;
  55.  
  56.       bzero(vcs[i].curbuf,  sizeof(vcs[i].curbuf));
  57.       bzero(vcs[i].prevbuf, sizeof(vcs[i].prevbuf));
  58.    }
  59.  
  60.    /* --------------- */
  61.  
  62.    parse(argc, argv); /* Take care of arguments (such as the VTs), */
  63.                       /* as well as open the files.                */
  64.  
  65.    /* Turn into a daemon, exit the parent. */
  66.    if (fork() != 0) exit(0);
  67.  
  68.    /* Need to switch to the active console. tty is what we pass */
  69.    /* to ioctl() with VT_ACTIVATE.                              */
  70.    if ((tty = open("/dev/tty", O_RDWR)) == ERROR) 
  71.    {
  72.       fprintf(stderr, "error with open(): %s\n", strerror(errno));
  73.       exit(ERROR);
  74.    }
  75.  
  76.    /* Used so that when we receive a signal to abort, we close up */
  77.    /* all the open file descriptors, and give them a message that */
  78.    /* we are aborting. If this is run as a daemon, then you would */
  79.    /* have to send it with kill -2, -3 or -15.                    */
  80.    signal(SIGINT,  sighandler);  
  81.    signal(SIGTERM, sighandler);
  82.    signal(SIGQUIT, sighandler);
  83.  
  84.    while (1)
  85.    {
  86.       for (i = 0; i < MAXVTNUM; i++) 
  87.       {
  88.          if (vcs[i].fd != -1) 
  89.          {
  90.         /* Copy the current buffer into the previous one, */
  91.             /* for later use.                                 */
  92.         strcpy(vcs[i].prevbuf, vcs[i].curbuf);
  93.  
  94.         /* Get to the beginning of the screen. */
  95.         lseek(vcs[i].fd, 0, SEEK_SET); 
  96.  
  97.         if ((read(vcs[i].fd, vcs[i].curbuf, sizeof(vcs[i].curbuf)))
  98.             == ERROR) {
  99.               perror("read");
  100.           clean(ERROR);
  101.         }
  102.  
  103.         /* Compare the buffer of the previous screen dumb with */
  104.         /* this one. If they are different, new data has been  */
  105.         /* received and we switch consoles.                    */
  106.  
  107.         if ((strcmp(vcs[i].curbuf, vcs[i].prevbuf) != 0) 
  108.                         && (vcs[i].prevbuf[0] != 0))
  109.             {
  110. #           ifdef BEEPONLY
  111.            ioctl(tty, VT_ACTIVATE, i+1);
  112. #           else
  113.                write(tty, '\a', 1);
  114. #           endif
  115.            usleep(500000);
  116.         }
  117.          } 
  118.       }
  119.    }
  120. }
  121.  
  122. void parse(int argc, char **argv)
  123. {
  124.    int i;
  125.    char *p;
  126.    char buf[512], bfa[512];
  127.  
  128.    if (argc < 2) syntax(argv);
  129.  
  130.    sprintf(buf, "Watching ");
  131.  
  132.    for (argc--; argc; argc--) 
  133.    {
  134.       if (strcasecmp(argv[argc], "all") == 0) 
  135.       {
  136.          for (i = 0; i < MAXVTNUM; i++) 
  137.          {
  138.             sprintf(buf, "/dev/vcs%d", i+1);
  139.  
  140.             vcs[i].fd = open(buf, O_RDONLY | O_NOCTTY);
  141.             if (vcs[i].fd == ERROR) 
  142.             {
  143.                fprintf(stderr, "error with open(): %s\n", strerror(errno));
  144.                clean(ERROR);
  145.             }
  146.          }
  147.  
  148.       printf("Watching all tty's...\n");
  149.      return;
  150.       }
  151.  
  152.       if (strncasecmp(argv[argc], "tty", 3) != 0) syntax(argv);
  153.  
  154.       strcat(buf, argv[argc]), strcat(buf, " ");
  155.       p = (argv[argc]+3);
  156.  
  157.       sprintf(bfa, "/dev/vcs%d", atoi(p));
  158.       vcs[atoi(p) - 1].fd = i = open(bfa, O_RDONLY | O_NOCTTY);
  159.  
  160.       if (vcs[atoi(p) - 1].fd == ERROR) 
  161.       {
  162.        fprintf(stderr, "error with open(): %s\n", strerror(errno));
  163.      clean(ERROR);
  164.       }
  165.    }
  166.  
  167.    buf[strlen(buf) - 1] = 0;
  168.  
  169.    strcat(buf, "...\n");
  170.    printf(buf);
  171. }
  172.  
  173. void syntax(char **argv)
  174. {
  175.    printf("Syntax: %s <all | tty2 tty3 ttyX ...>\n", argv[0]);
  176.    exit(ERROR);
  177. }
  178.  
  179. void clean(int val)
  180. {
  181.    register int i;
  182.  
  183.    for (i = 0; i < MAXVTNUM; i++)
  184.       if (vcs[i].fd != -1) close(vcs[i].fd);
  185.  
  186.    close(fd);
  187.    exit(val);
  188. }
  189.  
  190. void sighandler(int signum)
  191. {
  192.    char msg[] = "Received signal to abort. Now exiting.\n";
  193.  
  194.    close(fd);
  195.  
  196.    fd = open("/dev/tty", O_NOCTTY | O_WRONLY);
  197.    if (fd == ERROR) 
  198.    {
  199.       printf(msg);
  200.       clean(signum);
  201.    }
  202.  
  203.    /* Give aborting message to current VT. */
  204.    write(fd, msg, sizeof(msg)); 
  205.  
  206.    clean(signum);
  207. }
  208.