/*----- Utility functions -------------------------------------------------*/
-/* --- @quotify@ --- *
- *
- * Arguments: @dstr *d@ = where to write the answer
- * @const char *p@ = string to quotify
- *
- * Returns: ---
- *
- * Use: Quotes the given string if necessary, according to our
- * quoting rules.
- */
-
-static void quotify(dstr *d, const char *p)
-{
- if (d->len)
- dstr_putc(d, ' ');
- if (*p && !p[strcspn(p, "\"' \t\n\v")])
- dstr_puts(d, p);
- else {
- dstr_putc(d, '\"');
- while (*p) {
- if (*p == '\\' || *p == '\"')
- dstr_putc(d, '\\');
- dstr_putc(d, *p++);
- }
- dstr_putc(d, '\"');
- }
-}
-
/* --- @a_vformat@ --- *
*
* Arguments: @dstr *d@ = where to leave the formatted message
const addr *a = va_arg(ap, const addr *);
switch (a->sa.sa_family) {
case AF_INET:
- quotify(d, "INET");
- quotify(d, inet_ntoa(a->sin.sin_addr));
+ u_quotify(d, "INET");
+ u_quotify(d, inet_ntoa(a->sin.sin_addr));
dstr_putf(d, " %u", (unsigned)ntohs(a->sin.sin_port));
break;
default:
while (d->len && d->buf[d->len - 1] == '=') d->len--;
} else if (strcmp(fmt, "?TOKENS") == 0) {
const char *const *av = va_arg(ap, const char *const *);
- while (*av) quotify(d, *av++);
+ while (*av) u_quotify(d, *av++);
} else if (strcmp(fmt, "?PEER") == 0)
- quotify(d, p_name(va_arg(ap, peer *)));
+ u_quotify(d, p_name(va_arg(ap, peer *)));
else if (strcmp(fmt, "?ERRNO") == 0) {
dstr_putf(d, " E%d", errno);
- quotify(d, strerror(errno));
+ u_quotify(d, strerror(errno));
} else
abort();
} else {
if (*fmt == '!') fmt++;
DRESET(&dd);
dstr_vputf(&dd, fmt, &ap);
- quotify(d, dd.buf);
+ u_quotify(d, dd.buf);
}
fmt = va_arg(ap, const char *);
}
const char *fmt, va_list ap)
{
dstr d = DSTR_INIT;
+
if (tag) dstr_puts(&d, "BG");
dstr_puts(&d, status);
- if (tag) quotify(&d, tag);
+ if (tag) u_quotify(&d, tag);
a_vformat(&d, fmt, ap);
dstr_putc(&d, '\n');
dosend(a, d.buf, d.len);
const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
a_vwrite(a, status, tag, fmt, ap);
va_end(ap);
static void a_info(admin *a, const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
a_vwrite(a, "INFO", 0, fmt, ap);
va_end(ap);
static void a_fail(admin *a, const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
a_vwrite(a, "FAIL", 0, fmt, ap);
va_end(ap);
dstr_destroy(&d);
}
-static void a_valert(unsigned f_and, unsigned f_eq, const char *tag,
+static void a_valert(unsigned f_and, unsigned f_eq, const char *status,
const char *fmt, va_list ap)
{
dstr d = DSTR_INIT;
if (!(flags & F_INIT))
return;
a_vformat(&d, fmt, ap);
- a_rawalert(f_and, f_eq, tag, fmt ? d.buf : 0, fmt ? d.len : 0);
+ a_rawalert(f_and, f_eq, status, fmt ? d.buf : 0, fmt ? d.len : 0);
dstr_destroy(&d);
}
-static void a_alert(unsigned f_and, unsigned f_eq, const char *tag,
+static void a_alert(unsigned f_and, unsigned f_eq, const char *status,
const char *fmt, ...)
{
va_list ap;
+
va_start(ap, fmt);
- a_valert(f_and, f_eq, tag, fmt, ap);
+ a_valert(f_and, f_eq, status, fmt, ap);
va_end(ap);
}
*/
static void a_sighup(int sig, void *v)
-{
- a_warn("SERVER", "ignore", "signal", "SIGHUP", A_END);
-}
+ { a_warn("SERVER", "ignore", "signal", "SIGHUP", A_END); }
/* --- @a_parsetime@ --- *
*
/* --- Make sure someone's not got there already --- */
- if (!*av)
+ if (!av[0] || !av[1])
goto bad_syntax;
if (p_find(*av)) {
a_fail(a, "peer-exists", "%s", *av, A_END);
a_ok(a);
}
+static void a_canceljob(admin_bgop *bg)
+{
+ admin_svcop *svc = (admin_svcop *)bg;
+
+ a_write(svc->prov, "SVCCANCEL", 0, "%s", a_jobidencode(svc), A_END);
+ a_jobdestroy(svc);
+}
+
+static void acmd_svcsubmit(admin *a, unsigned ac, char *av[])
+{
+ const char *tag = 0;
+ admin_service *svc;
+ admin_svcop *svcop;
+ const char *ver = 0;
+ dstr d = DSTR_INIT;
+
+ OPTIONS(ac, av, {
+ OPTARG("-background", arg, { tag = arg; })
+ OPTARG("-version", arg, { ver = arg; })
+ });
+ if (!*av) goto bad_syntax;
+ if ((svc = a_svcfind(a, *av)) == 0) goto fail;
+ if (ver && versioncmp(svc->version, ver) < 0) {
+ a_fail(a, "service-too-old",
+ "%s", SYM_NAME(svc),
+ "%s", svc->version,
+ A_END);
+ goto fail;
+ }
+ if ((svcop = a_jobcreate(svc->prov)) == 0) {
+ a_fail(a, "provider-overloaded", A_END);
+ goto fail;
+ }
+ if (a_bgadd(a, &svcop->bg, tag, a_canceljob)) {
+ a_jobdestroy(svcop);
+ goto fail;
+ }
+ a_write(svc->prov, "SVCJOB", 0,
+ "%s", a_jobidencode(svcop),
+ "?TOKENS", av,
+ A_END);
+ goto done;
+
+bad_syntax:
+ a_fail(a, "bad-syntax", "svcsubmit", "[OPTIONS] SERVICE ARGS...", A_END);
+fail:
+done:
+ dstr_destroy(&d);
+ return;
+}
+
+static void a_replycmd(admin *a, const char *status, int donep, char *av[])
+{
+ admin_svcop *svc;
+
+ if ((svc = a_jobiddecode(&a->j, av[0])) == 0) {
+ a_fail(a, "unknown-jobid", av[0], A_END);
+ return;
+ }
+ a_write(svc->bg.a, status, svc->bg.tag, "?TOKENS", av + 1, A_END);
+ if (donep) {
+ a_jobdestroy(svc);
+ a_bgrelease(&svc->bg);
+ }
+ a_ok(a);
+}
+
+static void acmd_svcok(admin *a, unsigned ac, char *av[])
+ { a_replycmd(a, "OK", 1, av); }
+static void acmd_svcinfo(admin *a, unsigned ac, char *av[])
+ { a_replycmd(a, "INFO", 0, av); }
+static void acmd_svcfail(admin *a, unsigned ac, char *av[])
+ { a_replycmd(a, "FAIL", 1, av); }
+
/*----- Administration commands -------------------------------------------*/
/* --- Miscellaneous commands --- */
#endif
static void acmd_watch(admin *a, unsigned ac, char *av[])
-{
- traceish(a, ac, av, "watch", w_opts, &a->f);
-}
+ { traceish(a, ac, av, "watch", w_opts, &a->f); }
static void alertcmd(admin *a, unsigned f_and, unsigned f_eq,
const char *status, char *av[])
{ alertcmd(a, AF_WARN, AF_WARN, "WARN", av); }
static void acmd_port(admin *a, unsigned ac, char *av[])
-{
- a_info(a, "%u", p_port(), A_END);
- a_ok(a);
-}
+ { a_info(a, "%u", p_port(), A_END); a_ok(a); }
static void acmd_daemon(admin *a, unsigned ac, char *av[])
{
a_notify("DAEMON", A_END);
if (a_stdin)
a_destroy(a_stdin);
- if (u_daemon())
+ if (daemonize())
a_fail(a, "daemon-error", "?ERRNO", A_END);
else {
flags |= F_DAEMON;
static void acmd_list(admin *a, unsigned ac, char *av[])
{
peer *p;
+
for (p = p_first(); p; p = p_next(p))
a_info(a, "%s", p_name(p), A_END);
a_ok(a);
a_info(a, "start-time=%s", timestr(st->t_start), A_END);
a_info(a, "last-packet-time=%s", timestr(st->t_last), A_END);
a_info(a, "last-keyexch-time=%s", timestr(st->t_kx), A_END);
- a_info(a, "packets-in=%lu bytes-in=%lu", st->n_in, st->sz_in, A_END);
- a_info(a, "packets-out=%lu bytes-out=%lu",
- st->n_out, st->sz_out, A_END);
- a_info(a, "keyexch-packets-in=%lu keyexch-bytes-in=%lu",
- st->n_kxin, st->sz_kxin, A_END);
- a_info(a, "keyexch-packets-out=%lu keyexch-bytes-out=%lu",
- st->n_kxout, st->sz_kxout, A_END);
- a_info(a, "ip-packets-in=%lu ip-bytes-in=%lu",
- st->n_ipin, st->sz_ipin, A_END);
- a_info(a, "ip-packets-out=%lu ip-bytes-out=%lu",
- st->n_ipout, st->sz_ipout, A_END);
+ a_info(a, "packets-in=%lu", st->n_in, "bytes-in=%lu", st->sz_in, A_END);
+ a_info(a,
+ "packets-out=%lu", st->n_out,
+ "bytes-out=%lu", st->sz_out,
+ A_END);
+ a_info(a,
+ "keyexch-packets-in=%lu", st->n_kxin,
+ "keyexch-bytes-in=%lu", st->sz_kxin,
+ A_END);
+ a_info(a,
+ "keyexch-packets-out=%lu", st->n_kxout,
+ "keyexch-bytes-out=%lu", st->sz_kxout,
+ A_END);
+ a_info(a,
+ "ip-packets-in=%lu", st->n_ipin,
+ "ip-bytes-in=%lu", st->sz_ipin,
+ A_END);
+ a_info(a,
+ "ip-packets-out=%lu", st->n_ipout,
+ "ip-bytes-out=%lu", st->sz_ipout,
+ A_END);
a_info(a, "rejected-packets=%lu", st->n_reject, A_END);
a_ok(a);
}
static void acmd_kill(admin *a, unsigned ac, char *av[])
{
peer *p;
+
if ((p = a_findpeer(a, av[0])) != 0) {
p_destroy(p);
a_ok(a);
static void acmd_forcekx(admin *a, unsigned ac, char *av[])
{
peer *p;
+
if ((p = a_findpeer(a, av[0])) != 0) {
kx_start(&p->kx, 1);
a_ok(a);
static void acmd_tunnels(admin *a, unsigned ac, char *av[])
{
int i;
+
for (i = 0; tunnels[i]; i++)
a_info(a, "%s", tunnels[i]->name, A_END);
a_ok(a);
{ "setifname", "PEER NEW-NAME", 2, 2, acmd_setifname },
{ "svcclaim", "SERVICE VERSION", 2, 2, acmd_svcclaim },
{ "svcensure", "SERVICE [VERSION]", 1, 2, acmd_svcensure },
+ { "svcfail", "JOBID TOKENS...", 1, 0xffff, acmd_svcfail },
+ { "svcinfo", "JOBID TOKENS...", 1, 0xffff, acmd_svcinfo },
{ "svclist", 0, 0, 0, acmd_svclist },
+ { "svcok", "JOBID", 1, 1, acmd_svcok },
{ "svcquery", "SERVICE", 1, 1, acmd_svcquery },
{ "svcrelease", "SERVICE", 1, 1, acmd_svcrelease },
+ { "svcsubmit", "[OPTIONS] SERVICE TOKENS...",
+ 2, 0xffff, acmd_svcsubmit },
{ "stats", "PEER", 1, 1, acmd_stats },
#ifndef NTRACE
{ "trace", "[OPTIONS]", 0, 1, acmd_trace },
static void acmd_help(admin *a, unsigned ac, char *av[])
{
const acmd *c;
+
for (c = acmdtab; c->name; c++) {
if (c->help)
a_info(a, "%s", c->name, "*%s", c->help, A_END);
* Use: Informs the admin module that it's a daemon.
*/
-void a_daemon(void)
-{
- flags |= F_DAEMON;
-}
+void a_daemon(void) { flags |= F_DAEMON; }
/* --- @a_init@ --- *
*