sched: Dont use fixed-size buffer for task names.
[paraslash.git] / stdin.c
1 /*
2 * Copyright (C) 2006-2014 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 <regex.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "fd.h"
16 #include "error.h"
17 #include "stdin.h"
18 #include "buffer_tree.h"
19 #include "string.h"
20
21 /**
22 * The pre_select function of the stdin task.
23 *
24 * \param s The scheduler this task was registered to.
25 * \param t The task structure of the stdin task.
26 *
27 * This function is always successful. If there is space left in the
28 * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
29 * of \a s.
30 */
31 static void stdin_pre_select(struct sched *s, struct task *t)
32 {
33 struct stdin_task *sit = task_context(t);
34 int ret;
35
36 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
37 if (ret < 0)
38 sched_min_delay(s);
39 if (ret <= 0)
40 return;
41 if (btr_pool_unused(sit->btrp) > 0)
42 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
43 sched_request_timeout_ms(100, s);
44 }
45
46 /**
47 * The post select function of the stdin task.
48 *
49 * \param s The scheduler this task was registered to.
50 * \param t The task structure of the stdin task.
51 *
52 * This function checks if \p STDIN_FILENO was included by in the read fd set
53 * of \a s during the previous pre_select call. If yes, and \p STDIN_FILENO
54 * appears to be readable, data is read from stdin and fed into the buffer
55 * tree.
56 */
57 static int stdin_post_select(struct sched *s, struct task *t)
58 {
59 struct stdin_task *sit = task_context(t);
60 ssize_t ret;
61 size_t sz, n;
62 char *buf = NULL;
63
64 ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
65 if (ret < 0)
66 goto err;
67 if (ret == 0)
68 return 0;
69 sz = btr_pool_get_buffer(sit->btrp, &buf);
70 if (sz == 0)
71 return 0;
72 if (sit->must_set_nonblock_flag) {
73 ret = mark_fd_nonblocking(STDIN_FILENO);
74 if (ret < 0)
75 goto err;
76 sit->must_set_nonblock_flag = false;
77 }
78 /*
79 * Do not use the maximal size to avoid having only a single buffer
80 * reference for the whole pool. This is bad because if that single
81 * reference can not be freed, we're stuck.
82 */
83 sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
84 ret = read_nonblock(STDIN_FILENO, buf, sz, &s->rfds, &n);
85 if (n > 0)
86 btr_add_output_pool(sit->btrp, n, sit->btrn);
87 if (ret >= 0)
88 return 0;
89 err:
90 btr_remove_node(&sit->btrn);
91 /* Revert to blocking mode if necessary. */
92 fcntl(STDIN_FILENO, F_SETFL, sit->fd_flags);
93 //btr_pool_free(sit->btrp);
94 return ret;
95 }
96
97 /**
98 * Register a stdin task structure.
99 *
100 * \param sit The stdin task structure to register.
101 * \param s The task will be added to this scheduler's task list.
102 *
103 * This allocates a buffer tree pool for I/O, sets up \a sit and registers a
104 * task with \a sit as context pointer.
105 */
106 void stdin_task_register(struct stdin_task *sit, struct sched *s)
107 {
108 int ret;
109 struct task_info ti = {
110 .name = "stdin",
111 .pre_select = stdin_pre_select,
112 .post_select = stdin_post_select,
113 .context = sit,
114 };
115
116 sit->btrp = btr_pool_new("stdin", 128 * 1024);
117 /*
118 * Both STDIN_FILENO and STDOUT_FILENO may refer to the same open file
119 * description (the terminal), and thus share the same file status
120 * flags. In order to not interfere with the stdout task, we only get
121 * the file status flags for STDIN here and save a copy. The nonblock
122 * flag is set later on the first read.
123 */
124 ret = fcntl(STDIN_FILENO, F_GETFL);
125 if (ret < 0) {
126 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
127 exit(EXIT_FAILURE);
128 }
129 sit->fd_flags = ret;
130 sit->must_set_nonblock_flag = (sit->fd_flags & O_NONBLOCK) == 0;
131 sit->task = task_register(&ti, s);
132 }