From 6b26692584d60c2f438f0bcb6c40cb060133fad3 Mon Sep 17 00:00:00 2001 From: Jesse Smith Date: Sat, 7 Jul 2018 18:49:04 -0300 Subject: [PATCH] Added new runlevel log code which saves (and reads) the current runlevel from /var/run/runlevel. Added test code to runlevel.c to confirm it works. Will use this to save/restore runlevel on systems where utmp is not available. --- src/Makefile | 4 ++- src/paths.h | 1 + src/runlevel.c | 12 ++++++-- src/runlevellog.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++ src/runlevellog.h | 36 ++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 3 deletions(-) create mode 100644 src/runlevellog.c create mode 100644 src/runlevellog.h diff --git a/src/Makefile b/src/Makefile index 272a316..f5dcc2e 100644 --- a/src/Makefile +++ b/src/Makefile @@ -125,7 +125,7 @@ utmpdump: LDLIBS += $(STATIC) utmpdump: utmpdump.o runlevel: LDLIBS += $(STATIC) -runlevel: runlevel.o +runlevel: runlevel.o runlevellog.o sulogin: LDLIBS += $(SULOGINLIBS) $(STATIC) sulogin: sulogin.o consoles.o @@ -145,6 +145,8 @@ fstab-decode: fstab-decode.o sulogin.o: CPPFLAGS += $(SELINUX_DEF) sulogin.o: sulogin.c +runlevellog.o: runlevellog.h runlevellog.c paths.h + init.o: CPPFLAGS += $(SELINUX_DEF) init.o: init.c init.h initreq.h paths.h reboot.h set.h diff --git a/src/paths.h b/src/paths.h index 232a944..05f9c1b 100644 --- a/src/paths.h +++ b/src/paths.h @@ -37,6 +37,7 @@ #define INITSCRIPT "/etc/initscript" /* Initscript. */ #define PWRSTAT_OLD "/etc/powerstatus" /* COMPAT: SIGPWR reason (OK/BAD) */ #define PWRSTAT "/var/run/powerstatus" /* COMPAT: SIGPWR reason (OK/BAD) */ +#define RUNLEVEL_LOG "/var/run/runlevel" /* neutral place to store run level */ #if 0 #define INITLVL "/etc/initrunlvl" /* COMPAT: New runlevel */ diff --git a/src/runlevel.c b/src/runlevel.c index 65e4b31..3dd8df3 100644 --- a/src/runlevel.c +++ b/src/runlevel.c @@ -25,6 +25,7 @@ #include #include #include +#include "runlevellog.h" int main(argc, argv) int argc; @@ -32,6 +33,7 @@ char **argv; { struct utmp *ut; char prev; + int status, runlevel; if (argc > 1) utmpname(argv[1]); @@ -45,9 +47,15 @@ char **argv; exit(0); } } - - printf("unknown\n"); endutent(); + + status = Read_Runlevel_Log(&runlevel); + if (status) + { + printf("%c\n", runlevel); + return 0; + } + printf("Unknown.\n"); return(1); } diff --git a/src/runlevellog.c b/src/runlevellog.c new file mode 100644 index 0000000..363ce7d --- /dev/null +++ b/src/runlevellog.c @@ -0,0 +1,71 @@ +/* + * runlevellog - Saves and restores runlevel from distor-neutral location. + * + * + * This file is part of the sysvinit suite, + * Copyright (C) 2018 Jesse Smith + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include "paths.h" +#include "runlevellog.h" + +/* +Write the current runlevel to its log file. +The function returns TRUE on success and FALSE +on failure. +*/ +int Write_Runlevel_Log(int new_runlevel) +{ + FILE *log_file; + int status; + + log_file = fopen(RUNLEVEL_LOG, "w"); + if (! log_file) + return FALSE; + + status = fprintf(log_file, "%c", new_runlevel); + fclose(log_file); + if (status < 1) + return FALSE; + return TRUE; +} // end of writing to log function + + +/* +This function reads the last runlevel from the log file. +The function stores the read value at the addressed passed +into the function (aka runlevel). The function returns +TRUE on success and FALSE on failure. +*/ +int Read_Runlevel_Log(int *runlevel) +{ + FILE *log_file; + int status; + + log_file = fopen(RUNLEVEL_LOG, "r"); + if (! log_file) + return FALSE; + + status = fscanf(log_file, "%c", runlevel); + fclose(log_file); + if (status == EOF) + return FALSE; + return TRUE; + +} // end of reading from log function + + diff --git a/src/runlevellog.h b/src/runlevellog.h new file mode 100644 index 0000000..cccf0c6 --- /dev/null +++ b/src/runlevellog.h @@ -0,0 +1,36 @@ +/* + * runlevellog - Saves and restores runlevel from distor-neutral location. + * + * + * This file is part of the sysvinit suite, + * Copyright (C) 2018 Jesse Smith + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef RUNLEVEL_LOG_HEADER__ +#define RUNLEVEL_LOG_HEADER__ + +#ifndef TRUE +#define TRUE 1 +#endif +#ifndef FALSE +#define FALSE 0 +#endif + +int Write_Runlevel_Log(int new_runlevel); +int Read_Runlevel_Log(int *runlevel); + +#endif +