Skip to content

Commit e7fb33f

Browse files
committed
utmp: make sure we don't write the utmp reboot record twice on each boot
(Also, only send the audit msg once, too)
1 parent 1cea22a commit e7fb33f

File tree

2 files changed

+33
-57
lines changed

2 files changed

+33
-57
lines changed

src/shared/utmp-wtmp.c

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ int utmp_get_runlevel(int *runlevel, int *previous) {
4646
* very new and does not apply to the current script being
4747
* executed. */
4848

49-
if ((e = getenv("RUNLEVEL")) && e[0] > 0) {
49+
e = getenv("RUNLEVEL");
50+
if (e && e[0] > 0) {
5051
*runlevel = e[0];
5152

5253
if (previous) {
5354
/* $PREVLEVEL seems to be an Upstart thing */
5455

55-
if ((e = getenv("PREVLEVEL")) && e[0] > 0)
56+
e = getenv("PREVLEVEL");
57+
if (e && e[0] > 0)
5658
*previous = e[0];
5759
else
5860
*previous = 0;
@@ -66,7 +68,8 @@ int utmp_get_runlevel(int *runlevel, int *previous) {
6668

6769
setutxent();
6870

69-
if (!(found = getutxid(&lookup)))
71+
found = getutxid(&lookup);
72+
if (!found)
7073
r = -errno;
7174
else {
7275
int a, b;
@@ -232,7 +235,8 @@ int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
232235
lookup.ut_type = INIT_PROCESS; /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
233236
strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
234237

235-
if (!(found = getutxid(&lookup)))
238+
found = getutxid(&lookup);
239+
if (!found)
236240
return 0;
237241

238242
if (found->ut_pid != pid)
@@ -264,7 +268,8 @@ int utmp_put_runlevel(int runlevel, int previous) {
264268
if (previous <= 0) {
265269
/* Find the old runlevel automatically */
266270

267-
if ((r = utmp_get_runlevel(&previous, NULL)) < 0) {
271+
r = utmp_get_runlevel(&previous, NULL);
272+
if (r < 0) {
268273
if (r != -ESRCH)
269274
return r;
270275

@@ -343,16 +348,15 @@ static int write_to_terminal(const char *tty, const char *message) {
343348
}
344349

345350
int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
346-
struct utmpx *u;
351+
_cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
347352
char date[FORMAT_TIMESTAMP_MAX];
348-
char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
353+
struct utmpx *u;
349354
int r;
350355

351-
if (!(hn = gethostname_malloc()) ||
352-
!(un = getlogname_malloc())) {
353-
r = -ENOMEM;
354-
goto finish;
355-
}
356+
hn = gethostname_malloc();
357+
un = getlogname_malloc();
358+
if (!hn || !un)
359+
return -ENOMEM;
356360

357361
getttyname_harder(STDIN_FILENO, &tty);
358362

@@ -363,19 +367,17 @@ int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
363367
un, hn,
364368
tty ? " on " : "", strempty(tty),
365369
format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
366-
message) < 0) {
367-
r = -ENOMEM;
368-
goto finish;
369-
}
370+
message) < 0)
371+
return -ENOMEM;
370372

371373
setutxent();
372374

373375
r = 0;
374376

375377
while ((u = getutxent())) {
376-
int q;
378+
_cleanup_free_ char *buf = NULL;
377379
const char *path;
378-
char *buf = NULL;
380+
int q;
379381

380382
if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
381383
continue;
@@ -384,27 +386,18 @@ int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
384386
if (path_startswith(u->ut_line, "/dev/"))
385387
path = u->ut_line;
386388
else {
387-
if (asprintf(&buf, "/dev/%.*s",
388-
(int) sizeof(u->ut_line), u->ut_line) < 0) {
389-
r = -ENOMEM;
390-
goto finish;
391-
}
389+
if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
390+
return -ENOMEM;
392391

393392
path = buf;
394393
}
395394

396-
if (!match_tty || match_tty(path))
397-
if ((q = write_to_terminal(path, text)) < 0)
395+
if (!match_tty || match_tty(path)) {
396+
q = write_to_terminal(path, text);
397+
if (q < 0)
398398
r = q;
399-
400-
free(buf);
399+
}
401400
}
402401

403-
finish:
404-
free(hn);
405-
free(un);
406-
free(tty);
407-
free(text);
408-
409402
return r;
410403
}

src/update-utmp/update-utmp.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "utmp-wtmp.h"
3939
#include "bus-util.h"
4040
#include "bus-error.h"
41+
#include "unit-name.h"
4142

4243
typedef struct Context {
4344
sd_bus *bus;
@@ -93,27 +94,11 @@ static int get_current_runlevel(Context *c) {
9394
assert(c);
9495

9596
for (i = 0; i < ELEMENTSOF(table); i++) {
96-
_cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
97-
_cleanup_free_ char *state = NULL;
98-
const char *path = NULL;
97+
_cleanup_free_ char *state = NULL, *path = NULL;
9998

100-
r = sd_bus_call_method(
101-
c->bus,
102-
"org.freedesktop.systemd1",
103-
"/org/freedesktop/systemd1",
104-
"org.freedesktop.systemd1.Manager",
105-
"LoadUnit",
106-
&error,
107-
&reply,
108-
"s", table[i].special);
109-
if (r < 0) {
110-
log_warning("Failed to get runlevel: %s", bus_error_message(&error, -r));
111-
continue;
112-
}
113-
114-
r = sd_bus_message_read(reply, "o", &path);
115-
if (r < 0)
116-
return bus_log_parse_error(r);
99+
path = unit_dbus_path_from_name(table[i].special);
100+
if (!path)
101+
return log_oom();
117102

118103
r = sd_bus_get_property_string(
119104
c->bus,
@@ -202,21 +187,19 @@ static int on_runlevel(Context *c) {
202187

203188
/* First, get last runlevel */
204189
q = utmp_get_runlevel(&previous, NULL);
205-
if (q < 0) {
206190

191+
if (q < 0) {
207192
if (q != -ESRCH && q != -ENOENT) {
208193
log_error("Failed to get current runlevel: %s", strerror(-q));
209194
return q;
210195
}
211196

212-
/* Hmm, we didn't find any runlevel, that means we
213-
* have been rebooted */
214-
r = on_reboot(c);
215197
previous = 0;
216198
}
217199

218200
/* Secondly, get new runlevel */
219201
runlevel = get_current_runlevel(c);
202+
220203
if (runlevel < 0)
221204
return runlevel;
222205

0 commit comments

Comments
 (0)