2 * Copyright (C) 2006-2008 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() */
21 * The pre_select function of the stdout task.
23 * \param s The scheduler this task was registered to.
24 * \param t The task structure of the stdout task.
26 * This function is always successful. If there is data available in the input
27 * buffer, it adds \p STDOUT_FILENO to the write fd set of \a s.
29 static void stdout_pre_select(struct sched
*s
, struct task
*t
)
31 struct stdout_task
*sot
= t
->private_data
;
36 if (*sot
->input_error
) {
37 t
->ret
= *sot
->input_error
;
38 s
->timeout
.tv_sec
= 0;
39 s
->timeout
.tv_usec
= 1;
44 para_fd_set(STDOUT_FILENO
, &s
->wfds
, &s
->max_fileno
);
48 * The post select function of the stdout task.
50 * \param s The scheduler this task was registered to.
51 * \param t The task structure of the stdout task.
53 * This function checks if \p STDOUT_FILENO was included by in the write fd set
54 * of \a s during the previous pre_select call. If yes, and \p STDOUT_FILENO
55 * appeears to be writable, the data loaded in the input buffer is written to
58 static void stdout_post_select(struct sched
*s
, struct task
*t
)
60 struct stdout_task
*sot
= t
->private_data
;
65 if (*sot
->input_error
)
66 t
->ret
= *sot
->input_error
;
69 if (!FD_ISSET(STDOUT_FILENO
, &s
->wfds
))
71 t
->ret
= -E_STDOUT_WRITE
;
72 ret
= write(STDOUT_FILENO
, sot
->buf
, *sot
->loaded
);
77 memmove(sot
->buf
, sot
->buf
+ ret
, *sot
->loaded
);
81 static void stdout_default_event_handler(struct task
*t
)
83 PARA_NOTICE_LOG("%p: %s\n", t
, para_strerror(-t
->ret
));
88 * Initialize a stdout task structure with default values.
90 * \param sot The stdout task structure.
92 * This fills in the pre/post select function poinzters of the task structure
93 * given by \a sot. It also sets up a default error handler which unregisters
94 * the task on errors and clears the eof flag of \a sot.
96 void stdout_set_defaults(struct stdout_task
*sot
)
100 sot
->task
.private_data
= sot
;
101 sot
->task
.pre_select
= stdout_pre_select
;
102 sot
->task
.post_select
= stdout_post_select
;
103 sot
->task
.event_handler
= stdout_default_event_handler
;
105 sprintf(sot
->task
.status
, "stdout writer");
106 ret
= mark_fd_nonblocking(STDOUT_FILENO
);
109 PARA_EMERG_LOG("%s\n", para_strerror(-ret
));