diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2016-02-21 12:40:53 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2016-02-21 12:40:53 +0200 |
commit | 6aac230ce3d21f1e71333ec3863c7ea1afd051c3 (patch) | |
tree | bddb95396d1eb12401db23946119fdac79becf43 /src | |
parent | da6e295140fc1a1ef56db1c6f792e86793f7f2b1 (diff) | |
download | pies-6aac230ce3d21f1e71333ec3863c7ea1afd051c3.tar.gz pies-6aac230ce3d21f1e71333ec3863c7ea1afd051c3.tar.bz2 |
Provide fall-back entry in init mode
* src/sysvinit.c (inittab_parse): Provide default entry if
inittab cannot be read or if it defined no components.
* src/comp.c (component_list_is_empty): New function.
* src/pies.c (pies_read_config): Always return 0 in init mode.
(main): Use LOG_DAEMON in init mode.
* src/pies.h (component_list_is_empty): New proto.
Diffstat (limited to 'src')
-rw-r--r-- | src/comp.c | 6 | ||||
-rw-r--r-- | src/pies.c | 7 | ||||
-rw-r--r-- | src/pies.h | 2 | ||||
-rw-r--r-- | src/sysvinit.c | 131 |
4 files changed, 100 insertions, 46 deletions
@@ -100,6 +100,12 @@ component_unlink (struct component *comp) list->tail = comp->prev; } +int +component_list_is_empty (void) +{ + return !comp_list[cur].head; +} + struct component * component_lookup_tag (int idx, const char *tag) { @@ -1569,6 +1569,7 @@ pies_read_config (void) struct grecs_list_entry *ep; int err = 0; + logmsg (LOG_INFO, _("reading configuration")); component_config_begin (); for (ep = config_list->head; ep; ep = ep->next) @@ -1578,6 +1579,9 @@ pies_read_config (void) ++err; } + if (init_process) + err = 0; + if (err) component_config_rollback (); @@ -2008,7 +2012,10 @@ main (int argc, char **argv) /* Set default logging */ if (init_process) + { + log_facility = LOG_DAEMON; diag_flags = DIAG_TO_STDERR | DIAG_REOPEN_LOG; + } else diag_flags = DIAG_TO_SYSLOG | (stderr_closed_p () ? 0 : DIAG_TO_STDERR); @@ -396,6 +396,8 @@ void component_free (struct component *comp); void component_ref_incr (struct component *comp); void component_ref_decr (struct component *comp); +int component_list_is_empty (void); + void component_config_begin (void); void component_config_rollback (void); void component_config_commit (void); diff --git a/src/sysvinit.c b/src/sysvinit.c index 41d5b9d..695a22a 100644 --- a/src/sysvinit.c +++ b/src/sysvinit.c @@ -60,7 +60,8 @@ int prevlevel = 'N'; int initdefault; /* Default runlevel */ int dfl_level; -int emergency_shell; +char *emergency_shell = "/sbin/sulogin"; +int emergency; int @@ -442,8 +443,7 @@ sysvinit_set_runlevel (int newlevel) default: if (newlevel == runlevel) - { - } + break; else if (runlevel_index (newlevel) == -1) return -1; else @@ -757,8 +757,8 @@ sysvinit_begin () sysvinit_runlevel_setup (PIES_COMP_DEFAULT); add_extra_sigv (sigv, ARRAY_SIZE (sigv)); sysvinit_sysdep_begin (); - if (emergency_shell) - start_shell ("/sbin/sulogin"); + if (emergency) + start_shell (emergency_shell); } #define IS_RUNNING_DISABLED_PROG(prog) \ @@ -954,45 +954,43 @@ strupr (char *s) return s; } -int -inittab_parse (const char *file) +struct inittab_ctx { - FILE *fp; - size_t size = 0; - char *buf = NULL; - unsigned line_no = 0; - int err = 0; + char *buf; + size_t size; + char const *file; + unsigned line_no; +}; - fp = fopen (file, "r"); - if (!fp) +enum inittab_status { - logmsg (LOG_ERR, - _("cannot open configuration file %s: %s"), - file, strerror (errno)); - return 1; - } + inittab_ok, + inittab_err, + inittab_stop + }; - while (getline (&buf, &size, fp) >= 0) +static enum inittab_status +inittab_parse_line (struct inittab_ctx *ctx) { char *id, *runlevels, *action, *process, *p; struct action_parser *ap; struct component *comp; struct wordsplit ws; - line_no++; - for (p = buf; *p && c_isblank (*p); p++) + ctx->line_no++; + for (p = ctx->buf; *p && c_isblank (*p); p++) ; if (!p || *p == '\n') - continue; + return inittab_ok; if (*p == '#') { if (wordsplit (p+1, &ws, WRDSF_DEFFLAGS)) { - logmsg (LOG_ERR, "%s:%u: wordsplit: %s", file, line_no, + logmsg (LOG_ERR, "%s:%u: wordsplit: %s", ctx->file, ctx->line_no, strerror (errno)); - err = 1; + return inittab_err; } /* pies pragma debug N */ /* pies pragma next FORMAT FILE */ @@ -1014,7 +1012,7 @@ inittab_parse (const char *file) if (!synt) logmsg (LOG_ERR, "%s:%u: %s", - file, line_no, _("unknown syntax type")); + ctx->file, ctx->line_no, _("unknown syntax type")); else config_file_add (synt, ws.ws_wordv[4]); } @@ -1024,12 +1022,12 @@ inittab_parse (const char *file) else if (strcmp (ws.ws_wordv[2], "stop") == 0) { wordsplit_free (&ws); - break; + return inittab_stop; } } wordsplit_free (&ws); - continue; + return inittab_ok; } id = getfld (p, &p); @@ -1039,9 +1037,9 @@ inittab_parse (const char *file) if (!id || !runlevels || !action || !process) { - logmsg (LOG_ERR, "%s:%u: %s", file, line_no, _("not enough fields")); - err = 1; - continue; + logmsg (LOG_ERR, "%s:%u: %s", + ctx->file, ctx->line_no, _("not enough fields")); + return inittab_err; } if (strcmp (action, "initdefault") == 0) @@ -1049,24 +1047,24 @@ inittab_parse (const char *file) if (!runlevels[0] || !is_valid_runlevel (runlevels[0])) { logmsg (LOG_ERR, "%s:%u: %s", - file, line_no, _("invalid runlevel")); - err = 1; + ctx->file, ctx->line_no, _("invalid runlevel")); + return inittab_err; } else initdefault = toupper (runlevels[0]); - continue; + return inittab_ok; } if (strcmp (action, "off") == 0) /* Ignore the entry */ - continue; + return inittab_ok; ap = find_action_parser (action); if (!ap) { - logmsg (LOG_ERR, "%s:%u: %s", file, line_no, _("unknown action")); - err = 1; - continue; + logmsg (LOG_ERR, "%s:%u: %s", ctx->file, ctx->line_no, + _("unknown action")); + return inittab_err; } comp = component_create (id); @@ -1076,10 +1074,9 @@ inittab_parse (const char *file) if (wordsplit (process, &ws, WRDSF_DEFFLAGS)) { component_free (comp); - logmsg (LOG_ERR, "%s:%u: wordsplit: %s", file, line_no, + logmsg (LOG_ERR, "%s:%u: wordsplit: %s", ctx->file, ctx->line_no, strerror (errno)); - err = 1; - continue; + return inittab_err; } wordsplit_getwords (&ws, &comp->argc, &comp->argv); comp->program = grecs_strdup (comp->argv[0]); @@ -1087,16 +1084,58 @@ inittab_parse (const char *file) comp->flags |= CF_SIGGROUP; - if (ap->parser && ap->parser (comp, file, line_no)) + if (ap->parser && ap->parser (comp, ctx->file, ctx->line_no)) { component_free (comp); - err = 1; - continue; + return inittab_err; } + return inittab_ok; } - free (buf); +int +inittab_parse (const char *file) +{ + FILE *fp; + struct inittab_ctx ctx; + int err; + + ctx.size = 0; + ctx.buf = NULL; + ctx.file = file; + ctx.line_no = 0; + + fp = fopen (file, "r"); + if (fp) + { + while (getline (&ctx.buf, &ctx.size, fp) >= 0) + { + enum inittab_status st = inittab_parse_line (&ctx); + if (st == inittab_err) + err = 1; + else if (st == inittab_stop) + break; + } fclose (fp); + } + else + { + logmsg (LOG_ERR, + _("cannot open configuration file %s: %s"), + file, strerror (errno)); + err = 1; + } + + if (component_list_is_empty ()) + { + /* Provide default inittab entry */ + ctx.file = __FILE__; + ctx.line_no = __LINE__; + grecs_asprintf (&ctx.buf, &ctx.size, "~~:%s:wait:%s\n", + valid_runlevels, emergency_shell); + inittab_parse_line (&ctx); + } + + free (ctx.buf); return err; } @@ -1182,7 +1221,7 @@ sysvinit_parse_argv (int argc, char **argv) if (!strcmp (arg, "single") || !strcmp (arg, "-s")) dfl_level = 'S'; else if (!strcmp (arg, "-b") || !strcmp (arg, "emergency")) - emergency_shell = 1; + emergency = 1; else if (!arg[1] && strchr (valid_runlevels, (c = toupper (arg[0])))) dfl_level = c; } |