5fae20c28b2bcb093a1ab054b7c4e8c27be67829
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. */
17 #include "buffer_tree.h"
19 /* Add STDOUT_FILENO to the write fd set if there is input data available. */
20 static void stdout_pre_select(struct sched
*s
, void *context
)
22 struct stdout_task
*sot
= context
;
25 ret
= btr_node_status(sot
->btrn
, 0, BTR_NT_LEAF
);
27 para_fd_set(STDOUT_FILENO
, &s
->wfds
, &s
->max_fileno
);
33 * This function writes input data from the buffer tree to stdout if
34 * STDOUT_FILENO is writable.
36 static int stdout_post_select(struct sched
*s
, void *context
)
38 struct stdout_task
*sot
= context
;
39 struct btr_node
*btrn
= sot
->btrn
;
44 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
49 if (!FD_ISSET(STDOUT_FILENO
, &s
->wfds
))
52 if (sot
->must_set_nonblock_flag
) {
53 ret
= mark_fd_nonblocking(STDOUT_FILENO
);
56 sot
->must_set_nonblock_flag
= false;
59 sz
= btr_next_buffer(btrn
, &buf
);
62 ret
= xwrite(STDOUT_FILENO
, buf
, sz
);
65 btr_consume(btrn
, ret
);
69 btr_remove_node(&sot
->btrn
);
70 /* Revert to blocking mode if necessary. */
71 fcntl(STDOUT_FILENO
, F_SETFL
, sot
->fd_flags
);
77 * Register a stdout task structure.
79 * \param sot The stdout task structure to register.
80 * \param s The task will be added to this scheduler's task list.
82 * This sets up \a sot and registers a task with \a sot as context pointer.
84 void stdout_task_register(struct stdout_task
*sot
, struct sched
*s
)
87 struct task_info ti
= {
88 .pre_select
= stdout_pre_select
,
89 .post_select
= stdout_post_select
,
94 /* See stdin.c for details. */
95 ret
= fcntl(STDOUT_FILENO
, F_GETFL
);
97 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno
));
101 sot
->must_set_nonblock_flag
= (sot
->fd_flags
& O_NONBLOCK
) == 0;
102 sot
->task
= task_register(&ti
, s
);