int xbps_alternatives_register(struct xbps_handle *xhp, xbps_dictionary_t pkgd) { xbps_array_t allkeys; xbps_dictionary_t alternatives, pkg_alternatives; const char *pkgver; char *pkgname; int rv = 0; assert(xhp); if (xhp->pkgdb == NULL) return EINVAL; pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives"); if (!xbps_dictionary_count(pkg_alternatives)) return 0; alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_"); if (alternatives == NULL) { alternatives = xbps_dictionary_create(); xbps_dictionary_set(xhp->pkgdb, "_XBPS_ALTERNATIVES_", alternatives); xbps_object_release(alternatives); } alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_"); assert(alternatives); xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); pkgname = xbps_pkg_name(pkgver); if (pkgname == NULL) return EINVAL; allkeys = xbps_dictionary_all_keys(pkg_alternatives); for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) { xbps_array_t array; xbps_object_t keysym; const char *keyname; bool alloc = false; keysym = xbps_array_get(allkeys, i); keyname = xbps_dictionary_keysym_cstring_nocopy(keysym); array = xbps_dictionary_get(alternatives, keyname); if (array == NULL) { alloc = true; array = xbps_array_create(); } else { /* already registered */ if (xbps_match_string_in_array(array, pkgname)) continue; } xbps_array_add_cstring(array, pkgname); xbps_dictionary_set(alternatives, keyname, array); xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL, "%s: registered '%s' alternatives group", pkgver, keyname); if (alloc) { /* apply alternatives for this group */ rv = create_symlinks(xhp, xbps_dictionary_get(pkg_alternatives, keyname), keyname); xbps_object_release(array); if (rv != 0) break; } } xbps_object_release(allkeys); free(pkgname); return rv; }
int xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname, const char *group) { xbps_array_t allkeys; xbps_dictionary_t alternatives, pkg_alternatives, pkgd; const char *pkgver; int rv = 0; assert(xhp); assert(pkgname); alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_"); if (alternatives == NULL) return ENOENT; pkgd = xbps_pkgdb_get_pkg(xhp, pkgname); if (pkgd == NULL) return ENOENT; pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives"); if (!xbps_dictionary_count(pkg_alternatives)) return ENOENT; xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); allkeys = xbps_dictionary_all_keys(pkg_alternatives); for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) { xbps_array_t array; xbps_object_t keysym; xbps_string_t kstr; const char *keyname; keysym = xbps_array_get(allkeys, i); keyname = xbps_dictionary_keysym_cstring_nocopy(keysym); if (group && strcmp(keyname, group)) { rv = ENOENT; continue; } array = xbps_dictionary_get(alternatives, keyname); if (array == NULL) continue; /* put this alternative group at the head */ xbps_remove_string_from_array(array, pkgname); kstr = xbps_string_create_cstring(pkgname); xbps_array_add_first(array, kstr); xbps_object_release(kstr); /* apply the alternatives group */ xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_ADDED, 0, NULL, "%s: applying '%s' alternatives group", pkgver, keyname); rv = create_symlinks(xhp, xbps_dictionary_get(pkg_alternatives, keyname), keyname); if (rv != 0) break; } xbps_object_release(allkeys); return rv; }
int xbps_alternatives_unregister(struct xbps_handle *xhp, xbps_dictionary_t pkgd) { xbps_array_t allkeys; xbps_dictionary_t alternatives, pkg_alternatives; const char *pkgver; char *pkgname; bool update = false; int rv = 0; assert(xhp); alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_"); if (alternatives == NULL) return 0; pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives"); if (!xbps_dictionary_count(pkg_alternatives)) return 0; xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver); if ((pkgname = xbps_pkg_name(pkgver)) == NULL) return EINVAL; xbps_dictionary_get_bool(pkgd, "alternatives-update", &update); allkeys = xbps_dictionary_all_keys(pkg_alternatives); for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) { xbps_array_t array; xbps_object_t keysym; const char *first = NULL, *keyname; keysym = xbps_array_get(allkeys, i); keyname = xbps_dictionary_keysym_cstring_nocopy(keysym); array = xbps_dictionary_get(alternatives, keyname); if (array == NULL) continue; xbps_array_get_cstring_nocopy(array, 0, &first); if (strcmp(pkgname, first) == 0) { /* this pkg is the current alternative for this group */ rv = remove_symlinks(xhp, xbps_dictionary_get(pkg_alternatives, keyname), keyname); if (rv != 0) break; } xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_REMOVED, 0, NULL, "%s: unregistered '%s' alternatives group", pkgver, keyname); if (!update) xbps_remove_string_from_array(array, pkgname); if (xbps_array_count(array) == 0) { xbps_dictionary_remove(alternatives, keyname); } else { xbps_dictionary_t curpkgd; first = NULL; xbps_array_get_cstring_nocopy(array, 0, &first); curpkgd = xbps_pkgdb_get_pkg(xhp, first); assert(curpkgd); xbps_set_cb_state(xhp, XBPS_STATE_ALTGROUP_SWITCHED, 0, NULL, "Switched '%s' alternatives group to '%s'", keyname, first); pkg_alternatives = xbps_dictionary_get(curpkgd, "alternatives"); rv = create_symlinks(xhp, xbps_dictionary_get(pkg_alternatives, keyname), keyname); if (rv != 0) break; } } xbps_object_release(allkeys); free(pkgname); return rv; }
int main(int argc, char *argv[]) { int ret = -1; int lockfd = -1; gboolean foreground = FALSE; gboolean force_local_mode = FALSE; gboolean wrote_pidfile = FALSE; memdb_t *memdb = NULL; dfsm_t *dcdb = NULL; dfsm_t *status_fsm = NULL; qb_log_init("pmxcfs", LOG_DAEMON, LOG_DEBUG); /* remove default filter */ qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE, QB_LOG_FILTER_FILE, "*", LOG_DEBUG); qb_log_tags_stringify_fn_set(log_tags_stringify); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); update_qb_log_settings(); g_set_print_handler(glib_print_handler); g_set_printerr_handler(glib_print_handler); g_log_set_default_handler(glib_log_handler, NULL); GOptionContext *context; GOptionEntry entries[] = { { "debug", 'd', 0, G_OPTION_ARG_NONE, &cfs.debug, "Turn on debug messages", NULL }, { "foreground", 'f', 0, G_OPTION_ARG_NONE, &foreground, "Do not daemonize server", NULL }, { "local", 'l', 0, G_OPTION_ARG_NONE, &force_local_mode, "Force local mode (ignore cluster.conf, force quorum)", NULL }, { NULL }, }; context = g_option_context_new (""); g_option_context_add_main_entries (context, entries, NULL); GError *err = NULL; if (!g_option_context_parse (context, &argc, &argv, &err)) { cfs_critical("option parsing failed: %s", err->message); g_error_free (err); qb_log_fini(); exit (1); } g_option_context_free(context); if (optind < argc) { cfs_critical("too many arguments"); qb_log_fini(); exit(-1); } if (cfs.debug) { update_qb_log_settings(); } struct utsname utsname; if (uname(&utsname) != 0) { cfs_critical("Unable to read local node name"); qb_log_fini(); exit (-1); } for (int i=0; i < sizeof(utsname.nodename); i++) { if (utsname.nodename[i] =='.') utsname.nodename[i] = 0; } cfs.nodename = g_strdup(utsname.nodename); if (!(cfs.ip = lookup_node_ip(cfs.nodename))) { cfs_critical("Unable to get local IP address"); qb_log_fini(); exit(-1); } struct group *www_data = getgrnam("www-data"); if (!www_data) { cfs_critical("Unable to get www-data group ID"); qb_log_fini(); exit (-1); } cfs.gid = www_data->gr_gid; g_thread_init(NULL); umask(027); mkdir(VARLIBDIR, 0755); if ((lockfd = open(LOCKFILE, O_RDWR|O_CREAT|O_APPEND)) == -1) { cfs_critical("unable to create lock '%s': %s", LOCKFILE, strerror (errno)); goto err; } for (int i = 10; i >= 0; i--) { if (flock(lockfd, LOCK_EX|LOCK_NB) != 0) { if (!i) { cfs_critical("unable to aquire pmxcfs lock: %s", strerror (errno)); goto err; } if (i == 10) cfs_message("unable to aquire pmxcfs lock - trying again"); sleep(1); } } cfs_status_init(); gboolean create = !g_file_test(DBFILENAME, G_FILE_TEST_EXISTS); if (!(memdb = memdb_open (DBFILENAME))) { cfs_critical("memdb_open failed - unable to open database '%s'", DBFILENAME); goto err; } // automatically import cluster.conf from host if (create && !force_local_mode) { char *cdata = NULL; gsize clen = 0; if (g_file_get_contents(HOST_CLUSTER_CONF_FN, &cdata, &clen, NULL)) { guint32 mtime = time(NULL); memdb_create(memdb, "/cluster.conf", 0, mtime); if (memdb_write(memdb, "/cluster.conf", 0, mtime, cdata, clen, 0, 1) < 0) { cfs_critical("memdb_write failed - unable to import cluster.conf"); goto err; } } } // does cluster.conf exist? gpointer conf_data = NULL; int len = memdb_read(memdb, "cluster.conf", &conf_data); if (len >= 0) { if (force_local_mode) { cfs_message("forcing local mode (althought cluster.conf exists)"); cfs_set_quorate(1, TRUE); } else { if (!(dcdb = dcdb_new(memdb))) goto err; dcdb_sync_cluster_conf(memdb, 1); } } else { cfs_debug("using local mode (cluster.conf does not exist)"); cfs_set_quorate(1, TRUE); } if (conf_data) g_free(conf_data); cfs_plug_memdb_t *config = cfs_plug_memdb_new("memdb", memdb, dcdb); cfs_plug_base_t *bplug = cfs_plug_base_new("", (cfs_plug_t *)config); create_symlinks(bplug, cfs.nodename); root_plug = (cfs_plug_t *)bplug; system("umount -f " CFSDIR " >/dev/null 2>&1"); char *fa[] = { "-f", "-odefault_permissions", "-oallow_other", NULL}; struct fuse_args fuse_args = FUSE_ARGS_INIT(sizeof (fa)/sizeof(gpointer) - 1, fa); struct fuse_chan *fuse_chan = fuse_mount(CFSDIR, &fuse_args); if (!fuse_chan) { cfs_critical("fuse_mount error: %s", strerror(errno)); goto err; } if (!(fuse = fuse_new(fuse_chan, &fuse_args, &fuse_ops, sizeof(fuse_ops), NULL))) { cfs_critical("fuse_new error: %s", strerror(errno)); goto err; } fuse_set_signal_handlers(fuse_get_session(fuse)); if (!foreground) { pid_t cpid = fork(); if (cpid == -1) { cfs_critical("failed to daemonize program - %s", strerror (errno)); goto err; } else if (cpid) { write_pidfile(cpid); qb_log_fini(); _exit (0); } else { int nullfd; chroot("/"); if ((nullfd = open("/dev/null", O_RDWR, 0)) != -1) { dup2(nullfd, 0); dup2(nullfd, 1); dup2(nullfd, 2); if (nullfd > 2) close (nullfd); } // do not print to the console after this point qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE); setsid(); } } else { write_pidfile(getpid()); } wrote_pidfile = TRUE; cfs_loop_t *corosync_loop = cfs_loop_new(fuse); cfs_service_t *service_quorum = NULL; cfs_service_t *service_confdb = NULL; cfs_service_t *service_dcdb = NULL; cfs_service_t *service_status = NULL; if (dcdb) { service_quorum = service_quorum_new(); cfs_loop_add_service(corosync_loop, service_quorum, QB_LOOP_HIGH); service_confdb = service_confdb_new(); cfs_loop_add_service(corosync_loop, service_confdb, QB_LOOP_MED); service_dcdb = service_dfsm_new(dcdb); cfs_service_set_timer(service_dcdb, DCDB_VERIFY_TIME); cfs_loop_add_service(corosync_loop, service_dcdb, QB_LOOP_MED); status_fsm = cfs_status_dfsm_new(); service_status = service_dfsm_new(status_fsm); cfs_loop_add_service(corosync_loop, service_status, QB_LOOP_LOW); } cfs_loop_start_worker(corosync_loop); server_start(memdb); ret = fuse_loop_mt(fuse); cfs_message("teardown filesystem"); server_stop(); fuse_unmount(CFSDIR, fuse_chan); fuse_destroy(fuse); cfs_debug("set stop event loop flag"); cfs_loop_stop_worker(corosync_loop); cfs_loop_destroy(corosync_loop); cfs_debug("worker finished"); if (service_dcdb) service_dfsm_destroy(service_dcdb); if (service_confdb) service_confdb_destroy(service_confdb); if (service_quorum) service_quorum_destroy(service_quorum); if (service_status) service_dfsm_destroy(service_status); sleep(1); /* do not restart too fast */ ret: if (status_fsm) dfsm_destroy(status_fsm); if (dcdb) dfsm_destroy(dcdb); if (memdb) memdb_close(memdb); if (wrote_pidfile) unlink(CFS_PID_FN); cfs_message("exit proxmox configuration filesystem (%d)", ret); cfs_status_cleanup(); qb_log_fini(); exit(ret); err: goto ret; }