service_proto.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Shared service definitions and the VFS/tty protocol used by init and the
  2. * `service` CLI/GUI. */
  3. #ifndef SERVICE_PROTO_H
  4. #define SERVICE_PROTO_H
  5. #include "syscall.h"
  6. #define SVC_MAX 8
  7. #define SVC_LOG_MAX (32u * 1024u)
  8. #define SVC_INIT_STATE "/share/init.state"
  9. #define SVC_INIT_LOG "/share/init.log"
  10. #define SVC_STATE_MAGIC "NBSVC1\n"
  11. #define SVC_INIT_MAGIC "NBINIT1\n"
  12. #define SVC_CTL_MAGIC "NBI1"
  13. #define SVC_AUTOSTART 0x1
  14. enum svc_phase {
  15. SVC_STOPPED = 0,
  16. SVC_STARTING,
  17. SVC_RUNNING,
  18. SVC_STOPPING,
  19. SVC_RESTARTING
  20. };
  21. struct svc_def {
  22. const char *name;
  23. const char *path;
  24. const char *args;
  25. const char *desc;
  26. unsigned int flags;
  27. };
  28. extern const struct svc_def svc_defs[];
  29. extern const unsigned int svc_def_count;
  30. struct svc_state {
  31. char name[16];
  32. unsigned int state;
  33. unsigned int desired; /* 0 stopped, 1 running */
  34. unsigned int pid;
  35. unsigned int tty;
  36. unsigned int started; /* uptime ms */
  37. };
  38. struct init_state {
  39. unsigned int pid;
  40. unsigned int ctl;
  41. unsigned int wm;
  42. unsigned int valid;
  43. };
  44. const char *svc_state_name(unsigned int state);
  45. int svc_state_from_name(const char *s);
  46. int svc_find(const char *name);
  47. int svc_make_state_path(char *out, unsigned int max, const char *name);
  48. int svc_make_log_path(char *out, unsigned int max, const char *name);
  49. int svc_parse_state(const char *buf, unsigned int len, struct svc_state *out);
  50. int svc_format_state(char *out, unsigned int max, const struct svc_state *st);
  51. int svc_parse_init_state(const char *buf, unsigned int len,
  52. struct init_state *out);
  53. int svc_format_init_state(char *out, unsigned int max,
  54. const struct init_state *st);
  55. /* Send one control request to init and wait for its response.
  56. * Returns 0 on a response, -1 on timeout, -2 when init is not running and
  57. * -3 when the control/response tty could not be used. */
  58. int svc_request(const char *op, const char *name, unsigned int wait_ms,
  59. int *code, int *pid, char *msg, unsigned int msgmax);
  60. #endif