stdin/stdout: Restore fd flags on shutdown.
[paraslash.git] / stdout.c
1 /*
2 * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file stdout.c Functions that deal with writing to stdout. */
8
9 #include <assert.h>
10
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "fd.h"
15 #include "error.h"
16 #include "stdout.h"
17 #include "buffer_tree.h"
18
19 /**
20 * The pre_select function of the stdout task.
21 *
22 * \param s The scheduler this task was registered to.
23 * \param t The task structure of the stdout task.
24 *
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.
27 */
28 static void stdout_pre_select(struct sched *s, struct task *t)
29 {
30 struct stdout_task *sot = container_of(t, struct stdout_task, task);
31 int ret;
32
33 t->error = 0;
34 ret = btr_node_status(sot->btrn, 0, BTR_NT_LEAF);
35 if (ret > 0)
36 para_fd_set(STDOUT_FILENO, &s->wfds, &s->max_fileno);
37 else if (ret < 0)
38 sched_min_delay(s);
39 }
40
41 /**
42 * The post select function of the stdout task.
43 *
44 * \param s The scheduler this task was registered to.
45 * \param t The task structure of the stdout task.
46 *
47 * This function writes input data from the buffer tree to stdout if \p
48 * STDOUT_FILENO is writable.
49 */
50 static void stdout_post_select(struct sched *s, struct task *t)
51 {
52 struct stdout_task *sot = container_of(t, struct stdout_task, task);
53 struct btr_node *btrn = sot->btrn;
54 int ret;
55 char *buf;
56 size_t sz;
57
58 t->error = 0;
59 ret = btr_node_status(btrn, 0, BTR_NT_LEAF);
60 if (ret < 0)
61 goto out;
62 if (ret == 0)
63 return;
64 if (!FD_ISSET(STDOUT_FILENO, &s->wfds))
65 return;
66
67 if (sot->must_set_nonblock_flag) {
68 ret = mark_fd_nonblocking(STDOUT_FILENO);
69 if (ret < 0)
70 goto out;
71 sot->must_set_nonblock_flag = false;
72 }
73 for (;;) {
74 sz = btr_next_buffer(btrn, &buf);
75 if (sz == 0)
76 break;
77 ret = xwrite(STDOUT_FILENO, buf, sz);
78 if (ret <= 0)
79 break;
80 btr_consume(btrn, ret);
81 }
82 out:
83 if (ret < 0) {
84 btr_remove_node(&sot->btrn);
85 /* Revert to blocking mode if necessary. */
86 fcntl(STDOUT_FILENO, F_SETFL, sot->fd_flags);
87 }
88 t->error = ret;
89 }
90 /**
91 * Initialize a stdout task structure with default values.
92 *
93 * \param sot The stdout task structure.
94 *
95 * This fills in the pre/post select function pointers of the task structure
96 * given by \a sot.
97 */
98 void stdout_set_defaults(struct stdout_task *sot)
99 {
100 int ret;
101
102 sot->task.pre_select = stdout_pre_select;
103 sot->task.post_select = stdout_post_select;
104 sprintf(sot->task.status, "stdout");
105
106 /* See stdin.c for details. */
107 ret = fcntl(STDOUT_FILENO, F_GETFL);
108 if (ret < 0) {
109 PARA_EMERG_LOG("F_GETFL: %s\n", strerror(errno));
110 exit(EXIT_FAILURE);
111 }
112 sot->fd_flags = ret;
113 sot->must_set_nonblock_flag = (sot->fd_flags & O_NONBLOCK) == 0;
114 }