2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file stdin.c Functions that deal with reading from stdin. */
9 #include <dirent.h> /* readdir() */
20 #include "buffer_tree.h"
24 * The pre_select function of the stdin task.
26 * \param s The scheduler this task was registered to.
27 * \param t The task structure of the stdin task.
29 * This function is always successful. If there is space left in the
30 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
33 static void stdin_pre_select(struct sched *s, struct task *t)
35 struct stdin_task *sit = container_of(t, struct stdin_task, task);
39 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
44 if (btr_pool_unused(sit->btrp) > 0)
45 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
46 sched_request_timeout_ms(100, s);
50 * The post select function of the stdin task.
52 * \param s The scheduler this task was registered to.
53 * \param t The task structure of the stdin task.
55 * This function checks if \p STDIN_FILENO was included by in the read fd set
56 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
57 * appears to be readable, data is read from stdin and fed into the buffer
60 static void stdin_post_select(struct sched *s, struct task *t)
62 struct stdin_task *sit = container_of(t, struct stdin_task, task);
68 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
73 sz = btr_pool_get_buffer(sit->btrp, &buf);
77 * Do not use the maximal size to avoid having only a single buffer
78 * reference for the whole pool. This is bad because if that single
79 * reference can not be freed, we're stuck.
81 sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
82 ret = read_nonblock(STDIN_FILENO, buf, sz, &s->rfds, &n);
84 btr_add_output_pool(sit->btrp, n, sit->btrn);
88 btr_remove_node(sit->btrn);
89 //btr_pool_free(sit->btrp);
94 * Initialize a stdin task structure with default values.
96 * \param sit The stdin task structure.
98 * This fills in the pre/post select function pointers of the task structure
99 * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
100 * mode, and a buffer tree is created.
102 void stdin_set_defaults(struct stdin_task *sit)
106 sit->task.pre_select = stdin_pre_select;
107 sit->task.post_select = stdin_post_select;
108 sit->btrp = btr_pool_new("stdin", 64 * 1024);
109 sprintf(sit->task.status, "stdin reader");
110 ret = mark_fd_nonblocking(STDIN_FILENO);
113 PARA_EMERG_LOG("%s\n", para_strerror(-ret));