stdin.h: Add missing doxygen comment for stdin_task->btrp.
[paraslash.git] / stdout.c
1 /*
2 * Copyright (C) 2006-2011 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 #include <stdbool.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "fd.h"
17 #include "error.h"
18 #include "stdout.h"
19 #include "buffer_tree.h"
20
21 /**
22 * The pre_select function of the stdout task.
23 *
24 * \param s The scheduler this task was registered to.
25 * \param t The task structure of the stdout task.
26 *
27 * This function is always successful. If there is input data available, it
28 * adds \p STDOUT_FILENO to the write fd set of \a s.
29 */
30 static void stdout_pre_select(struct sched *s, struct task *t)
31 {
32 struct stdout_task *sot = container_of(t, struct stdout_task, task);
33 int ret;
34
35 t->error = 0;
36 ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
37 if (ret > 0)
38 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
39 else if (ret < 0)
40 sched_min_delay(s);
41 }
42
43 /**
44 * The post select function of the stdout task.
45 *
46 * \param s The scheduler this task was registered to.
47 * \param t The task structure of the stdout task.
48 *
49 * This function writes input data from the buffer tree to stdout if \p
50 * STDOUT_FILENO is writable.
51 */
52 static void stdout_post_select(struct sched *s, struct task *t)
53 {
54 struct stdout_task *sot = container_of(t, struct stdout_task, task);
55 struct btr_node *btrn = sot->btrn;
56 int ret;
57 char *buf;
58 size_t sz;
59
60 t->error = 0;
61 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
62 if (ret < 0)
63 goto out;
64 if (ret == 0)
65 return;
66 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
67 return;
68
69 for (;;) {
70 sz = btr_next_buffer(btrn, &buf);
71 if (sz == 0)
72 break;
73 ret = write_nonblock(STDOUT_FILENO, buf, sz);
74 if (ret <= 0)
75 break;
76 btr_consume(btrn, ret);
77 }
78 out:
79 if (ret < 0)
80 btr_remove_node(btrn);
81 t->error = ret;
82 }
83 /**
84 * Initialize a stdout task structure with default values.
85 *
86 * \param sot The stdout task structure.
87 *
88 * This fills in the pre/post select function pointers of the task structure
89 * given by \a sot and sets the stdout file descriptor to nonblocking mode.
90 */
91 void stdout_set_defaults(struct stdout_task *sot)
92 {
93 int ret;
94
95 sot->task.pre_select = stdout_pre_select;
96 sot->task.post_select = stdout_post_select;
97 sprintf(sot->task.status, "stdout");
98 ret = mark_fd_nonblocking(STDOUT_FILENO);
99 if (ret >= 0)
100 return;
101 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
102 exit(EXIT_FAILURE);
103 }