int sched_setuptaskfiles(FAR _TCB *tcb) { /* Allocate file descriptors for the TCB */ #if CONFIG_NFILE_DESCRIPTORS > 0 tcb->filelist = files_alloclist(); if (!tcb->filelist) { return -ENOMEM; } #endif /* Allocate socket descriptors for the TCB */ #if CONFIG_NSOCKET_DESCRIPTORS > 0 tcb->sockets = net_alloclist(); if (!tcb->sockets) { return -ENOMEM; } #endif /* Duplicate the parent task's file descriptors */ sched_dupfiles(tcb); /* Duplicate the parent task's socket descriptors */ sched_dupsockets(tcb); /* Allocate file/socket streams for the new TCB */ #if CONFIG_NFILE_STREAMS > 0 return sched_setupstreams(tcb); #else return OK; #endif }
int sched_setupidlefiles(FAR _TCB *tcb) { #if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE) int fd; #endif #if CONFIG_NFILE_DESCRIPTORS > 0 /* Allocate file descriptors for the TCB */ tcb->filelist = files_alloclist(); if (!tcb->filelist) { return -ENOMEM; } #endif /* CONFIG_NFILE_DESCRIPTORS */ #if CONFIG_NSOCKET_DESCRIPTORS > 0 /* Allocate socket descriptors for the TCB */ tcb->sockets = net_alloclist(); if (!tcb->sockets) { return -ENOMEM; } #endif /* CONFIG_NSOCKET_DESCRIPTORS */ #if CONFIG_NFILE_DESCRIPTORS > 0 && defined(CONFIG_DEV_CONSOLE) /* Open stdin, dup to get stdout and stderr. This should always * be the first file opened and, hence, should always get file * descriptor 0. */ fd = open("/dev/console", O_RDWR); if (fd == 0) { /* Successfully opened /dev/console as stdin (fd == 0) */ (void)file_dup2(0, 1); (void)file_dup2(0, 2); } else { /* We failed to open /dev/console OR for some reason, we opened * it and got some file descriptor other than 0. */ if (fd >- 0) { slldbg("Open /dev/console fd: %d\n", fd); (void)close(fd); } else { slldbg("Failed to open /dev/console: %d\n", errno); } return -ENFILE; } #if CONFIG_NFILE_STREAMS > 0 /* Allocate file strems for the TCB */ return sched_setupstreams(tcb); #else return OK; #endif /* CONFIG_NFILE_STREAMS */ #else return OK; #endif /* CONFIG_NFILE_DESCRIPTORS && CONFIG_DEV_CONSOLE */ }