//---------------------------------------------------------------- // setconsole.cpp // // This utility allows a user possessing root privilege // to redirect 'printk' output to a designated console. // // compile using: // root# gcc -o setconsole setconsole.cpp // root# chmod a+s setconsole // // execute using: // user$ setconsole 4 // // Code used here is from an example by Alesandro Rubini, // "Linux Device Drivers (2nd Edition)," pages 99-100. // // programmer: ALLAN CRUSE // written on: 24 NOV 2002 // revised on: 24 JUN 2007 -- to use "/dev/console" device //---------------------------------------------------------------- #include // for open() <--- added #include // for fprintf() #include // for errno #include // for exit() #include // for STDIN_FILENO #include // for strerror() #include // for ioctl() #include // for TIOCLINUX int main( int argc, char **argv ) { char bytes[ 2 ] = { 11, 0 }; // 11 is the TIOCLINUX command-number if ( argc == 2 ) bytes[1] = atoi( argv[1] ); // console id-number else { fprintf( stderr, "%s: need a single argument\n", argv[0] ); exit(1); } int fd = open( "/dev/console", O_RDWR ); // <--- added if ( fd < 0 ) { perror( "/dev/console" ); exit(1); } // <--- added if ( ioctl( fd, TIOCLINUX, bytes ) < 0 ) // <--- changed { fprintf( stderr, "%s: ioctl( fd, TIOCLINUX ): %s\n", // <--- argv[0], strerror( errno ) ); exit(1); } exit(0); }