generic_filter_pre_select(): Fix off-by-one.
[paraslash.git] / stdin.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file stdin.c Functions that deal with reading from stdin. */
8
9 #include <dirent.h> /* readdir() */
10 #include <assert.h>
11 #include <stdbool.h>
12 #include <regex.h>
13
14 #include "para.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "fd.h"
18 #include "error.h"
19 #include "stdin.h"
20 #include "buffer_tree.h"
21 #include "string.h"
22
23 /**
24  * The pre_select function of the stdin task.
25  *
26  * \param s The scheduler this task was registered to.
27  * \param t The task structure of the stdin task.
28  *
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
31  * of \a s.
32  */
33 static void stdin_pre_select(struct sched *s, struct task *t)
34 {
35         struct stdin_task *sit = container_of(t, struct stdin_task, task);
36
37         if (sit->output_error && *sit->output_error < 0) {
38                 t->error = *sit->output_error;
39                 return;
40         }
41         t->error = 0;
42         sit->check_fd = 0;
43         if (sit->loaded >= sit->bufsize)
44                 return;
45         sit->check_fd = 1;
46         para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
47 }
48
49 #define STDIN_MAX_PENDING (100 * 1024)
50
51 static void stdin_pre_select_btr(struct sched *s, struct task *t)
52 {
53         struct stdin_task *sit = container_of(t, struct stdin_task, task);
54
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);
58                 sit->btrn = NULL;
59                 return;
60         }
61         t->error = 0;
62         if (btr_bytes_pending(sit->btrn) > STDIN_MAX_PENDING)
63                 sit->check_fd = 0;
64         else {
65                 sit->check_fd = 1;
66                 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
67         }
68 }
69
70 /**
71  * The post select function of the stdin task.
72  *
73  * \param s The scheduler this task was registered to.
74  * \param t The task structure of the stdin task.
75  *
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
79  * stdin task.
80  */
81 static void stdin_post_select(struct sched *s, struct task *t)
82 {
83         struct stdin_task *sit = container_of(t, struct stdin_task, task);
84         ssize_t ret;
85
86         if (sit->output_error && *sit->output_error < 0) {
87                 t->error = *sit->output_error;
88                 return;
89         }
90         t->error = 0;
91         if (!sit->check_fd)
92                 return;
93         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
94                 return;
95         ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
96         if (ret < 0)
97                 t->error = ERRNO_TO_PARA_ERROR(errno);
98         else if (ret > 0)
99                 sit->loaded += ret;
100         else
101                 t->error = -E_STDIN_EOF;
102 }
103
104 #define STDIN_INPUT_BUFFER_SIZE 4000
105 static void stdin_post_select_btr(struct sched *s, struct task *t)
106 {
107         struct stdin_task *sit = container_of(t, struct stdin_task, task);
108         ssize_t ret;
109         char *buf = NULL;
110
111         t->error = -E_STDIN_NO_CHILD;
112         if (btr_no_children(sit->btrn))
113                 goto err;
114
115         t->error = 0;
116         if (!sit->check_fd)
117                 return;
118         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
119                 return;
120
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);
124         if (ret < 0)
125                 t->error = -ERRNO_TO_PARA_ERROR(errno);
126         if (ret == 0)
127                 t->error = -E_STDIN_EOF;
128         if (t->error < 0)
129                 goto err;
130         btr_add_output(buf, ret, sit->btrn);
131         return;
132 err:
133         free(buf);
134         btr_del_node(sit->btrn);
135         sit->btrn = NULL;
136 }
137
138 /**
139  * Initialize a stdin task structure with default values.
140  *
141  * \param sit The stdin task structure.
142  *
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).
146  */
147 void stdin_set_defaults(struct stdin_task *sit)
148 {
149         int ret;
150
151         sit->bufsize = 32 * 1024;
152         if (sit->btrn) {
153                 sit->task.pre_select = stdin_pre_select_btr;
154                 sit->task.post_select = stdin_post_select_btr;
155         } else {
156                 sit->task.pre_select = stdin_pre_select;
157                 sit->task.post_select = stdin_post_select;
158         }
159         sprintf(sit->task.status, "stdin reader");
160         ret = mark_fd_nonblocking(STDIN_FILENO);
161         if (ret >= 0)
162                 return;
163         sit->output_error = NULL;
164         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
165         exit(EXIT_FAILURE);
166 }