Client: Simplify signal handling.
[paraslash.git] / file_write.c
1 /*
2  * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file file_write.c simple output plugin for testing purposes */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.h"
17 #include "buffer_tree.h"
18 #include "write.h"
19 #include "write_common.h"
20 #include "string.h"
21 #include "fd.h"
22 #include "file_write.cmdline.h"
23 #include "error.h"
24
25 /** Data specific to the file writer. */
26 struct private_file_write_data {
27         /** The file descriptor of the output file. */
28         int fd;
29 };
30
31 /*
32  * Get a random filename.
33  *
34  * This is by no means a secure way to create temporary files in a hostile
35  * directory like \p /tmp. However, we use it only for creating temp files in
36  * ~/.paraslash, for which it is OK. Result must be freed by the caller.
37  */
38 __must_check __malloc static char *random_filename(void)
39 {
40         char *result, *home = para_homedir();
41         struct timeval tv;
42
43         gettimeofday(&tv, NULL);
44         srandom(tv.tv_usec);
45         result = make_message("%s/.paraslash/%08lu", home,
46                 para_random(99999999));
47         free(home);
48         return result;
49 }
50
51 static int prepare_output_file(struct writer_node *wn)
52 {
53         struct file_write_args_info *conf = wn->conf;
54         char *filename;
55         int ret;
56         struct private_file_write_data *pfwd = para_calloc(sizeof(*pfwd));
57
58         if (conf->filename_given)
59                 filename = conf->filename_arg;
60         else
61                 filename = random_filename();
62         ret = para_open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
63         if (!conf->filename_given)
64                 free(filename);
65         if (ret < 0)
66                 goto out;
67         pfwd->fd = ret;
68         ret = mark_fd_blocking(pfwd->fd);
69         if (ret < 0)
70                 goto out_close;
71         wn->private_data = pfwd;
72         return 1;
73 out_close:
74         close(pfwd->fd);
75 out:
76         free(pfwd);
77         return ret;
78 }
79
80 static void file_write_pre_select(struct sched *s, struct task *t)
81 {
82         struct writer_node *wn = container_of(t, struct writer_node, task);
83         struct private_file_write_data *pfwd = wn->private_data;
84         int ret = btr_node_status(wn->btrn, wn->min_iqs, BTR_NT_LEAF);
85
86         if (ret == 0)
87                 return;
88         if (ret < 0 || !pfwd)
89                 return sched_min_delay(s);
90         para_fd_set(pfwd->fd, &s->wfds, &s->max_fileno);
91 }
92
93 static void file_write_close(struct writer_node *wn)
94 {
95         struct private_file_write_data *pfwd = wn->private_data;
96
97         if (!pfwd)
98                 return;
99         close(pfwd->fd);
100         free(pfwd);
101 }
102
103 static void file_write_post_select(__a_unused struct sched *s,
104                 struct task *t)
105 {
106         struct writer_node *wn = container_of(t, struct writer_node, task);
107         struct private_file_write_data *pfwd = wn->private_data;
108         struct btr_node *btrn = wn->btrn;
109         int ret;
110         char *buf;
111         size_t bytes;
112
113         t->error = 0;
114         ret = btr_node_status(btrn, wn->min_iqs, BTR_NT_LEAF);
115         if (ret <= 0)
116                 goto out;
117         if (!pfwd) {
118                 ret = prepare_output_file(wn);
119                 goto out;
120         }
121         if (!FD_ISSET(pfwd->fd, &s->wfds))
122                 return;
123         bytes = btr_next_buffer(btrn, &buf);
124         assert(bytes > 0);
125         //PARA_INFO_LOG("writing %zu\n", bytes);
126         ret = write(pfwd->fd, buf, bytes);
127         if (ret < 0)
128                 goto out;
129         btr_consume(btrn, ret);
130 out:
131         if (ret < 0)
132                 btr_remove_node(btrn);
133         t->error = ret;
134 }
135
136 __malloc static void *file_write_parse_config_or_die(const char *options)
137 {
138         struct file_write_args_info *conf = para_calloc(sizeof(*conf));
139
140         /* exits on errors */
141         file_cmdline_parser_string(options, conf, "file_write");
142         return conf;
143 }
144
145 static void file_write_free_config(void *conf)
146 {
147         file_cmdline_parser_free(conf);
148 }
149
150 /** the init function of the file writer */
151 void file_write_init(struct writer *w)
152 {
153         struct file_write_args_info dummy;
154
155         file_cmdline_parser_init(&dummy);
156         w->pre_select = file_write_pre_select;
157         w->post_select = file_write_post_select;
158         w->parse_config_or_die = file_write_parse_config_or_die;
159         w->free_config = file_write_free_config;
160         w->close = file_write_close;
161         w->shutdown = NULL; /* nothing to do */
162         w->help = (struct ggo_help) {
163                 .short_help = file_write_args_info_help,
164                 .detailed_help = file_write_args_info_detailed_help
165         };
166         file_cmdline_parser_free(&dummy);
167 }