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.
This commit is contained in:
Jesse Smith 2018-07-07 18:49:04 -03:00
parent aa80ddcc4b
commit 6b26692584
5 changed files with 121 additions and 3 deletions

View File

@ -125,7 +125,7 @@ utmpdump: LDLIBS += $(STATIC)
utmpdump: utmpdump.o utmpdump: utmpdump.o
runlevel: LDLIBS += $(STATIC) runlevel: LDLIBS += $(STATIC)
runlevel: runlevel.o runlevel: runlevel.o runlevellog.o
sulogin: LDLIBS += $(SULOGINLIBS) $(STATIC) sulogin: LDLIBS += $(SULOGINLIBS) $(STATIC)
sulogin: sulogin.o consoles.o sulogin: sulogin.o consoles.o
@ -145,6 +145,8 @@ fstab-decode: fstab-decode.o
sulogin.o: CPPFLAGS += $(SELINUX_DEF) sulogin.o: CPPFLAGS += $(SELINUX_DEF)
sulogin.o: sulogin.c sulogin.o: sulogin.c
runlevellog.o: runlevellog.h runlevellog.c paths.h
init.o: CPPFLAGS += $(SELINUX_DEF) init.o: CPPFLAGS += $(SELINUX_DEF)
init.o: init.c init.h initreq.h paths.h reboot.h set.h init.o: init.c init.h initreq.h paths.h reboot.h set.h

View File

@ -37,6 +37,7 @@
#define INITSCRIPT "/etc/initscript" /* Initscript. */ #define INITSCRIPT "/etc/initscript" /* Initscript. */
#define PWRSTAT_OLD "/etc/powerstatus" /* COMPAT: SIGPWR reason (OK/BAD) */ #define PWRSTAT_OLD "/etc/powerstatus" /* COMPAT: SIGPWR reason (OK/BAD) */
#define PWRSTAT "/var/run/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 #if 0
#define INITLVL "/etc/initrunlvl" /* COMPAT: New runlevel */ #define INITLVL "/etc/initrunlvl" /* COMPAT: New runlevel */

View File

@ -25,6 +25,7 @@
#include <utmp.h> #include <utmp.h>
#include <time.h> #include <time.h>
#include <stdlib.h> #include <stdlib.h>
#include "runlevellog.h"
int main(argc, argv) int main(argc, argv)
int argc; int argc;
@ -32,6 +33,7 @@ char **argv;
{ {
struct utmp *ut; struct utmp *ut;
char prev; char prev;
int status, runlevel;
if (argc > 1) utmpname(argv[1]); if (argc > 1) utmpname(argv[1]);
@ -45,9 +47,15 @@ char **argv;
exit(0); exit(0);
} }
} }
printf("unknown\n");
endutent(); endutent();
status = Read_Runlevel_Log(&runlevel);
if (status)
{
printf("%c\n", runlevel);
return 0;
}
printf("Unknown.\n");
return(1); return(1);
} }

71
src/runlevellog.c Normal file
View File

@ -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 <stdio.h>
#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

36
src/runlevellog.h Normal file
View File

@ -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