2 * Copyright (C) 2006-2014 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file stdout.c Functions that deal with writing to stdout. */
15 #include "buffer_tree.h"
17 /* Add STDOUT_FILENO to the write fd set if there is input data available. */
18 static void stdout_pre_select(struct sched
*s
, void *context
)
20 struct stdout_task
*sot
= context
;
23 ret
= btr_node_status(sot
->btrn
, 0, BTR_NT_LEAF
);
25 para_fd_set(STDOUT_FILENO
, &s
->wfds
, &s
->max_fileno
);
31 * This function writes input data from the buffer tree to stdout if
32 * STDOUT_FILENO is writable.
34 static int stdout_post_select(struct sched
*s
, void *context
)
36 struct stdout_task
*sot
= context
;
37 struct btr_node
*btrn
= sot
->btrn
;
42 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
47 if (!FD_ISSET(STDOUT_FILENO
, &s
->wfds
))
50 if (sot
->must_set_nonblock_flag
) {
51 ret
= mark_fd_nonblocking(STDOUT_FILENO
);
54 sot
->must_set_nonblock_flag
= false;
57 sz
= btr_next_buffer(btrn
, &buf
);
60 ret
= xwrite(STDOUT_FILENO
, buf
, sz
);
63 btr_consume(btrn
, ret
);
67 btr_remove_node(&sot
->btrn
);
68 /* Revert to blocking mode if necessary. */
69 fcntl(STDOUT_FILENO
, F_SETFL
, sot
->fd_flags
);
75 * Register a stdout task structure.
77 * \param sot The stdout task structure to register.
78 * \param s The task will be added to this scheduler's task list.
80 * This sets up \a sot and registers a task with \a sot as context pointer.
82 void stdout_task_register(struct stdout_task
*sot
, struct sched
*s
)
85 struct task_info ti
= {
86 .pre_select
= stdout_pre_select
,
87 .post_select
= stdout_post_select
,
92 /* See stdin.c for details. */
93 ret
= fcntl(STDOUT_FILENO
, F_GETFL
);
95 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno
));
99 sot
->must_set_nonblock_flag
= (sot
->fd_flags
& O_NONBLOCK
) == 0;
100 sot
->task
= task_register(&ti
, s
);