2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file stdout.c Functions that deal with writing to stdout. */
9 #include <dirent.h> /* readdir() */
19 #include "buffer_tree.h"
22 * The pre_select function of the stdout task.
24 * \param s The scheduler this task was registered to.
25 * \param t The task structure of the stdout task.
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.
30 static void stdout_pre_select(struct sched *s, struct task *t)
32 struct stdout_task *sot = container_of(t, struct stdout_task, task);
36 ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
38 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
44 * The post select function of the stdout task.
46 * \param s The scheduler this task was registered to.
47 * \param t The task structure of the stdout task.
49 * This function writes input data from the buffer tree to stdout if \p
50 * STDOUT_FILENO is writable.
52 static void stdout_post_select(struct sched *s, struct task *t)
54 struct stdout_task *sot = container_of(t, struct stdout_task, task);
55 struct btr_node *btrn = sot->btrn;
61 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
66 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
70 sz = btr_next_buffer(btrn, &buf);
73 ret = write_nonblock(STDOUT_FILENO, buf, sz);
76 btr_consume(btrn, ret);
80 btr_remove_node(btrn);
84 * Initialize a stdout task structure with default values.
86 * \param sot The stdout task structure.
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.
91 void stdout_set_defaults(struct stdout_task *sot)
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);
101 PARA_EMERG_LOG("%s\n", para_strerror(-ret));