diff --git a/Makefile.am b/Makefile.am index 9890da80..343e8788 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,7 +1,8 @@ ACLOCAL_AMFLAGS = -I m4 sbin_PROGRAMS = finit initctl reboot finit_SOURCES = finit.c api.c client.c conf.c exec.c getty.c helpers.c pid.c \ - sig.c svc.c service.c plugin.c tty.c cond.c cond-w.c sm.c + sig.c svc.c service.c plugin.c tty.c cond.c cond-w.c sm.c \ + utmp-api.c utmp-api.h pkginclude_HEADERS = cond.h finit.h helpers.h inetd.h plugin.h svc.h if INETD finit_SOURCES += inetd.c diff --git a/helpers.c b/helpers.c index 5a66c20d..8fcf5670 100644 --- a/helpers.c +++ b/helpers.c @@ -31,14 +31,13 @@ #include #include #include -#include #include -#include #include #include "finit.h" #include "helpers.h" #include "private.h" +#include "utmp-api.h" char *strip_line(char *line) { @@ -57,24 +56,9 @@ char *strip_line(char *line) return line; } -static int encode(int lvl) -{ - if (!lvl) return 0; - return lvl + '0'; -} - void runlevel_set(int pre, int now) { - struct utmp utent; - - memset(&utent, 0, sizeof(utent)); - utent.ut_type = RUN_LVL; - utent.ut_pid = (encode(pre) << 8) | (encode(now) & 0xFF); - strlcpy(utent.ut_user, "runlevel", sizeof(utent.ut_user)); - - setutent(); - pututline(&utent); - endutent(); + utmp_set_runlevel(pre, now); } /** diff --git a/utmp-api.c b/utmp-api.c index 1dbefdd0..1b140df5 100644 --- a/utmp-api.c +++ b/utmp-api.c @@ -24,6 +24,35 @@ #include #include #include +#include + +int utmp_set(int type, int pid, char *user) +{ + int result; + struct utmp ut; + + memset(&ut, 0, sizeof(ut)); + ut.ut_type = type; + ut.ut_pid = pid; + strlcpy(ut.ut_user, user, sizeof(ut.ut_user)); + + setutent(); + result = pututline(&ut) ? 0 : 1;; + endutent(); + + return result; +} + +static int encode(int lvl) +{ + if (!lvl) return 0; + return lvl + '0'; +} + +int utmp_set_runlevel(int pre, int now) +{ + return utmp_set(RUN_LVL, (encode(pre) << 8) | (encode(now) & 0xFF), "runlevel"); +} int utmp_show(char *file) { diff --git a/utmp-api.h b/utmp-api.h index aa2222b2..5df60503 100644 --- a/utmp-api.h +++ b/utmp-api.h @@ -21,6 +21,8 @@ * THE SOFTWARE. */ +int utmp_set (int type, int pid, char *user); +int utmp_set_runlevel(int pre, int now); int utmp_show(char *file); /**