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