diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2016-01-02 11:18:28 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2016-01-02 11:20:42 +0200 |
commit | 061afaf6385340f87bbeaa6d7e8ff6befa532551 (patch) | |
tree | 7d42952187b6c035caba9c74cfb6cb8dfd651354 | |
parent | 00e6c3c3ed06a258a02943fc49fa7c528025d747 (diff) | |
download | pies-061afaf6385340f87bbeaa6d7e8ff6befa532551.tar.gz pies-061afaf6385340f87bbeaa6d7e8ff6befa532551.tar.bz2 |
piesctl: Implement all basic commands, except "restart"
-rw-r--r-- | src/ctl.c | 4 | ||||
-rw-r--r-- | src/piesctl-cl.opt | 8 | ||||
-rw-r--r-- | src/piesctl.c | 338 |
3 files changed, 336 insertions, 14 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* This file is part of GNU Pies. | 1 | /* This file is part of GNU Pies. |
2 | Copyright (C) 2007-2013 Sergey Poznyakoff | 2 | Copyright (C) 2007-2016 Sergey Poznyakoff |
3 | 3 | ||
4 | GNU Pies is free software; you can redistribute it and/or modify | 4 | GNU Pies is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by | 5 | it under the terms of the GNU General Public License as published by |
@@ -1446,7 +1446,7 @@ res_programs (struct ctlio *io, enum http_method meth, | |||
1446 | ctlio_reply (io, 409, "already running"); | 1446 | ctlio_reply (io, 409, "already running"); |
1447 | else | 1447 | else |
1448 | { | 1448 | { |
1449 | prog->v.p.comp->flags &= CF_DISABLED; | 1449 | prog->v.p.comp->flags &= ~CF_DISABLED; |
1450 | prog->v.p.status = status_enabled; | 1450 | prog->v.p.status = status_enabled; |
1451 | kill (getpid (), SIGALRM); | 1451 | kill (getpid (), SIGALRM); |
1452 | ctlio_reply (io, 200, "Component started"); | 1452 | ctlio_reply (io, 200, "Component started"); |
diff --git a/src/piesctl-cl.opt b/src/piesctl-cl.opt index 04ff051..a83616e 100644 --- a/src/piesctl-cl.opt +++ b/src/piesctl-cl.opt | |||
@@ -1,5 +1,5 @@ | |||
1 | /* This file is part of GNU Pies. -*- c -*- | 1 | /* This file is part of GNU Pies. -*- c -*- |
2 | Copyright (C) 2008-2014 Sergey Poznyakoff | 2 | Copyright (C) 2008-2016 Sergey Poznyakoff |
3 | 3 | ||
4 | GNU Pies is free software; you can redistribute it and/or modify | 4 | GNU Pies is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by | 5 | it under the terms of the GNU General Public License as published by |
@@ -52,6 +52,12 @@ BEGIN | |||
52 | ++verbose; | 52 | ++verbose; |
53 | END | 53 | END |
54 | 54 | ||
55 | OPTION(dump,d,, | ||
56 | [<dump obtained responsed verbatim>]) | ||
57 | BEGIN | ||
58 | ++dump; | ||
59 | END | ||
60 | |||
55 | OPTION(url,u,URL, | 61 | OPTION(url,u,URL, |
56 | [<connect to this socket>]) | 62 | [<connect to this socket>]) |
57 | BEGIN | 63 | BEGIN |
diff --git a/src/piesctl.c b/src/piesctl.c index 0618caa..9c5ef8b 100644 --- a/src/piesctl.c +++ b/src/piesctl.c | |||
@@ -1,5 +1,5 @@ | |||
1 | /* This file is part of GNU Pies. | 1 | /* This file is part of GNU Pies. |
2 | Copyright (C) 2015 Sergey Poznyakoff | 2 | Copyright (C) 2015, 2016 Sergey Poznyakoff |
3 | 3 | ||
4 | GNU Pies is free software; you can redistribute it and/or modify | 4 | GNU Pies is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by | 5 | it under the terms of the GNU General Public License as published by |
@@ -35,6 +35,7 @@ | |||
35 | #include "progname.h" | 35 | #include "progname.h" |
36 | #include "libpies.h" | 36 | #include "libpies.h" |
37 | #include "grecsasrt.h" | 37 | #include "grecsasrt.h" |
38 | #include "fprintftime.h" | ||
38 | 39 | ||
39 | struct pies_url *default_url; /* Control socket URL */ | 40 | struct pies_url *default_url; /* Control socket URL */ |
40 | struct pies_url *url; | 41 | struct pies_url *url; |
@@ -43,6 +44,7 @@ char *config_file; | |||
43 | char default_config_file[] = SYSCONFDIR "/piesctl.conf"; | 44 | char default_config_file[] = SYSCONFDIR "/piesctl.conf"; |
44 | int preprocess_only; | 45 | int preprocess_only; |
45 | int verbose; | 46 | int verbose; |
47 | int dump; | ||
46 | 48 | ||
47 | static void config_help (void); | 49 | static void config_help (void); |
48 | 50 | ||
@@ -794,21 +796,29 @@ shttp_get_reply (struct shttp_connection *conn) | |||
794 | } | 796 | } |
795 | 797 | ||
796 | static void | 798 | static void |
797 | stderr_writer (void *closure, char const *text, size_t len) | 799 | fp_writer (void *closure, char const *text, size_t len) |
798 | { | 800 | { |
799 | fwrite (text, len, 1, stderr); | 801 | fwrite (text, len, 1, (FILE*) closure); |
800 | } | 802 | } |
801 | 803 | ||
802 | static void | 804 | static void |
803 | shttp_format_result (struct shttp_connection *conn) | 805 | print_json (FILE *fp, struct json_value *v) |
804 | { | 806 | { |
805 | struct json_format fmt = { | 807 | struct json_format fmt = { |
806 | .indent = 2, | 808 | .indent = 2, |
807 | .precision = 0, | 809 | .precision = 0, |
808 | .write = stderr_writer, | 810 | .write = fp_writer, |
811 | .data = fp | ||
809 | }; | 812 | }; |
813 | json_format_value (v, &fmt); | ||
814 | fputc ('\n', fp); | ||
815 | } | ||
816 | |||
817 | static void | ||
818 | shttp_format_result (struct shttp_connection *conn, FILE *fp) | ||
819 | { | ||
810 | fprintf (stderr, "%s: raw JSON reply follows:\n", program_name); | 820 | fprintf (stderr, "%s: raw JSON reply follows:\n", program_name); |
811 | json_format_value (conn->result, &fmt); | 821 | print_json (fp, conn->result); |
812 | } | 822 | } |
813 | 823 | ||
814 | static void | 824 | static void |
@@ -821,7 +831,7 @@ shttp_fatal (struct shttp_connection *conn) | |||
821 | if (jv->type == json_string) | 831 | if (jv->type == json_string) |
822 | grecs_error (NULL, 0, "%s", jv->v.s); | 832 | grecs_error (NULL, 0, "%s", jv->v.s); |
823 | else | 833 | else |
824 | shttp_format_result (conn); | 834 | shttp_format_result (conn, stderr); |
825 | } | 835 | } |
826 | else | 836 | else |
827 | grecs_error (NULL, 0, "%s", conn->status_line[2]); | 837 | grecs_error (NULL, 0, "%s", conn->status_line[2]); |
@@ -927,6 +937,8 @@ shttp_process (struct shttp_connection *conn, int method, char const *uri) | |||
927 | { | 937 | { |
928 | shttp_request_send (conn, method, uri); | 938 | shttp_request_send (conn, method, uri); |
929 | shttp_get_reply (conn); | 939 | shttp_get_reply (conn); |
940 | if (dump && conn->result) | ||
941 | shttp_format_result (conn, stdout); | ||
930 | if (conn->resp.code / 100 == 2) | 942 | if (conn->resp.code / 100 == 2) |
931 | return; | 943 | return; |
932 | if (conn->resp.code == 401 && isatty (fileno (stdin))) | 944 | if (conn->resp.code == 401 && isatty (fileno (stdin))) |
@@ -936,24 +948,274 @@ shttp_process (struct shttp_connection *conn, int method, char const *uri) | |||
936 | } | 948 | } |
937 | } | 949 | } |
938 | 950 | ||
951 | static struct json_value * | ||
952 | shttp_getval (struct shttp_connection *conn, char const *name, | ||
953 | enum json_value_type type) | ||
954 | { | ||
955 | struct json_value *p = NULL; | ||
956 | |||
957 | switch (json_object_get (conn->result, name, &p)) | ||
958 | { | ||
959 | case 0: | ||
960 | if (p->type != type) | ||
961 | { | ||
962 | grecs_error (NULL, 0, _("\"%s\" has wrong type"), name); | ||
963 | p = NULL; | ||
964 | } | ||
965 | break; | ||
966 | |||
967 | case 1: | ||
968 | grecs_error (NULL, 0, _("no \"%s\" member"), name); | ||
969 | break; | ||
970 | |||
971 | default: | ||
972 | grecs_error (NULL, errno, _("can't get value of \"%s\""), name); | ||
973 | } | ||
974 | return p; | ||
975 | } | ||
976 | |||
977 | struct kwtrans | ||
978 | { | ||
979 | char const *name; | ||
980 | int c; | ||
981 | }; | ||
982 | |||
983 | struct kwtrans mode_trans[] = { | ||
984 | { "exec", 'C' }, | ||
985 | { "accept", 'A' }, | ||
986 | { "inetd", 'I' }, | ||
987 | { "pass_fd", 'P' }, | ||
988 | { "wait", 'W' }, | ||
989 | { "once", 'c' }, | ||
990 | { "boot", 'B' }, | ||
991 | { "bootwait", 'w' }, | ||
992 | { "powerfail", 'F' }, | ||
993 | { "powerwait", 'f' }, | ||
994 | { "powerokwait", 'o' }, | ||
995 | { "ctrlaltdel", '3' }, | ||
996 | { "ondemand", 'D' }, | ||
997 | { "sysinit", 'i' }, | ||
998 | { "powerfailnow", 'n' }, | ||
999 | { "kbrequest", 'k' }, | ||
1000 | { NULL } | ||
1001 | }; | ||
1002 | |||
1003 | struct kwtrans status_trans[] = { | ||
1004 | { "enabled", 'R' }, | ||
1005 | { "disabled", 'D' }, | ||
1006 | { "listener", 'L' }, | ||
1007 | { "sleeping", 's' }, | ||
1008 | { "stopping", 'S' }, | ||
1009 | { "finished", 'f' }, | ||
1010 | { NULL } | ||
1011 | }; | ||
1012 | |||
1013 | static int | ||
1014 | kwtoc (char const *s, struct kwtrans const *trans) | ||
1015 | { | ||
1016 | for (; trans->name; trans++) | ||
1017 | { | ||
1018 | if (strcmp (trans->name, s) == 0) | ||
1019 | return trans->c; | ||
1020 | } | ||
1021 | return '-'; | ||
1022 | } | ||
1023 | |||
1024 | static struct json_value * | ||
1025 | getval (struct json_value *v, char const *name, enum json_value_type type, | ||
1026 | int optional) | ||
1027 | { | ||
1028 | struct json_value *p = NULL; | ||
1029 | |||
1030 | switch (json_object_get (v, name, &p)) | ||
1031 | { | ||
1032 | case 0: | ||
1033 | if (p->type != type) | ||
1034 | { | ||
1035 | grecs_error (NULL, 0, _("\"%s\" has wrong type"), name); | ||
1036 | p = NULL; | ||
1037 | } | ||
1038 | break; | ||
1039 | |||
1040 | case 1: | ||
1041 | if (!optional) | ||
1042 | grecs_error (NULL, 0, _("no \"%s\" member"), name); | ||
1043 | break; | ||
1044 | |||
1045 | default: | ||
1046 | grecs_error (NULL, errno, _("can't get value of \"%s\""), name); | ||
1047 | } | ||
1048 |