Merge pull request #10891 from yuwata/use-main-macro-2

tree-wide: use recently introduced macros
This commit is contained in:
Lennart Poettering
2018-11-23 00:46:02 +01:00
committed by GitHub
14 changed files with 256 additions and 359 deletions
+4 -1
View File
@@ -5,6 +5,7 @@
#include <string.h>
#include <unistd.h>
#include "bus-util.h"
#include "def.h"
#include "env-util.h"
#include "fd-util.h"
@@ -68,7 +69,7 @@ static void context_free_locale(Context *c) {
c->locale[p] = mfree(c->locale[p]);
}
void context_free(Context *c) {
void context_clear(Context *c) {
context_free_locale(c);
context_free_x11(c);
context_free_vconsole(c);
@@ -76,6 +77,8 @@ void context_free(Context *c) {
sd_bus_message_unref(c->locale_cache);
sd_bus_message_unref(c->x11_cache);
sd_bus_message_unref(c->vc_cache);
bus_verify_polkit_async_registry_free(c->polkit_registry);
};
void locale_simplify(char *locale[_VARIABLE_LC_MAX]) {
+4 -1
View File
@@ -3,6 +3,7 @@
#include "sd-bus.h"
#include "hashmap.h"
#include "locale-util.h"
#include "time-util.h"
@@ -22,6 +23,8 @@ typedef struct Context {
usec_t vc_mtime;
char *vc_keymap;
char *vc_keymap_toggle;
Hashmap *polkit_registry;
} Context;
int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap);
@@ -32,7 +35,7 @@ int locale_read_data(Context *c, sd_bus_message *m);
int vconsole_read_data(Context *c, sd_bus_message *m);
int x11_read_data(Context *c, sd_bus_message *m);
void context_free(Context *c);
void context_clear(Context *c);
int vconsole_convert_to_x11(Context *c);
int vconsole_write_data(Context *c);
int x11_convert_to_vconsole(Context *c);
+19 -30
View File
@@ -19,6 +19,7 @@
#include "keymap-util.h"
#include "locale-util.h"
#include "macro.h"
#include "main-func.h"
#include "path-util.h"
#include "selinux-util.h"
#include "signal-util.h"
@@ -26,8 +27,6 @@
#include "strv.h"
#include "user-util.h"
static Hashmap *polkit_registry = NULL;
static int locale_update_system_manager(Context *c, sd_bus *bus) {
_cleanup_free_ char **l_unset = NULL;
_cleanup_strv_free_ char **l_set = NULL;
@@ -362,7 +361,7 @@ static int method_set_locale(sd_bus_message *m, void *userdata, sd_bus_error *er
NULL,
interactive,
UID_INVALID,
&polkit_registry,
&c->polkit_registry,
error);
if (r < 0)
return r;
@@ -433,7 +432,7 @@ static int method_set_vc_keyboard(sd_bus_message *m, void *userdata, sd_bus_erro
NULL,
interactive,
UID_INVALID,
&polkit_registry,
&c->polkit_registry,
error);
if (r < 0)
return r;
@@ -626,7 +625,7 @@ static int method_set_x11_keyboard(sd_bus_message *m, void *userdata, sd_bus_err
NULL,
interactive,
UID_INVALID,
&polkit_registry,
&c->polkit_registry,
error);
if (r < 0)
return r;
@@ -710,8 +709,8 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return 0;
}
int main(int argc, char *argv[]) {
_cleanup_(context_free) Context context = {
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context context = {
.locale_mtime = USEC_INFINITY,
.vc_mtime = USEC_INFINITY,
.x11_mtime = USEC_INFINITY,
@@ -725,44 +724,34 @@ int main(int argc, char *argv[]) {
umask(0022);
mac_selinux_init();
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
if (argc != 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
r = sd_event_default(&event);
if (r < 0) {
log_error_errno(r, "Failed to allocate event loop: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
(void) sd_event_set_watchdog(event, true);
r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to install SIGINT handler: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to install SIGINT handler: %m");
r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to install SIGTERM handler: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to install SIGTERM handler: %m");
r = connect_bus(&context, event, &bus);
if (r < 0)
goto finish;
return r;
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0)
log_error_errno(r, "Failed to run event loop: %m");
return log_error_errno(r, "Failed to run event loop: %m");
finish:
bus_verify_polkit_async_registry_free(polkit_registry);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+2 -2
View File
@@ -65,7 +65,7 @@ static void test_find_legacy_keymap(void) {
}
static void test_vconsole_convert_to_x11(void) {
_cleanup_(context_free) Context c = {};
_cleanup_(context_clear) Context c = {};
log_info("/*** %s ***/", __func__);
@@ -119,7 +119,7 @@ static void test_vconsole_convert_to_x11(void) {
}
static void test_x11_convert_to_vconsole(void) {
_cleanup_(context_free) Context c = {};
_cleanup_(context_clear) Context c = {};
int r;
log_info("/*** %s ***/", __func__);
+15 -22
View File
@@ -15,6 +15,7 @@
#include "escape.h"
#include "gcrypt-util.h"
#include "in-addr-util.h"
#include "main-func.h"
#include "netlink-util.h"
#include "pager.h"
#include "parse-util.h"
@@ -56,6 +57,11 @@ static const char *arg_set_dns_over_tls = NULL;
static const char *arg_set_dnssec = NULL;
static char **arg_set_nta = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_ifname, freep);
STATIC_DESTRUCTOR_REGISTER(arg_set_dns, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_set_domain, strv_freep);
STATIC_DESTRUCTOR_REGISTER(arg_set_nta, strv_freep);
typedef enum StatusMode {
STATUS_ALL,
STATUS_DNS,
@@ -3064,8 +3070,8 @@ static int compat_main(int argc, char *argv[], sd_bus *bus) {
return 0;
}
int main(int argc, char **argv) {
sd_bus *bus = NULL;
static int run(int argc, char **argv) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
setlocale(LC_ALL, "");
@@ -3079,29 +3085,16 @@ int main(int argc, char **argv) {
else
r = native_parse_argv(argc, argv);
if (r <= 0)
goto finish;
return r;
r = sd_bus_open_system(&bus);
if (r < 0) {
log_error_errno(r, "sd_bus_open_system: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "sd_bus_open_system: %m");
if (STR_IN_SET(program_invocation_short_name, "systemd-resolve", "resolvconf"))
r = compat_main(argc, argv, bus);
else
r = native_main(argc, argv, bus);
return compat_main(argc, argv, bus);
finish:
/* make sure we terminate the bus connection first, and then close the
* pager, see issue #3543 for the details. */
sd_bus_flush_close_unref(bus);
pager_close();
free(arg_ifname);
strv_free(arg_set_dns);
strv_free(arg_set_domain);
strv_free(arg_set_nta);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return native_main(argc, argv, bus);
}
DEFINE_MAIN_FUNCTION(run);
+74 -101
View File
@@ -12,6 +12,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "io-util.h"
#include "main-func.h"
#include "mkdir.h"
#include "parse-util.h"
#include "proc-cmdline.h"
@@ -31,6 +32,11 @@ typedef struct write_queue_item {
int state;
} write_queue_item;
typedef struct Context {
LIST_HEAD(write_queue_item, write_queue);
int rfkill_fd;
} Context;
static struct write_queue_item* write_queue_item_free(struct write_queue_item *item) {
if (!item)
return NULL;
@@ -205,16 +211,14 @@ static int determine_state_file(
return 0;
}
static int load_state(
int rfkill_fd,
const struct rfkill_event *event) {
static int load_state(Context *c, const struct rfkill_event *event) {
_cleanup_free_ char *state_file = NULL, *value = NULL;
struct rfkill_event we;
ssize_t l;
int b, r;
assert(rfkill_fd >= 0);
assert(c);
assert(c->rfkill_fd >= 0);
assert(event);
if (shall_restore_state() == 0)
@@ -248,7 +252,7 @@ static int load_state(
.soft = b,
};
l = write(rfkill_fd, &we, sizeof(we));
l = write(c->rfkill_fd, &we, sizeof(we));
if (l < 0)
return log_error_errno(errno, "Failed to restore rfkill state for %i: %m", event->idx);
if (l != sizeof(we))
@@ -259,39 +263,34 @@ static int load_state(
return 0;
}
static void save_state_queue_remove(
struct write_queue_item **write_queue,
int idx,
char *state_file) {
static void save_state_queue_remove(Context *c, int idx, const char *state_file) {
struct write_queue_item *item, *tmp;
LIST_FOREACH_SAFE(queue, item, tmp, *write_queue) {
assert(c);
LIST_FOREACH_SAFE(queue, item, tmp, c->write_queue) {
if ((state_file && streq(item->file, state_file)) || idx == item->rfkill_idx) {
log_debug("Canceled previous save state of '%s' to %s.", one_zero(item->state), item->file);
LIST_REMOVE(queue, *write_queue, item);
LIST_REMOVE(queue, c->write_queue, item);
write_queue_item_free(item);
}
}
}
static int save_state_queue(
struct write_queue_item **write_queue,
int rfkill_fd,
const struct rfkill_event *event) {
static int save_state_queue(Context *c, const struct rfkill_event *event) {
_cleanup_free_ char *state_file = NULL;
struct write_queue_item *item;
int r;
assert(rfkill_fd >= 0);
assert(c);
assert(c->rfkill_fd >= 0);
assert(event);
r = determine_state_file(event, &state_file);
if (r < 0)
return r;
save_state_queue_remove(write_queue, event->idx, state_file);
save_state_queue_remove(c, event->idx, state_file);
item = new0(struct write_queue_item, 1);
if (!item)
@@ -301,108 +300,90 @@ static int save_state_queue(
item->rfkill_idx = event->idx;
item->state = event->soft;
LIST_APPEND(queue, *write_queue, item);
LIST_APPEND(queue, c->write_queue, item);
return 0;
}
static int save_state_cancel(
struct write_queue_item **write_queue,
int rfkill_fd,
const struct rfkill_event *event) {
static int save_state_cancel(Context *c, const struct rfkill_event *event) {
_cleanup_free_ char *state_file = NULL;
int r;
assert(rfkill_fd >= 0);
assert(c);
assert(c->rfkill_fd >= 0);
assert(event);
r = determine_state_file(event, &state_file);
save_state_queue_remove(write_queue, event->idx, state_file);
save_state_queue_remove(c, event->idx, state_file);
if (r < 0)
return r;
return 0;
}
static int save_state_write(struct write_queue_item **write_queue) {
struct write_queue_item *item, *tmp;
int result = 0;
bool error_logged = false;
static int save_state_write_one(struct write_queue_item *item) {
int r;
LIST_FOREACH_SAFE(queue, item, tmp, *write_queue) {
r = write_string_file(item->file, one_zero(item->state), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
if (r < 0) {
result = r;
if (!error_logged) {
log_error_errno(r, "Failed to write state file %s: %m", item->file);
error_logged = true;
} else
log_warning_errno(r, "Failed to write state file %s: %m", item->file);
} else
log_debug("Saved state '%s' to %s.", one_zero(item->state), item->file);
r = write_string_file(item->file, one_zero(item->state), WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
if (r < 0)
return log_error_errno(r, "Failed to write state file %s: %m", item->file);
LIST_REMOVE(queue, *write_queue, item);
write_queue_item_free(item);
}
return result;
log_debug("Saved state '%s' to %s.", one_zero(item->state), item->file);
return 0;
}
int main(int argc, char *argv[]) {
LIST_HEAD(write_queue_item, write_queue);
_cleanup_close_ int rfkill_fd = -1;
static void context_save_and_clear(Context *c) {
struct write_queue_item *i;
assert(c);
while ((i = c->write_queue)) {
LIST_REMOVE(queue, c->write_queue, i);
(void) save_state_write_one(i);
write_queue_item_free(i);
}
safe_close(c->rfkill_fd);
}
static int run(int argc, char *argv[]) {
_cleanup_(context_save_and_clear) Context c = { .rfkill_fd = -1 };
bool ready = false;
int r, n;
if (argc > 1) {
log_error("This program requires no arguments.");
return EXIT_FAILURE;
}
LIST_HEAD_INIT(write_queue);
if (argc > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires no arguments.");
log_setup_service();
umask(0022);
r = mkdir_p("/var/lib/systemd/rfkill", 0755);
if (r < 0) {
log_error_errno(r, "Failed to create rfkill directory: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to create rfkill directory: %m");
n = sd_listen_fds(false);
if (n < 0) {
r = log_error_errno(n, "Failed to determine whether we got any file descriptors passed: %m");
goto finish;
}
if (n > 1) {
log_error("Got too many file descriptors.");
r = -EINVAL;
goto finish;
}
if (n < 0)
return log_error_errno(n, "Failed to determine whether we got any file descriptors passed: %m");
if (n > 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Got too many file descriptors.");
if (n == 0) {
rfkill_fd = open("/dev/rfkill", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (rfkill_fd < 0) {
c.rfkill_fd = open("/dev/rfkill", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
if (c.rfkill_fd < 0) {
if (errno == ENOENT) {
log_debug_errno(errno, "Missing rfkill subsystem, or no device present, exiting.");
r = 0;
goto finish;
return 0;
}
r = log_error_errno(errno, "Failed to open /dev/rfkill: %m");
goto finish;
return log_error_errno(errno, "Failed to open /dev/rfkill: %m");
}
} else {
rfkill_fd = SD_LISTEN_FDS_START;
c.rfkill_fd = SD_LISTEN_FDS_START;
r = fd_nonblock(rfkill_fd, 1);
if (r < 0) {
log_error_errno(r, "Failed to make /dev/rfkill socket non-blocking: %m");
goto finish;
}
r = fd_nonblock(c.rfkill_fd, 1);
if (r < 0)
return log_error_errno(r, "Failed to make /dev/rfkill socket non-blocking: %m");
}
for (;;) {
@@ -410,7 +391,7 @@ int main(int argc, char *argv[]) {
const char *type;
ssize_t l;
l = read(rfkill_fd, &event, sizeof(event));
l = read(c.rfkill_fd, &event, sizeof(event));
if (l < 0) {
if (errno == EAGAIN) {
@@ -425,13 +406,11 @@ int main(int argc, char *argv[]) {
/* Hang around for a bit, maybe there's more coming */
r = fd_wait_for_event(rfkill_fd, POLLIN, EXIT_USEC);
r = fd_wait_for_event(c.rfkill_fd, POLLIN, EXIT_USEC);
if (r == -EINTR)
continue;
if (r < 0) {
log_error_errno(r, "Failed to poll() on device: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to poll() on device: %m");
if (r > 0)
continue;
@@ -442,11 +421,8 @@ int main(int argc, char *argv[]) {
log_error_errno(errno, "Failed to read from /dev/rfkill: %m");
}
if (l != RFKILL_EVENT_SIZE_V1) {
log_error("Read event structure of invalid size.");
r = -EIO;
goto finish;
}
if (l != RFKILL_EVENT_SIZE_V1)
return log_error_errno(SYNTHETIC_ERRNO(EIO), "Read event structure of invalid size.");
type = rfkill_type_to_string(event.type);
if (!type) {
@@ -458,17 +434,17 @@ int main(int argc, char *argv[]) {
case RFKILL_OP_ADD:
log_debug("A new rfkill device has been added with index %i and type %s.", event.idx, type);
(void) load_state(rfkill_fd, &event);
(void) load_state(&c, &event);
break;
case RFKILL_OP_DEL:
log_debug("An rfkill device has been removed with index %i and type %s", event.idx, type);
(void) save_state_cancel(&write_queue, rfkill_fd, &event);
(void) save_state_cancel(&c, &event);
break;
case RFKILL_OP_CHANGE:
log_debug("An rfkill device has changed state with index %i and type %s", event.idx, type);
(void) save_state_queue(&write_queue, rfkill_fd, &event);
(void) save_state_queue(&c, &event);
break;
default:
@@ -477,10 +453,7 @@ int main(int argc, char *argv[]) {
}
}
r = 0;
finish:
(void) save_state_write(&write_queue);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+24 -35
View File
@@ -18,6 +18,7 @@
#include "alloc-util.h"
#include "fd-util.h"
#include "log.h"
#include "main-func.h"
#include "parse-util.h"
#include "path-util.h"
#include "pretty-print.h"
@@ -74,7 +75,7 @@ static void connection_free(Connection *c) {
free(c);
}
static void context_free(Context *context) {
static void context_clear(Context *context) {
assert(context);
set_free_with_destructor(context->listen, sd_event_source_unref);
@@ -616,8 +617,8 @@ static int parse_argv(int argc, char *argv[]) {
return 1;
}
int main(int argc, char *argv[]) {
Context context = {};
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context context = {};
int r, n, fd;
log_parse_environment();
@@ -625,53 +626,41 @@ int main(int argc, char *argv[]) {
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
return r;
r = sd_event_default(&context.event);
if (r < 0) {
log_error_errno(r, "Failed to allocate event loop: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
r = sd_resolve_default(&context.resolve);
if (r < 0) {
log_error_errno(r, "Failed to allocate resolver: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to allocate resolver: %m");
r = sd_resolve_attach_event(context.resolve, context.event, 0);
if (r < 0) {
log_error_errno(r, "Failed to attach resolver: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to attach resolver: %m");
sd_event_set_watchdog(context.event, true);
n = sd_listen_fds(1);
if (n < 0) {
log_error("Failed to receive sockets from parent.");
r = n;
goto finish;
} else if (n == 0) {
log_error("Didn't get any sockets passed in.");
r = -EINVAL;
goto finish;
}
r = sd_listen_fds(1);
if (r < 0)
return log_error_errno(r, "Failed to receive sockets from parent.");
if (r == 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Didn't get any sockets passed in.");
n = r;
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
r = add_listen_socket(&context, fd);
if (r < 0)
goto finish;
return r;
}
r = sd_event_loop(context.event);
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
finish:
context_free(&context);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+4 -4
View File
@@ -1922,9 +1922,9 @@ static int read_config_files(char **args) {
int main(int argc, char *argv[]) {
_cleanup_close_ int lock = -1;
Iterator iterator;
int r;
char *n, **v;
Item *i;
char *n;
int r;
r = parse_argv(argc, argv);
if (r <= 0)
@@ -2015,8 +2015,8 @@ finish:
ordered_hashmap_free_with_destructor(groups, item_free);
ordered_hashmap_free_with_destructor(users, item_free);
while ((n = ordered_hashmap_first_key(members))) {
strv_free(ordered_hashmap_steal_first(members));
while ((v = ordered_hashmap_steal_first_key_and_value(members, (void **) &n))) {
strv_free(v);
free(n);
}
ordered_hashmap_free(members);
+23 -34
View File
@@ -22,6 +22,7 @@
#include "fd-util.h"
#include "fs-util.h"
#include "main-func.h"
#include "missing.h"
#include "signal-util.h"
#include "time-util.h"
@@ -185,61 +186,49 @@ static int clock_state_update(
return r;
}
int main(int argc, char * argv[]) {
int r;
static int run(int argc, char * argv[]) {
_cleanup_(sd_event_unrefp) sd_event *event;
ClockState state = {
_cleanup_(clock_state_release) ClockState state = {
.timerfd_fd = -1,
.inotify_fd = -1,
.run_systemd_wd = -1,
.run_systemd_timesync_wd = -1,
};
int r;
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
r = sd_event_default(&event);
if (r < 0) {
log_error_errno(r, "Failed to allocate event loop: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to create sigterm event source: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to create sigterm event source: %m");
r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to create sigint event source: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to create sigint event source: %m");
r = sd_event_set_watchdog(event, true);
if (r < 0) {
log_error_errno(r, "Failed to create watchdog event source: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to create watchdog event source: %m");
r = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if (r < 0) {
log_error_errno(errno, "Failed to create inotify descriptor: %m");
goto finish;
}
if (r < 0)
return log_error_errno(errno, "Failed to create inotify descriptor: %m");
state.inotify_fd = r;
r = sd_event_add_io(event, &state.inotify_event_source, state.inotify_fd,
EPOLLIN, inotify_handler, &state);
if (r < 0) {
log_error_errno(r, "Failed to create notify event source: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to create notify event source: %m");
r = inotify_add_watch(state.inotify_fd, "/run/systemd/", IN_CREATE);
if (r < 0) {
log_error_errno(errno, "Failed to watch /run/systemd/: %m");
goto finish;
}
if (r < 0)
return log_error_errno(errno, "Failed to watch /run/systemd/: %m");
state.run_systemd_wd = r;
(void) update_notify_run_systemd_timesync(&state);
@@ -257,7 +246,7 @@ int main(int argc, char * argv[]) {
if (state.adjtime_state == TIME_ERROR)
log_info("Exit without adjtimex synchronized.");
finish:
clock_state_release(&state);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return r;
}
DEFINE_MAIN_FUNCTION(run);
+21 -34
View File
@@ -18,6 +18,7 @@
#include "fs-util.h"
#include "hashmap.h"
#include "list.h"
#include "main-func.h"
#include "path-util.h"
#include "selinux-util.h"
#include "signal-util.h"
@@ -69,7 +70,7 @@ static void unit_status_info_free(UnitStatusInfo *p) {
free(p);
}
static void context_free(Context *c) {
static void context_clear(Context *c) {
UnitStatusInfo *p;
assert(c);
@@ -958,8 +959,8 @@ static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
return 0;
}
int main(int argc, char *argv[]) {
Context context = {};
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context context = {};
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
@@ -968,58 +969,44 @@ int main(int argc, char *argv[]) {
umask(0022);
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
if (argc != 1)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, -1) >= 0);
r = sd_event_default(&event);
if (r < 0) {
log_error_errno(r, "Failed to allocate event loop: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
(void) sd_event_set_watchdog(event, true);
r = sd_event_add_signal(event, NULL, SIGINT, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to install SIGINT handler: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to install SIGINT handler: %m");
r = sd_event_add_signal(event, NULL, SIGTERM, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to install SIGTERM handler: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to install SIGTERM handler: %m");
r = connect_bus(&context, event, &bus);
if (r < 0)
goto finish;
return r;
(void) sd_bus_negotiate_timestamp(bus, true);
r = context_read_data(&context);
if (r < 0) {
log_error_errno(r, "Failed to read time zone data: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to read time zone data: %m");
r = context_parse_ntp_services(&context);
if (r < 0)
goto finish;
return r;
r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
finish:
context_free(&context);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+1 -2
View File
@@ -47,12 +47,11 @@ int main(int argc, char *argv[]) {
r = mac_selinux_init();
if (r < 0) {
log_error_errno(r, "SELinux setup failed: %m");
goto finish;
return EXIT_FAILURE;
}
r = apply_timestamp("/etc/.updated", &st.st_mtim);
q = apply_timestamp("/var/.updated", &st.st_mtim);
finish:
return r < 0 || q < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
+14 -11
View File
@@ -30,6 +30,17 @@ typedef struct Context {
#endif
} Context;
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
}
static usec_t get_startup_time(Context *c) {
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
usec_t t = 0;
@@ -205,7 +216,7 @@ static int on_runlevel(Context *c) {
}
int main(int argc, char *argv[]) {
Context c = {
_cleanup_(context_clear) Context c = {
#if HAVE_AUDIT
.audit_fd = -1
#endif
@@ -236,8 +247,7 @@ int main(int argc, char *argv[]) {
r = bus_connect_system_systemd(&c.bus);
if (r < 0) {
log_error_errno(r, "Failed to get D-Bus connection: %m");
r = -EIO;
goto finish;
return EXIT_FAILURE;
}
log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
@@ -250,17 +260,10 @@ int main(int argc, char *argv[]) {
r = on_runlevel(&c);
else {
log_error("Unknown command %s", argv[1]);
r = -EINVAL;
return EXIT_FAILURE;
}
log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
finish:
#if HAVE_AUDIT
if (c.audit_fd >= 0)
audit_close(c.audit_fd);
#endif
sd_bus_flush_close_unref(c.bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
+18 -24
View File
@@ -13,6 +13,7 @@
#include "generator.h"
#include "hexdecoct.h"
#include "id128-util.h"
#include "main-func.h"
#include "mkdir.h"
#include "parse-util.h"
#include "proc-cmdline.h"
@@ -22,12 +23,16 @@
#define SYSTEMD_VERITYSETUP_SERVICE "systemd-veritysetup@root.service"
static char *arg_dest = NULL;
static const char *arg_dest = NULL;
static bool arg_enabled = true;
static char *arg_root_hash = NULL;
static char *arg_data_what = NULL;
static char *arg_hash_what = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
STATIC_DESTRUCTOR_REGISTER(arg_data_what, freep);
STATIC_DESTRUCTOR_REGISTER(arg_hash_what, freep);
static int create_device(void) {
_cleanup_free_ char *u = NULL, *v = NULL, *d = NULL, *e = NULL, *u_escaped = NULL, *v_escaped = NULL, *root_hash_escaped = NULL;
_cleanup_fclose_ FILE *f = NULL;
@@ -199,13 +204,11 @@ static int determine_devices(void) {
return 1;
}
int main(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
int r;
if (argc > 1 && argc != 4) {
log_error("This program takes three or no arguments.");
return EXIT_FAILURE;
}
if (argc > 1 && argc != 4)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes three or no arguments.");
if (argc > 1)
arg_dest = argv[1];
@@ -213,33 +216,24 @@ int main(int argc, char *argv[]) {
log_setup_generator();
r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
if (r < 0) {
log_warning_errno(r, "Failed to parse kernel command line: %m");
goto finish;
}
if (r < 0)
return log_warning_errno(r, "Failed to parse kernel command line: %m");
/* For now we only support the root device on verity. Later on we might want to add support for /etc/veritytab
* or similar to define additional mappings */
if (!arg_enabled) {
r = 0;
goto finish;
}
if (!arg_enabled)
return 0;
r = determine_devices();
if (r < 0)
goto finish;
return r;
r = create_device();
if (r < 0)
goto finish;
return r;
r = 0;
finish:
free(arg_root_hash);
free(arg_data_what);
free(arg_hash_what);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return 0;
}
DEFINE_MAIN_FUNCTION(run);
+33 -58
View File
@@ -8,6 +8,7 @@
#include "crypt-util.h"
#include "hexdecoct.h"
#include "log.h"
#include "main-func.h"
#include "pretty-print.h"
#include "string-util.h"
#include "terminal-util.h"
@@ -16,6 +17,10 @@ static char *arg_root_hash = NULL;
static char *arg_data_what = NULL;
static char *arg_hash_what = NULL;
STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
STATIC_DESTRUCTOR_REGISTER(arg_data_what, freep);
STATIC_DESTRUCTOR_REGISTER(arg_hash_what, freep);
static int help(void) {
_cleanup_free_ char *link = NULL;
int r;
@@ -36,20 +41,15 @@ static int help(void) {
return 0;
}
int main(int argc, char *argv[]) {
static int run(int argc, char *argv[]) {
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
int r;
if (argc <= 1) {
r = help();
goto finish;
}
if (argc <= 1)
return help();
if (argc < 3) {
log_error("This program requires at least two arguments.");
r = -EINVAL;
goto finish;
}
if (argc < 3)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires at least two arguments.");
log_setup_service();
@@ -60,82 +60,57 @@ int main(int argc, char *argv[]) {
crypt_status_info status;
size_t l;
if (argc < 6) {
log_error("attach requires at least two arguments.");
r = -EINVAL;
goto finish;
}
if (argc < 6)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "attach requires at least two arguments.");
r = unhexmem(argv[5], strlen(argv[5]), &m, &l);
if (r < 0) {
log_error("Failed to parse root hash.");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to parse root hash: %m");
r = crypt_init(&cd, argv[4]);
if (r < 0) {
log_error_errno(r, "Failed to open verity device %s: %m", argv[4]);
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to open verity device %s: %m", argv[4]);
crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
status = crypt_status(cd, argv[2]);
if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
log_info("Volume %s already active.", argv[2]);
r = 0;
goto finish;
return 0;
}
r = crypt_load(cd, CRYPT_VERITY, NULL);
if (r < 0) {
log_error_errno(r, "Failed to load verity superblock: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to load verity superblock: %m");
r = crypt_set_data_device(cd, argv[3]);
if (r < 0) {
log_error_errno(r, "Failed to configure data device: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to configure data device: %m");
r = crypt_activate_by_volume_key(cd, argv[2], m, l, CRYPT_ACTIVATE_READONLY);
if (r < 0) {
log_error_errno(r, "Failed to set up verity device: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to set up verity device: %m");
} else if (streq(argv[1], "detach")) {
r = crypt_init_by_name(&cd, argv[2]);
if (r == -ENODEV) {
log_info("Volume %s already inactive.", argv[2]);
goto finish;
} else if (r < 0) {
log_error_errno(r, "crypt_init_by_name() failed: %m");
goto finish;
return 0;
}
if (r < 0)
return log_error_errno(r, "crypt_init_by_name() failed: %m");
crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
r = crypt_deactivate(cd, argv[2]);
if (r < 0) {
log_error_errno(r, "Failed to deactivate: %m");
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to deactivate: %m");
} else {
log_error("Unknown verb %s.", argv[1]);
r = -EINVAL;
goto finish;
}
} else
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown verb %s.", argv[1]);
r = 0;
finish:
free(arg_root_hash);
free(arg_data_what);
free(arg_hash_what);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return 0;
}
DEFINE_MAIN_FUNCTION(run);