Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Nov 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 19 additions & 6 deletions sched/group/group_setupidlefiles.c
Expand Up @@ -58,7 +58,7 @@

int group_setupidlefiles(FAR struct task_tcb_s *tcb)
{
#ifdef CONFIG_DEV_CONSOLE
#if defined(CONFIG_DEV_CONSOLE) || defined(CONFIG_DEV_NULL)
int fd;
#endif

Expand All @@ -69,34 +69,47 @@ int group_setupidlefiles(FAR struct task_tcb_s *tcb)
* descriptor 0.
*/

#if defined(CONFIG_DEV_CONSOLE) || defined(CONFIG_DEV_NULL)
#ifdef CONFIG_DEV_CONSOLE
fd = nx_open("/dev/console", O_RDWR);
#else
fd = nx_open("/dev/null", O_RDWR);
#endif
if (fd == 0)
{
/* Successfully opened /dev/console as stdin (fd == 0) */
/* Successfully opened stdin (fd == 0) */

nx_dup2(0, 1);
nx_dup2(0, 2);
}
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.
*/

if (fd > 0)
{
sinfo("Open /dev/console fd: %d\n", fd);
sinfo("Open stdin fd: %d\n", fd);
nx_close(fd);
}
else
{
serr("ERROR: Failed to open /dev/console: %d\n", fd);
serr("ERROR: Failed to open stdin: %d\n", fd);
}

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 */

Expand Down