2 * Copyright (C) 2006-2009 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
);
37 if (sit
->output_error
&& *sit
->output_error
< 0) {
38 t
->error
= *sit
->output_error
;
43 if (sit
->loaded
>= sit
->bufsize
)
46 para_fd_set(STDIN_FILENO
, &s
->rfds
, &s
->max_fileno
);
49 #define STDIN_MAX_PENDING (100 * 1024)
51 static void stdin_pre_select_btr(struct sched
*s
, struct task
*t
)
53 struct stdin_task
*sit
= container_of(t
, struct stdin_task
, task
);
55 if (btr_no_children(sit
->btrn
)) { /* TODO: defer node deletion to post select */
56 t
->error
= -E_STDIN_NO_CHILD
;
57 btr_del_node(sit
->btrn
);
62 if (btr_bytes_pending(sit
->btrn
) > STDIN_MAX_PENDING
)
66 para_fd_set(STDIN_FILENO
, &s
->rfds
, &s
->max_fileno
);
71 * The post select function of the stdin task.
73 * \param s The scheduler this task was registered to.
74 * \param t The task structure of the stdin task.
76 * This function checks if \p STDIN_FILENO was included by in the read fd set
77 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
78 * appears to be readable, data is read from stdin into the buffer of the
81 static void stdin_post_select(struct sched
*s
, struct task
*t
)
83 struct stdin_task
*sit
= container_of(t
, struct stdin_task
, task
);
86 if (sit
->output_error
&& *sit
->output_error
< 0) {
87 t
->error
= *sit
->output_error
;
93 if (!FD_ISSET(STDIN_FILENO
, &s
->rfds
))
95 ret
= read(STDIN_FILENO
, sit
->buf
+ sit
->loaded
, sit
->bufsize
- sit
->loaded
);
97 t
->error
= ERRNO_TO_PARA_ERROR(errno
);
101 t
->error
= -E_STDIN_EOF
;
104 #define STDIN_INPUT_BUFFER_SIZE (1024 * 32)
105 static void stdin_post_select_btr(struct sched
*s
, struct task
*t
)
107 struct stdin_task
*sit
= container_of(t
, struct stdin_task
, task
);
111 t
->error
= -E_STDIN_NO_CHILD
;
112 if (btr_no_children(sit
->btrn
))
118 if (!FD_ISSET(STDIN_FILENO
, &s
->rfds
))
121 buf
= para_malloc(STDIN_INPUT_BUFFER_SIZE
);
122 ret
= read(STDIN_FILENO
, buf
, STDIN_INPUT_BUFFER_SIZE
);
123 //PARA_CRIT_LOG("read ret: %d\n", ret);
125 t
->error
= -ERRNO_TO_PARA_ERROR(errno
);
127 t
->error
= -E_STDIN_EOF
;
130 btr_add_output(buf
, ret
, sit
->btrn
);
134 btr_del_node(sit
->btrn
);
139 * Initialize a stdin task structure with default values.
141 * \param sit The stdin task structure.
143 * This fills in the pre/post select function pointers of the task structure
144 * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
145 * mode and \a bufsize is initialized to 16 KB (but no buffer is allocated).
147 void stdin_set_defaults(struct stdin_task
*sit
)
151 sit
->bufsize
= 32 * 1024;
153 sit
->task
.pre_select
= stdin_pre_select_btr
;
154 sit
->task
.post_select
= stdin_post_select_btr
;
156 sit
->task
.pre_select
= stdin_pre_select
;
157 sit
->task
.post_select
= stdin_post_select
;
159 sprintf(sit
->task
.status
, "stdin reader");
160 ret
= mark_fd_nonblocking(STDIN_FILENO
);
163 sit
->output_error
= NULL
;
164 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));