diff options
Diffstat (limited to 'src/ctl.c')
-rw-r--r-- | src/ctl.c | 109 |
1 files changed, 102 insertions, 7 deletions
@@ -102,6 +102,7 @@ ctlbuf_chomp (struct ctlbuf *buf) #define CTL_END_STATE 0 #define CTL_INITIAL_STATE 0x01 #define CTL_AUTHENTICATED_STATE 0x02 +#define CTL_ACTION_STATE 0x04 #define CTL_ALL_STATES (CTL_INITIAL_STATE|CTL_AUTHENTICATED_STATE) struct ctlio; @@ -111,6 +112,11 @@ static void cmd_noop (struct ctlio *, size_t, char **); static void cmd_help (struct ctlio *, size_t, char **); static void cmd_id (struct ctlio *, size_t, char **); static void cmd_list (struct ctlio *, size_t, char **); +static void cmd_start (struct ctlio *, size_t, char **); +static void cmd_stop (struct ctlio *, size_t, char **); +static void cmd_restart (struct ctlio *, size_t, char **); +static void cmd_reboot (struct ctlio *, size_t, char **); +static void cmd_shutdown (struct ctlio *, size_t, char **); struct ctlio_command { @@ -133,6 +139,16 @@ static struct ctlio_command cmdtab[] = { CTL_ALL_STATES, 1, 1, cmd_help }, { "list", "list components", CTL_AUTHENTICATED_STATE, 1, 0, cmd_list }, + { "stop", "stop component", + CTL_AUTHENTICATED_STATE, 2, 2, cmd_stop }, + { "start", "start component", + CTL_AUTHENTICATED_STATE, 2, 2, cmd_start }, + { "restart", "restart component", + CTL_AUTHENTICATED_STATE, 2, 2, cmd_restart }, + { "reboot", "restart pies", + CTL_AUTHENTICATED_STATE, 1, 1, cmd_reboot }, + { "shutdown", "stop pies", + CTL_AUTHENTICATED_STATE, 1, 1, cmd_shutdown }, { NULL } }; @@ -152,6 +168,7 @@ ctlio_command_find (char const *verb) struct ctlio { int state; + int action; struct ctlbuf ibuf; struct ctlbuf obuf; struct wordsplit ws; @@ -165,6 +182,7 @@ ctlio_create (void) io = xmalloc (sizeof (*io)); io->state = CTL_AUTHENTICATED_STATE; //FIXME CTL_INITIAL_STATE; + io->action = ACTION_CONT; ctlbuf_init (&io->ibuf); ctlbuf_init (&io->obuf); io->ws.ws_delim = " \t()"; @@ -678,7 +696,8 @@ pcond_parse_binary (struct pcond_parser_state *state, struct pcond_node **ret) else { pcond_free (pleft); - ctlio_reply (state->io, "551", "expected 'and' or 'or', but found %s", term); + ctlio_reply (state->io, "551", "expected 'and' or 'or', but found %s", + term); return -1; } @@ -754,16 +773,16 @@ list_prog (struct prog *prog, void *data) ctlio_printf (io, "PID: %lu%s", (unsigned long) prog->pid, CRLF); else if (prog->v.p.status == status_listener && prog->v.p.comp->socket_url) - ctlio_printf (io, "URL: %10s%s", prog->v.p.comp->socket_url->string, CRLF); + ctlio_printf (io, "URL: %10s%s", prog->v.p.comp->socket_url->string, + CRLF); -#if 0 - //FIXME if (prog->v.p.status == status_sleeping) { - time_t t = prog->v.p.timestamp + SLEEPTIME; - fprintftime (fp, "%H:%M:%S", localtime (&t), 0, 0); + ctlio_printf (io, "Wakeup-Time: %lu%s", + (unsigned long) (prog->v.p.timestamp + SLEEPTIME), + CRLF); } -#endif + ctlio_printf (io, "Command:"); for (i = 0; i < prog->v.p.comp->argc; i++) ctlio_printf (io, " %s", quotearg (prog->v.p.comp->argv[i])); @@ -813,6 +832,77 @@ cmd_list (struct ctlio *io, size_t argc, char **argv) pcond_free (env.cond); } + +static void +cmd_start (struct ctlio *io, size_t argc, char **argv) +{ + char const *tag = argv[1]; + struct prog *prog = progman_locate (tag); + if (!prog) + ctlio_reply (io, "552", "Component not found"); + else if (!IS_COMPONENT (prog)) + ctlio_reply (io, "553", "Not a component"); + else if (prog->v.p.status != status_disabled) + ctlio_reply (io, "554", "Not stopped"); + else + { + prog->v.p.comp->flags &= CF_DISABLED; + prog->v.p.status = status_enabled; + kill (getpid (), SIGALRM); + ctlio_reply (io, "250", "Complete"); + } +} + +static void +cmd_stop (struct ctlio *io, size_t argc, char **argv) +{ + char const *tag = argv[1]; + struct prog *prog = progman_locate (tag); + if (!prog) + ctlio_reply (io, "552", "Component not found"); + else if (!IS_COMPONENT (prog)) + ctlio_reply (io, "553", "Not a component"); + else + { + progman_stop_component (prog); + prog->v.p.comp->flags |= CF_DISABLED; + ctlio_reply (io, "250", "Complete"); + } +} + +static void +cmd_restart (struct ctlio *io, size_t argc, char **argv) +{ + char const *tag = argv[1]; + struct prog *prog = progman_locate (tag); + if (!prog) + ctlio_reply (io, "552", "Component not found"); + else if (!(IS_COMPONENT (prog) + && !(prog->v.p.comp->mode == pies_comp_inetd + && prog->v.p.listener))) + ctlio_reply (io, "553", "Not a component"); + else + { + progman_stop_component (prog); + ctlio_reply (io, "250", "Complete"); + } +} + +static void +cmd_reboot (struct ctlio *io, size_t argc, char **argv) +{ + io->action = ACTION_RESTART; + io->state = CTL_ACTION_STATE; + ctlio_reply (io, "250", "Rebooting, closing connection"); +} + +static void +cmd_shutdown (struct ctlio *io, size_t argc, char **argv) +{ + io->action = ACTION_STOP; + io->state = CTL_ACTION_STATE; + ctlio_reply (io, "250", "Shuttign down, connection will be closed"); +} static int ctlrd (int fd, void *data); static int ctlwr (int fd, void *data); @@ -864,6 +954,11 @@ ctlwr (int fd, void *data) write (fd, &c, 1); else if (io->state == CTL_END_STATE) return ctlio_end (fd, io); + else if (io->state == CTL_ACTION_STATE) + { + pies_schedule_action (io->action); + return ctlio_end (fd, io); + } else { update_socket (fd, PIES_EVT_WR, NULL); |