summaryrefslogtreecommitdiffabout
path: root/src
authorSergey Poznyakoff <gray@gnu.org>2016-01-31 11:43:18 (GMT)
committer Sergey Poznyakoff <gray@gnu.org>2016-01-31 11:43:18 (GMT)
commit6f9f2fd7a6952b544dccbf0bdc7f9c312f602afe (patch) (side-by-side diff)
tree8bf0b45bb6578dee844dc92c1a90a93474f0fa7a /src
parent7f20aa4f7e26d8f740b55bef98f0c3c78eca0e79 (diff)
downloadpies-6f9f2fd7a6952b544dccbf0bdc7f9c312f602afe.tar.gz
pies-6f9f2fd7a6952b544dccbf0bdc7f9c312f602afe.tar.bz2
Fix runlevel switching; handle powerfail commands.
* src/ctl.c (prog_serialize): List runlevels. * src/pies.c (main): Set got_alarm after reloading configuration. Handle ACTION_POWER. * src/pies.h (ACTION_POWER): New constant. (progman_running_p): Rename to progman_waiting_p. All uses changed. (sysvinit_power): New proto. (POWER_STAT_FILE): New define. (POWER_STAT_FAIL,POWER_STAT_LOW,POWER_STAT_OK): New constants. * src/prog.h (prog) <idx>: Remove. (prog) <wait>: New member. * src/progman.c (prog_lookup_by_idx): Remove. (progman_waiting_p): Return 1 only if there is at least one prog with v.p.wait set. (prog_start): Initialize v.p.wait for sysvinit components. (progman_start): Don't do anything if waiting for components from the previous runlevel. (progman_wake_sleeping): Likewise. (progman_cleanup): Assume expect_term if waiting for components from the previous runlevel. Clear v.p.wait on exited progs. * src/sysvinit.c (sysvinit_fifo_handler): Don't call progman_stop when handling runlevel changes. Handle INIT_CMD_POWERFAIL, INIT_CMD_POWERFAILNOW, and INIT_CMD_POWEROK. (sysvinit_sigtrans): Handle SIGPWR. (is_comp_wait): Rewrite. (power_stat_file): New variable. (sysvinit_power): New function.
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/ctl.c10
-rw-r--r--src/pies.c7
-rw-r--r--src/pies.h16
-rw-r--r--src/prog.h2
-rw-r--r--src/progman.c35
-rw-r--r--src/sysvinit.c94
6 files changed, 135 insertions, 29 deletions
diff --git a/src/ctl.c b/src/ctl.c
index d3e6b64..0e5ea97 100644
--- a/src/ctl.c
+++ b/src/ctl.c
@@ -1826,7 +1826,9 @@ prog_serialize (struct json_value *ret, struct prog *prog)
+ if (prog->v.p.comp->runlevels)
+ json_object_set_string (ret, "runlevels", "%s",
+ prog->v.p.comp->runlevels);
+
if (prog->v.p.status == status_sleeping)
- {
- json_object_set_number (ret, "wakeup-time",
- prog->v.p.timestamp + SLEEPTIME);
- }
+ json_object_set_number (ret, "wakeup-time",
+ prog->v.p.timestamp + SLEEPTIME);
diff --git a/src/pies.c b/src/pies.c
index 1bce1eb..96e9281 100644
--- a/src/pies.c
+++ b/src/pies.c
@@ -2192,2 +2192,3 @@ main (int argc, char **argv)
pies_reload ();
+ got_alarm = 1;
action = ACTION_CONT;
@@ -2218,2 +2219,8 @@ main (int argc, char **argv)
break;
+
+ case ACTION_POWER:
+ debug (1, ("SIGPWR"));
+ sysvinit_power ();
+ got_alarm = 1;
+ action = ACTION_CONT;
}
diff --git a/src/pies.h b/src/pies.h
index ad8e8ee..39977c7 100644
--- a/src/pies.h
+++ b/src/pies.h
@@ -272,3 +272,4 @@ enum pies_action {
ACTION_CTRLALTDEL,
- ACTION_KBREQUEST
+ ACTION_KBREQUEST,
+ ACTION_POWER
};
@@ -318,3 +319,3 @@ void free_action (struct action *act);
void register_prog (struct component *comp);
-int progman_running_p (void);
+int progman_waiting_p (void);
size_t progman_running_count (void);
@@ -515,2 +516,3 @@ void sysvinit_runlevel_setup (int mask, int *wait);
void sysvinit_sysdep_begin (void);
+void sysvinit_power (void);
@@ -523,2 +525,12 @@ extern char *init_fifo;
+#ifndef POWER_STAT_FILE
+# define POWER_STAT_FILE "/var/run/powerstatus"
+#endif
+
+/* Power status values */
+#define POWER_STAT_FAIL 'F'
+#define POWER_STAT_LOW 'L'
+#define POWER_STAT_OK 'O'
+
+/* Request codes */
#define INIT_MAGIC 0x03091969
diff --git a/src/prog.h b/src/prog.h
index a29de04..fe42d3a 100644
--- a/src/prog.h
+++ b/src/prog.h
@@ -54,3 +54,3 @@ struct prog
struct component *comp;
- size_t idx; /* Numeric identifier */
+ int wait;
int socket;
diff --git a/src/progman.c b/src/progman.c
index 3bbd8b3..4ca3824 100644
--- a/src/progman.c
+++ b/src/progman.c
@@ -116,12 +116,2 @@ progman_lookup_tcpmux (const char *service, const char *master)
-struct prog *
-prog_lookup_by_idx (unsigned idx)
-{
- struct prog *prog;
- for (prog = proghead; prog; prog = prog->next)
- if (IS_COMPONENT (prog) && prog->v.p.idx == idx)
- break;
- return prog;
-}
-
void prog_stop (struct prog *prog, int sig);
@@ -307,3 +297,3 @@ register_command (char *tag, char *command, pid_t pid)
int
-progman_running_p ()
+progman_waiting_p ()
{
@@ -313,5 +303,9 @@ progman_running_p ()
{
- if (IS_COMPONENT (prog) && is_comp_wait (prog->v.p.comp) &&
- prog->pid > 0)
- return 1;
+ if (IS_COMPONENT (prog) && prog->v.p.wait && prog->pid > 0)
+ {
+ debug(1, ("%s: waiting for %s (%lu)",
+ __FUNCTION__, prog_tag (prog),
+ (unsigned long) prog->pid));
+ return 1;
+ }
}
@@ -1061,2 +1055,3 @@ prog_start (struct prog *prog)
}
+ prog->v.p.wait = is_comp_wait (prog->v.p.comp);
debug (1, ("ok to start %s", prog->v.p.comp->tag));
@@ -1503,2 +1498,6 @@ progman_start ()
+ if (progman_waiting_p ())
+ /* Noting to do if there are processes left in the previous runlevel */
+ return;
+
recompute_alarm = 0;
@@ -1548,2 +1547,6 @@ progman_wake_sleeping (int onalrm)
+ if (progman_waiting_p ())
+ /* Noting to do if there are processes left in the previous runlevel */
+ return;
+
debug (1, (_("managing sleeping/stopping components")));
@@ -2256,2 +2259,5 @@ progman_cleanup (int expect_term)
int status;
+
+ if (!expect_term)
+ expect_term = progman_waiting_p ();
while ((pid = waitpid (-1, &status, WNOHANG)) > 0)
@@ -2306,2 +2312,3 @@ progman_cleanup (int expect_term)
prog->v.p.status = status_finished;
+ prog->v.p.wait = 0;
}
diff --git a/src/sysvinit.c b/src/sysvinit.c
index d8b7cc5..be7aef8 100644
--- a/src/sysvinit.c
+++ b/src/sysvinit.c
@@ -404,3 +404,5 @@ sysvinit_setenv (char const *data, int size)
char *init_fifo = INIT_FIFO;
+
static void create_fifo (void);
+static void powerfailcmd (int power_stat);
@@ -458,3 +460,2 @@ sysvinit_fifo_handler (int fd, void *data)
{
- progman_stop ();
dfl_level = buf.req.runlevel;
@@ -469,2 +470,14 @@ sysvinit_fifo_handler (int fd, void *data)
+ case INIT_CMD_POWERFAIL:
+ powerfailcmd (POWER_STAT_FAIL);
+ break;
+
+ case INIT_CMD_POWERFAILNOW:
+ powerfailcmd (POWER_STAT_LOW);
+ break;
+
+ case INIT_CMD_POWEROK:
+ powerfailcmd (POWER_STAT_OK);
+ break;
+
/* FIXME: react on other commands */
@@ -575,2 +588,5 @@ sysvinit_sigtrans (int sig, int *pact)
break;
+ case SIGPWR:
+ *pact = ACTION_POWER;
+ break;
default:
@@ -656,3 +672,3 @@ inittrans ()
- if (progman_running_p ())
+ if (progman_waiting_p ())
/* Noting to do if there are processes left in the previous runlevel */
@@ -728,8 +744,10 @@ is_comp_wait (struct component *comp)
{
- case pies_comp_boot:
- case pies_comp_powerfail:
- case pies_comp_ctrlaltdel:
+ case pies_comp_sysinit:
+ case pies_comp_bootwait:
+ case pies_comp_wait:
+ case pies_comp_powerwait:
case pies_comp_powerfailnow:
- case pies_comp_kbrequest:
- return 0;
+ case pies_comp_powerokwait:
+ case pies_comp_ctrlaltdel:
+ return 1;
default:
@@ -737,3 +755,3 @@ is_comp_wait (struct component *comp)
}
- return 1;
+ return 0;
}
@@ -973 +991,61 @@ inittab_parse (const char *file)
}
+
+char *power_stat_file = POWER_STAT_FILE;
+
+void
+sysvinit_power (void)
+{
+ int power_stat = POWER_STAT_FAIL;
+ int fd;
+
+ fd = open (power_stat_file, O_RDONLY);
+ if (fd >= 0)
+ {
+ char c;
+ switch (read (fd, &c, 1))
+ {
+ case 1:
+ power_stat = c;
+ break;
+
+ case 0:
+ logmsg (LOG_NOTICE, _("unexpected EOF on %s"), power_stat_file);
+ break;
+
+ case -1:
+ logmsg (LOG_ERR, _("error reading from %s: %s"),
+ power_stat_file, strerror (errno));
+ }
+ close (fd);
+ if (unlink (power_stat_file))
+ logmsg (LOG_ERR, _("can't unlink %s: %s"), power_stat_file,
+ strerror (errno));
+ }
+ else
+ debug (1, (_("can't open %s: %s"), power_stat_file, strerror (errno)));
+
+ powerfailcmd (power_stat);
+}
+
+static void
+powerfailcmd (int power_stat)
+{
+ int mask;
+
+ switch (power_stat)
+ {
+ case POWER_STAT_OK: /* The power is OK */
+ mask = PIES_COMP_MASK (pies_comp_powerokwait);
+ break;
+
+ case POWER_STAT_LOW: /* Low battery: shut down now */
+ mask = PIES_COMP_MASK (pies_comp_powerfailnow);
+ break;
+
+ default: /* Power failure */
+ mask = PIES_COMP_MASK (pies_comp_powerfail)
+ | PIES_COMP_MASK (pies_comp_powerwait);
+ }
+
+ sysvinit_runlevel_setup (mask, NULL);
+}

Return to:

Send suggestions and report system problems to the System administrator.