audiod: Only call audiod_status_dump twice per second.
[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
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
19 /**
20  * The pre_select function of the stdin task.
21  *
22  * \param s The scheduler this task was registered to.
23  * \param t The task structure of the stdin task.
24  *
25  * This function is always successful. If there is space left in the
26  * buffer of the stdin task, it adds \p STDIN_FILENO to the read fd set
27  * of \a s.
28  */
29 static void stdin_pre_select(struct sched *s, struct task *t)
30 {
31         struct stdin_task *sit = container_of(t, struct stdin_task, task);
32
33         if (sit->output_error && *sit->output_error < 0) {
34                 t->error = *sit->output_error;
35                 return;
36         }
37         t->error = 0;
38         sit->check_fd = 0;
39         if (sit->loaded >= sit->bufsize)
40                 return;
41         sit->check_fd = 1;
42         para_fd_set(STDIN_FILENO, &s->rfds, &s->max_fileno);
43 }
44
45 /**
46  * The post select function of the stdin task.
47  *
48  * \param s The scheduler this task was registered to.
49  * \param t The task structure of the stdin task.
50  *
51  * This function checks if \p STDIN_FILENO was included by in the read fd set
52  * of \a s during the previous pre_select call.  If yes, and \p STDIN_FILENO
53  * appears to be readable, data is read from stdin into the buffer of the
54  * stdin task.
55  */
56 static void stdin_post_select(struct sched *s, struct task *t)
57 {
58         struct stdin_task *sit = container_of(t, struct stdin_task, task);
59         ssize_t ret;
60
61         if (sit->output_error && *sit->output_error < 0) {
62                 t->error = *sit->output_error;
63                 return;
64         }
65         t->error = 0;
66         if (!sit->check_fd)
67                 return;
68         if (!FD_ISSET(STDIN_FILENO, &s->rfds))
69                 return;
70         ret = read(STDIN_FILENO, sit->buf + sit->loaded, sit->bufsize - sit->loaded);
71         if (ret < 0)
72                 t->error = ERRNO_TO_PARA_ERROR(errno);
73         else if (ret > 0)
74                 sit->loaded += ret;
75         else
76                 t->error = -E_STDIN_EOF;
77 }
78
79 /**
80  * Initialize a stdin task structure with default values.
81  *
82  * \param sit The stdin task structure.
83  *
84  * This fills in the pre/post select function pointers of the task structure
85  * given by \a sit. Moreover, the stdin file desctiptor is set to nonblocking
86  * mode and \a bufsize is initialized to 16 KB (but no buffer is allocated).
87  */
88 void stdin_set_defaults(struct stdin_task *sit)
89 {
90         int ret;
91
92         sit->bufsize = 16 * 1024,
93         sit->task.pre_select = stdin_pre_select;
94         sit->task.post_select = stdin_post_select;
95         sprintf(sit->task.status, "stdin reader");
96         ret = mark_fd_nonblocking(STDIN_FILENO);
97         if (ret >= 0)
98                 return;
99         sit->output_error = NULL;
100         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
101         exit(EXIT_FAILURE);
102 }