static int on_runlevel(Context *c) { int r = 0, q, previous, runlevel; assert(c); /* We finished changing runlevel, so let's write the * utmp record and send the audit msg */ /* First, get last runlevel */ q = utmp_get_runlevel(&previous, NULL); if (q < 0) { if (q != -ESRCH && q != -ENOENT) { log_error("Failed to get current runlevel: %s", strerror(-q)); return q; } previous = 0; } /* Secondly, get new runlevel */ runlevel = get_current_runlevel(c); if (runlevel < 0) return runlevel; if (previous == runlevel) return 0; #ifdef HAVE_AUDIT if (c->audit_fd >= 0) { _cleanup_free_ char *s = NULL; if (asprintf(&s, "old-level=%c new-level=%c", previous > 0 ? previous : 'N', runlevel > 0 ? runlevel : 'N') < 0) return log_oom(); if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0 && errno != EPERM) { log_error("Failed to send audit message: %m"); r = -errno; } } #endif q = utmp_put_runlevel(runlevel, previous); if (q < 0 && q != -ESRCH && q != -ENOENT) { log_error("Failed to write utmp record: %s", strerror(-q)); r = q; } return r; }
static int on_runlevel(Context *c) { int r = 0, q, previous, runlevel; assert(c); /* We finished changing runlevel, so let's write the * utmp record and send the audit msg */ /* First, get last runlevel */ if ((q = utmp_get_runlevel(&previous, NULL)) < 0) { if (q != -ESRCH && q != -ENOENT) { log_error("Failed to get current runlevel: %s", strerror(-q)); return q; } /* Hmm, we didn't find any runlevel, that means we * have been rebooted */ r = on_reboot(c); previous = 0; } /* Secondly, get new runlevel */ if ((runlevel = get_current_runlevel(c)) < 0) return runlevel; if (previous == runlevel) return 0; #ifdef HAVE_AUDIT if (c->audit_fd >= 0) { char *s = NULL; if (asprintf(&s, "old-level=%c new-level=%c", previous > 0 ? previous : 'N', runlevel > 0 ? runlevel : 'N') < 0) return -ENOMEM; if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) { log_error("Failed to send audit message: %m"); r = -errno; } free(s); } #endif if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) { log_error("Failed to write utmp record: %s", strerror(-q)); r = q; } return r; }