initctl: Break out UTMP logic to separate file

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson committed 2016-08-07 22:22:42 +02:00
1 parent 07f06c8b95
commit 7cdd42195b
4 files changed
+103 -39

No files matched your search

+2 -1
View File
@@ -15,7 +15,8 @@ else
finit_LDADD += -ldl finit_LDADD += -ldl
endif endif
initctl_SOURCES = initctl.c svc.c helpers.c cond.c initctl_SOURCES = initctl.c svc.c svc.h helpers.c helpers.h cond.c cond.h \
utmp-api.c utmp-api.h
initctl_CFLAGS = $(lite_CFLAGS) initctl_CFLAGS = $(lite_CFLAGS)
initctl_LDADD = $(lite_LIBS) initctl_LDADD = $(lite_LIBS)
+3 -38
View File
@@ -28,8 +28,6 @@
#include <stdio.h> #include <stdio.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/un.h> #include <sys/un.h>
#include <time.h>
#include <utmp.h>
#include <lite/lite.h> #include <lite/lite.h>
#include "config.h" #include "config.h"
@@ -37,6 +35,7 @@
#include "cond.h" #include "cond.h"
#include "helpers.h" #include "helpers.h"
#include "service.h" #include "service.h"
#include "utmp-api.h"
#define _PATH_COND _PATH_VARRUN "finit/cond/" #define _PATH_COND _PATH_VARRUN "finit/cond/"
@@ -282,44 +281,10 @@ static int do_halt (char *UNUSED(arg)) { return kill(1, SIGUSR1); }
static int do_poweroff(char *UNUSED(arg)) { return kill(1, SIGUSR2); } static int do_poweroff(char *UNUSED(arg)) { return kill(1, SIGUSR2); }
static int do_reboot (char *UNUSED(arg)) { return kill(1, SIGTERM); } static int do_reboot (char *UNUSED(arg)) { return kill(1, SIGTERM); }
static int do_utmp_show(char *file)
{
time_t sec;
struct utmp *ut;
struct tm *sectm;
int pid;
char id[sizeof(ut->ut_id)], user[sizeof(ut->ut_user)], when[80];
printf("%s =============================================================================================\n", file);
utmpname(file);
setutent();
while ((ut = getutent())) {
memset(id, 0, sizeof(id));
strncpy(id, ut->ut_id, sizeof(ut->ut_id));
memset(user, 0, sizeof(user));
strncpy(user, ut->ut_user, sizeof(ut->ut_user));
sec = ut->ut_tv.tv_sec;
sectm = localtime(&sec);
strftime(when, sizeof(when), "%F %T", sectm);
pid = ut->ut_pid;
printf("[%d] [%05d] [%-4.4s] [%-8.8s] [%-12.12s] [%-20.20s] [%-15.15s] [%-19.19s]\n",
ut->ut_type, pid, id, user, ut->ut_line, ut->ut_host, "0.0.0.0", when);
}
endutent();
return 0;
}
static int do_utmp(char *UNUSED(cmd)) static int do_utmp(char *UNUSED(cmd))
{ {
return do_utmp_show(_PATH_UTMP) || return utmp_show(_PATH_UTMP) ||
do_utmp_show(_PATH_WTMP); utmp_show(_PATH_WTMP);
} }
static int show_version(char *UNUSED(arg)) static int show_version(char *UNUSED(arg))
+67
View File
@@ -0,0 +1,67 @@
/* UTMP API
*
* Copyright (c) 2016 Joachim Nilsson <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdio.h>
#include <time.h>
#include <utmp.h>
int utmp_show(char *file)
{
time_t sec;
struct utmp *ut;
struct tm *sectm;
int pid;
char id[sizeof(ut->ut_id)], user[sizeof(ut->ut_user)], when[80];
printf("%s =============================================================================================\n", file);
utmpname(file);
setutent();
while ((ut = getutent())) {
memset(id, 0, sizeof(id));
strncpy(id, ut->ut_id, sizeof(ut->ut_id));
memset(user, 0, sizeof(user));
strncpy(user, ut->ut_user, sizeof(ut->ut_user));
sec = ut->ut_tv.tv_sec;
sectm = localtime(&sec);
strftime(when, sizeof(when), "%F %T", sectm);
pid = ut->ut_pid;
printf("[%d] [%05d] [%-4.4s] [%-8.8s] [%-12.12s] [%-20.20s] [%-15.15s] [%-19.19s]\n",
ut->ut_type, pid, id, user, ut->ut_line, ut->ut_host, "0.0.0.0", when);
}
endutent();
return 0;
}
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/
+31
View File
@@ -0,0 +1,31 @@
/* UTMP API
*
* Copyright (c) 2016 Joachim Nilsson <troglobit@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
int utmp_show(char *file);
/**
* Local Variables:
* indent-tabs-mode: t
* c-file-style: "linux"
* End:
*/