Email reply to <ldd3> "setconsole. c" alesssandro Rubini

Source: Internet
Author: User
Email Reply to "setconsole. c" alesssandro Rubini

With a try-on mentality, I wrote a help e-mail to the idol and replied !!! My little heart ~

Haha ~ Who is Rubini...

My help is mainly about setconsole. C. This is a small program of ldd3.

Setconsole. c
/* * setconsole.c -- choose a console to receive kernel messages * * Copyright (C) 1998,2000,2001 Alessandro Rubini *  *   This program is free software; you can redistribute it and/or modify *   it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version. * *   This program is distributed in the hope that it will be useful, *   but WITHOUT ANY WARRANTY; without even the implied warranty of *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *   GNU General Public License for more details. * *   You should have received a copy of the GNU General Public License *   along with this program; if not, write to the Free Software *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/ioctl.h>int main(int argc, char **argv){    char bytes[2] = {11,0}; /* 11 is the TIOCLINUX cmd number */    if (argc==2) bytes[1] = atoi(argv[1]); /* the chosen console */                                                                                                   else {        fprintf(stderr, "%s: need a single arg\n",argv[0]); exit(1);    }    if (ioctl(STDIN_FILENO, TIOCLINUX, bytes)<0) {    /* use stdin */        fprintf(stderr,"%s: ioctl(stdin, TIOCLINUX): %s\n",                argv[0], strerror(errno));        exit(1);    }    exit(0);}

However, the test between the virtual console has never been successful ~ I used * a search engine to retrieve many blogs, all of which are water... the content of the original book is directly copied, and only one of them is found to have been tested for the program, but the test is not successful. Google cannot use it. I don't know the situation. If it is the same, it is retrieved using TTT, there is no substantive setconsole. c's discussion and testing!

The following is the reply from Rubini to me:

First, I pointed out my mistakes.

1. IOCTL (tioclinux) does not affect printf, but printk!

2. tty or terminal is not the console! CTRL + ALT + F * (1 ~ 6) The console is reached, and F7 is a graphical environment, not a console!

Allan cruse made some improvements to setconsole in 2007

//----------------------------------------------------------------//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 <fcntl.h>// for open()<--- added #include <stdio.h>// for fprintf()#include <errno.h>// for errno#include <stdlib.h>// for exit()#include <unistd.h>// for STDIN_FILENO#include <string.h>// for strerror()#include <sys/ioctl.h>// for ioctl()#include <asm/ioctls.h>// for TIOCLINUXint main( int argc, char **argv ){charbytes[ 2 ] = { 11, 0 };// 11 is the TIOCLINUX command-numberif ( argc == 2 ) bytes[1] = atoi( argv[1] );// console id-numberelse{fprintf( stderr, "%s: need a single argument\n", argv[0] );exit(1);}intfd = open( "/dev/console", O_RDWR );// <--- addedif ( fd < 0 ) { perror( "/dev/console" ); exit(1); }// <--- addedif ( ioctl( fd, TIOCLINUX, bytes ) < 0 )// <--- changed{fprintf( stderr, "%s: ioctl( fd, TIOCLINUX ): %s\n", // <--- argv[0], strerror( errno ) );exit(1);}exit(0);}

Yes, Io can be redirected between consoles !!

This figure shows tty4. I redirected the console Io to Console 3, that is,/dev/tty3.

Switch to tty2. We can insert the hello. Ko module, call printk, and print Hello world.

I deliberately executed the TTY shell program and prompted the reader to perform the insmod operation in tty2.

Why is there a hello World? Where have you been? Console 3!

Press CTRL + ALT + F3 to switch to Console 3.

Finally! IOCTL is used to implement Io redirection between different consoles!

Finally, thank you ~ Alesssandro Rubini & Allan cruse

Email reply to <ldd3> "setconsole. c" alesssandro Rubini

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.