service_proto.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Shared service table, state files and the init control-tty client. */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "service_proto.h"
  5. #include "ucmd_lib.h"
  6. static unsigned int slen(const char *s)
  7. {
  8. unsigned int n = 0;
  9. while (s && s[n])
  10. n++;
  11. return n;
  12. }
  13. static unsigned int atou(const char *s)
  14. {
  15. unsigned int n = 0;
  16. while (*s >= '0' && *s <= '9')
  17. n = n * 10 + (unsigned int)(*s++ - '0');
  18. return n;
  19. }
  20. static int key_is(const char *line, const char *key)
  21. {
  22. unsigned int n = slen(key);
  23. return strncmp(line, key, n) == 0 && line[n] == '=';
  24. }
  25. const char *svc_state_name(unsigned int state)
  26. {
  27. switch (state) {
  28. case SVC_STARTING: return "starting";
  29. case SVC_RUNNING: return "running";
  30. case SVC_STOPPING: return "stopping";
  31. case SVC_RESTARTING: return "restarting";
  32. default: return "stopped";
  33. }
  34. }
  35. int svc_state_from_name(const char *s)
  36. {
  37. if (!s)
  38. return SVC_STOPPED;
  39. if (strcmp(s, "starting") == 0)
  40. return SVC_STARTING;
  41. if (strcmp(s, "running") == 0)
  42. return SVC_RUNNING;
  43. if (strcmp(s, "stopping") == 0)
  44. return SVC_STOPPING;
  45. if (strcmp(s, "restarting") == 0)
  46. return SVC_RESTARTING;
  47. return SVC_STOPPED;
  48. }
  49. int svc_find(const char *name)
  50. {
  51. unsigned int i;
  52. if (!name)
  53. return -1;
  54. for (i = 0; i < svc_def_count; i++)
  55. if (strcmp(svc_defs[i].name, name) == 0)
  56. return (int)i;
  57. return -1;
  58. }
  59. int svc_make_state_path(char *out, unsigned int max, const char *name)
  60. {
  61. int n = snprintf(out, max, "/share/svc-%s.state", name);
  62. return n > 0 && (unsigned int)n < max ? 0 : -1;
  63. }
  64. int svc_make_log_path(char *out, unsigned int max, const char *name)
  65. {
  66. int n = snprintf(out, max, "/share/svc-%s.log", name);
  67. return n > 0 && (unsigned int)n < max ? 0 : -1;
  68. }
  69. static void copy_str(char *dst, unsigned int max, const char *src)
  70. {
  71. unsigned int i = 0;
  72. for (i = 0; src && src[i] && i + 1 < max; i++)
  73. dst[i] = src[i];
  74. dst[i] = '\0';
  75. }
  76. int svc_parse_state(const char *buf, unsigned int len, struct svc_state *out)
  77. {
  78. const char *p = buf, *end = buf + len;
  79. unsigned int have_name = 0;
  80. if (!buf || !out || len < sizeof(SVC_STATE_MAGIC) - 1 ||
  81. strncmp(buf, SVC_STATE_MAGIC, sizeof(SVC_STATE_MAGIC) - 1) != 0)
  82. return -1;
  83. memset(out, 0, sizeof(*out));
  84. p += sizeof(SVC_STATE_MAGIC) - 1;
  85. while (p < end) {
  86. const char *e = p;
  87. unsigned int n;
  88. while (e < end && *e != '\n')
  89. e++;
  90. n = (unsigned int)(e - p);
  91. if (n) {
  92. if (key_is(p, "name")) {
  93. char tmp[16];
  94. unsigned int m = n - 5;
  95. if (m >= sizeof(tmp))
  96. m = sizeof(tmp) - 1;
  97. memcpy(tmp, p + 5, m);
  98. tmp[m] = '\0';
  99. copy_str(out->name, sizeof(out->name), tmp);
  100. have_name = 1;
  101. } else if (key_is(p, "state")) {
  102. char tmp[16];
  103. unsigned int m = n - 6;
  104. if (m >= sizeof(tmp))
  105. m = sizeof(tmp) - 1;
  106. memcpy(tmp, p + 6, m);
  107. tmp[m] = '\0';
  108. out->state = (unsigned int)svc_state_from_name(tmp);
  109. } else if (key_is(p, "desired")) {
  110. out->desired = (n > 8 && p[8] == 'r') ? 1u : 0u;
  111. } else if (key_is(p, "pid")) {
  112. out->pid = atou(p + 4);
  113. } else if (key_is(p, "tty")) {
  114. out->tty = atou(p + 4);
  115. } else if (key_is(p, "started")) {
  116. out->started = atou(p + 8);
  117. }
  118. }
  119. p = e < end ? e + 1 : end;
  120. }
  121. return have_name ? 0 : -1;
  122. }
  123. int svc_format_state(char *out, unsigned int max, const struct svc_state *st)
  124. {
  125. int n = snprintf(out, max,
  126. SVC_STATE_MAGIC
  127. "name=%s\nstate=%s\ndesired=%s\npid=%u\ntty=%u\nstarted=%u\n",
  128. st->name, svc_state_name(st->state),
  129. st->desired ? "running" : "stopped",
  130. st->pid, st->tty, st->started);
  131. return n > 0 && (unsigned int)n < max ? 0 : -1;
  132. }
  133. int svc_parse_init_state(const char *buf, unsigned int len,
  134. struct init_state *out)
  135. {
  136. const char *p, *end;
  137. if (!buf || !out || len < sizeof(SVC_INIT_MAGIC) - 1 ||
  138. strncmp(buf, SVC_INIT_MAGIC, sizeof(SVC_INIT_MAGIC) - 1) != 0)
  139. return -1;
  140. memset(out, 0, sizeof(*out));
  141. out->valid = 1;
  142. p = buf + sizeof(SVC_INIT_MAGIC) - 1;
  143. end = buf + len;
  144. while (p < end) {
  145. const char *e = p;
  146. while (e < end && *e != '\n')
  147. e++;
  148. if (key_is(p, "pid"))
  149. out->pid = atou(p + 4);
  150. else if (key_is(p, "ctl"))
  151. out->ctl = atou(p + 4);
  152. else if (key_is(p, "wm"))
  153. out->wm = atou(p + 3);
  154. p = e < end ? e + 1 : end;
  155. }
  156. return 0;
  157. }
  158. int svc_format_init_state(char *out, unsigned int max,
  159. const struct init_state *st)
  160. {
  161. int n = snprintf(out, max, SVC_INIT_MAGIC "pid=%u\nctl=%u\nwm=%u\n",
  162. st->pid, st->ctl, st->wm);
  163. return n > 0 && (unsigned int)n < max ? 0 : -1;
  164. }
  165. /* Parse "NBI1 <seq> <code> <pid> <message>" and copy the message. */
  166. static int parse_response(const char *s, unsigned int *seq,
  167. int *code, int *pid, char *msg, unsigned int msgmax)
  168. {
  169. const char *p = s;
  170. unsigned int v;
  171. if (strncmp(p, SVC_CTL_MAGIC, 4) != 0 || p[4] != ' ')
  172. return -1;
  173. p += 5;
  174. v = atou(p);
  175. while (*p >= '0' && *p <= '9')
  176. p++;
  177. if (*p != ' ')
  178. return -1;
  179. p++;
  180. *seq = v;
  181. *code = (int)atou(p);
  182. while (*p >= '0' && *p <= '9')
  183. p++;
  184. if (*p != ' ')
  185. return -1;
  186. p++;
  187. *pid = (int)atou(p);
  188. while (*p >= '0' && *p <= '9')
  189. p++;
  190. if (*p == ' ')
  191. p++;
  192. copy_str(msg, msgmax, p);
  193. return 0;
  194. }
  195. int svc_request(const char *op, const char *name, unsigned int wait_ms,
  196. int *code, int *pid, char *msg, unsigned int msgmax)
  197. {
  198. char buf[512];
  199. char req[128];
  200. char rsp[256];
  201. struct init_state is;
  202. unsigned int seq, used = 0, waited = 0;
  203. int n, ret = -1, tries;
  204. int resp;
  205. n = u_read_file(SVC_INIT_STATE, buf, sizeof(buf) - 1);
  206. if (n <= 0)
  207. return -2;
  208. buf[n] = '\0';
  209. if (svc_parse_init_state(buf, (unsigned int)n, &is) != 0 ||
  210. !is.valid || !is.pid || !u_alive(is.pid))
  211. return -2;
  212. resp = u_tty_alloc();
  213. if (resp <= 0)
  214. return -3;
  215. seq = (u_ticks_ms() * 2654435761u) ^ (unsigned int)u_getpid();
  216. if (!seq)
  217. seq = 1;
  218. n = snprintf(req, sizeof(req), "%s %u %s %s %d\n",
  219. SVC_CTL_MAGIC, seq, op, name, resp);
  220. if (n <= 0 || (unsigned int)n >= sizeof(req)) {
  221. u_tty_close(resp);
  222. return -3;
  223. }
  224. for (tries = 0; tries < 50; tries++) {
  225. if (u_tty_write(is.ctl, req, (unsigned int)n) == 0)
  226. break;
  227. u_sleep_ms(10);
  228. }
  229. if (tries == 50) {
  230. u_tty_close(resp);
  231. return -3;
  232. }
  233. rsp[0] = '\0';
  234. while (waited < wait_ms) {
  235. n = u_tty_read(resp, rsp + used,
  236. (unsigned int)(sizeof(rsp) - 1 - used));
  237. if (n < 0)
  238. break;
  239. if (n > 0) {
  240. used += (unsigned int)n;
  241. rsp[used] = '\0';
  242. if (strchr(rsp, '\n'))
  243. break;
  244. }
  245. u_sleep_ms(10);
  246. waited += 10;
  247. }
  248. {
  249. unsigned int rseq = 0;
  250. if (parse_response(rsp, &rseq, code, pid, msg, msgmax) == 0 &&
  251. rseq == seq)
  252. ret = 0;
  253. }
  254. u_tty_close(resp);
  255. return ret;
  256. }