2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2010-08-11 01:43:23 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2010-08-11 15:19:50 +02:00
|
|
|
#include <libaudit.h>
|
|
|
|
#endif
|
|
|
|
|
2013-08-12 02:51:56 +02:00
|
|
|
#include "sd-bus.h"
|
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
|
|
|
#include "bus-error.h"
|
|
|
|
#include "bus-util.h"
|
2016-11-07 16:14:59 +01:00
|
|
|
#include "format-util.h"
|
2010-08-11 01:43:23 +02:00
|
|
|
#include "log.h"
|
|
|
|
#include "macro.h"
|
2019-08-05 15:53:28 +02:00
|
|
|
#include "main-func.h"
|
2017-07-20 16:19:18 +02:00
|
|
|
#include "process-util.h"
|
2010-08-11 01:43:23 +02:00
|
|
|
#include "special.h"
|
2019-10-21 20:58:18 +02:00
|
|
|
#include "stdio-util.h"
|
2016-10-20 19:20:22 +02:00
|
|
|
#include "strv.h"
|
2014-01-30 14:18:46 +01:00
|
|
|
#include "unit-name.h"
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "util.h"
|
|
|
|
#include "utmp-wtmp.h"
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
typedef struct Context {
|
2013-08-12 02:51:56 +02:00
|
|
|
sd_bus *bus;
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2010-08-11 01:43:23 +02:00
|
|
|
int audit_fd;
|
|
|
|
#endif
|
|
|
|
} Context;
|
|
|
|
|
2018-11-22 20:38:06 +01:00
|
|
|
static void context_clear(Context *c) {
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
c->bus = sd_bus_flush_close_unref(c->bus);
|
|
|
|
#if HAVE_AUDIT
|
|
|
|
if (c->audit_fd >= 0)
|
|
|
|
audit_close(c->audit_fd);
|
|
|
|
c->audit_fd = -1;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-08-11 01:43:23 +02:00
|
|
|
static usec_t get_startup_time(Context *c) {
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2013-11-07 22:17:19 +01:00
|
|
|
usec_t t = 0;
|
2013-08-12 02:51:56 +02:00
|
|
|
int r;
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
assert(c);
|
|
|
|
|
2013-11-07 22:17:19 +01:00
|
|
|
r = sd_bus_get_property_trivial(
|
2012-08-08 17:19:30 +02:00
|
|
|
c->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
"/org/freedesktop/systemd1",
|
2013-08-12 02:51:56 +02:00
|
|
|
"org.freedesktop.systemd1.Manager",
|
2013-11-07 22:17:19 +01:00
|
|
|
"UserspaceTimestamp",
|
|
|
|
&error,
|
|
|
|
't', &t);
|
2013-08-12 02:51:56 +02:00
|
|
|
if (r < 0) {
|
2015-09-24 13:33:55 +02:00
|
|
|
log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
|
2013-11-07 22:17:19 +01:00
|
|
|
return 0;
|
2010-08-11 01:43:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_current_runlevel(Context *c) {
|
|
|
|
static const struct {
|
|
|
|
const int runlevel;
|
|
|
|
const char *special;
|
|
|
|
} table[] = {
|
|
|
|
/* The first target of this list that is active or has
|
2010-08-25 03:31:59 +02:00
|
|
|
* a job scheduled wins. We prefer runlevels 5 and 3
|
|
|
|
* here over the others, since these are the main
|
|
|
|
* runlevels used on Fedora. It might make sense to
|
|
|
|
* change the order on some distributions. */
|
2015-02-18 20:17:49 +01:00
|
|
|
{ '5', SPECIAL_GRAPHICAL_TARGET },
|
|
|
|
{ '3', SPECIAL_MULTI_USER_TARGET },
|
|
|
|
{ '1', SPECIAL_RESCUE_TARGET },
|
2010-08-11 01:43:23 +02:00
|
|
|
};
|
|
|
|
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
|
2013-08-12 02:51:56 +02:00
|
|
|
int r;
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
assert(c);
|
|
|
|
|
2020-07-02 14:25:06 +02:00
|
|
|
for (size_t i = 0; i < ELEMENTSOF(table); i++) {
|
2014-01-30 14:18:46 +01:00
|
|
|
_cleanup_free_ char *state = NULL, *path = NULL;
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2014-01-30 14:18:46 +01:00
|
|
|
path = unit_dbus_path_from_name(table[i].special);
|
|
|
|
if (!path)
|
|
|
|
return log_oom();
|
2013-08-12 02:51:56 +02:00
|
|
|
|
2013-11-07 22:17:19 +01:00
|
|
|
r = sd_bus_get_property_string(
|
2012-08-08 17:19:30 +02:00
|
|
|
c->bus,
|
|
|
|
"org.freedesktop.systemd1",
|
|
|
|
path,
|
2013-11-07 22:17:19 +01:00
|
|
|
"org.freedesktop.systemd1.Unit",
|
|
|
|
"ActiveState",
|
2013-08-12 02:51:56 +02:00
|
|
|
&error,
|
2013-11-07 22:17:19 +01:00
|
|
|
&state);
|
2015-09-24 13:33:55 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2016-10-20 19:20:22 +02:00
|
|
|
if (STR_IN_SET(state, "active", "reloading"))
|
2013-08-12 02:51:56 +02:00
|
|
|
return table[i].runlevel;
|
2010-08-11 01:43:23 +02:00
|
|
|
}
|
|
|
|
|
2013-08-12 02:51:56 +02:00
|
|
|
return 0;
|
2010-08-11 01:43:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int on_reboot(Context *c) {
|
|
|
|
int r = 0, q;
|
|
|
|
usec_t t;
|
|
|
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
/* We finished start-up, so let's write the utmp
|
|
|
|
* record and send the audit msg */
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2010-08-11 01:43:23 +02:00
|
|
|
if (c->audit_fd >= 0)
|
2014-11-04 00:47:44 +01:00
|
|
|
if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
|
2020-04-12 18:26:05 +02:00
|
|
|
errno != EPERM)
|
2015-09-24 13:33:55 +02:00
|
|
|
r = log_error_errno(errno, "Failed to send audit message: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If this call fails it will return 0, which
|
|
|
|
* utmp_put_reboot() will then fix to the current time */
|
|
|
|
t = get_startup_time(c);
|
|
|
|
|
2014-01-30 13:28:56 +01:00
|
|
|
q = utmp_put_reboot(t);
|
2020-11-19 18:35:24 +01:00
|
|
|
if (q < 0)
|
|
|
|
r = log_error_errno(q, "Failed to write utmp record: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int on_shutdown(Context *c) {
|
|
|
|
int r = 0, q;
|
|
|
|
|
|
|
|
assert(c);
|
|
|
|
|
|
|
|
/* We started shut-down, so let's write the utmp
|
|
|
|
* record and send the audit msg */
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2010-08-11 01:43:23 +02:00
|
|
|
if (c->audit_fd >= 0)
|
2014-11-04 00:47:44 +01:00
|
|
|
if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
|
2020-04-12 18:26:05 +02:00
|
|
|
errno != EPERM)
|
2015-09-24 13:33:55 +02:00
|
|
|
r = log_error_errno(errno, "Failed to send audit message: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
#endif
|
|
|
|
|
2014-01-30 13:28:56 +01:00
|
|
|
q = utmp_put_shutdown();
|
2020-11-19 18:35:24 +01:00
|
|
|
if (q < 0)
|
|
|
|
r = log_error_errno(q, "Failed to write utmp record: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
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 */
|
2014-01-30 13:28:56 +01:00
|
|
|
q = utmp_get_runlevel(&previous, NULL);
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2014-01-30 14:18:46 +01:00
|
|
|
if (q < 0) {
|
2017-10-04 16:01:32 +02:00
|
|
|
if (!IN_SET(q, -ESRCH, -ENOENT))
|
2014-11-28 18:50:43 +01:00
|
|
|
return log_error_errno(q, "Failed to get current runlevel: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
previous = 0;
|
|
|
|
}
|
|
|
|
|
2010-08-11 15:19:50 +02:00
|
|
|
/* Secondly, get new runlevel */
|
2014-01-30 13:28:56 +01:00
|
|
|
runlevel = get_current_runlevel(c);
|
|
|
|
if (runlevel < 0)
|
2010-08-11 01:43:23 +02:00
|
|
|
return runlevel;
|
2020-09-08 12:51:23 +02:00
|
|
|
if (runlevel == 0) {
|
|
|
|
log_warning("Failed to get new runlevel, utmp update skipped.");
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-05 13:01:58 +02:00
|
|
|
|
2010-08-11 01:43:23 +02:00
|
|
|
if (previous == runlevel)
|
|
|
|
return 0;
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2010-08-11 01:43:23 +02:00
|
|
|
if (c->audit_fd >= 0) {
|
2019-10-21 20:58:18 +02:00
|
|
|
char s[STRLEN("old-level=_ new-level=_") + 1];
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2019-10-21 20:58:18 +02:00
|
|
|
xsprintf(s, "old-level=%c new-level=%c",
|
|
|
|
previous > 0 ? previous : 'N',
|
|
|
|
runlevel);
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2019-10-21 20:58:18 +02:00
|
|
|
if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s,
|
|
|
|
"systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
|
2015-09-24 13:33:55 +02:00
|
|
|
r = log_error_errno(errno, "Failed to send audit message: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-01-30 13:28:56 +01:00
|
|
|
q = utmp_put_runlevel(runlevel, previous);
|
2019-10-21 20:58:18 +02:00
|
|
|
if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT))
|
|
|
|
return log_error_errno(q, "Failed to write utmp record: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2019-08-05 15:53:28 +02:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-11-22 20:38:06 +01:00
|
|
|
_cleanup_(context_clear) Context c = {
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2014-01-30 13:28:56 +01:00
|
|
|
.audit_fd = -1
|
2010-08-11 01:43:23 +02:00
|
|
|
#endif
|
2014-01-30 13:28:56 +01:00
|
|
|
};
|
|
|
|
int r;
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2019-08-05 15:53:28 +02:00
|
|
|
if (argc != 2)
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"This program requires one argument.");
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2018-11-20 11:18:22 +01:00
|
|
|
log_setup_service();
|
2010-08-11 01:43:23 +02:00
|
|
|
|
2011-08-01 20:52:18 +02:00
|
|
|
umask(0022);
|
|
|
|
|
2017-10-03 10:41:51 +02:00
|
|
|
#if HAVE_AUDIT
|
2019-08-05 15:53:28 +02:00
|
|
|
/* If the kernel lacks netlink or audit support, don't worry about it. */
|
2014-01-30 13:28:56 +01:00
|
|
|
c.audit_fd = audit_open();
|
2019-08-05 15:53:28 +02:00
|
|
|
if (c.audit_fd < 0)
|
|
|
|
log_full_errno(IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT) ? LOG_DEBUG : LOG_ERR,
|
|
|
|
errno, "Failed to connect to audit log: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
#endif
|
2015-09-24 13:30:10 +02:00
|
|
|
r = bus_connect_system_systemd(&c.bus);
|
2019-08-05 15:53:28 +02:00
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to get D-Bus connection: %m");
|
2010-08-11 01:43:23 +02:00
|
|
|
|
|
|
|
if (streq(argv[1], "reboot"))
|
2019-08-05 15:53:28 +02:00
|
|
|
return on_reboot(&c);
|
|
|
|
if (streq(argv[1], "shutdown"))
|
|
|
|
return on_shutdown(&c);
|
|
|
|
if (streq(argv[1], "runlevel"))
|
|
|
|
return on_runlevel(&c);
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command %s", argv[1]);
|
2010-08-11 01:43:23 +02:00
|
|
|
}
|
2019-08-05 15:53:28 +02:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|