[btr] Add the node type parameter to btr_node_status().
[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 para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
58 else if (ret < 0) {
59 s->timeout.tv_sec = 0;
60 s->timeout.tv_usec = 1;
61 }
62 }
63
64 /**
65 * The post select function of the stdin task.
66 *
67 * \param s The scheduler this task was registered to.
68 * \param t The task structure of the stdin task.
69 *
70 * This function checks if \p STDIN_FILENO was included by in the read fd set
71 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
72 * appears to be readable, data is read from stdin into the buffer of the
73 * stdin task.
74 */
75 static void stdin_post_select(struct sched *s, struct task *t)
76 {
77 struct stdin_task *sit = container_of(t, struct stdin_task, task);
78 ssize_t ret;
79
80 if (sit->output_error && *sit->output_error < 0) {
81 t->error = *sit->output_error;
82 return;
83 }
84 t->error = 0;
85 if (!sit->check_fd)
86 return;
87 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
88 return;
89 ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
90 if (ret < 0)
91 t->error = ERRNO_TO_PARA_ERROR(errno);
92 else if (ret > 0)
93 sit->loaded += ret;
94 else
95 t->error = -E_STDIN_EOF;
96 }
97
98 #define STDIN_INPUT_BUFFER_SIZE (1024 * 32)
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 char *buf = NULL;
104
105 t->error = 0;
106 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
107 if (ret < 0)
108 goto err;
109 if (ret == 0)
110 return;
111 if (!FD_ISSET(STDIN_FILENO, &s->rfds))
112 return;
113 buf = para_malloc(STDIN_INPUT_BUFFER_SIZE);
114 ret = read(STDIN_FILENO, buf, STDIN_INPUT_BUFFER_SIZE);
115 //PARA_CRIT_LOG("read ret: %d\n", ret);
116 if (ret < 0)
117 t->error = -ERRNO_TO_PARA_ERROR(errno);
118 if (ret == 0)
119 t->error = -E_STDIN_EOF;
120 if (t->error < 0)
121 goto err;
122 btr_add_output(buf, ret, sit->btrn);
123 return;
124 err:
125 free(buf);
126 btr_remove_node(sit->btrn);
127 t->error = ret;
128 }
129
130 /**
131 * Initialize a stdin task structure with default values.
132 *
133 * \param sit The stdin task structure.
134 *
135 * This fills in the pre/post select function pointers of the task structure
136 * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
137 * mode and \a bufsize is initialized to 16 KB (but no buffer is allocated).
138 */
139 void stdin_set_defaults(struct stdin_task *sit)
140 {
141 int ret;
142
143 sit->bufsize = 32 * 1024;
144 if (sit->btrn) {
145 sit->task.pre_select = stdin_pre_select_btr;
146 sit->task.post_select = stdin_post_select_btr;
147 } else {
148 sit->task.pre_select = stdin_pre_select;
149 sit->task.post_select = stdin_post_select;
150 }
151 sprintf(sit->task.status, "stdin reader");
152 ret = mark_fd_nonblocking(STDIN_FILENO);
153 if (ret >= 0)
154 return;
155 sit->output_error = NULL;
156 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
157 exit(EXIT_FAILURE);
158 }