2 * Copyright (C) 2006-2013 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. */
17 #include "buffer_tree.h"
20 * The pre_select function of the stdout task.
22 * \param s The scheduler this task was registered to.
23 * \param t The task structure of the stdout task.
25 * This function is always successful. If there is input data available, it
26 * adds \p STDOUT_FILENO to the write fd set of \a s.
28 static void stdout_pre_select(struct sched *s, struct task *t)
30 struct stdout_task *sot = container_of(t, struct stdout_task, task);
34 ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
36 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
42 * The post select function of the stdout task.
44 * \param s The scheduler this task was registered to.
45 * \param t The task structure of the stdout task.
47 * This function writes input data from the buffer tree to stdout if \p
48 * STDOUT_FILENO is writable.
50 static void stdout_post_select(struct sched *s, struct task *t)
52 struct stdout_task *sot = container_of(t, struct stdout_task, task);
53 struct btr_node *btrn = sot->btrn;
59 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
64 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
67 if (sot->must_set_nonblock_flag) {
68 ret = mark_fd_nonblocking(STDOUT_FILENO);
71 sot->must_set_nonblock_flag = false;
74 sz = btr_next_buffer(btrn, &buf);
77 ret = xwrite(STDOUT_FILENO, buf, sz);
80 btr_consume(btrn, ret);
84 btr_remove_node(&sot->btrn);
85 /* Revert to blocking mode if necessary. */
86 fcntl(STDOUT_FILENO, F_SETFL, sot->fd_flags);
91 * Initialize a stdout task structure with default values.
93 * \param sot The stdout task structure.
95 * This fills in the pre/post select function pointers of the task structure
98 void stdout_set_defaults(struct stdout_task *sot)
102 sot->task.pre_select = stdout_pre_select;
103 sot->task.post_select = stdout_post_select;
104 sprintf(sot->task.status, "stdout");
106 /* See stdin.c for details. */
107 ret = fcntl(STDOUT_FILENO, F_GETFL);
109 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
113 sot->must_set_nonblock_flag = (sot->fd_flags & O_NONBLOCK) == 0;