LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-15-2007, 07:23 AM   #1
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Rep: Reputation: 15
Redirecting stdout, stderr to pty0?


Hello!

I'm really not a Linux expert and I'm not using it on day to day basic. At work I'm working on embedded device running Linux.

Platform:
MIPS32, 4MB Flash, 16MB Ram
bootloader: uBoot
OS: Linux 2.4.31
BusyBox 1.1.3

We are connecting to the box via serial port RS-232 cable. I was assigned to write driver for the add-on module which also connects to the embedded device via serial port. Problem is we don't have 2 serial ports on the embedded device, but just one. It's not a problem because I installed telnet daemon and now I'm connecting to the device from the PC by telnet and port is free to be used by add-on module. Problem is that while developing device driver I use a lot of printk function and these printouts go to the serial console!

Can somebody tell me or direct me on how to redirect kernel printouts to the /pts device? I know that pts device is created until new telnet client connects, but would it be possible to compile Linux kernel in such a way that while /dev/pts/0 device is not present all printouts should go to the /dev/null device and if /dev/pts/0 is present, printouts are redirected to the /dev/pts/0 device.

As a matter of fact where do I select where do Linux kernel default stdout, stderr streams go?

Thanks a lot for all the help you can give me.

Best regards,

Last edited by Rostfrei; 03-15-2007 at 07:48 AM.
 
Old 03-15-2007, 09:18 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Hiya! By default, stdout and stderr both appear on 'the console'. By 'the concole' I mean the messages will appear on the screen of the terminal or console window from which the application is running.
For example, in my syslogger config file, I have a line something like:
* dev/tty12
which directs everything caught by the system-logger to the console on tty12. I use a graphical X environment, but if I want to see what was the latest output from something I click over to tty12 and there are all the messages from the syslogger.

Now I am not any sort of expert with Linux or Bash, but here's a few basics about error redirection:
there are 3 streams in total, stdin, stdout, and stderr, numbered 0,1 and 2 respectively.
You may have seen the expression 2&>1 here and there. This means direct stderr to stdout.
You can direct errors to a file too:
print "message goes here" > /home/desktop/filename
or
2&> /home/desktop/filename
If you want error output to not show up at all, you would use 2>/dev/null. This sends errors to the bitbucket, or null device (it disappears basicallly).
Your printk functions could be like this:
printk "Hello there, this is a message" > dev/tty2
which would direct the output of the printk statement to tty2, instead of to the console.
If you want as you wrote, to send output to one device if it exists, and if not, to the other device, some scripting like the following at the start of the script would be along the right idea:

if [ -e /dev/pty/0 }; then
LOGGER = /dev/pty/0
else
LOGGER = /dev/null
fi

...
...
printk "now we have a message" > $LOGGER

This would figure out if the pts/0 device exists at the start of the program, and if so, use that as the logger; otherwise, if it doesnt exist, use /dev/null.

To direct both errors and stdout to somewhere else (my syntax may be backwards here) use:
printk " message here " >/dev/null 2>&1
and finally, using only one > symbol when sending to a log file will overwrite the file if it exists, while using >> will append to the file instead.
For proper syntax, and definitely better accuracy as to how to do this stuff, just Google stuff like 'shell error redirect' or 'stderr' or even '2>&1' and you will find loads of tutorial pages about the shell errors.
 
Old 03-16-2007, 01:56 AM   #3
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Original Poster
Rep: Reputation: 15
Hello!

First of all thank you for such an extensive reply.

I know there are stdin, stdout and stderr streams and also how to redirect them when I'm in the bash console already. Your suggestion with bash script is very good, but here I have a little more specific problem. I'm writing just one specific driver for the device which connects to the main embedded machine trough serial RS-232 port. If I want this device driver to work properly:
- file descriptor ttyS0 should be free and not taken by the bash
- serial port should not be disturbed with some characters coming from the kernel printouts or other programs printouts

Moreover I don't want to redirect just my printouts, but all the code printouts also from programs and drivers I didn't wrote. So option printk "now we have a message" > $LOGGER solves just the printouts I generate, but not what others generate.

In the first sentence you said By default, stdout and stderr both appear on 'the console', but what exactly is it defined by "the console"? In my embedded device it is serial port ttyS0 on my host it is some other virtual device (pty?) which prints characters on the display directly. Where this redirection is defined? In kernel somewhere? On my host machine nothing goes to the serial port by default, but on the embedded device everything goes there. So, I'm guessing this has to be defined somewhere.

Best regards,
 
Old 03-16-2007, 02:58 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Hmmm interesting proposition.
I am slightly guessing here, but 2 suggestions (and this is based on my experience with Slackware, with which I am by no means an expert):
"The Console" in my case would be devices tty1 to tty12,which are 'virtual consoles', depending on which console it was in which my system started running. With my system, the default device is tty0, with the screen output appearing on virtual-terminal 6, where root ends up before starting the X environent for me, the user, on tty7. This configuration is determined by various system files located in both the /etc directory, and in the etc/X11 directory (X11 is for the X graphical server).
So yes, the default, or system console in your case would also likely be defined as a default device somewhere in the system config/startup files or scripts which are run or parsed when your system boots.
As for redrecting ALL output from ALL running apps, services, drivers, etc, well, that may not be the simplest thing, however there is typically a daemon running called sysklogd, which is the system logger. The purpose of this is to 'catch' all messages that the system and/or kernel produce, and direct them somewhere.
In my case, for example, messages from the kernel all go to a file I made called 'kern-info'. All mail messages go to user(s), and all emergency messages go to all users.
As I mentioned, I also set up the sysklogd to redirect ALL messages of ANY type to tty12.
Now, that said, assuming your system has a similar system-logger daemon, it will have a configuration file somewhere, which tells the logger what to do with the various types of messages it catches. If this is the case, then the system logger is what you will want to configure, to send NONE of ANY messages to 'the console', but to send ALL of ALL messages to the device you want them sent to.
The kernel does have a bit to do with the verbosity and source of some amount of log and debug messages; many options within the kernel can be configured to be more or less verbose; many drivers and other system daemons and drivers can also be configured with varying levels of verbosity, and can also likely be configured to send their messages to other devices.
I think what you need is one of 2 solutions:
a) Investigate the system-logger daemon/method of your system, and see what options you can start it up with to work towards your goal of having ALL system messages directed to where you want.
or
b) running a daemon either alongside of, or instead of, the 'sysklogd daemon' default systemlogger, and set up this logger to your own personal desires.
Finally, as to determining exactly what 'the console' is on your machine, if you are logged in as root, type the letter 'w' at the command line, and it will tell you who is logged on, and where they are.
When I run it on my system, it tells me that I (user Sasha) is the only user, and is logged onto tty0. Since I know that my X server graphical environment is running on 'Virtual Terminal 7' (vt7, which is one of the tty1 - tty12 that I mentioned above) I am going to guess that in my case, 'the console' by default is tty0. Perhaps this is the default case for most systems? I'm not sure. But try typing the 'w' as root, from your default console, and see what it says for which console you are in.
At this point, you would atleast know 'what' you have to redirect from/to, and in the case of stuff that you wish to keep AWAY from the serial port, the same principle applies: determine which logging method is being used, and if by default it is or isn't sending stuff to where you don't/do want it to go, change the logger.
(Golly, I hope I haven't gotten you as confused by now as I think I am.. This little text area I'm typing in just isn't doing it )
LOL.. The short answer: "1)Yes, I'd say it is defined mostly or completely in configuration file(s). 2) I don't believe you can configure where stdin, stdout and stderr go from within the kernel configuration. and 3) I don't believe it is possible to completely 'quiet the kernel', only redirect the noise."
Best of luck!! If you need me to confuse you more, please, just ask , but really, I hope all this helps you even a little bit!
Take care
~Sasha
PS - I'd also venture a guess that whatever method is used to connect to the server initially, in your case via rs232, that device may end up being 'the console', so you *might* have no way around the RS232 being the receiver of kernel messages.

Last edited by GrapefruiTgirl; 03-16-2007 at 03:25 AM.
 
Old 03-20-2007, 03:15 AM   #5
Rostfrei
Member
 
Registered: Aug 2005
Location: Slovenia
Distribution: Fedora, Ubuntu
Posts: 69

Original Poster
Rep: Reputation: 15
Solution found!

I found solution to my problem. Two actually and both are very simple.

Solution 1.) In the kernel configuration I found the flag CONFIG_SERIAL_CONSOLE. When you are in the kernel configuration (run make menuconfig in the kernel root folder) you go under Character devices ---> Support for console on serial port and turn it off. In my case I also had to make small corrections to the serial driver file because BSP guys did not use define CONFIG_SERIAL_CONSOLE on all places properly and I had undefined references. But if everything is ok, all what is needed is turning off console on serial port and recompiling the kernel. Now /dev/console is not created (it's NULL actually) and all system printouts don't go to the console anymore. Telnet works perfectly and ttyS0 is still there to be used.

Solution 2.) In my case we use boot loader called uBoot. Under many things there, I can also configure "Kernel command line" or specifically variable bootargs variable which placed in the kernel command line when kernel is started. There I added string console=null,115200. It basically says that console device is null. Speed part is not relevant and I didn't try without it. Here also telnet works perfectly and ttyS0 is still there to be used.

I hope this will help someone else looking for the solution to the similar problem.

GrapefruiTgirl thank you again for all the help you generously gave to me.

Best regards,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
send stderr to a file and also to stdout pranavchoudhary Linux - Newbie 7 08-25-2008 01:57 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM
redirecting stdout and stderr to a file Avatar33 Programming 4 03-12-2005 07:55 AM
Reopen the stdout and stderr rahul_kulkarni Programming 3 02-21-2005 06:55 AM
redirecting stderr nodger Programming 2 03-11-2004 10:58 AM


All times are GMT -5. The time now is 08:27 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration