Merge branch 'maint'
[paraslash.git] / stdout.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 stdout.c Functions that deal with writing to stdout. */
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 "stdout.h"
18
19 /**
20  * The pre_select function of the stdout task.
21  *
22  * \param s The scheduler this task was registered to.
23  * \param t The task structure of the stdout task.
24  *
25  * This function is always successful. If there is data available in the input
26  * buffer, it adds \p STDOUT_FILENO to the write fd set of \a s.
27  */
28 static void stdout_pre_select(struct sched *s, struct task *t)
29 {
30         struct stdout_task *sot = container_of(t, struct stdout_task, task);
31
32         t->error = 0;
33         sot->check_fd = 0;
34         if (!*sot->loaded) {
35                 if (*sot->input_error < 0) {
36                         t->error = *sot->input_error;
37                         s->timeout.tv_sec = 0;
38                         s->timeout.tv_usec = 1;
39                 }
40                 return;
41         }
42         sot->check_fd = 1;
43         para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
44 }
45
46 /**
47  * The post select function of the stdout task.
48  *
49  * \param s The scheduler this task was registered to.
50  * \param t The task structure of the stdout task.
51  *
52  * This function checks if \p STDOUT_FILENO was included by in the write fd set
53  * of \a s during the previous pre_select call.  If yes, and \p STDOUT_FILENO
54  * appeears to be writable, the data loaded in the input buffer is written to
55  * stdout.
56  */
57 static void stdout_post_select(struct sched *s, struct task *t)
58 {
59         struct stdout_task *sot = container_of(t, struct stdout_task, task);
60         ssize_t ret;
61
62         t->error = 0;
63         if (!sot->check_fd) {
64                 if (!*sot->loaded && *sot->input_error < 0)
65                         t->error = *sot->input_error;
66                 return;
67         }
68         if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
69                 return;
70         ret = write(STDOUT_FILENO, *sot->bufp, *sot->loaded);
71         if (ret < 0) {
72                 t->error = -ERRNO_TO_PARA_ERROR(errno);
73                 return;
74         }
75         *sot->loaded -= ret;
76         if (*sot->loaded)
77                 memmove(*sot->bufp, *sot->bufp + ret, *sot->loaded);
78 }
79
80 /**
81  * Initialize a stdout task structure with default values.
82  *
83  * \param sot The stdout task structure.
84  *
85  * This fills in the pre/post select function poinzters of the task structure
86  * given by \a sot.
87  */
88 void stdout_set_defaults(struct stdout_task *sot)
89 {
90         int ret;
91
92         sot->task.pre_select = stdout_pre_select;
93         sot->task.post_select = stdout_post_select;
94         sprintf(sot->task.status, "stdout writer");
95         ret = mark_fd_nonblocking(STDOUT_FILENO);
96         if (ret >= 0)
97                 return;
98         PARA_EMERG_LOG("%s\n", para_strerror(-ret));
99         exit(EXIT_FAILURE);
100 }