From: Charles Hedrick (hedrick@geneva.rutgers.edu)
Date: 01/02/93


From: hedrick@geneva.rutgers.edu (Charles Hedrick)
Subject: diffs to allow xterm to get console output
Date: 2 Jan 1993 20:22:55 GMT

In 0.99pl2, Linus added a feature to allow xterm to snarf console
output. Here's a diff for xterm to implement it. This only works if
xterm is setuid root, which of course it normally should be.

The one loose end is cleaning up the subprocess if xterm terminates.
Normally when I exit from xterm, the cat subprocess goes away. But
once it didn't. I'm not sure what is going on. I suspect most people
start up X and leave it active. If so, this isn't an issue.

*** main.c.O Thu Apr 9 21:43:26 1992
--- main.c Sat Jan 2 14:57:15 1993
***************
*** 982,992 ****
  #ifdef TIOCCONS
                {
                    struct stat sbuf;
  
                    /* Must be owner and have read/write permission.
                       xdm cooperates to give the console the right user. */
                    if ( !stat("/dev/console", &sbuf) &&
! (sbuf.st_uid == getuid()) &&
                         !access("/dev/console", R_OK|W_OK))
                    {
                        Console = TRUE;
--- 1023,1034 ----
  #ifdef TIOCCONS
                {
                    struct stat sbuf;
+ int myuid = getuid();
  
                    /* Must be owner and have read/write permission.
                       xdm cooperates to give the console the right user. */
                    if ( !stat("/dev/console", &sbuf) &&
! (sbuf.st_uid == myuid || myuid == 0) &&
                         !access("/dev/console", R_OK|W_OK))
                    {
                        Console = TRUE;
***************
*** 2127,2132 ****
--- 2177,2183 ----
  #endif /* sony */
  #endif /* !USE_SYSV_TERMIO */
  #ifdef TIOCCONS
+ #ifndef linux
                    if (Console) {
                        int on = 1;
                        if (ioctl (tty, TIOCCONS, (char *)&on) == -1)
***************
*** 2133,2138 ****
--- 2184,2190 ----
                            fprintf(stderr, "%s: cannot open console\n",
                                    xterm_name);
                    }
+ #endif /* ! linux */
  #endif /* TIOCCONS */
                }
  
***************
*** 2371,2376 ****
--- 2425,2450 ----
                (void)write(cp_pipe[1], (char *)&handshake, sizeof(handshake));
  #endif /* USE_HANDSHAKE */
  #endif/* UTMP */
+
+ #ifdef linux
+ if (Console) {
+ /* the obvious thing would be system("cat </proc/kmsg&"). Unfortunately
+ * that isn't safe for a setuid program, which this is. This is also why
+ * we use an explicit path for cat.
+ */
+ if (fork() == 0) {
+ int procfd;
+
+ if ((procfd = open("/proc/kmsg", 0)) < 0)
+ exit(1);
+ dup2(procfd, 0);
+ close(procfd);
+ execl("/bin/cat", "cat", 0);
+ execl("/usr/bin/cat", "cat", 0);
+ exit(1);
+ }
+ }
+ #endif
  
                (void) setgid (screen->gid);
  #ifdef HAS_BSD_GROUPS