| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /* Shared service table, state files and the init control-tty client. */
- #include <stdio.h>
- #include <string.h>
- #include "service_proto.h"
- #include "ucmd_lib.h"
- static unsigned int slen(const char *s)
- {
- unsigned int n = 0;
- while (s && s[n])
- n++;
- return n;
- }
- static unsigned int atou(const char *s)
- {
- unsigned int n = 0;
- while (*s >= '0' && *s <= '9')
- n = n * 10 + (unsigned int)(*s++ - '0');
- return n;
- }
- static int key_is(const char *line, const char *key)
- {
- unsigned int n = slen(key);
- return strncmp(line, key, n) == 0 && line[n] == '=';
- }
- const char *svc_state_name(unsigned int state)
- {
- switch (state) {
- case SVC_STARTING: return "starting";
- case SVC_RUNNING: return "running";
- case SVC_STOPPING: return "stopping";
- case SVC_RESTARTING: return "restarting";
- default: return "stopped";
- }
- }
- int svc_state_from_name(const char *s)
- {
- if (!s)
- return SVC_STOPPED;
- if (strcmp(s, "starting") == 0)
- return SVC_STARTING;
- if (strcmp(s, "running") == 0)
- return SVC_RUNNING;
- if (strcmp(s, "stopping") == 0)
- return SVC_STOPPING;
- if (strcmp(s, "restarting") == 0)
- return SVC_RESTARTING;
- return SVC_STOPPED;
- }
- int svc_find(const char *name)
- {
- unsigned int i;
- if (!name)
- return -1;
- for (i = 0; i < svc_def_count; i++)
- if (strcmp(svc_defs[i].name, name) == 0)
- return (int)i;
- return -1;
- }
- int svc_make_state_path(char *out, unsigned int max, const char *name)
- {
- int n = snprintf(out, max, "/share/svc-%s.state", name);
- return n > 0 && (unsigned int)n < max ? 0 : -1;
- }
- int svc_make_log_path(char *out, unsigned int max, const char *name)
- {
- int n = snprintf(out, max, "/share/svc-%s.log", name);
- return n > 0 && (unsigned int)n < max ? 0 : -1;
- }
- static void copy_str(char *dst, unsigned int max, const char *src)
- {
- unsigned int i = 0;
- for (i = 0; src && src[i] && i + 1 < max; i++)
- dst[i] = src[i];
- dst[i] = '\0';
- }
- int svc_parse_state(const char *buf, unsigned int len, struct svc_state *out)
- {
- const char *p = buf, *end = buf + len;
- unsigned int have_name = 0;
- if (!buf || !out || len < sizeof(SVC_STATE_MAGIC) - 1 ||
- strncmp(buf, SVC_STATE_MAGIC, sizeof(SVC_STATE_MAGIC) - 1) != 0)
- return -1;
- memset(out, 0, sizeof(*out));
- p += sizeof(SVC_STATE_MAGIC) - 1;
- while (p < end) {
- const char *e = p;
- unsigned int n;
- while (e < end && *e != '\n')
- e++;
- n = (unsigned int)(e - p);
- if (n) {
- if (key_is(p, "name")) {
- char tmp[16];
- unsigned int m = n - 5;
- if (m >= sizeof(tmp))
- m = sizeof(tmp) - 1;
- memcpy(tmp, p + 5, m);
- tmp[m] = '\0';
- copy_str(out->name, sizeof(out->name), tmp);
- have_name = 1;
- } else if (key_is(p, "state")) {
- char tmp[16];
- unsigned int m = n - 6;
- if (m >= sizeof(tmp))
- m = sizeof(tmp) - 1;
- memcpy(tmp, p + 6, m);
- tmp[m] = '\0';
- out->state = (unsigned int)svc_state_from_name(tmp);
- } else if (key_is(p, "desired")) {
- out->desired = (n > 8 && p[8] == 'r') ? 1u : 0u;
- } else if (key_is(p, "pid")) {
- out->pid = atou(p + 4);
- } else if (key_is(p, "tty")) {
- out->tty = atou(p + 4);
- } else if (key_is(p, "started")) {
- out->started = atou(p + 8);
- }
- }
- p = e < end ? e + 1 : end;
- }
- return have_name ? 0 : -1;
- }
- int svc_format_state(char *out, unsigned int max, const struct svc_state *st)
- {
- int n = snprintf(out, max,
- SVC_STATE_MAGIC
- "name=%s\nstate=%s\ndesired=%s\npid=%u\ntty=%u\nstarted=%u\n",
- st->name, svc_state_name(st->state),
- st->desired ? "running" : "stopped",
- st->pid, st->tty, st->started);
- return n > 0 && (unsigned int)n < max ? 0 : -1;
- }
- int svc_parse_init_state(const char *buf, unsigned int len,
- struct init_state *out)
- {
- const char *p, *end;
- if (!buf || !out || len < sizeof(SVC_INIT_MAGIC) - 1 ||
- strncmp(buf, SVC_INIT_MAGIC, sizeof(SVC_INIT_MAGIC) - 1) != 0)
- return -1;
- memset(out, 0, sizeof(*out));
- out->valid = 1;
- p = buf + sizeof(SVC_INIT_MAGIC) - 1;
- end = buf + len;
- while (p < end) {
- const char *e = p;
- while (e < end && *e != '\n')
- e++;
- if (key_is(p, "pid"))
- out->pid = atou(p + 4);
- else if (key_is(p, "ctl"))
- out->ctl = atou(p + 4);
- else if (key_is(p, "wm"))
- out->wm = atou(p + 3);
- p = e < end ? e + 1 : end;
- }
- return 0;
- }
- int svc_format_init_state(char *out, unsigned int max,
- const struct init_state *st)
- {
- int n = snprintf(out, max, SVC_INIT_MAGIC "pid=%u\nctl=%u\nwm=%u\n",
- st->pid, st->ctl, st->wm);
- return n > 0 && (unsigned int)n < max ? 0 : -1;
- }
- /* Parse "NBI1 <seq> <code> <pid> <message>" and copy the message. */
- static int parse_response(const char *s, unsigned int *seq,
- int *code, int *pid, char *msg, unsigned int msgmax)
- {
- const char *p = s;
- unsigned int v;
- if (strncmp(p, SVC_CTL_MAGIC, 4) != 0 || p[4] != ' ')
- return -1;
- p += 5;
- v = atou(p);
- while (*p >= '0' && *p <= '9')
- p++;
- if (*p != ' ')
- return -1;
- p++;
- *seq = v;
- *code = (int)atou(p);
- while (*p >= '0' && *p <= '9')
- p++;
- if (*p != ' ')
- return -1;
- p++;
- *pid = (int)atou(p);
- while (*p >= '0' && *p <= '9')
- p++;
- if (*p == ' ')
- p++;
- copy_str(msg, msgmax, p);
- return 0;
- }
- int svc_request(const char *op, const char *name, unsigned int wait_ms,
- int *code, int *pid, char *msg, unsigned int msgmax)
- {
- char buf[512];
- char req[128];
- char rsp[256];
- struct init_state is;
- unsigned int seq, used = 0, waited = 0;
- int n, ret = -1, tries;
- int resp;
- n = u_read_file(SVC_INIT_STATE, buf, sizeof(buf) - 1);
- if (n <= 0)
- return -2;
- buf[n] = '\0';
- if (svc_parse_init_state(buf, (unsigned int)n, &is) != 0 ||
- !is.valid || !is.pid || !u_alive(is.pid))
- return -2;
- resp = u_tty_alloc();
- if (resp <= 0)
- return -3;
- seq = (u_ticks_ms() * 2654435761u) ^ (unsigned int)u_getpid();
- if (!seq)
- seq = 1;
- n = snprintf(req, sizeof(req), "%s %u %s %s %d\n",
- SVC_CTL_MAGIC, seq, op, name, resp);
- if (n <= 0 || (unsigned int)n >= sizeof(req)) {
- u_tty_close(resp);
- return -3;
- }
- for (tries = 0; tries < 50; tries++) {
- if (u_tty_write(is.ctl, req, (unsigned int)n) == 0)
- break;
- u_sleep_ms(10);
- }
- if (tries == 50) {
- u_tty_close(resp);
- return -3;
- }
- rsp[0] = '\0';
- while (waited < wait_ms) {
- n = u_tty_read(resp, rsp + used,
- (unsigned int)(sizeof(rsp) - 1 - used));
- if (n < 0)
- break;
- if (n > 0) {
- used += (unsigned int)n;
- rsp[used] = '\0';
- if (strchr(rsp, '\n'))
- break;
- }
- u_sleep_ms(10);
- waited += 10;
- }
- {
- unsigned int rseq = 0;
- if (parse_response(rsp, &rseq, code, pid, msg, msgmax) == 0 &&
- rseq == seq)
- ret = 0;
- }
- u_tty_close(resp);
- return ret;
- }
|