diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2008-11-21 09:46:34 +0000 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2008-11-21 09:46:34 +0000 |
commit | ab854f93828c3c2f9797e3d458ec7a6f2b0961ec (patch) | |
tree | 2b329b5abb77452a8898ff63b75e833c6a6b5027 /pies | |
parent | 39883adc914bc73fe56e24e9ec4060b84a6393fb (diff) | |
download | pies-ab854f93828c3c2f9797e3d458ec7a6f2b0961ec.tar.gz pies-ab854f93828c3c2f9797e3d458ec7a6f2b0961ec.tar.bz2 |
Improve pies.
* pies/pies.c (main): New options --force, --dump-prereq and
--dump-depmap.
Refuse to start if another instance is already running.
(pies_check_status): New function.
(pies_status): Rewrite using pies_check_status.
* pies/progman.c (component_fixup_depend): Bugfix: move
mu_list_append off the conditional.
(progman_dump_prereq, progman_dump_depmap): New functions.
* pies/meta1gram.y (yyerror): Return 0.
* pies/pies.h (progman_dump_prereq, progman_dump_depmap): New
protos.
(meta1_parser_set_debug, meta1lex, meta1error)
(meta1parse): Likewise.
Diffstat (limited to 'pies')
-rw-r--r-- | pies/meta1gram.y | 1 | ||||
-rw-r--r-- | pies/pies.c | 158 | ||||
-rw-r--r-- | pies/pies.h | 8 | ||||
-rw-r--r-- | pies/progman.c | 46 |
4 files changed, 175 insertions, 38 deletions
diff --git a/pies/meta1gram.y b/pies/meta1gram.y index ae73f63..334cb61 100644 --- a/pies/meta1gram.y +++ b/pies/meta1gram.y @@ -226,6 +226,7 @@ int yyerror (char *s) { meta1_parse_error ("%s", s); + return 0; } void diff --git a/pies/pies.c b/pies/pies.c index e70c8d5..f39f1d4 100644 --- a/pies/pies.c +++ b/pies/pies.c @@ -31,6 +31,7 @@ mode_t pies_umask = 0; unsigned long shutdown_timeout = 5; mu_acl_t pies_acl; limits_record_t pies_limits; +int force_option; /* Logging */ @@ -903,9 +904,12 @@ static const char *capa[] = { enum { OPT_FOREGROUND=256, - OPTION_LOG_TAG, + OPT_LOG_TAG, OPT_SYSLOG, - OPT_STDERR + OPT_STDERR, + OPT_DUMP_PREREQ, + OPT_DUMP_DEPMAP, + OPT_FORCE }; #define OPT_RESTART 'R' @@ -918,10 +922,12 @@ static struct argp_option options[] = { { "foreground", OPT_FOREGROUND, 0, 0, N_("Remain in foreground."), GRP+1}, { "stderr", OPT_STDERR, NULL, 0, N_("Log to stderr"), }, { "syslog", OPT_SYSLOG, NULL, 0, N_("Log to syslog"), }, - { "log-tag", OPTION_LOG_TAG, N_("STRING"), 0, + { "log-tag", OPT_LOG_TAG, N_("STRING"), 0, N_("Set the identifier used in syslog messages to STRING"), GRP+1 }, { "debug", 'x', N_("LEVEL"), 0, N_("Set debug verbosity level."), GRP+1 }, + { "force", OPT_FORCE, NULL, 0, + N_("Force startup even if another instance may be running"), GRP+1 }, #undef GRP #define GRP 10 @@ -935,6 +941,13 @@ static struct argp_option options[] = { { "stop", OPT_STOP, NULL, 0, N_("Stop the running instance."), GRP+1 }, #undef GRP + +#define GRP 20 + { "dump-prereq", OPT_DUMP_PREREQ, NULL, 0, + N_("Dump prerequisite charts."), GRP+1 }, + { "dump-depmap", OPT_DUMP_DEPMAP, NULL, 0, + N_("Dump dependency map."), GRP+1 }, +#undef GRP { NULL } }; @@ -953,10 +966,12 @@ parse_opt (int key, char *arg, struct argp_state *state) case OPT_STATUS: case OPT_STOP: case OPT_RESTART: + case OPT_DUMP_PREREQ: + case OPT_DUMP_DEPMAP: log_to_stderr = 1; command = key; break; - + case OPT_SYSLOG: log_to_stderr = 0; break; @@ -968,8 +983,12 @@ parse_opt (int key, char *arg, struct argp_state *state) case 'x': mu_argp_node_list_new (&lst, "debug", arg); break; + + case OPT_FORCE: + force_option = 1; + break; - case OPTION_LOG_TAG: + case OPT_LOG_TAG: log_tag = arg; break; @@ -1109,6 +1128,38 @@ pidfile_read (int must_exist) } +enum pies_status + { + pies_status_ctr, /* clear to run */ + pies_status_stale, + pies_status_noresp, + pies_status_running + }; + +enum pies_status +pies_check_status (pid_t *ppid) +{ + pid_t pid = pidfile_read (0); + int i; + int rc; + + if (pid == -1) + return pies_status_ctr; + + if (kill (pid, SIGUSR2)) + return pies_status_stale; + + *ppid = pid; + + for (i = 0; i < 4 && (rc = access (statfile, R_OK)); i++) + sleep (1); + + if (rc) + return pies_status_noresp; + return pies_status_running; +} + + void stop_components () { @@ -1187,53 +1238,57 @@ pies_reload () (unsigned long) pid); return kill (pid, SIGHUP) ? EX_SOFTWARE : 0; } - + int pies_status () { + pid_t pid; FILE *fp; - pid_t pid = pidfile_read (0); - int i; - if (pid == -1) + if (unlink (statfile) && errno != ENOENT) + mu_diag_output (MU_DIAG_ERR, _("cannot unlink statfile `%s': %s"), + statfile, mu_strerror (errno)); + switch (pies_check_status (&pid)) { + case pies_status_ctr: mu_diag_output (MU_DIAG_INFO, _("pies is not running")); - return EX_USAGE; - } - - if (kill (pid, SIGUSR2)) - { + break; + + case pies_status_stale: mu_diag_output (MU_DIAG_INFO, _("pies is not running, but a pidfile " "is found (pid %lu)"), (unsigned long) pid); - return EX_USAGE; - } - - mu_diag_output (MU_DIAG_INFO, - _("pies is running; PID %lu"), - (unsigned long) pid); - - for (i = 0; i < 4 && access (statfile, R_OK); i++) - sleep (1); - - fp = fopen (statfile, "r"); - if (!fp) - mu_diag_output (MU_DIAG_ERR, _("cannot open statfile `%s': %s"), - statfile, mu_strerror (errno)); - else - { - char c; + return 1; /* FIXME: hm? */ + case pies_status_noresp: + mu_diag_output (MU_DIAG_INFO, + _("pies seems to run with pid %lu, but is not responding"), + (unsigned long) pid); if (unlink (statfile)) mu_diag_output (MU_DIAG_ERR, _("cannot unlink statfile `%s': %s"), statfile, mu_strerror (errno)); - while ((c = fgetc (fp)) != EOF) - fputc (c, stdout); - fclose (fp); + return 2; + + case pies_status_running: + fp = fopen (statfile, "r"); + if (!fp) + mu_diag_output (MU_DIAG_ERR, _("cannot open statfile `%s': %s"), + statfile, mu_strerror (errno)); + else + { + char c; + + if (unlink (statfile)) + mu_diag_output (MU_DIAG_ERR, _("cannot unlink statfile `%s': %s"), + statfile, mu_strerror (errno)); + while ((c = fgetc (fp)) != EOF) + fputc (c, stdout); + fclose (fp); + } } return 0; -} +} int pies_stop () @@ -1258,11 +1313,13 @@ pies_cfg_verifier () return progman_build_depmap (); } + int main (int argc, char **argv) { int rc; int index; + pid_t pid; extern char **environ; mf_init_nls (); @@ -1290,6 +1347,7 @@ main (int argc, char **argv) exit (EX_CONFIG); } + progman_build_depmap (); switch (command) { case OPT_RESTART: @@ -1307,12 +1365,41 @@ main (int argc, char **argv) case OPT_STOP: exit (pies_stop ()); + case OPT_DUMP_PREREQ: + progman_dump_prereq (); + exit (0); + + case OPT_DUMP_DEPMAP: + progman_dump_depmap (); + exit (0); + default: mf_priv_setup (&pies_privs); if (pies_umask) umask (pies_umask); } + switch (pies_check_status (&pid)) + { + case pies_status_ctr: + break; + case pies_status_stale: + case pies_status_noresp: + if (!force_option) + { + mu_error (_("Another pies instance may be running (pid %lu), " + "use --force to override"), + (unsigned long) pid); + exit (EX_USAGE); + } + break; + + case pies_status_running: + mu_error (_("Another pies instance already running (pid %lu)"), + (unsigned long) pid); + exit (EX_USAGE); + } + mu_diag_output (MU_DIAG_INFO, _("%s starting"), program_version); if (!foreground && daemon (0, 0) == -1) @@ -1334,7 +1421,6 @@ main (int argc, char **argv) signal_setup (sig_handler); - progman_build_depmap (); progman_create_sockets (); progman_start (); diff --git a/pies/pies.h b/pies/pies.h index f1b8520..1b6f07e 100644 --- a/pies/pies.h +++ b/pies/pies.h @@ -151,6 +151,8 @@ void progman_stop (void); void progman_cleanup (int expect_term); void progman_stop_component (const char *name); void progman_dump_stats (const char *filename); +void progman_dump_prereq (void); +void progman_dump_depmap (void); int progman_accept (int socket); int progman_build_depmap (void); void progman_create_sockets (void); @@ -186,3 +188,9 @@ int create_socket (mu_url_t url, const char *user, mode_t umask); int parse_limits (limits_record_t *plrec, char *str, char **endp); int set_limits (const char *name, limits_record_t lrec); + +void meta1_parser_set_debug (void); +int meta1lex (void); +int meta1error (char *s); +int meta1parse (void); + diff --git a/pies/progman.c b/pies/progman.c index 4035950..29db153 100644 --- a/pies/progman.c +++ b/pies/progman.c @@ -898,9 +898,9 @@ component_fixup_depend (struct component *comp) mu_error (_("cannot create list: %s"), mu_strerror (rc)); continue; } - /* FIXME: memory allocation */ - mu_list_append (tgt->prereq, xstrdup (comp->tag)); } + /* FIXME: memory allocation */ + mu_list_append (tgt->prereq, xstrdup (comp->tag)); } mu_list_destroy (&comp->depend); } @@ -941,6 +941,48 @@ print_dep (struct prog *prog) mu_diag_printf (MU_DIAG_NOTICE, "%s\n", prog->tag); } +void +progman_dump_prereq () +{ + struct prog *prog; + for (prog = proghead; prog; prog = prog->next) + if (prog->prereq) + { + int i; + printf ("%s:", prog->tag); + for (i = 0; prog->prereq[i]; i++) + printf (" %s", prog->prereq[i]); + printf ("\n"); + } +} + +void +progman_dump_depmap () +{ + struct prog *prog; + unsigned i, j; + + printf ("%s:\n", _("Dependency map")); + printf (" "); + for (i = 0; i < numprog; i++) + printf (" %2d", i); + printf ("\n"); + for (i = 0; i < numprog; i++) + { + printf ("%2d ", i); + for (j = 0; j < numprog; j++) + printf (" %c ", depmap_isset (depmap, i, j) ? 'X' : ' '); + printf ("\n"); + } + printf ("\n%s:\n", _("Legend")); + for (i = 0; i < numprog; i++) + { + prog = prog_lookup_by_idx (i); + if (prog) + printf ("%2d: %s\n", i, prog->tag); + } +} + int progman_build_depmap () { |