client.c: Add comment on the supervisor task.
[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 static void stdin_pre_select_btr(struct sched *s, struct task *t)
50 {
51         struct stdin_task *sit = container_of(t, struct stdin_task, task);
52         int ret;
53
54         t->error = 0;
55         ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
56         if (ret < 0)
57                 sched_min_delay(s);
58         if (ret <= 0)
59                 return;
60         if (btr_pool_unused(sit->btrp) > 0)
61                 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
62         sched_request_timeout_ms(100, s);
63 }
64
65 /**
66  * The post select function of the stdin task.
67  *
68  * \param s The scheduler this task was registered to.
69  * \param t The task structure of the stdin task.
70  *
71  * This function checks if \p STDIN_FILENO was included by in the read fd set
72  * of \a s during the previous pre_select call.  If yes, and \p STDIN_FILENO
73  * appears to be readable, data is read from stdin into the buffer of the
74  * stdin task.
75  */
76 static void stdin_post_select(struct sched *s, struct task *t)
77 {
78         struct stdin_task *sit = container_of(t, struct stdin_task, task);
79         ssize_t ret;
80
81         if (sit->output_error && *sit->output_error < 0) {
82                 t->error = *sit->output_error;
83                 return;
84         }
85         t->error = 0;
86         if (!sit->check_fd)
87                 return;
88         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
89                 return;
90         ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
91         if (ret < 0)
92                 t->error = ERRNO_TO_PARA_ERROR(errno);
93         else if (ret > 0)
94                 sit->loaded += ret;
95         else
96                 t->error = -E_STDIN_EOF;
97 }
98
99 static void stdin_post_select_btr(struct sched *s, struct task *t)
100 {
101         struct stdin_task *sit = container_of(t, struct stdin_task, task);
102         ssize_t ret;
103         size_t sz;
104         char *buf = NULL;
105
106         t->error = 0;
107         ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
108         if (ret < 0)
109                 goto err;
110         if (ret == 0)
111                 return;
112         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
113                 return;
114         sz = btr_pool_get_buffer(sit->btrp, &buf);
115         if (sz == 0)
116                 return;
117         /*
118          * Do not use the maximal size to avoid having only a single buffer
119          * reference for the whole pool. This is bad because if that single
120          * reference can not be freed, we're stuck.
121          */
122         sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
123         ret = read(STDIN_FILENO, buf, sz);
124         if (ret < 0)
125                 ret = -ERRNO_TO_PARA_ERROR(errno);
126         if (ret == 0)
127                 ret = -E_STDIN_EOF;
128         if (ret < 0)
129                 goto err;
130         btr_add_output_pool(sit->btrp, ret, sit->btrn);
131         return;
132 err:
133         btr_remove_node(sit->btrn);
134         //btr_pool_free(sit->btrp);
135         t->error = ret;
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 (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                 sit->btrp = btr_pool_new("stdin", 64 * 1024);
156         } else {
157                 sit->task.pre_select = stdin_pre_select;
158                 sit->task.post_select = stdin_post_select;
159         }
160         sprintf(sit->task.status, "stdin reader");
161         ret = mark_fd_nonblocking(STDIN_FILENO);
162         if (ret >= 0)
163                 return;
164         sit->output_error = NULL;
165         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
166         exit(EXIT_FAILURE);
167 }