Define _cleanup_ helper for setutxent()+endutxent()

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-07-02 16:24:04 +02:00
parent 272ac70a21
commit c2a9909377
3 changed files with 30 additions and 42 deletions

View file

@ -4,9 +4,6 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include <linux/vt.h>
#if ENABLE_UTMP
#include <utmpx.h>
#endif
#include "sd-device.h"
@ -29,6 +26,7 @@
#include "udev-util.h"
#include "user-util.h"
#include "userdb.h"
#include "utmp-wtmp.h"
void manager_reset_config(Manager *m) {
assert(m);
@ -685,13 +683,14 @@ bool manager_all_buttons_ignored(Manager *m) {
int manager_read_utmp(Manager *m) {
#if ENABLE_UTMP
int r;
_cleanup_(utxent_cleanup) bool utmpx = false;
assert(m);
if (utmpxname(_PATH_UTMPX) < 0)
return log_error_errno(errno, "Failed to set utmp path to " _PATH_UTMPX ": %m");
setutxent();
utmpx = utxent_start();
for (;;) {
_cleanup_free_ char *t = NULL;
@ -704,8 +703,7 @@ int manager_read_utmp(Manager *m) {
if (!u) {
if (errno != 0)
log_warning_errno(errno, "Failed to read " _PATH_UTMPX ", ignoring: %m");
r = 0;
break;
return 0;
}
if (u->ut_type != USER_PROCESS)
@ -715,18 +713,14 @@ int manager_read_utmp(Manager *m) {
continue;
t = strndup(u->ut_line, sizeof(u->ut_line));
if (!t) {
r = log_oom();
break;
}
if (!t)
return log_oom();
c = path_startswith(t, "/dev/");
if (c) {
r = free_and_strdup(&t, c);
if (r < 0) {
log_oom();
break;
}
if (r < 0)
return log_oom();
}
if (isempty(t))
@ -756,8 +750,6 @@ int manager_read_utmp(Manager *m) {
log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s->tty, s->id);
}
endutxent();
return r;
#else
return 0;
#endif

View file

@ -25,8 +25,8 @@
#include "utmp-wtmp.h"
int utmp_get_runlevel(int *runlevel, int *previous) {
_cleanup_(utxent_cleanup) bool utmpx = false;
struct utmpx *found, lookup = { .ut_type = RUN_LVL };
int r;
const char *e;
assert(runlevel);
@ -58,27 +58,17 @@ int utmp_get_runlevel(int *runlevel, int *previous) {
if (utmpxname(_PATH_UTMPX) < 0)
return -errno;
setutxent();
utmpx = utxent_start();
found = getutxid(&lookup);
if (!found)
r = -errno;
else {
int a, b;
return -errno;
a = found->ut_pid & 0xFF;
b = (found->ut_pid >> 8) & 0xFF;
*runlevel = found->ut_pid & 0xFF;
if (previous)
*previous = (found->ut_pid >> 8) & 0xFF;
*runlevel = a;
if (previous)
*previous = b;
r = 0;
}
endutxent();
return r;
return 0;
}
static void init_timestamp(struct utmpx *store, usec_t t) {
@ -106,7 +96,7 @@ static void init_entry(struct utmpx *store, usec_t t) {
}
static int write_entry_utmp(const struct utmpx *store) {
int r;
_cleanup_(utxent_cleanup) bool utmpx = false;
assert(store);
@ -117,16 +107,11 @@ static int write_entry_utmp(const struct utmpx *store) {
if (utmpxname(_PATH_UTMPX) < 0)
return -errno;
setutxent();
utmpx = utxent_start();
if (!pututxline(store))
r = -errno;
else
r = 0;
endutxent();
return r;
return -errno;
return 0;
}
static int write_entry_wtmp(const struct utmpx *store) {

View file

@ -8,6 +8,8 @@
#include "util.h"
#if ENABLE_UTMP
#include <utmpx.h>
int utmp_get_runlevel(int *runlevel, int *previous);
int utmp_put_shutdown(void);
@ -24,6 +26,15 @@ int utmp_wall(
bool (*match_tty)(const char *tty, void *userdata),
void *userdata);
static inline bool utxent_start(void) {
setutxent();
return true;
}
static inline void utxent_cleanup(bool *initialized) {
if (initialized)
endutxent();
}
#else /* ENABLE_UTMP */
static inline int utmp_get_runlevel(int *runlevel, int *previous) {