]> git.ipfire.org Git - thirdparty/qemu.git/blob - tools/virtiofsd/helper.c
virtiofsd: make -f (foreground) the default
[thirdparty/qemu.git] / tools / virtiofsd / helper.c
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4 *
5 * Helper functions to create (simple) standalone programs. With the
6 * aid of these functions it should be possible to create full FUSE
7 * file system by implementing nothing but the request handlers.
8
9 * This program can be distributed under the terms of the GNU LGPLv2.
10 * See the file COPYING.LIB.
11 */
12
13 #include "qemu/osdep.h"
14 #include "fuse_i.h"
15 #include "fuse_lowlevel.h"
16 #include "fuse_misc.h"
17 #include "fuse_opt.h"
18
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/param.h>
26 #include <unistd.h>
27
28 #define FUSE_HELPER_OPT(t, p) \
29 { \
30 t, offsetof(struct fuse_cmdline_opts, p), 1 \
31 }
32 #define FUSE_HELPER_OPT_VALUE(t, p, v) \
33 { \
34 t, offsetof(struct fuse_cmdline_opts, p), v \
35 }
36
37
38 static const struct fuse_opt fuse_helper_opts[] = {
39 FUSE_HELPER_OPT("-h", show_help),
40 FUSE_HELPER_OPT("--help", show_help),
41 FUSE_HELPER_OPT("-V", show_version),
42 FUSE_HELPER_OPT("--version", show_version),
43 FUSE_HELPER_OPT("-d", debug),
44 FUSE_HELPER_OPT("debug", debug),
45 FUSE_HELPER_OPT("-d", foreground),
46 FUSE_HELPER_OPT("debug", foreground),
47 FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP),
48 FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP),
49 FUSE_HELPER_OPT("-f", foreground),
50 FUSE_HELPER_OPT_VALUE("--daemonize", foreground, 0),
51 FUSE_HELPER_OPT("fsname=", nodefault_subtype),
52 FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP),
53 FUSE_HELPER_OPT("subtype=", nodefault_subtype),
54 FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP),
55 FUSE_HELPER_OPT("max_idle_threads=%u", max_idle_threads),
56 FUSE_OPT_END
57 };
58
59 struct fuse_conn_info_opts {
60 int atomic_o_trunc;
61 int no_remote_posix_lock;
62 int no_remote_flock;
63 int splice_write;
64 int splice_move;
65 int splice_read;
66 int no_splice_write;
67 int no_splice_move;
68 int no_splice_read;
69 int auto_inval_data;
70 int no_auto_inval_data;
71 int no_readdirplus;
72 int no_readdirplus_auto;
73 int async_dio;
74 int no_async_dio;
75 int writeback_cache;
76 int no_writeback_cache;
77 int async_read;
78 int sync_read;
79 unsigned max_write;
80 unsigned max_readahead;
81 unsigned max_background;
82 unsigned congestion_threshold;
83 unsigned time_gran;
84 int set_max_write;
85 int set_max_readahead;
86 int set_max_background;
87 int set_congestion_threshold;
88 int set_time_gran;
89 };
90
91 #define CONN_OPTION(t, p, v) \
92 { \
93 t, offsetof(struct fuse_conn_info_opts, p), v \
94 }
95 static const struct fuse_opt conn_info_opt_spec[] = {
96 CONN_OPTION("max_write=%u", max_write, 0),
97 CONN_OPTION("max_write=", set_max_write, 1),
98 CONN_OPTION("max_readahead=%u", max_readahead, 0),
99 CONN_OPTION("max_readahead=", set_max_readahead, 1),
100 CONN_OPTION("max_background=%u", max_background, 0),
101 CONN_OPTION("max_background=", set_max_background, 1),
102 CONN_OPTION("congestion_threshold=%u", congestion_threshold, 0),
103 CONN_OPTION("congestion_threshold=", set_congestion_threshold, 1),
104 CONN_OPTION("sync_read", sync_read, 1),
105 CONN_OPTION("async_read", async_read, 1),
106 CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1),
107 CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1),
108 CONN_OPTION("no_remote_lock", no_remote_flock, 1),
109 CONN_OPTION("no_remote_flock", no_remote_flock, 1),
110 CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1),
111 CONN_OPTION("splice_write", splice_write, 1),
112 CONN_OPTION("no_splice_write", no_splice_write, 1),
113 CONN_OPTION("splice_move", splice_move, 1),
114 CONN_OPTION("no_splice_move", no_splice_move, 1),
115 CONN_OPTION("splice_read", splice_read, 1),
116 CONN_OPTION("no_splice_read", no_splice_read, 1),
117 CONN_OPTION("auto_inval_data", auto_inval_data, 1),
118 CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1),
119 CONN_OPTION("readdirplus=no", no_readdirplus, 1),
120 CONN_OPTION("readdirplus=yes", no_readdirplus, 0),
121 CONN_OPTION("readdirplus=yes", no_readdirplus_auto, 1),
122 CONN_OPTION("readdirplus=auto", no_readdirplus, 0),
123 CONN_OPTION("readdirplus=auto", no_readdirplus_auto, 0),
124 CONN_OPTION("async_dio", async_dio, 1),
125 CONN_OPTION("no_async_dio", no_async_dio, 1),
126 CONN_OPTION("writeback_cache", writeback_cache, 1),
127 CONN_OPTION("no_writeback_cache", no_writeback_cache, 1),
128 CONN_OPTION("time_gran=%u", time_gran, 0),
129 CONN_OPTION("time_gran=", set_time_gran, 1),
130 FUSE_OPT_END
131 };
132
133
134 void fuse_cmdline_help(void)
135 {
136 printf(" -h --help print help\n"
137 " -V --version print version\n"
138 " -d -o debug enable debug output (implies -f)\n"
139 " -f foreground operation\n"
140 " --daemonize run in background\n"
141 " -o max_idle_threads the maximum number of idle worker "
142 "threads\n"
143 " allowed (default: 10)\n");
144 }
145
146 static int fuse_helper_opt_proc(void *data, const char *arg, int key,
147 struct fuse_args *outargs)
148 {
149 (void)data;
150 (void)outargs;
151
152 switch (key) {
153 case FUSE_OPT_KEY_NONOPT:
154 fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg);
155 return -1;
156
157 default:
158 /* Pass through unknown options */
159 return 1;
160 }
161 }
162
163 int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
164 {
165 memset(opts, 0, sizeof(struct fuse_cmdline_opts));
166
167 opts->max_idle_threads = 10;
168 opts->foreground = 1;
169
170 if (fuse_opt_parse(args, opts, fuse_helper_opts, fuse_helper_opt_proc) ==
171 -1) {
172 return -1;
173 }
174
175 return 0;
176 }
177
178
179 int fuse_daemonize(int foreground)
180 {
181 int ret = 0, rett;
182 if (!foreground) {
183 int nullfd;
184 int waiter[2];
185 char completed;
186
187 if (pipe(waiter)) {
188 perror("fuse_daemonize: pipe");
189 return -1;
190 }
191
192 /*
193 * demonize current process by forking it and killing the
194 * parent. This makes current process as a child of 'init'.
195 */
196 switch (fork()) {
197 case -1:
198 perror("fuse_daemonize: fork");
199 return -1;
200 case 0:
201 break;
202 default:
203 _exit(read(waiter[0], &completed,
204 sizeof(completed) != sizeof(completed)));
205 }
206
207 if (setsid() == -1) {
208 perror("fuse_daemonize: setsid");
209 return -1;
210 }
211
212 ret = chdir("/");
213
214 nullfd = open("/dev/null", O_RDWR, 0);
215 if (nullfd != -1) {
216 rett = dup2(nullfd, 0);
217 if (!ret) {
218 ret = rett;
219 }
220 rett = dup2(nullfd, 1);
221 if (!ret) {
222 ret = rett;
223 }
224 rett = dup2(nullfd, 2);
225 if (!ret) {
226 ret = rett;
227 }
228 if (nullfd > 2) {
229 close(nullfd);
230 }
231 }
232
233 /* Propagate completion of daemon initialization */
234 completed = 1;
235 rett = write(waiter[1], &completed, sizeof(completed));
236 if (!ret) {
237 ret = rett;
238 }
239 close(waiter[0]);
240 close(waiter[1]);
241 } else {
242 ret = chdir("/");
243 }
244 return ret;
245 }
246
247 void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts,
248 struct fuse_conn_info *conn)
249 {
250 if (opts->set_max_write) {
251 conn->max_write = opts->max_write;
252 }
253 if (opts->set_max_background) {
254 conn->max_background = opts->max_background;
255 }
256 if (opts->set_congestion_threshold) {
257 conn->congestion_threshold = opts->congestion_threshold;
258 }
259 if (opts->set_time_gran) {
260 conn->time_gran = opts->time_gran;
261 }
262 if (opts->set_max_readahead) {
263 conn->max_readahead = opts->max_readahead;
264 }
265
266 #define LL_ENABLE(cond, cap) \
267 if (cond) \
268 conn->want |= (cap)
269 #define LL_DISABLE(cond, cap) \
270 if (cond) \
271 conn->want &= ~(cap)
272
273 LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ);
274 LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ);
275
276 LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE);
277 LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE);
278
279 LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE);
280 LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE);
281
282 LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
283 LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA);
284
285 LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS);
286 LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO);
287
288 LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO);
289 LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO);
290
291 LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
292 LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE);
293
294 LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ);
295 LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ);
296
297 LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS);
298 LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS);
299 }
300
301 struct fuse_conn_info_opts *fuse_parse_conn_info_opts(struct fuse_args *args)
302 {
303 struct fuse_conn_info_opts *opts;
304
305 opts = calloc(1, sizeof(struct fuse_conn_info_opts));
306 if (opts == NULL) {
307 fuse_log(FUSE_LOG_ERR, "calloc failed\n");
308 return NULL;
309 }
310 if (fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) == -1) {
311 free(opts);
312 return NULL;
313 }
314 return opts;
315 }