16e16465ad2d9671c6d9e818f10caed6aae2cb91
2 * Copyright (C) 2006-2009 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 data available in the input
28 * buffer, it 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
);
37 if (*sot
->input_error
< 0) {
38 t
->error
= *sot
->input_error
;
39 s
->timeout
.tv_sec
= 0;
40 s
->timeout
.tv_usec
= 1;
45 para_fd_set(STDOUT_FILENO
, &s
->wfds
, &s
->max_fileno
);
48 static void stdout_pre_select_btr(struct sched
*s
, struct task
*t
)
50 struct stdout_task
*sot
= container_of(t
, struct stdout_task
, task
);
55 ret
= btr_node_status(sot
->btrn
, 0, BTR_NT_LEAF
);
57 para_fd_set(STDOUT_FILENO
, &s
->wfds
, &s
->max_fileno
);
59 s
->timeout
.tv_sec
= 0;
60 s
->timeout
.tv_usec
= 1;
65 * The post select function of the stdout task.
67 * \param s The scheduler this task was registered to.
68 * \param t The task structure of the stdout task.
70 * This function checks if \p STDOUT_FILENO was included by in the write fd set
71 * of \a s during the previous pre_select call. If yes, and \p STDOUT_FILENO
72 * appeears to be writable, the data loaded in the input buffer is written to
75 static void stdout_post_select(struct sched
*s
, struct task
*t
)
77 struct stdout_task
*sot
= container_of(t
, struct stdout_task
, task
);
82 if (!*sot
->loaded
&& *sot
->input_error
< 0)
83 t
->error
= *sot
->input_error
;
86 if (!FD_ISSET(STDOUT_FILENO
, &s
->wfds
))
88 ret
= write(STDOUT_FILENO
, *sot
->bufp
, *sot
->loaded
);
90 t
->error
= -ERRNO_TO_PARA_ERROR(errno
);
95 memmove(*sot
->bufp
, *sot
->bufp
+ ret
, *sot
->loaded
);
98 static void stdout_post_select_btr(struct sched
*s
, struct task
*t
)
100 struct stdout_task
*sot
= container_of(t
, struct stdout_task
, task
);
101 struct btr_node
*btrn
= sot
->btrn
;
107 ret
= btr_node_status(btrn
, 0, BTR_NT_LEAF
);
112 if (!FD_ISSET(STDOUT_FILENO
, &s
->wfds
))
114 sz
= btr_next_buffer(btrn
, &buf
);
116 ret
= write_nonblock(STDOUT_FILENO
, buf
, sz
, 0);
119 btr_consume(btrn
, ret
);
122 btr_remove_node(btrn
);
126 * Initialize a stdout task structure with default values.
128 * \param sot The stdout task structure.
130 * This fills in the pre/post select function poinzters of the task structure
133 void stdout_set_defaults(struct stdout_task
*sot
)
138 sot
->task
.pre_select
= stdout_pre_select_btr
;
139 sot
->task
.post_select
= stdout_post_select_btr
;
141 sot
->task
.pre_select
= stdout_pre_select
;
142 sot
->task
.post_select
= stdout_post_select
;
144 sprintf(sot
->task
.status
, "stdout writer");
145 ret
= mark_fd_nonblocking(STDOUT_FILENO
);
148 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));