static admin *a_dead;
static sel_file sock;
static const char *sockname;
+static sym_table a_svcs;
static unsigned flags = 0;
static admin *a_stdin = 0;
static sig s_term, s_int, s_hup;
return (0);
}
+/*----- Services infrastructure -------------------------------------------*/
+
+/* --- @a_svcfind@ --- *
+ *
+ * Arguments: @admin *a@ = the requesting client
+ * @const char *name@ = service name wanted
+ *
+ * Returns: The service requested, or null.
+ *
+ * Use: Finds a service; reports an error if the service couldn't be
+ * found.
+ */
+
+static admin_service *a_svcfind(admin *a, const char *name)
+{
+ admin_service *svc;
+
+ if ((svc = sym_find(&a_svcs, name, -1, 0, 0)) == 0) {
+ a_fail(a, "unknown-service", "%s", name, A_END);
+ return (0);
+ }
+ return (svc);
+}
+
+/* --- @a_svcunlink@ --- *
+ *
+ * Arguments: @admin_service *svc@ = pointer to service structure
+ *
+ * Returns: ---
+ *
+ * Use: Unlinks the service from its provider, without issuing a
+ * message or freeing the structure. The version string is
+ * freed, however.
+ */
+
+static void a_svcunlink(admin_service *svc)
+{
+ if (svc->next)
+ svc->next->prev = svc->prev;
+ if (svc->prev)
+ svc->prev->next = svc->next;
+ else
+ svc->prov->svcs = svc->next;
+ xfree(svc->version);
+}
+
+/* --- @a_svcrelease@ --- *
+ *
+ * Arguments: @admin_service *svc@ = pointer to service structure
+ *
+ * Returns: ---
+ *
+ * Use: Releases a service and frees its structure.
+ */
+
+static void a_svcrelease(admin_service *svc)
+{
+ a_notify("SVCRELEASE", "%s", SYM_NAME(svc), A_END);
+ a_svcunlink(svc);
+ sym_remove(&a_svcs, svc);
+}
+
/*----- Name resolution operations ----------------------------------------*/
/* --- @a_resolved@ --- *
if (*p) {
struct servent *s = getservbyname(av[i + 1], "udp");
if (!s) {
- a_fail(a, "unknown-port", "%s", av[i + 1], A_END);
+ a_fail(a, "unknown-service", "%s", av[i + 1], A_END);
goto fail;
}
pt = ntohs(s->s_port);
static void acmd_eping(admin *a, unsigned ac, char *av[])
{ a_ping(a, ac, av, "eping", MISC_EPING); }
+/*----- Service commands --------------------------------------------------*/
+
+static void acmd_svcclaim(admin *a, unsigned ac, char *av[])
+{
+ admin_service *svc;
+ unsigned f;
+
+ svc = sym_find(&a_svcs, av[0], -1, sizeof(*svc), &f);
+ if (f) {
+ if (versioncmp(av[1], svc->version) <= 0) {
+ a_fail(a,
+ "service-exists",
+ "%s", SYM_NAME(svc),
+ "%s", svc->version,
+ A_END);
+ return;
+ }
+ a_write(svc->prov, "SVCCLAIM", 0, "%s", av[0], "%s", av[1], A_END);
+ a_svcunlink(svc);
+ }
+ svc->prov = a;
+ svc->version = xstrdup(av[1]);
+ svc->next = a->svcs;
+ svc->prev = 0;
+ if (a->svcs) a->svcs->prev = svc;
+ a->svcs = svc;
+ a_notify("SVCCLAIM", "%s", SYM_NAME(svc), "%s", svc->version, A_END);
+ a_ok(a);
+}
+
+static void acmd_svcrelease(admin *a, unsigned ac, char *av[])
+{
+ admin_service *svc;
+
+ if ((svc = a_svcfind(a, av[0])) == 0)
+ return;
+ if (svc->prov != a) {
+ a_fail(a, "not-service-provider", "%s", SYM_NAME(svc), A_END);
+ return;
+ }
+ a_svcrelease(svc);
+ a_ok(a);
+}
+
+static void acmd_svcensure(admin *a, unsigned ac, char *av[])
+{
+ admin_service *svc;
+
+ if ((svc = a_svcfind(a, av[0])) == 0)
+ return;
+ if (av[1] && versioncmp(svc->version, av[1]) < 0) {
+ a_fail(a, "service-too-old",
+ "%s", SYM_NAME(svc),
+ "%s", svc->version,
+ A_END);
+ return;
+ }
+ a_ok(a);
+}
+
+static void acmd_svcquery(admin *a, unsigned ac, char *av[])
+{
+ admin_service *svc;
+
+ if ((svc = a_svcfind(a, av[0])) != 0) {
+ a_info(a, "name=%s", SYM_NAME(svc), "version=%s", svc->version, A_END);
+ a_ok(a);
+ }
+}
+
+static void acmd_svclist(admin *a, unsigned ac, char *av[])
+{
+ admin_service *svc;
+ sym_iter i;
+
+ for (sym_mkiter(&i, &a_svcs); (svc = sym_next(&i)) != 0; )
+ a_info(a, "%s", SYM_NAME(svc), "%s", svc->version, A_END);
+ a_ok(a);
+}
+
/*----- Administration commands -------------------------------------------*/
/* --- Miscellaneous commands --- */
{ "reload", 0, 0, 0, acmd_reload },
{ "servinfo", 0, 0, 0, acmd_servinfo },
{ "setifname", "PEER NEW-NAME", 2, 2, acmd_setifname },
+ { "svcclaim", "SERVICE VERSION", 2, 2, acmd_svcclaim },
+ { "svcensure", "SERVICE [VERSION]", 1, 2, acmd_svcensure },
+ { "svclist", 0, 0, 0, acmd_svclist },
+ { "svcquery", "SERVICE", 1, 1, acmd_svcquery },
+ { "svcrelease", "SERVICE", 1, 1, acmd_svcrelease },
{ "stats", "PEER", 1, 1, acmd_stats },
#ifndef NTRACE
{ "trace", "[OPTIONS]", 0, 1, acmd_trace },
{
admin *a, *aa;
admin_bgop *bg, *bbg;
+ admin_service *svc, *ssvc;
/* --- Destroy connections marked as pending --- */
xfree(bg);
}
+ /* --- Release services I hold --- */
+
+ for (svc = a->svcs; svc; svc = ssvc) {
+ ssvc = svc->next;
+ a_svcrelease(svc);
+ }
+
/* --- Close file descriptors and selectory --- */
selbuf_destroy(&a->b);
T( trace(T_ADMIN, "admin: accepted connection %u", a->seq); )
a->bg = 0;
a->ref = 0;
+ a->svcs = 0;
a->f = f;
if (fd_in == STDIN_FILENO) a_stdin = a;
fdflags(fd_in, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
struct sigaction sa;
size_t sz;
+ /* --- Create services table --- */
+
+ sym_create(&a_svcs);
+
/* --- Set up the socket address --- */
sz = strlen(name) + 1;