/* Attempts to convert string 's' into a UUID in 'uuid'. Returns true if * successful, which will be the case only if 's' has the exact format * specified by RFC 4122. Returns false on failure. On failure, 'uuid' will * be set to all-zero-bits. */ bool uuid_from_string(struct uuid *uuid, const char *s) { if (!uuid_from_string_prefix(uuid, s)) { return false; } else if (s[UUID_LEN] != '\0') { uuid_zero(uuid); return false; } else { return true; } }
/* Same as uuid_from_string() but s[UUID_LEN] is not required to be a null byte * to succeed; that is, 's' need only begin with UUID syntax, not consist * entirely of it. */ bool uuid_from_string_prefix(struct uuid *uuid, const char *s) { /* 0 1 2 3 */ /* 012345678901234567890123456789012345 */ /* ------------------------------------ */ /* 00000000-1111-1111-2222-222233333333 */ bool ok; uuid->parts[0] = hexits_value(s, 8, &ok); if (!ok || s[8] != '-') { goto error; } uuid->parts[1] = hexits_value(s + 9, 4, &ok) << 16; if (!ok || s[13] != '-') { goto error; } uuid->parts[1] += hexits_value(s + 14, 4, &ok); if (!ok || s[18] != '-') { goto error; } uuid->parts[2] = hexits_value(s + 19, 4, &ok) << 16; if (!ok || s[23] != '-') { goto error; } uuid->parts[2] += hexits_value(s + 24, 4, &ok); if (!ok) { goto error; } uuid->parts[3] = hexits_value(s + 28, 8, &ok); if (!ok) { goto error; } return true; error: uuid_zero(uuid); return false; }
/** \brief Initialization of examsgd daemon. * * Command line must contain the node name and the interface name to use * for control messages. * * Accepts hidden option -d (debugging mode). * * \param[in] argc Argument count. * \param[in] argv Array of argument values. * * \return exit code. */ int daemon_init(int argc, char *argv[]) { int s; /* getopt variables */ static struct option long_opts[] = { { "cluster-id", required_argument, NULL, 'c' }, { "help", no_argument, NULL, 'h' }, { "hostname", required_argument, NULL, 'N' }, { "incarnation", required_argument, NULL, 'I' }, { "mcast-addr", required_argument, NULL, 'm' }, { "mcast-port", required_argument, NULL, 'p' }, { "node-id", required_argument, NULL, 'i' }, { "node-name", required_argument, NULL, 'n' }, { "stats", no_argument, NULL, 's' }, { NULL, 0, NULL, 0 } }; int long_idx, c; char *e; extern char *optarg; extern int optind; /* configurable options and default values */ const char *node_name = NULL; const char *hostname = NULL; const char *mgroup = EXAMSG_MCASTIP; unsigned short mport = EXAMSG_PORT; unsigned short inca = 0; exa_uuid_t cluster_uuid; exa_nodeid_t nodeid; bool err = false; uuid_zero(&cluster_uuid); nodeid = EXA_NODEID_NONE; /* options parsing */ while ((c = os_getopt_long(argc, argv, "c:dhi:I:m:n:N:p:s", long_opts, &long_idx)) != -1) switch (c) { case 'c': if (uuid_scan(optarg, &cluster_uuid) < 0) { fprintf(stderr, "invalid cluster id: '%s'\n", optarg); return -EINVAL; } break; case 'i': nodeid = (exa_nodeid_t)strtol(optarg, &e, 10); if (*e || !EXA_NODEID_VALID(nodeid)) { fprintf(stderr, "invalid node id: '%s'\n", optarg); return -EINVAL; } break; case 'I': inca = (unsigned short)strtol(optarg, &e, 10); if (*e || inca == 0) { fprintf(stderr, "invalid incarnation: '%s'\n", optarg); return -EINVAL; } break; /* multicast group */ case 'm': mgroup = optarg; break; case 'n': node_name = optarg; break; /* hostname */ case 'N': hostname = optarg; break; /* communication port */ case 'p': mport = strtol(optarg, &e, 0); if (*e != '\0') { fprintf(stderr, "invalid port number '%s'\n", optarg); return -EINVAL; } break; case 's': examsg_show_stats(); return 0; break; /* usage */ case 'h': case '?': default: usage(argv[0]); return -EINVAL; } if (uuid_is_zero(&cluster_uuid)) { fprintf(stderr, "missing cluster id\n"); err = true; } if (nodeid == EXA_NODEID_NONE) { fprintf(stderr, "missing node id\n"); err = true; } if (node_name == NULL) { fprintf(stderr, "missing node name\n"); err = true; } if (hostname == NULL) { fprintf(stderr, "missing hostname\n"); err = true; } if (inca == 0) { fprintf(stderr, "missing incarnation\n"); err = true; } if (err) return -EINVAL; /* Get cluster id, number of nodes, node id, node name and interface parameters */ if (argc - optind != 0) { fprintf(stderr, "stray parameters\n"); usage(argv[0]); return -EINVAL; } signal(SIGTERM, sig_term); signal(SIGINT, sig_term); s = examsg_static_init(EXAMSG_STATIC_GET); if (s) { fprintf(stderr, "Can't initialize messaging layer."); return s; } exalog_static_init(); /* Log as exa_msgd by default */ exalog_as(EXAMSG_CMSGD_ID); #ifdef USE_YAOURT if (yaourt_init()) exalog_debug("Yaourt: Examsgd init OK"); else exalog_warning("Yaourt: Examsgd init FAILED (%s)", yaourt_error); #endif /* set up network communication */ return startup(&cluster_uuid, node_name, hostname, nodeid, mgroup, mport, inca); }
static void __reset_token(token_t *token) { uuid_zero(&token->uuid); token->holder = EXA_NODEID_NONE; os_strlcpy(token->holder_addr, "", sizeof(token->holder_addr)); }