Using less in environment without controlling tty

Denys Vlasenko vda.linux at googlemail.com
Wed Aug 7 16:25:02 UTC 2013


On Tue, Aug 6, 2013 at 1:34 AM, Harald Becker <ralda at gmx.de> wrote:
>>Sure I can. In your example, replace
>>
>>echo -e "\0377\0376\0020>|less\0"
>>
>>with
>>
>>echo -e "\0377\0376\0020>|setsid sh -c 'exec less </dev/ttyFOO
>>>/dev/ttyFOO 2>&1'\0"
>>
>>What will happen? The
>>
>>       setsid sh -c 'exec less </dev/ttyFOO >/dev/ttyFOO 2>&1'
>>
>>will be a new COMMAND, won't it be?
>
> No! It will be:
>
> output from console_buffer | sh -c "setsid sh -c 'exec less
> </dev/ttyFOO >/dev/ttyFOO 2>&1'"
>
> ... needs an extra copy of the shell in memory (and even on
> MMU+COW it's space is not zero)

In other words, you concede that it will work.
(If you really concerned about extra memory,
then use "exec setsid...").

>>And the problem with less continuing to run after
>>console_buffer died is ... ?
>
> Nobody may ever kill the less, hanging around until system dies
> or one manually searches for the pid and kills the less :(

Or pressing 'Q' in less.

>>ttyname() pulls in a static buffer. Need to use ttyname_r()
>>to avoid that.
>
> So use xmalloc_ttyname, you have already a function for this.

Yes, that's why I used it.
I tried a few ways to shrink the current git's code
(without changing the logic), so far I didn't find a smaller one.
If you find one, please send the patch.

>>If the error path is taken, stdout is set to nonblocking mode
>>*and never restored*.
>
> Sure and intentional, it is the fallback case which only happens
> when no /proc, /sys *AND* no ctty available / accessible. Rare
> case but happens e.g. during system startup. To use less in this
> case, we either need to fallback to use stdout/stderr or the
> possibility to pass in an open fd (either case will satisfy, but
> one is required).
>
> Have you ever tried to startup Linux with init=/bin/sh (last
> resort rescue), and then tried dmesg|less ... won't work :(

A lot of things won't work. E.g. "ping IP" and oops,
you forgot that ^C won't work because you have no ctty,
thus you can't do anything but look at endless ping output. :(
I was there more than once.

Solution #1: acquire a ctty.
Solution #2: patch "ping" so that ^C works even without ctty.
Repeat ad infinitum for all other tools.

I still think solution 1 makes much more sense.

However, I am willing to accept "less --tty=TTY" patch or similar.
Special casing of fd 3 feels... wrong.


More information about the busybox mailing list