Problems with output to console

Rob Landley rob at
Sat Dec 17 22:33:42 UTC 2005

On Saturday 17 December 2005 09:58, Steven Scholz wrote:
> Rob,
> > Read init/init.c (which is, alas, a mess o' suckage).
> >
> > static char console[CONSOLE_BUFF_SIZE] = _PATH_CONSOLE;
> >
> > (That's from some header file).
> >
> > static void console_init(void)
> > {
> > ...
> >     if ((s = getenv("CONSOLE")) != NULL || (s = getenv("console")) !=
> > NULL) { safe_strncpy(console, s, sizeof(console));
> >
> > And the read on for the full horror of serial console detection and tty
> > and such...  It gets ugly quickly...
> I wonder how I could set CONSOLE or console!?

Kernel command line.  Anything of the form "keyword=value" that's not 
interpreted as a kernel __setup() value gets set as an environment variable 
for PID 1 (init).  The easy way to make sure something is passed on to 
userspace is to use an uppercase keyword, since all the kernel's are lower 

> If I do it in /etc/profile it is much too late!
> So which environment is used in console_init()?
> The default one that is setup by libbb/setup_environment.c ?

PID 1 (init) has its environment set up by the kernel.

> I noticed that when /dev/ttyS0 does not exist, busybox uses /dev/console
> and I see the output from my application as expected.
>  init: Starting pid 124, console /dev/console: '/opt/imc/start_imc'
> But if it exists busybox uses /dev/ttyS0 as console
>  init: Starting pid 127, console /dev/ttyS0: '/opt/imc/start_imc'
> Fair enough. But I still don't understand why I don't see any outputs
> from start_imc when the console is /dev/ttyS0!

What I do to trace this kind of thing is stick printfs() into the code.  In 
the case of init trying to set stdin and stdout to a reasonable console this 
is slightly tricky, but not that bad.  Just create a global variable:

int debugfd;

Then in main do debugfd=open("/dev/tty0",O_RDWR) (or whatever device you want 
the output to go to), and then instead of sprinkling in printf("ook\n") 
instead go dprintf(debugfd,"ook\n");

> Is it why /dev/console is a different device then /dev/ttyS0 (chardev
> 4,64 vs. 5,1) although it is both the same serial port?
> Or is it because I start a shell on this port and thus /dev/ttyS0 can't
> no longer be used as a console for applications started early in the
> boot process?
> PLEASE shed some light, Rob.

I'm not familiar with your environment, and the last time I looked at the init 
code was in the context of a massive rewrite tha tnever got checked in, and 
most of what I'm remembering is what I changed it _to_ (because what it was 
doing before was crazy).  But that's how you would go about debugging it.

It's possible I'll resurrect the init rewrite in the 1.2 timeframe, but it 
would be behind the bzip2 compression support which is pretty far down the 

Steve Ballmer: Innovation!  Inigo Montoya: You keep using that word.
I do not think it means what you think it means.

More information about the busybox mailing list