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