]>
Commit | Line | Data |
---|---|---|
fe000966 | 1 | /* |
84c98315 | 2 | Copyright (C) 2010 - 2020 Proxmox Server Solutions GmbH |
fe000966 DM |
3 | |
4 | This program is free software: you can redistribute it and/or modify | |
5 | it under the terms of the GNU Affero General Public License as published by | |
6 | the Free Software Foundation, either version 3 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU Affero General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Affero General Public License | |
15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | Author: Dietmar Maurer <dietmar@proxmox.com> | |
18 | ||
19 | */ | |
20 | ||
21 | #ifdef HAVE_CONFIG_H | |
22 | #include <config.h> | |
23 | #endif /* HAVE_CONFIG_H */ | |
24 | ||
25 | #include <stdlib.h> | |
26 | #include <stdio.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <glib.h> | |
fe000966 DM |
30 | #include <sys/types.h> |
31 | #include <sys/stat.h> | |
32 | #include <sys/mount.h> | |
33 | #include <fcntl.h> | |
34 | #include <errno.h> | |
35 | #include <sys/file.h> | |
36 | #include <sys/types.h> | |
37 | #include <dirent.h> | |
38 | #include <sys/utsname.h> | |
39 | #include <grp.h> | |
40 | #include <netdb.h> | |
41 | #include <sys/socket.h> | |
42 | #include <netinet/in.h> | |
43 | #include <arpa/inet.h> | |
b1f92b4e | 44 | |
fe000966 DM |
45 | #include <qb/qbdefs.h> |
46 | #include <qb/qbutil.h> | |
64405e32 | 47 | #include <qb/qblog.h> |
fe000966 DM |
48 | |
49 | #include "cfs-utils.h" | |
50 | #include "cfs-plug.h" | |
51 | #include "cfs-plug-memdb.h" | |
52 | #include "status.h" | |
53 | #include "dcdb.h" | |
54 | #include "dfsm.h" | |
55 | #include "quorum.h" | |
56 | #include "confdb.h" | |
57 | #include "server.h" | |
58 | ||
59 | #define DBFILENAME VARLIBDIR "/config.db" | |
60 | #define LOCKFILE VARLIBDIR "/.pmxcfs.lockfile" | |
68da7070 | 61 | #define RESTART_FLAG_FILE RUNDIR "/cfs-restart-flag" |
fe000966 | 62 | |
b1f92b4e | 63 | #define CFSDIR "/etc/pve" |
fe000966 DM |
64 | |
65 | cfs_t cfs = { | |
66 | .debug = 0, | |
fe000966 DM |
67 | }; |
68 | ||
69 | static struct fuse *fuse = NULL; | |
70 | ||
71 | static cfs_plug_t *root_plug; | |
72 | ||
73 | static void glib_print_handler(const gchar *string) | |
74 | { | |
64405e32 | 75 | printf("%s", string); |
fe000966 DM |
76 | } |
77 | ||
78 | static void glib_log_handler(const gchar *log_domain, | |
79 | GLogLevelFlags log_level, | |
80 | const gchar *message, | |
81 | gpointer user_data) | |
82 | { | |
83 | ||
e26a1b5f | 84 | cfs_log(log_domain, log_level, NULL, 0, NULL, "%s", message); |
fe000966 DM |
85 | } |
86 | ||
87 | static gboolean write_pidfile(pid_t pid) | |
88 | { | |
89 | char *strpid = g_strdup_printf("%d\n", pid); | |
90 | gboolean res = atomic_write_file(CFS_PID_FN, strpid, strlen(strpid), 0644, getgid()); | |
91 | g_free(strpid); | |
92 | ||
93 | return res; | |
94 | } | |
95 | ||
96 | static cfs_plug_t *find_plug(const char *path, char **sub) | |
97 | { | |
98 | g_return_val_if_fail(root_plug != NULL, NULL); | |
99 | g_return_val_if_fail(path != NULL, NULL); | |
100 | ||
101 | while(*path == '/') path++; | |
102 | ||
103 | cfs_debug("find_plug start %s", path); | |
104 | ||
105 | char *tmppath = g_strdup(path); | |
106 | char *subpath = tmppath; | |
107 | ||
108 | cfs_plug_t *plug = root_plug->lookup_plug(root_plug, &subpath); | |
109 | ||
84e1bd35 | 110 | cfs_debug("find_plug end %s = %p (%s)", path, (void *) plug, subpath); |
fe000966 | 111 | |
b1f92b4e | 112 | if (subpath && subpath[0]) |
fe000966 DM |
113 | *sub = g_strdup(subpath); |
114 | ||
115 | g_free(tmppath); | |
116 | ||
117 | return plug; | |
118 | } | |
119 | ||
120 | void *cfs_fuse_init(struct fuse_conn_info *conn) | |
121 | { | |
122 | return NULL; | |
123 | } | |
124 | ||
125 | static int cfs_fuse_getattr(const char *path, struct stat *stbuf) | |
126 | { | |
127 | cfs_debug("enter cfs_fuse_getattr %s", path); | |
128 | ||
129 | int ret = -EACCES; | |
130 | ||
131 | char *subpath = NULL; | |
132 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 133 | |
fe000966 DM |
134 | if (plug && plug->ops && plug->ops->getattr) { |
135 | ret = plug->ops->getattr(plug, subpath ? subpath : "", stbuf); | |
136 | ||
137 | stbuf->st_gid = cfs.gid; | |
138 | ||
42789cda | 139 | if (path_is_private(path)) { |
fe000966 | 140 | stbuf->st_mode &= 0777700; |
42789cda DM |
141 | } else { |
142 | if (S_ISDIR(stbuf->st_mode) || S_ISLNK(stbuf->st_mode)) { | |
143 | stbuf->st_mode &= 0777755; // access for other users | |
144 | } else { | |
145 | if (path_is_lxc_conf(path)) { | |
146 | stbuf->st_mode &= 0777755; // access for other users | |
147 | } else { | |
148 | stbuf->st_mode &= 0777750; // no access for other users | |
149 | } | |
150 | } | |
151 | } | |
fe000966 DM |
152 | } |
153 | ||
154 | cfs_debug("leave cfs_fuse_getattr %s (%d)", path, ret); | |
155 | ||
156 | if (subpath) | |
157 | g_free(subpath); | |
158 | ||
159 | return ret; | |
160 | ||
161 | } | |
162 | ||
163 | static int cfs_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler, | |
164 | off_t offset, struct fuse_file_info *fi) | |
165 | { | |
166 | (void) offset; | |
167 | (void) fi; | |
168 | ||
169 | cfs_debug("enter cfs_fuse_readdir %s", path); | |
170 | ||
171 | int ret = -EACCES; | |
172 | ||
173 | char *subpath = NULL; | |
174 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 175 | |
fe000966 DM |
176 | if (!plug) |
177 | goto ret; | |
178 | ||
179 | if (plug->ops && plug->ops->readdir) | |
180 | ret = plug->ops->readdir(plug, subpath ? subpath : "", buf, filler, 0, fi); | |
181 | ret: | |
182 | cfs_debug("leave cfs_fuse_readdir %s (%d)", path, ret); | |
b1f92b4e | 183 | |
fe000966 DM |
184 | if (subpath) |
185 | g_free(subpath); | |
b1f92b4e | 186 | |
fe000966 DM |
187 | return ret; |
188 | } | |
189 | ||
70901ae9 SP |
190 | static int cfs_fuse_chmod(const char *path, mode_t mode) |
191 | { | |
192 | int ret = -EPERM; | |
193 | ||
194 | cfs_debug("enter cfs_fuse_chmod %s", path); | |
195 | ||
196 | mode_t allowed_mode = (S_IRUSR | S_IWUSR); | |
197 | if (!path_is_private(path)) | |
198 | allowed_mode |= (S_IRGRP); | |
199 | ||
200 | // allow only setting our supported modes (0600 for priv, 0640 for rest) | |
146ea965 DC |
201 | // mode has additional bits set, which we ignore; see stat(2) |
202 | if ((mode & ALLPERMS) == allowed_mode) | |
70901ae9 SP |
203 | ret = 0; |
204 | ||
205 | cfs_debug("leave cfs_fuse_chmod %s (%d) mode: %o", path, ret, (int)mode); | |
206 | ||
207 | return ret; | |
208 | } | |
209 | ||
210 | static int cfs_fuse_chown(const char *path, uid_t user, gid_t group) | |
211 | { | |
212 | int ret = -EPERM; | |
213 | ||
214 | cfs_debug("enter cfs_fuse_chown %s", path); | |
215 | ||
216 | // we get -1 if no change should be made | |
217 | if ((user == 0 || user == -1) && (group == cfs.gid || group == -1)) | |
218 | ret = 0; | |
219 | ||
220 | cfs_debug("leave cfs_fuse_chown %s (%d) (uid: %d; gid: %d)", path, ret, user, group); | |
221 | ||
222 | return ret; | |
223 | } | |
224 | ||
fe000966 DM |
225 | static int cfs_fuse_mkdir(const char *path, mode_t mode) |
226 | { | |
227 | cfs_debug("enter cfs_fuse_mkdir %s", path); | |
228 | ||
229 | int ret = -EACCES; | |
230 | ||
231 | char *subpath = NULL; | |
232 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 233 | |
fe000966 DM |
234 | if (!plug) |
235 | goto ret; | |
236 | ||
237 | if (subpath && plug->ops && plug->ops->mkdir) | |
238 | ret = plug->ops->mkdir(plug, subpath, mode); | |
239 | ||
240 | ret: | |
241 | cfs_debug("leave cfs_fuse_mkdir %s (%d)", path, ret); | |
242 | ||
243 | if (subpath) | |
244 | g_free(subpath); | |
245 | ||
246 | return ret; | |
247 | } | |
248 | ||
249 | static int cfs_fuse_rmdir(const char *path) | |
250 | { | |
251 | cfs_debug("enter cfs_fuse_rmdir %s", path); | |
252 | ||
253 | int ret = -EACCES; | |
254 | ||
255 | char *subpath = NULL; | |
256 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 257 | |
fe000966 DM |
258 | if (!plug) |
259 | goto ret; | |
260 | ||
261 | if (subpath && plug->ops && plug->ops->rmdir) | |
262 | ret = plug->ops->rmdir(plug, subpath); | |
263 | ||
264 | ret: | |
265 | cfs_debug("leave cfs_fuse_rmdir %s (%d)", path, ret); | |
266 | ||
267 | if (subpath) | |
268 | g_free(subpath); | |
269 | ||
270 | return ret; | |
271 | } | |
272 | ||
273 | static int cfs_fuse_rename(const char *from, const char *to) | |
274 | { | |
275 | cfs_debug("enter cfs_fuse_rename from %s to %s", from, to); | |
276 | ||
277 | int ret = -EACCES; | |
278 | ||
279 | char *sub_from = NULL; | |
280 | cfs_plug_t *plug_from = find_plug(from, &sub_from); | |
281 | ||
282 | char *sub_to = NULL; | |
283 | cfs_plug_t *plug_to = find_plug(to, &sub_to); | |
284 | ||
285 | if (!plug_from || !plug_to || plug_from != plug_to) | |
286 | goto ret; | |
287 | ||
288 | if (plug_from->ops && plug_from->ops->rename && sub_from && sub_to) | |
289 | ret = plug_from->ops->rename(plug_from, sub_from, sub_to); | |
290 | ||
291 | ret: | |
292 | cfs_debug("leave cfs_fuse_rename from %s to %s (%d)", from, to, ret); | |
293 | ||
294 | if (sub_from) | |
295 | g_free(sub_from); | |
296 | ||
297 | if (sub_to) | |
298 | g_free(sub_to); | |
299 | ||
300 | return ret; | |
301 | } | |
302 | ||
303 | static int cfs_fuse_open(const char *path, struct fuse_file_info *fi) | |
304 | { | |
305 | cfs_debug("enter cfs_fuse_open %s", path); | |
306 | ||
307 | fi->direct_io = 1; | |
308 | fi->keep_cache = 0; | |
309 | ||
310 | int ret = -EACCES; | |
311 | ||
312 | char *subpath = NULL; | |
313 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e DM |
314 | |
315 | if (plug && plug->ops) { | |
fe000966 DM |
316 | if ((subpath || !plug->ops->readdir) && plug->ops->open) { |
317 | ret = plug->ops->open(plug, subpath ? subpath : "", fi); | |
318 | } | |
319 | } | |
320 | ||
321 | cfs_debug("leave cfs_fuse_open %s (%d)", path, ret); | |
322 | ||
323 | if (subpath) | |
324 | g_free(subpath); | |
325 | ||
326 | return ret; | |
327 | } | |
328 | ||
329 | static int cfs_fuse_read(const char *path, char *buf, size_t size, off_t offset, | |
330 | struct fuse_file_info *fi) | |
331 | { | |
332 | (void) fi; | |
333 | ||
e5a5a3ea | 334 | cfs_debug("enter cfs_fuse_read %s %zu %jd", path, size, offset); |
fe000966 DM |
335 | |
336 | int ret = -EACCES; | |
337 | ||
338 | char *subpath = NULL; | |
339 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e DM |
340 | |
341 | if (plug && plug->ops) { | |
fe000966 DM |
342 | if ((subpath || !plug->ops->readdir) && plug->ops->read) |
343 | ret = plug->ops->read(plug, subpath ? subpath : "", buf, size, offset, fi); | |
344 | } | |
345 | ||
346 | cfs_debug("leave cfs_fuse_read %s (%d)", path, ret); | |
347 | ||
348 | if (subpath) | |
349 | g_free(subpath); | |
350 | ||
351 | return ret; | |
352 | } | |
353 | ||
354 | static int cfs_fuse_write(const char *path, const char *buf, size_t size, | |
355 | off_t offset, struct fuse_file_info *fi) | |
356 | { | |
357 | (void) fi; | |
358 | ||
e5a5a3ea | 359 | cfs_debug("enter cfs_fuse_write %s %zu %jd", path, size, offset); |
fe000966 DM |
360 | |
361 | int ret = -EACCES; | |
362 | ||
363 | char *subpath = NULL; | |
364 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e DM |
365 | |
366 | if (plug && plug->ops) { | |
fe000966 | 367 | if ((subpath || !plug->ops->readdir) && plug->ops->write) |
b1f92b4e | 368 | ret = plug->ops->write(plug, subpath ? subpath : "", |
fe000966 DM |
369 | buf, size, offset, fi); |
370 | } | |
b1f92b4e | 371 | |
fe000966 DM |
372 | cfs_debug("leave cfs_fuse_write %s (%d)", path, ret); |
373 | ||
374 | if (subpath) | |
375 | g_free(subpath); | |
376 | ||
377 | return ret; | |
378 | } | |
379 | ||
380 | static int cfs_fuse_truncate(const char *path, off_t size) | |
381 | { | |
e5a5a3ea | 382 | cfs_debug("enter cfs_fuse_truncate %s %jd", path, size); |
fe000966 DM |
383 | |
384 | int ret = -EACCES; | |
385 | ||
386 | char *subpath = NULL; | |
387 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e DM |
388 | |
389 | if (plug && plug->ops) { | |
fe000966 DM |
390 | if ((subpath || !plug->ops->readdir) && plug->ops->truncate) |
391 | ret = plug->ops->truncate(plug, subpath ? subpath : "", size); | |
392 | } | |
393 | ||
394 | cfs_debug("leave cfs_fuse_truncate %s (%d)", path, ret); | |
395 | ||
396 | if (subpath) | |
397 | g_free(subpath); | |
398 | ||
399 | return ret; | |
400 | } | |
401 | ||
402 | static int cfs_fuse_create(const char *path, mode_t mode, struct fuse_file_info *fi) | |
403 | { | |
404 | cfs_debug("enter cfs_fuse_create %s", path); | |
405 | ||
406 | int ret = -EACCES; | |
407 | ||
408 | char *subpath = NULL; | |
409 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 410 | |
fe000966 DM |
411 | if (!plug) |
412 | goto ret; | |
413 | ||
414 | if (subpath && plug->ops && plug->ops->create) | |
415 | ret = plug->ops->create(plug, subpath, mode, fi); | |
416 | ||
417 | ret: | |
418 | cfs_debug("leave cfs_fuse_create %s (%d)", path, ret); | |
419 | ||
420 | if (subpath) | |
421 | g_free(subpath); | |
422 | ||
423 | return ret; | |
424 | } | |
425 | ||
426 | static int cfs_fuse_unlink(const char *path) | |
427 | { | |
428 | cfs_debug("enter cfs_fuse_unlink %s", path); | |
429 | ||
430 | int ret = -EACCES; | |
431 | ||
432 | char *subpath = NULL; | |
433 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 434 | |
fe000966 DM |
435 | if (!plug) |
436 | goto ret; | |
437 | ||
438 | if (subpath && plug->ops && plug->ops->unlink) | |
439 | ret = plug->ops->unlink(plug, subpath); | |
440 | ||
441 | ret: | |
442 | cfs_debug("leave cfs_fuse_unlink %s (%d)", path, ret); | |
443 | ||
444 | if (subpath) | |
445 | g_free(subpath); | |
b1f92b4e | 446 | |
fe000966 DM |
447 | return ret; |
448 | } | |
449 | ||
450 | static int cfs_fuse_readlink(const char *path, char *buf, size_t max) | |
451 | { | |
452 | cfs_debug("enter cfs_fuse_readlink %s", path); | |
453 | ||
454 | int ret = -EACCES; | |
455 | ||
456 | char *subpath = NULL; | |
457 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 458 | |
fe000966 DM |
459 | if (!plug) |
460 | goto ret; | |
461 | ||
462 | if (plug->ops && plug->ops->readlink) | |
463 | ret = plug->ops->readlink(plug, subpath ? subpath : "", buf, max); | |
b1f92b4e | 464 | |
fe000966 DM |
465 | ret: |
466 | cfs_debug("leave cfs_fuse_readlink %s (%d)", path, ret); | |
467 | ||
468 | if (subpath) | |
469 | g_free(subpath); | |
470 | ||
471 | return ret; | |
472 | } | |
473 | ||
474 | static int cfs_fuse_utimens(const char *path, const struct timespec tv[2]) | |
475 | { | |
476 | cfs_debug("enter cfs_fuse_utimens %s", path); | |
477 | ||
478 | int ret = -EACCES; | |
479 | ||
480 | char *subpath = NULL; | |
481 | cfs_plug_t *plug = find_plug(path, &subpath); | |
b1f92b4e | 482 | |
fe000966 DM |
483 | if (!plug) |
484 | goto ret; | |
485 | ||
486 | if (plug->ops && plug->ops->utimens) | |
487 | ret = plug->ops->utimens(plug, subpath ? subpath : "", tv); | |
b1f92b4e | 488 | |
fe000966 DM |
489 | ret: |
490 | cfs_debug("leave cfs_fuse_utimens %s (%d)", path, ret); | |
491 | ||
492 | if (subpath) | |
493 | g_free(subpath); | |
494 | ||
495 | return ret; | |
496 | } | |
497 | ||
498 | static int cfs_fuse_statfs(const char *path, struct statvfs *stbuf) | |
499 | { | |
500 | g_return_val_if_fail(root_plug != NULL, PARAM_CHECK_ERRNO); | |
501 | ||
502 | cfs_debug("enter cfs_fuse_statfs %s", path); | |
503 | ||
504 | int ret = -EACCES; | |
505 | ||
506 | if (root_plug && root_plug->ops && root_plug->ops->statfs) | |
507 | ret = root_plug->ops->statfs(root_plug, "", stbuf); | |
508 | ||
509 | return ret; | |
510 | } | |
511 | ||
512 | static struct fuse_operations fuse_ops = { | |
513 | .getattr = cfs_fuse_getattr, | |
514 | .readdir = cfs_fuse_readdir, | |
515 | .mkdir = cfs_fuse_mkdir, | |
516 | .rmdir = cfs_fuse_rmdir, | |
517 | .rename = cfs_fuse_rename, | |
518 | .open = cfs_fuse_open, | |
519 | .read = cfs_fuse_read, | |
520 | .write = cfs_fuse_write, | |
521 | .truncate = cfs_fuse_truncate, | |
522 | .create = cfs_fuse_create, | |
523 | .unlink = cfs_fuse_unlink, | |
524 | .readlink = cfs_fuse_readlink, | |
525 | .utimens = cfs_fuse_utimens, | |
526 | .statfs = cfs_fuse_statfs, | |
70901ae9 SP |
527 | .init = cfs_fuse_init, |
528 | .chown = cfs_fuse_chown, | |
529 | .chmod = cfs_fuse_chmod | |
fe000966 DM |
530 | }; |
531 | ||
532 | static char * | |
533 | create_dot_version_cb(cfs_plug_t *plug) | |
534 | { | |
535 | GString *outbuf = g_string_new(NULL); | |
536 | char *data = NULL; | |
537 | ||
538 | if (cfs_create_version_msg(outbuf) == 0) { | |
539 | data = outbuf->str; | |
540 | g_string_free(outbuf, FALSE); | |
541 | } else { | |
542 | g_string_free(outbuf, TRUE); | |
543 | } | |
544 | ||
545 | return data; | |
546 | } | |
547 | ||
548 | static char * | |
549 | create_dot_members_cb(cfs_plug_t *plug) | |
550 | { | |
551 | GString *outbuf = g_string_new(NULL); | |
552 | char *data = NULL; | |
553 | ||
554 | if (cfs_create_memberlist_msg(outbuf) == 0) { | |
555 | data = outbuf->str; | |
556 | g_string_free(outbuf, FALSE); | |
557 | } else { | |
558 | g_string_free(outbuf, TRUE); | |
559 | } | |
560 | ||
561 | return data; | |
562 | } | |
563 | ||
564 | static char * | |
565 | create_dot_vmlist_cb(cfs_plug_t *plug) | |
566 | { | |
567 | GString *outbuf = g_string_new(NULL); | |
568 | char *data = NULL; | |
569 | ||
570 | if (cfs_create_vmlist_msg(outbuf) == 0) { | |
571 | data = outbuf->str; | |
572 | g_string_free(outbuf, FALSE); | |
573 | } else { | |
574 | g_string_free(outbuf, TRUE); | |
575 | } | |
576 | ||
577 | return data; | |
578 | } | |
579 | ||
580 | static char * | |
581 | create_dot_rrd_cb(cfs_plug_t *plug) | |
582 | { | |
583 | GString *outbuf = g_string_new(NULL); | |
584 | ||
585 | cfs_rrd_dump(outbuf); | |
586 | char *data = outbuf->str; | |
587 | g_string_free(outbuf, FALSE); | |
588 | ||
589 | return data; | |
590 | } | |
591 | ||
592 | static char * | |
593 | create_dot_clusterlog_cb(cfs_plug_t *plug) | |
594 | { | |
595 | GString *outbuf = g_string_new(NULL); | |
596 | ||
597 | cfs_cluster_log_dump(outbuf, NULL, 50); | |
598 | char *data = outbuf->str; | |
599 | g_string_free(outbuf, FALSE); | |
600 | ||
601 | return data; | |
602 | } | |
603 | ||
604 | static char * | |
605 | read_debug_setting_cb(cfs_plug_t *plug) | |
606 | { | |
b1f92b4e | 607 | return g_strdup_printf("%d\n", !!cfs.debug); |
fe000966 DM |
608 | } |
609 | ||
65420fb3 DM |
610 | static void |
611 | my_qb_log_filter(struct qb_log_callsite *cs) | |
612 | { | |
613 | int32_t priority = cfs.debug ? LOG_DEBUG : LOG_INFO; | |
614 | ||
615 | if (qb_bit_is_set(cs->tags, QB_LOG_TAG_LIBQB_MSG_BIT)) { | |
616 | if (cs->priority <= (cfs.debug ? priority : LOG_WARNING)) { | |
617 | qb_bit_set(cs->targets, QB_LOG_SYSLOG); | |
618 | } else { | |
619 | qb_bit_clear(cs->targets, QB_LOG_SYSLOG); | |
620 | } | |
621 | if (cs->priority <= priority) { | |
622 | qb_bit_set(cs->targets, QB_LOG_STDERR); | |
623 | } else { | |
624 | qb_bit_clear(cs->targets, QB_LOG_STDERR); | |
625 | } | |
626 | } else { | |
627 | if (cs->priority <= priority) { | |
628 | qb_bit_set(cs->targets, QB_LOG_SYSLOG); | |
629 | qb_bit_set(cs->targets, QB_LOG_STDERR); | |
630 | } else { | |
631 | qb_bit_clear(cs->targets, QB_LOG_SYSLOG); | |
632 | qb_bit_clear(cs->targets, QB_LOG_STDERR); | |
633 | } | |
634 | } | |
635 | } | |
636 | ||
b1f92b4e DM |
637 | static void |
638 | update_qb_log_settings(void) | |
64405e32 | 639 | { |
65420fb3 | 640 | qb_log_filter_fn_set(my_qb_log_filter); |
92bc48dd | 641 | |
64405e32 DM |
642 | if (cfs.debug) { |
643 | qb_log_format_set(QB_LOG_SYSLOG, "[%g] %p: %b (%f:%l:%n)"); | |
644 | qb_log_format_set(QB_LOG_STDERR, "[%g] %p: %b (%f:%l:%n)"); | |
64405e32 DM |
645 | } else { |
646 | qb_log_format_set(QB_LOG_SYSLOG, "[%g] %p: %b"); | |
647 | qb_log_format_set(QB_LOG_STDERR, "[%g] %p: %b"); | |
64405e32 DM |
648 | } |
649 | } | |
650 | ||
b1f92b4e | 651 | static int |
65420fb3 | 652 | write_debug_setting_cb( |
b1f92b4e | 653 | cfs_plug_t *plug, |
fe000966 DM |
654 | const char *buf, |
655 | size_t size) | |
656 | { | |
657 | int res = -EIO; | |
658 | ||
659 | if (size < 2) | |
660 | return res; | |
661 | ||
662 | if (strncmp(buf, "0\n", 2) == 0) { | |
663 | if (cfs.debug) { | |
664 | cfs_message("disable debug mode"); | |
665 | cfs.debug = 0; | |
64405e32 | 666 | update_qb_log_settings(); |
fe000966 DM |
667 | } |
668 | return 2; | |
669 | } else if (strncmp(buf, "1\n", 2) == 0) { | |
670 | if (!cfs.debug) { | |
fe000966 | 671 | cfs.debug = 1; |
64405e32 DM |
672 | update_qb_log_settings(); |
673 | cfs_message("enable debug mode"); | |
fe000966 DM |
674 | } |
675 | return 2; | |
676 | } | |
677 | ||
678 | return res; | |
679 | } | |
680 | ||
b1f92b4e | 681 | static void |
fe000966 DM |
682 | create_symlinks(cfs_plug_base_t *bplug, const char *nodename) |
683 | { | |
684 | g_return_if_fail(bplug != NULL); | |
685 | g_return_if_fail(nodename != NULL); | |
686 | ||
687 | char *lnktarget = g_strdup_printf("nodes/%s", nodename); | |
688 | cfs_plug_link_t *lnk = cfs_plug_link_new("local", lnktarget); | |
689 | g_free(lnktarget); | |
690 | cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk); | |
691 | ||
692 | lnktarget = g_strdup_printf("nodes/%s/qemu-server", nodename); | |
693 | lnk = cfs_plug_link_new("qemu-server", lnktarget); | |
694 | g_free(lnktarget); | |
695 | cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk); | |
b1f92b4e | 696 | |
c63b596f | 697 | // FIXME: remove openvz stuff for 7.x |
fe000966 DM |
698 | lnktarget = g_strdup_printf("nodes/%s/openvz", nodename); |
699 | lnk = cfs_plug_link_new("openvz", lnktarget); | |
700 | g_free(lnktarget); | |
701 | cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk); | |
702 | ||
7f66b436 DM |
703 | lnktarget = g_strdup_printf("nodes/%s/lxc", nodename); |
704 | lnk = cfs_plug_link_new("lxc", lnktarget); | |
705 | g_free(lnktarget); | |
706 | cfs_plug_base_insert(bplug, (cfs_plug_t*)lnk); | |
707 | ||
fe000966 DM |
708 | cfs_plug_func_t *fplug = cfs_plug_func_new(".version", 0440, create_dot_version_cb, NULL); |
709 | cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug); | |
b1f92b4e | 710 | |
fe000966 DM |
711 | fplug = cfs_plug_func_new(".members", 0440, create_dot_members_cb, NULL); |
712 | cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug); | |
713 | ||
714 | fplug = cfs_plug_func_new(".vmlist", 0440, create_dot_vmlist_cb, NULL); | |
715 | cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug); | |
716 | ||
717 | fplug = cfs_plug_func_new(".rrd", 0440, create_dot_rrd_cb, NULL); | |
718 | cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug); | |
719 | ||
720 | fplug = cfs_plug_func_new(".clusterlog", 0440, create_dot_clusterlog_cb, NULL); | |
721 | cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug); | |
722 | ||
723 | fplug = cfs_plug_func_new(".debug", 0640, read_debug_setting_cb, write_debug_setting_cb); | |
724 | cfs_plug_base_insert(bplug, (cfs_plug_t*)fplug); | |
725 | ||
726 | ||
727 | } | |
728 | ||
729 | static char * | |
b1f92b4e | 730 | lookup_node_ip(const char *nodename) |
fe000966 | 731 | { |
6f2ef085 | 732 | char buf[INET6_ADDRSTRLEN]; |
fe000966 DM |
733 | struct addrinfo *ainfo; |
734 | struct addrinfo ahints; | |
ef105625 | 735 | char *res = NULL; |
fe000966 DM |
736 | memset(&ahints, 0, sizeof(ahints)); |
737 | ||
738 | if (getaddrinfo(nodename, NULL, &ahints, &ainfo)) | |
739 | return NULL; | |
740 | ||
741 | if (ainfo->ai_family == AF_INET) { | |
fe000966 DM |
742 | struct sockaddr_in *sa = (struct sockaddr_in *)ainfo->ai_addr; |
743 | inet_ntop(ainfo->ai_family, &sa->sin_addr, buf, sizeof(buf)); | |
744 | if (strncmp(buf, "127.", 4) != 0) { | |
ef105625 | 745 | res = g_strdup(buf); |
fe000966 | 746 | } |
b1f92b4e | 747 | } else if (ainfo->ai_family == AF_INET6) { |
6f2ef085 WB |
748 | struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ainfo->ai_addr; |
749 | inet_ntop(ainfo->ai_family, &sa->sin6_addr, buf, sizeof(buf)); | |
750 | if (strcmp(buf, "::1") != 0) { | |
ef105625 | 751 | res = g_strdup(buf); |
6f2ef085 WB |
752 | } |
753 | } | |
fe000966 | 754 | |
ef105625 WB |
755 | freeaddrinfo(ainfo); |
756 | ||
757 | return res; | |
fe000966 | 758 | } |
64405e32 | 759 | |
b1f92b4e | 760 | static const char* |
64405e32 DM |
761 | log_tags_stringify(uint32_t tags) { |
762 | if (qb_bit_is_set(tags, QB_LOG_TAG_LIBQB_MSG_BIT)) { | |
763 | return "libqb"; | |
764 | } else { | |
765 | GQuark quark = tags; | |
766 | const char *domain = g_quark_to_string(quark); | |
767 | if (domain != NULL) { | |
768 | return domain; | |
769 | } else { | |
770 | return "main"; | |
771 | } | |
772 | } | |
773 | } | |
774 | ||
fe000966 DM |
775 | int main(int argc, char *argv[]) |
776 | { | |
777 | int ret = -1; | |
778 | int lockfd = -1; | |
3db2f0de | 779 | int pipefd[2]; |
fe000966 DM |
780 | |
781 | gboolean foreground = FALSE; | |
782 | gboolean force_local_mode = FALSE; | |
783 | gboolean wrote_pidfile = FALSE; | |
784 | memdb_t *memdb = NULL; | |
785 | dfsm_t *dcdb = NULL; | |
786 | dfsm_t *status_fsm = NULL; | |
787 | ||
64405e32 | 788 | qb_log_init("pmxcfs", LOG_DAEMON, LOG_DEBUG); |
65420fb3 | 789 | /* remove default filter */ |
b1f92b4e | 790 | qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE, |
65420fb3 DM |
791 | QB_LOG_FILTER_FILE, "*", LOG_DEBUG); |
792 | ||
64405e32 DM |
793 | qb_log_tags_stringify_fn_set(log_tags_stringify); |
794 | ||
795 | qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); | |
64405e32 DM |
796 | |
797 | update_qb_log_settings(); | |
fe000966 DM |
798 | |
799 | g_set_print_handler(glib_print_handler); | |
800 | g_set_printerr_handler(glib_print_handler); | |
801 | g_log_set_default_handler(glib_log_handler, NULL); | |
802 | ||
803 | GOptionContext *context; | |
fe000966 DM |
804 | |
805 | GOptionEntry entries[] = { | |
806 | { "debug", 'd', 0, G_OPTION_ARG_NONE, &cfs.debug, "Turn on debug messages", NULL }, | |
807 | { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Do not daemonize server", NULL }, | |
b1f92b4e | 808 | { "local", 'l', 0, G_OPTION_ARG_NONE, &force_local_mode, |
2113d031 | 809 | "Force local mode (ignore corosync.conf, force quorum)", NULL }, |
fe000966 DM |
810 | { NULL }, |
811 | }; | |
812 | ||
813 | context = g_option_context_new (""); | |
814 | g_option_context_add_main_entries (context, entries, NULL); | |
815 | ||
816 | GError *err = NULL; | |
817 | if (!g_option_context_parse (context, &argc, &argv, &err)) | |
818 | { | |
819 | cfs_critical("option parsing failed: %s", err->message); | |
820 | g_error_free (err); | |
64405e32 | 821 | qb_log_fini(); |
fe000966 DM |
822 | exit (1); |
823 | } | |
824 | g_option_context_free(context); | |
825 | ||
826 | if (optind < argc) { | |
827 | cfs_critical("too many arguments"); | |
64405e32 | 828 | qb_log_fini(); |
fe000966 DM |
829 | exit(-1); |
830 | } | |
64405e32 DM |
831 | |
832 | if (cfs.debug) { | |
833 | update_qb_log_settings(); | |
834 | } | |
835 | ||
fe000966 DM |
836 | struct utsname utsname; |
837 | if (uname(&utsname) != 0) { | |
838 | cfs_critical("Unable to read local node name"); | |
64405e32 | 839 | qb_log_fini(); |
fe000966 DM |
840 | exit (-1); |
841 | } | |
b1f92b4e | 842 | |
b1bc4b27 WB |
843 | char *dot = strchr(utsname.nodename, '.'); |
844 | if (dot) | |
845 | *dot = 0; | |
fe000966 DM |
846 | |
847 | cfs.nodename = g_strdup(utsname.nodename); | |
848 | ||
b1f92b4e | 849 | if (!(cfs.ip = lookup_node_ip(cfs.nodename))) { |
fe000966 | 850 | cfs_critical("Unable to get local IP address"); |
64405e32 | 851 | qb_log_fini(); |
fe000966 DM |
852 | exit(-1); |
853 | } | |
854 | ||
855 | struct group *www_data = getgrnam("www-data"); | |
856 | if (!www_data) { | |
857 | cfs_critical("Unable to get www-data group ID"); | |
64405e32 | 858 | qb_log_fini(); |
fe000966 DM |
859 | exit (-1); |
860 | } | |
861 | cfs.gid = www_data->gr_gid; | |
862 | ||
fe000966 DM |
863 | umask(027); |
864 | ||
865 | mkdir(VARLIBDIR, 0755); | |
68da7070 | 866 | mkdir(RUNDIR, 0755); |
6b15bdba | 867 | chown(RUNDIR, 0, cfs.gid); |
fe000966 | 868 | |
fbe6b41e | 869 | if ((lockfd = open(LOCKFILE, O_RDWR|O_CREAT|O_APPEND, 0600)) == -1) { |
fe000966 DM |
870 | cfs_critical("unable to create lock '%s': %s", LOCKFILE, strerror (errno)); |
871 | goto err; | |
872 | } | |
873 | ||
874 | for (int i = 10; i >= 0; i--) { | |
875 | if (flock(lockfd, LOCK_EX|LOCK_NB) != 0) { | |
876 | if (!i) { | |
e8c6be91 | 877 | cfs_critical("unable to acquire pmxcfs lock: %s", strerror (errno)); |
fe000966 DM |
878 | goto err; |
879 | } | |
880 | if (i == 10) | |
e8c6be91 | 881 | cfs_message("unable to acquire pmxcfs lock - trying again"); |
fe000966 DM |
882 | |
883 | sleep(1); | |
884 | } | |
885 | } | |
886 | ||
887 | cfs_status_init(); | |
888 | ||
889 | gboolean create = !g_file_test(DBFILENAME, G_FILE_TEST_EXISTS); | |
890 | ||
891 | if (!(memdb = memdb_open (DBFILENAME))) { | |
892 | cfs_critical("memdb_open failed - unable to open database '%s'", DBFILENAME); | |
893 | goto err; | |
894 | } | |
895 | ||
2113d031 | 896 | // automatically import corosync.conf from host |
fe000966 DM |
897 | if (create && !force_local_mode) { |
898 | char *cdata = NULL; | |
899 | gsize clen = 0; | |
900 | if (g_file_get_contents(HOST_CLUSTER_CONF_FN, &cdata, &clen, NULL)) { | |
901 | ||
902 | guint32 mtime = time(NULL); | |
903 | ||
2113d031 DM |
904 | memdb_create(memdb, "/corosync.conf", 0, mtime); |
905 | if (memdb_write(memdb, "/corosync.conf", 0, mtime, cdata, clen, 0, 1) < 0) { | |
906 | cfs_critical("memdb_write failed - unable to import corosync.conf"); | |
fe000966 DM |
907 | goto err; |
908 | } | |
909 | } | |
910 | } | |
911 | ||
2113d031 | 912 | // does corosync.conf exist? |
fe000966 | 913 | gpointer conf_data = NULL; |
2113d031 | 914 | int len = memdb_read(memdb, "corosync.conf", &conf_data); |
fe000966 DM |
915 | if (len >= 0) { |
916 | if (force_local_mode) { | |
c6e5424f | 917 | cfs_message("forcing local mode (although corosync.conf exists)"); |
fe000966 DM |
918 | cfs_set_quorate(1, TRUE); |
919 | } else { | |
920 | if (!(dcdb = dcdb_new(memdb))) | |
921 | goto err; | |
2113d031 | 922 | dcdb_sync_corosync_conf(memdb, 1); |
fe000966 DM |
923 | } |
924 | } else { | |
2113d031 | 925 | cfs_debug("using local mode (corosync.conf does not exist)"); |
fe000966 DM |
926 | cfs_set_quorate(1, TRUE); |
927 | } | |
928 | if (conf_data) g_free(conf_data); | |
929 | ||
930 | cfs_plug_memdb_t *config = cfs_plug_memdb_new("memdb", memdb, dcdb); | |
b1f92b4e | 931 | |
fe000966 DM |
932 | cfs_plug_base_t *bplug = cfs_plug_base_new("", (cfs_plug_t *)config); |
933 | ||
934 | create_symlinks(bplug, cfs.nodename); | |
935 | ||
936 | root_plug = (cfs_plug_t *)bplug; | |
937 | ||
8ce10e6f | 938 | umount2(CFSDIR, MNT_FORCE); |
fe000966 | 939 | |
27b69f59 | 940 | mkdir(CFSDIR, 0755); |
b1f92b4e | 941 | |
fe000966 DM |
942 | char *fa[] = { "-f", "-odefault_permissions", "-oallow_other", NULL}; |
943 | ||
b1f92b4e DM |
944 | struct fuse_args fuse_args = FUSE_ARGS_INIT(sizeof (fa)/sizeof(gpointer) - 1, fa); |
945 | ||
fe000966 DM |
946 | struct fuse_chan *fuse_chan = fuse_mount(CFSDIR, &fuse_args); |
947 | if (!fuse_chan) { | |
948 | cfs_critical("fuse_mount error: %s", strerror(errno)); | |
949 | goto err; | |
950 | } | |
951 | ||
952 | if (!(fuse = fuse_new(fuse_chan, &fuse_args, &fuse_ops, sizeof(fuse_ops), NULL))) { | |
953 | cfs_critical("fuse_new error: %s", strerror(errno)); | |
954 | goto err; | |
955 | } | |
956 | ||
957 | fuse_set_signal_handlers(fuse_get_session(fuse)); | |
958 | ||
fe000966 | 959 | if (!foreground) { |
3db2f0de DC |
960 | if (pipe(pipefd) == -1) { |
961 | cfs_critical("pipe error: %s", strerror(errno)); | |
962 | goto err; | |
963 | } | |
964 | ||
fe000966 DM |
965 | pid_t cpid = fork(); |
966 | ||
967 | if (cpid == -1) { | |
968 | cfs_critical("failed to daemonize program - %s", strerror (errno)); | |
969 | goto err; | |
970 | } else if (cpid) { | |
3db2f0de DC |
971 | int readbytes, errno_tmp; |
972 | char readbuffer; | |
973 | close(pipefd[1]); | |
974 | readbytes = read(pipefd[0], &readbuffer, sizeof(readbuffer)); | |
975 | errno_tmp = errno; | |
976 | close(pipefd[0]); | |
977 | if (readbytes == -1) { | |
978 | cfs_critical("read error: %s", strerror(errno_tmp)); | |
979 | kill(cpid, SIGKILL); | |
980 | goto err; | |
981 | } else if (readbytes != 1 || readbuffer != '1') { | |
982 | cfs_critical("child failed to send '1'"); | |
983 | kill(cpid, SIGKILL); | |
984 | goto err; | |
985 | } | |
986 | /* child finished starting up */ | |
fe000966 | 987 | write_pidfile(cpid); |
64405e32 | 988 | qb_log_fini(); |
fe000966 DM |
989 | _exit (0); |
990 | } else { | |
991 | int nullfd; | |
3db2f0de | 992 | close(pipefd[0]); |
fe000966 DM |
993 | |
994 | chroot("/"); | |
995 | ||
996 | if ((nullfd = open("/dev/null", O_RDWR, 0)) != -1) { | |
997 | dup2(nullfd, 0); | |
998 | dup2(nullfd, 1); | |
999 | dup2(nullfd, 2); | |
1000 | if (nullfd > 2) | |
1001 | close (nullfd); | |
1002 | } | |
1003 | ||
1004 | // do not print to the console after this point | |
64405e32 DM |
1005 | qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE); |
1006 | ||
fe000966 DM |
1007 | setsid(); |
1008 | } | |
1009 | } else { | |
1010 | write_pidfile(getpid()); | |
1011 | } | |
1012 | ||
1013 | wrote_pidfile = TRUE; | |
1014 | ||
1015 | cfs_loop_t *corosync_loop = cfs_loop_new(fuse); | |
1016 | ||
1017 | cfs_service_t *service_quorum = NULL; | |
1018 | cfs_service_t *service_confdb = NULL; | |
1019 | cfs_service_t *service_dcdb = NULL; | |
1020 | cfs_service_t *service_status = NULL; | |
1021 | ||
1022 | if (dcdb) { | |
1023 | ||
1024 | service_quorum = service_quorum_new(); | |
1025 | ||
1026 | cfs_loop_add_service(corosync_loop, service_quorum, QB_LOOP_HIGH); | |
1027 | ||
1028 | service_confdb = service_confdb_new(); | |
1029 | ||
1030 | cfs_loop_add_service(corosync_loop, service_confdb, QB_LOOP_MED); | |
1031 | ||
1032 | service_dcdb = service_dfsm_new(dcdb); | |
1033 | cfs_service_set_timer(service_dcdb, DCDB_VERIFY_TIME); | |
1034 | ||
1035 | cfs_loop_add_service(corosync_loop, service_dcdb, QB_LOOP_MED); | |
1036 | ||
1037 | status_fsm = cfs_status_dfsm_new(); | |
1038 | service_status = service_dfsm_new(status_fsm); | |
1039 | ||
1040 | cfs_loop_add_service(corosync_loop, service_status, QB_LOOP_LOW); | |
1041 | ||
1042 | } | |
1043 | ||
1044 | cfs_loop_start_worker(corosync_loop); | |
1045 | ||
1046 | server_start(memdb); | |
1047 | ||
68da7070 TL |
1048 | unlink(RESTART_FLAG_FILE); |
1049 | ||
3db2f0de DC |
1050 | if (!foreground) { |
1051 | /* finished starting up, signaling parent */ | |
1052 | write(pipefd[1], "1", 1); | |
1053 | close(pipefd[1]); | |
1054 | } | |
1055 | ||
fe000966 DM |
1056 | ret = fuse_loop_mt(fuse); |
1057 | ||
6b15bdba TL |
1058 | open(RESTART_FLAG_FILE, O_CREAT|O_NOCTTY|O_NONBLOCK, S_IRUSR | S_IRGRP); |
1059 | chown(RESTART_FLAG_FILE, 0, cfs.gid); | |
68da7070 | 1060 | |
fe000966 DM |
1061 | cfs_message("teardown filesystem"); |
1062 | ||
1063 | server_stop(); | |
1064 | ||
1065 | fuse_unmount(CFSDIR, fuse_chan); | |
1066 | ||
1067 | fuse_destroy(fuse); | |
1068 | ||
1069 | cfs_debug("set stop event loop flag"); | |
1070 | ||
1071 | cfs_loop_stop_worker(corosync_loop); | |
1072 | ||
1073 | cfs_loop_destroy(corosync_loop); | |
1074 | ||
1075 | cfs_debug("worker finished"); | |
1076 | ||
1077 | if (service_dcdb) | |
1078 | service_dfsm_destroy(service_dcdb); | |
1079 | ||
1080 | if (service_confdb) | |
1081 | service_confdb_destroy(service_confdb); | |
1082 | ||
1083 | if (service_quorum) | |
1084 | service_quorum_destroy(service_quorum); | |
1085 | ||
1086 | if (service_status) | |
1087 | service_dfsm_destroy(service_status); | |
1088 | ||
fe000966 DM |
1089 | ret: |
1090 | ||
b1f92b4e | 1091 | if (status_fsm) |
fe000966 DM |
1092 | dfsm_destroy(status_fsm); |
1093 | ||
1094 | if (dcdb) | |
1095 | dfsm_destroy(dcdb); | |
1096 | ||
b1f92b4e | 1097 | if (memdb) |
fe000966 DM |
1098 | memdb_close(memdb); |
1099 | ||
1100 | if (wrote_pidfile) | |
1101 | unlink(CFS_PID_FN); | |
1102 | ||
1103 | cfs_message("exit proxmox configuration filesystem (%d)", ret); | |
1104 | ||
1105 | cfs_status_cleanup(); | |
1106 | ||
64405e32 DM |
1107 | qb_log_fini(); |
1108 | ||
fe000966 DM |
1109 | exit(ret); |
1110 | ||
1111 | err: | |
1112 | goto ret; | |
1113 | } |