stream cipher: Allow in-place encryption.
[paraslash.git] / stdin.c
1 /*
2 * Copyright (C) 2006-2011 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 <assert.h>
10 #include <stdbool.h>
11 #include <regex.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "fd.h"
17 #include "error.h"
18 #include "stdin.h"
19 #include "buffer_tree.h"
20 #include "string.h"
21
22 /**
23 * The pre_select function of the stdin task.
24 *
25 * \param s The scheduler this task was registered to.
26 * \param t The task structure of the stdin task.
27 *
28 * This function is always successful. If there is space left in the
29 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
30 * of \a s.
31 */
32 static void stdin_pre_select(struct sched *s, struct task *t)
33 {
34 struct stdin_task *sit = container_of(t, struct stdin_task, task);
35 int ret;
36
37 t->error = 0;
38 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
39 if (ret < 0)
40 sched_min_delay(s);
41 if (ret <= 0)
42 return;
43 if (btr_pool_unused(sit->btrp) > 0)
44 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
45 sched_request_timeout_ms(100, s);
46 }
47
48 /**
49 * The post select function of the stdin task.
50 *
51 * \param s The scheduler this task was registered to.
52 * \param t The task structure of the stdin task.
53 *
54 * This function checks if \p STDIN_FILENO was included by in the read fd set
55 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
56 * appears to be readable, data is read from stdin and fed into the buffer
57 * tree.
58 */
59 static void stdin_post_select(struct sched *s, struct task *t)
60 {
61 struct stdin_task *sit = container_of(t, struct stdin_task, task);
62 ssize_t ret;
63 size_t sz, n;
64 char *buf = NULL;
65
66 t->error = 0;
67 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
68 if (ret < 0)
69 goto err;
70 if (ret == 0)
71 return;
72 sz = btr_pool_get_buffer(sit->btrp, &buf);
73 if (sz == 0)
74 return;
75 /*
76 * Do not use the maximal size to avoid having only a single buffer
77 * reference for the whole pool. This is bad because if that single
78 * reference can not be freed, we're stuck.
79 */
80 sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
81 ret = read_nonblock(STDIN_FILENO, buf, sz, &s->rfds, &n);
82 if (n > 0)
83 btr_add_output_pool(sit->btrp, n, sit->btrn);
84 if (ret >= 0)
85 return;
86 err:
87 btr_remove_node(sit->btrn);
88 //btr_pool_free(sit->btrp);
89 t->error = ret;
90 }
91
92 /**
93 * Initialize a stdin task structure with default values.
94 *
95 * \param sit The stdin task structure.
96 *
97 * This fills in the pre/post select function pointers of the task structure
98 * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
99 * mode, and a buffer tree is created.
100 */
101 void stdin_set_defaults(struct stdin_task *sit)
102 {
103 int ret;
104
105 sit->task.pre_select = stdin_pre_select;
106 sit->task.post_select = stdin_post_select;
107 sit->btrp = btr_pool_new("stdin", 64 * 1024);
108 sprintf(sit->task.status, "stdin reader");
109 ret = mark_fd_nonblocking(STDIN_FILENO);
110 if (ret >= 0)
111 return;
112 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
113 exit(EXIT_FAILURE);
114 }