procd: improve early console handling
[openwrt.git] / package / system / procd / patches / 0001-early-keep-stdio-files-open.patch
1 From 13ebb50d2789de7bd47cebe57e3f6eba58fdcc7e Mon Sep 17 00:00:00 2001
2 From: Gabor Juhos <juhosg@openwrt.org>
3 Date: Fri, 19 Jul 2013 08:43:35 +0200
4 Subject: [PATCH 1/2] early: keep stdio files open
5
6 At the end of the 'early_console' function, the
7 file descriptor is closed unconditionally. This
8 'close' call closes the stdio files if the fd
9 returned by the 'open(dev/console)' call equals
10 with any of the STD{IN,OUT,ERR}_FILENO values.
11 When this happens, all subsequent accesses to
12 the stdio files will fail and early console
13 access won't work.
14
15 To avoid this, don't close the file descriptor if
16 that equals with any of the STD*_FILENO values.
17
18 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
19 ---
20 Note:
21
22 The issue happens if Linux is unable to open the
23 initial console before calling init. In this case,
24 the 'open(dev/console)' call in the 'early_console'
25 function returns with zero.
26 ---
27  early.c |    6 +++++-
28  1 file changed, 5 insertions(+), 1 deletion(-)
29
30 diff --git a/early.c b/early.c
31 index 27d0929..204623b 100644
32 --- a/early.c
33 +++ b/early.c
34 @@ -65,7 +65,11 @@ static void early_console(const char *dev)
35         dup2(dd, STDIN_FILENO);
36         dup2(dd, STDOUT_FILENO);
37         dup2(dd, STDERR_FILENO);
38 -       close(dd);
39 +
40 +       if (dd != STDIN_FILENO &&
41 +           dd != STDOUT_FILENO &&
42 +           dd != STDERR_FILENO)
43 +               close(dd);
44  }
45  
46  static void early_env(void)
47 -- 
48 1.7.10
49