group_setupidlefiles: fall back to /dev/null if /dev/console is disabled

Tested with a modified nsh on esp32-devkitc.
```
 int main(int argc, FAR char *argv[])
  {
  +  int fd = open("/dev/null", O_RDWR);
  +  _info("hey _info %d\n", fd);
  +  write(1, "hey stdout\n", 11);
  +  write(2, "hey stderr\n", 11);
```
This commit is contained in:
YAMAMOTO Takashi
2022-11-09 15:55:14 +09:00
committed by Xiang Xiao
parent 4501490302
commit 316f6aea71
+19 -6
View File
@@ -58,7 +58,7 @@
int group_setupidlefiles(FAR struct task_tcb_s *tcb) int group_setupidlefiles(FAR struct task_tcb_s *tcb)
{ {
#ifdef CONFIG_DEV_CONSOLE #if defined(CONFIG_DEV_CONSOLE) || defined(CONFIG_DEV_NULL)
int fd; int fd;
#endif #endif
@@ -69,34 +69,47 @@ int group_setupidlefiles(FAR struct task_tcb_s *tcb)
* descriptor 0. * descriptor 0.
*/ */
#if defined(CONFIG_DEV_CONSOLE) || defined(CONFIG_DEV_NULL)
#ifdef CONFIG_DEV_CONSOLE #ifdef CONFIG_DEV_CONSOLE
fd = nx_open("/dev/console", O_RDWR); fd = nx_open("/dev/console", O_RDWR);
#else
fd = nx_open("/dev/null", O_RDWR);
#endif
if (fd == 0) if (fd == 0)
{ {
/* Successfully opened /dev/console as stdin (fd == 0) */ /* Successfully opened stdin (fd == 0) */
nx_dup2(0, 1); nx_dup2(0, 1);
nx_dup2(0, 2); nx_dup2(0, 2);
} }
else else
{ {
/* We failed to open /dev/console OR for some reason, we opened /* We failed to open stdin OR for some reason, we opened
* it and got some file descriptor other than 0. * it and got some file descriptor other than 0.
*/ */
if (fd > 0) if (fd > 0)
{ {
sinfo("Open /dev/console fd: %d\n", fd); sinfo("Open stdin fd: %d\n", fd);
nx_close(fd); nx_close(fd);
} }
else else
{ {
serr("ERROR: Failed to open /dev/console: %d\n", fd); serr("ERROR: Failed to open stdin: %d\n", fd);
} }
return -ENFILE; return -ENFILE;
} }
#endif #else
/* This configuration can confuse user programs and libraries.
* Eg. a program which opens a file and then prints something to
* STDERR_FILENO (2) can end up with something undesirable if the
* file descriptor for the file happens to be 2.
* It's a common practice to keep 0-2 always open even if they are
* /dev/null to avoid that kind of problems. Thus the following warning.
*/
#warning file descriptors 0-2 are not opened
#endif /* defined(CONFIG_DEV_CONSOLE) || defined(CONFIG_DEV_NULL) */
/* Allocate file/socket streams for the TCB */ /* Allocate file/socket streams for the TCB */