diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-01-05 17:42:13 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2016-01-05 18:31:05 +0200 |
commit | 7dd7f4e2d7a5eaed676651caf456c1de0704b7cd (patch) | |
tree | 2073caae8a686914c84b5bde8bcdf59331240ea6 | |
parent | 6ac0f1e8e19922c63a1542740a7c88116989bb7c (diff) | |
download | pies-7dd7f4e2d7a5eaed676651caf456c1de0704b7cd.tar.gz pies-7dd7f4e2d7a5eaed676651caf456c1de0704b7cd.tar.bz2 |
Include argv in the output of ctl id command.
* ctl.c (res_instance): Include "argv" in the return json.
Use pies_master_argv[0] to report binary path.
* pies.c (pies_master_argv,pies_master_argc): New globals.
(main): Initialize them.
Refuse to restart unless argv[0] begins with a slash.
* pies.h (pies_master_argv,pies_master_argc): New globals.
* piesctl.c (com_id): Rewrite output formatting.
-rw-r--r-- | src/ctl.c | 22 | ||||
-rw-r--r-- | src/pies.c | 17 | ||||
-rw-r--r-- | src/pies.h | 3 | ||||
-rw-r--r-- | src/piesctl.c | 108 |
4 files changed, 125 insertions, 25 deletions
@@ -1299,6 +1299,23 @@ idfmt_pid (struct ctlio *io, char const *name, void *ptr) | |||
1299 | } | 1299 | } |
1300 | 1300 | ||
1301 | static void | 1301 | static void |
1302 | idfmt_argv (struct ctlio *io, char const *name, void *ptr) | ||
1303 | { | ||
1304 | struct json_value *ar = json_new_array (); | ||
1305 | size_t i; | ||
1306 | |||
1307 | for (i = 0; i < pies_master_argc; i++) | ||
1308 | json_array_append (ar, json_new_string (pies_master_argv[i])); | ||
1309 | json_object_set (io->output.reply, name, ar); | ||
1310 | } | ||
1311 | |||
1312 | static void | ||
1313 | idfmt_binary (struct ctlio *io, char const *name, void *ptr) | ||
1314 | { | ||
1315 | json_object_set_string (io->output.reply, name, "%s", pies_master_argv[0]); | ||
1316 | } | ||
1317 | |||
1318 | static void | ||
1302 | res_instance (struct ctlio *io, enum http_method meth, | 1319 | res_instance (struct ctlio *io, enum http_method meth, |
1303 | char const *uri, struct json_value *req) | 1320 | char const *uri, struct json_value *req) |
1304 | { | 1321 | { |
@@ -1309,11 +1326,10 @@ res_instance (struct ctlio *io, enum http_method meth, | |||
1309 | } idparam[] = { | 1326 | } idparam[] = { |
1310 | { "package", idfmt_string, PACKAGE_NAME }, | 1327 | { "package", idfmt_string, PACKAGE_NAME }, |
1311 | { "version", idfmt_string, PACKAGE_VERSION }, | 1328 | { "version", idfmt_string, PACKAGE_VERSION }, |
1312 | #if HAVE_DECL_PROGRAM_INVOCATION_NAME | 1329 | { "binary", idfmt_binary, NULL }, |
1313 | { "binary", idfmt_string_ptr, &program_invocation_name }, | ||
1314 | #endif | ||
1315 | { "PID", idfmt_pid, NULL }, | 1330 | { "PID", idfmt_pid, NULL }, |
1316 | { "instance", idfmt_string_ptr, &instance }, | 1331 | { "instance", idfmt_string_ptr, &instance }, |
1332 | { "argv", idfmt_argv, NULL }, | ||
1317 | { NULL } | 1333 | { NULL } |
1318 | }; | 1334 | }; |
1319 | struct idparam *p; | 1335 | struct idparam *p; |
@@ -2130,6 +2130,9 @@ set_state_file_names (const char *base) | |||
2130 | qotdfile = mkfilename (statedir, base, ".qotd"); | 2130 | qotdfile = mkfilename (statedir, base, ".qotd"); |
2131 | } | 2131 | } |
2132 | 2132 | ||
2133 | size_t pies_master_argc; | ||
2134 | char **pies_master_argv; | ||
2135 | |||
2133 | int | 2136 | int |
2134 | main (int argc, char **argv) | 2137 | main (int argc, char **argv) |
2135 | { | 2138 | { |
@@ -2146,6 +2149,9 @@ main (int argc, char **argv) | |||
2146 | textdomain (PACKAGE); | 2149 | textdomain (PACKAGE); |
2147 | #endif | 2150 | #endif |
2148 | mf_proctitle_init (argc, argv, environ); | 2151 | mf_proctitle_init (argc, argv, environ); |
2152 | |||
2153 | pies_master_argc = argc; | ||
2154 | pies_master_argv = argv; | ||
2149 | 2155 | ||
2150 | set_quoting_style (NULL, shell_quoting_style); | 2156 | set_quoting_style (NULL, shell_quoting_style); |
2151 | 2157 | ||
@@ -2392,8 +2398,15 @@ main (int argc, char **argv) | |||
2392 | pies_pause (); | 2398 | pies_pause (); |
2393 | switch (action) | 2399 | switch (action) |
2394 | { | 2400 | { |
2395 | case ACTION_STOP: | ||
2396 | case ACTION_RESTART: | 2401 | case ACTION_RESTART: |
2402 | if (argv[0][0] != '/' || init_process) | ||
2403 | { | ||
2404 | logmsg (LOG_INFO, _("restart command ignored")); | ||
2405 | action = ACTION_CONT; | ||
2406 | } | ||
2407 | break; | ||
2408 | |||
2409 | case ACTION_STOP: | ||
2397 | if (init_process) | 2410 | if (init_process) |
2398 | { | 2411 | { |
2399 | debug (1, ("ignoring stop/restart")); | 2412 | debug (1, ("ignoring stop/restart")); |
@@ -2436,7 +2449,7 @@ main (int argc, char **argv) | |||
2436 | progman_stop (); | 2449 | progman_stop (); |
2437 | remove_pidfile (pidfile); | 2450 | remove_pidfile (pidfile); |
2438 | 2451 | ||
2439 | if (action == ACTION_RESTART && argv[0][0] == '/') | 2452 | if (action == ACTION_RESTART) |
2440 | { | 2453 | { |
2441 | int minfd = DIAG_OUTPUT (DIAG_TO_STDERR) ? 2 : 0; | 2454 | int minfd = DIAG_OUTPUT (DIAG_TO_STDERR) ? 2 : 0; |
2442 | int i; | 2455 | int i; |
@@ -290,6 +290,9 @@ extern char *console_device; | |||
290 | extern int initdefault; | 290 | extern int initdefault; |
291 | extern int dfl_level; | 291 | extern int dfl_level; |
292 | 292 | ||
293 | extern size_t pies_master_argc; | ||
294 | extern char **pies_master_argv; | ||
295 | |||
293 | enum config_syntax | 296 | enum config_syntax |
294 | { | 297 | { |
295 | CONF_PIES, | 298 | CONF_PIES, |
diff --git a/src/piesctl.c b/src/piesctl.c index a9555a1..fcc346a 100644 --- a/src/piesctl.c +++ b/src/piesctl.c | |||
@@ -1558,33 +1558,87 @@ com_restart (struct shttp_connection *conn, int argc, char **argv) | |||
1558 | return 0; | 1558 | return 0; |
1559 | } | 1559 | } |
1560 | 1560 | ||
1561 | static void | ||
1562 | id_fmt_string (struct json_value *val) | ||
1563 | { | ||
1564 | fputs (val->v.s, stdout); | ||
1565 | } | ||
1566 | |||
1567 | static void | ||
1568 | id_fmt_pid (struct json_value *val) | ||
1569 | { | ||
1570 | printf ("%.0f", val->v.n); | ||
1571 | } | ||
1572 | |||
1573 | static void | ||
1574 | id_fmt_argv (struct json_value *val) | ||
1575 | { | ||
1576 | size_t i, n; | ||
1577 | char const *delim = NULL; | ||
1578 | |||
1579 | n = json_array_size (val); | ||
1580 | for (i = 0; i < n; i++) | ||
1581 | { | ||
1582 | struct json_value *elt; | ||
1583 | if (json_array_get (val, i, &elt) == 0 && elt->type == json_string) | ||
1584 | { | ||
1585 | if (delim) | ||
1586 | fputs (delim, stdout); | ||
1587 | fputs (elt->v.s, stdout); | ||
1588 | delim = " "; | ||
1589 | } | ||
1590 | } | ||
1591 | } | ||
1592 | |||
1593 | struct id_fmt | ||
1594 | { | ||
1595 | char *name; | ||
1596 | enum json_value_type type; | ||
1597 | void (*format) (struct json_value *); | ||
1598 | }; | ||
1599 | |||
1600 | static struct id_fmt id_fmt[] = { | ||
1601 | { "package", json_string, id_fmt_string }, | ||
1602 | { "version", json_string, id_fmt_string }, | ||
1603 | { "instance", json_string, id_fmt_string }, | ||
1604 | { "binary", json_string, id_fmt_string }, | ||
1605 | { "argv", json_arr, id_fmt_argv }, | ||
1606 | { "PID", json_number, id_fmt_pid }, | ||
1607 | { NULL } | ||
1608 | }; | ||
1609 | |||
1610 | static struct id_fmt * | ||
1611 | find_id_fmt (char const *name) | ||
1612 | { | ||
1613 | struct id_fmt *p; | ||
1614 | |||
1615 | for (p = id_fmt; p->name; p++) | ||
1616 | if (strcmp (p->name, name) == 0) | ||
1617 | return p; | ||
1618 | return NULL; | ||
1619 | } | ||
1620 | |||
1561 | static int | 1621 | static int |
1562 | com_id (struct shttp_connection *conn, int argc, char **argv) | 1622 | com_id (struct shttp_connection *conn, int argc, char **argv) |
1563 | { | 1623 | { |
1564 | struct json_value *v; | 1624 | struct json_value *v; |
1565 | 1625 | struct id_fmt *fmt; | |
1626 | |||
1566 | if (argc == 1) | 1627 | if (argc == 1) |
1567 | { | 1628 | { |
1568 | size_t i; | ||
1569 | static char *keywords[] = { | ||
1570 | "package", | ||
1571 | "version", | ||
1572 | "instance", | ||
1573 | "binary", | ||
1574 | }; | ||
1575 | |||
1576 | shttp_io_init (&conn->req); | 1629 | shttp_io_init (&conn->req); |
1577 | shttp_process (conn, METH_GET, "/instance"); | 1630 | shttp_process (conn, METH_GET, "/instance"); |
1578 | 1631 | if (!dump) | |
1579 | for (i = 0; i < sizeof (keywords)/ sizeof (keywords[0]); i++) | 1632 | for (fmt = id_fmt; fmt->name; fmt++) |
1580 | { | 1633 | { |
1581 | v = shttp_getval (conn, keywords[i], json_string); | 1634 | v = shttp_getval (conn, fmt->name, fmt->type); |
1582 | if (v) | 1635 | if (v) |
1583 | printf ("%s: %s\n", keywords[i], v->v.s); | 1636 | { |
1584 | } | 1637 | printf ("%s: ", fmt->name); |
1585 | v = shttp_getval (conn, "PID", json_number); | 1638 | fmt->format (v); |
1586 | if (v) | 1639 | putchar ('\n'); |
1587 | printf ("PID: %.0f\n", v->v.n); | 1640 | } |
1641 | } | ||
1588 | } | 1642 | } |
1589 | else | 1643 | else |
1590 | { | 1644 | { |
@@ -1597,10 +1651,24 @@ com_id (struct shttp_connection *conn, int argc, char **argv) | |||
1597 | grecs_asprintf (&buf, &size, "/instance/%s", argv[i]); | 1651 | grecs_asprintf (&buf, &size, "/instance/%s", argv[i]); |
1598 | shttp_io_init (&conn->req); | 1652 | shttp_io_init (&conn->req); |
1599 | shttp_process (conn, METH_GET, buf); | 1653 | shttp_process (conn, METH_GET, buf); |
1600 | if (json_object_get (conn->result, argv[i], &v) == 0) | 1654 | if (dump) |
1655 | continue; | ||
1656 | fmt = find_id_fmt (argv[i]); | ||
1657 | if (fmt) | ||
1658 | { | ||
1659 | v = shttp_getval (conn, fmt->name, fmt->type); | ||
1660 | if (v) | ||
1661 | { | ||
1662 | printf ("%s: ", fmt->name); | ||
1663 | fmt->format (v); | ||
1664 | putchar |