Define _cleanup_ helper for setutxent()+endutxent()

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2020-07-05 12:16:01 +02:00
parent 272ac70a21
commit c2a9909377
3 changed files with 30 additions and 42 deletions
+11 -26
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) {
+11
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) {