stdin/stdout: Restore fd flags on shutdown.
[paraslash.git] / stdin.c
1 /*
2  * Copyright (C) 2006-2013 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 = container_of(t, struct stdin_task, task);
34         int ret;
35
36         t->error = 0;
37         ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
38         if (ret < 0)
39                 sched_min_delay(s);
40         if (ret <= 0)
41                 return;
42         if (btr_pool_unused(sit->btrp) > 0)
43                 return para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
44         sched_request_timeout_ms(100, s);
45 }
46
47 /**
48  * The post select function of the stdin task.
49  *
50  * \param s The scheduler this task was registered to.
51  * \param t The task structure of the stdin task.
52  *
53  * This function checks if \p STDIN_FILENO was included by in the read fd set
54  * of \a s during the previous pre_select call.  If yes, and \p STDIN_FILENO
55  * appears to be readable, data is read from stdin and fed into the buffer
56  * tree.
57  */
58 static void stdin_post_select(struct sched *s, struct task *t)
59 {
60         struct stdin_task *sit = container_of(t, struct stdin_task, task);
61         ssize_t ret;
62         size_t sz, n;
63         char *buf = NULL;
64
65         t->error = 0;
66         ret = btr_node_status(sit->btrn, 0, BTR_NT_ROOT);
67         if (ret < 0)
68                 goto err;
69         if (ret == 0)
70                 return;
71         sz = btr_pool_get_buffer(sit->btrp, &buf);
72         if (sz == 0)
73                 return;
74         if (sit->must_set_nonblock_flag) {
75                 ret = mark_fd_nonblocking(STDIN_FILENO);
76                 if (ret < 0)
77                         goto err;
78                 sit->must_set_nonblock_flag = false;
79         }
80         /*
81          * Do not use the maximal size to avoid having only a single buffer
82          * reference for the whole pool. This is bad because if that single
83          * reference can not be freed, we're stuck.
84          */
85         sz = PARA_MIN(sz, btr_pool_size(sit->btrp) / 2);
86         ret = read_nonblock(STDIN_FILENO, buf, sz, &s->rfds, &n);
87         if (n > 0)
88                 btr_add_output_pool(sit->btrp, n, sit->btrn);
89         if (ret >= 0)
90                 return;
91 err:
92         btr_remove_node(&sit->btrn);
93         /* Revert to blocking mode if necessary. */
94         fcntl(STDIN_FILENO, F_SETFL, sit->fd_flags);
95         //btr_pool_free(sit->btrp);
96         t->error = ret;
97 }
98
99 /**
100  * Initialize a stdin task structure with default values.
101  *
102  * \param sit The stdin task structure.
103  *
104  * This fills in the pre/post select function pointers of the task structure
105  * given by \a sit and creates a buffer tree for I/O.
106  */
107 void stdin_set_defaults(struct stdin_task *sit)
108 {
109         int ret;
110
111         sit->task.pre_select = stdin_pre_select;
112         sit->task.post_select = stdin_post_select;
113         sit->btrp = btr_pool_new("stdin", 128 * 1024);
114         sprintf(sit->task.status, "stdin reader");
115         /*
116          * Both STDIN_FILENO and STDOUT_FILENO may refer to the same open file
117          * description (the terminal), and thus share the same file status
118          * flags. In order to not interfere with the stdout task, we only get
119          * the file status flags for STDIN here and save a copy. The nonblock
120          * flag is set later on the first read.
121          */
122         ret = fcntl(STDIN_FILENO, F_GETFL);
123         if (ret < 0) {
124                 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
125                 exit(EXIT_FAILURE);
126         }
127         sit->fd_flags = ret;
128         sit->must_set_nonblock_flag = (sit->fd_flags & O_NONBLOCK) == 0;
129 }