2 * Copyright (C) 2006-2013 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. */
18 #include "buffer_tree.h"
22 * The pre_select function of the stdin task.
24 * \param s The scheduler this task was registered to.
25 * \param t The task structure of the stdin task.
27 * This function is always successful. If there is space left in the
28 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
31 static void stdin_pre_select(struct sched
*s
, struct task
*t
)
33 struct stdin_task
*sit
= container_of(t
, struct stdin_task
, task
);
36 ret
= btr_node_status(sit
->btrn
, 0, BTR_NT_ROOT
);
41 if (btr_pool_unused(sit
->btrp
) > 0)
42 return para_fd_set(STDIN_FILENO
, &s
->rfds
, &s
->max_fileno
);
43 sched_request_timeout_ms(100, s
);
47 * The post select function of the stdin task.
49 * \param s The scheduler this task was registered to.
50 * \param t The task structure of the stdin task.
52 * This function checks if \p STDIN_FILENO was included by in the read fd set
53 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
54 * appears to be readable, data is read from stdin and fed into the buffer
57 static int stdin_post_select(struct sched
*s
, struct task
*t
)
59 struct stdin_task
*sit
= container_of(t
, struct stdin_task
, task
);
64 ret
= btr_node_status(sit
->btrn
, 0, BTR_NT_ROOT
);
69 sz
= btr_pool_get_buffer(sit
->btrp
, &buf
);
72 if (sit
->must_set_nonblock_flag
) {
73 ret
= mark_fd_nonblocking(STDIN_FILENO
);
76 sit
->must_set_nonblock_flag
= false;
79 * Do not use the maximal size to avoid having only a single buffer
80 * reference for the whole pool. This is bad because if that single
81 * reference can not be freed, we're stuck.
83 sz
= PARA_MIN(sz
, btr_pool_size(sit
->btrp
) / 2);
84 ret
= read_nonblock(STDIN_FILENO
, buf
, sz
, &s
->rfds
, &n
);
86 btr_add_output_pool(sit
->btrp
, n
, sit
->btrn
);
90 btr_remove_node(&sit
->btrn
);
91 /* Revert to blocking mode if necessary. */
92 fcntl(STDIN_FILENO
, F_SETFL
, sit
->fd_flags
);
93 //btr_pool_free(sit->btrp);
98 * Initialize a stdin task structure with default values.
100 * \param sit The stdin task structure.
102 * This fills in the pre/post select function pointers of the task structure
103 * given by \a sit and creates a buffer tree for I/O.
105 void stdin_set_defaults(struct stdin_task
*sit
)
109 sit
->task
.pre_select
= stdin_pre_select
;
110 sit
->task
.post_select
= stdin_post_select
;
111 sit
->btrp
= btr_pool_new("stdin", 128 * 1024);
112 sprintf(sit
->task
.status
, "stdin reader");
114 * Both STDIN_FILENO and STDOUT_FILENO may refer to the same open file
115 * description (the terminal), and thus share the same file status
116 * flags. In order to not interfere with the stdout task, we only get
117 * the file status flags for STDIN here and save a copy. The nonblock
118 * flag is set later on the first read.
120 ret
= fcntl(STDIN_FILENO
, F_GETFL
);
122 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno
));
126 sit
->must_set_nonblock_flag
= (sit
->fd_flags
& O_NONBLOCK
) == 0;