Merge pull request #10845 from poettering/static-destruct
RFC: introduce automatic destructors for static variables
This commit is contained in:
commit
52048013b7
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include <getopt.h>
|
||||
|
||||
#include "main-func.h"
|
||||
#include "util.h"
|
||||
|
||||
static bool arg_verbose = false;
|
||||
|
|
|
@ -506,23 +506,4 @@ static inline int __coverity_check__(int condition) {
|
|||
DEFINE_PUBLIC_TRIVIAL_REF_FUNC(type, name); \
|
||||
DEFINE_PUBLIC_TRIVIAL_UNREF_FUNC(type, name, free_func);
|
||||
|
||||
/* Negative return values from impl are mapped to EXIT_FAILURE, and
|
||||
* everything else means success! */
|
||||
#define DEFINE_MAIN_FUNCTION(impl) \
|
||||
int main(int argc, char *argv[]) { \
|
||||
int r; \
|
||||
r = impl(argc, argv); \
|
||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; \
|
||||
}
|
||||
|
||||
/* Zero is mapped to EXIT_SUCCESS, and both negative and positive values
|
||||
* are mapped to EXIT_FAILURE.
|
||||
* Note: this means "true" maps to EXIT_FAILURE. */
|
||||
#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl) \
|
||||
int main(int argc, char *argv[]) { \
|
||||
int r; \
|
||||
r = impl(argc, argv); \
|
||||
return r != 0 ? EXIT_FAILURE : EXIT_SUCCESS; \
|
||||
}
|
||||
|
||||
#include "log.h"
|
||||
|
|
27
src/basic/main-func.h
Normal file
27
src/basic/main-func.h
Normal file
|
@ -0,0 +1,27 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
#pragma once
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "static-destruct.h"
|
||||
|
||||
/* Negative return values from impl are mapped to EXIT_FAILURE, and
|
||||
* everything else means success! */
|
||||
#define DEFINE_MAIN_FUNCTION(impl) \
|
||||
int main(int argc, char *argv[]) { \
|
||||
int r; \
|
||||
r = impl(argc, argv); \
|
||||
static_destruct(); \
|
||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; \
|
||||
}
|
||||
|
||||
/* Zero is mapped to EXIT_SUCCESS, and both negative and positive values
|
||||
* are mapped to EXIT_FAILURE.
|
||||
* Note: this means "true" maps to EXIT_FAILURE. */
|
||||
#define DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(impl) \
|
||||
int main(int argc, char *argv[]) { \
|
||||
int r; \
|
||||
r = impl(argc, argv); \
|
||||
static_destruct(); \
|
||||
return r != 0 ? EXIT_FAILURE : EXIT_SUCCESS; \
|
||||
}
|
|
@ -183,6 +183,7 @@ basic_sources = files('''
|
|||
special.h
|
||||
stat-util.c
|
||||
stat-util.h
|
||||
static-destruct.h
|
||||
stdio-util.h
|
||||
strbuf.c
|
||||
strbuf.h
|
||||
|
|
51
src/basic/static-destruct.h
Normal file
51
src/basic/static-destruct.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#pragma once
|
||||
|
||||
#include "macro.h"
|
||||
|
||||
/* A framework for registering static variables that shall be freed on shutdown of a process. It's a bit like gcc's
|
||||
* destructor attribute, but allows us to precisely schedule when we want to free the variables. This is supposed to
|
||||
* feel a bit like the gcc cleanup attribute, but for static variables. Note that this does not work for static
|
||||
* variables declared in .so's, as the list is private to the same linking unit. But maybe that's a good thing. */
|
||||
|
||||
typedef struct StaticDestructor {
|
||||
void *data;
|
||||
void (*destroy)(void *p);
|
||||
} StaticDestructor;
|
||||
|
||||
#define STATIC_DESTRUCTOR_REGISTER(variable, func) \
|
||||
_STATIC_DESTRUCTOR_REGISTER(UNIQ, variable, func)
|
||||
|
||||
#define _STATIC_DESTRUCTOR_REGISTER(uq, variable, func) \
|
||||
/* Type-safe destructor */ \
|
||||
static void UNIQ_T(static_destructor_wrapper, uq)(void *p) { \
|
||||
typeof(variable) *q = p; \
|
||||
func(q); \
|
||||
} \
|
||||
/* The actual destructor structure */ \
|
||||
__attribute__ ((__section__("SYSTEMD_STATIC_DESTRUCT"))) \
|
||||
__attribute__ ((__aligned__(__BIGGEST_ALIGNMENT__))) \
|
||||
__attribute__ ((__used__)) \
|
||||
static const StaticDestructor UNIQ_T(static_destructor_entry, uq) = { \
|
||||
.data = &(variable), \
|
||||
.destroy = UNIQ_T(static_destructor_wrapper, uq), \
|
||||
}
|
||||
|
||||
/* Beginning and end of our section listing the destructors. We define these as weak as we want this to work even if
|
||||
* there's not a single destructor is defined in which case the section will be missing. */
|
||||
extern const struct StaticDestructor __attribute__((__weak__)) __start_SYSTEMD_STATIC_DESTRUCT[];
|
||||
extern const struct StaticDestructor __attribute__((__weak__)) __stop_SYSTEMD_STATIC_DESTRUCT[];
|
||||
|
||||
/* The function to destroy everything. (Note that this must be static inline, as it's key that it remains in the same
|
||||
* linking unit as the variables we want to destroy. */
|
||||
static inline void static_destruct(void) {
|
||||
const StaticDestructor *d;
|
||||
|
||||
if (!__start_SYSTEMD_STATIC_DESTRUCT)
|
||||
return;
|
||||
|
||||
d = ALIGN_TO_PTR(__start_SYSTEMD_STATIC_DESTRUCT, __BIGGEST_ALIGNMENT__);
|
||||
while (d < __stop_SYSTEMD_STATIC_DESTRUCT) {
|
||||
d->destroy(d->data);
|
||||
d = ALIGN_TO_PTR(d + 1, __BIGGEST_ALIGNMENT__);
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@
|
|||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "alloc-util.h"
|
||||
#include "bus-error.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "util.h"
|
||||
|
||||
static int help(void) {
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "hashmap.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
#include "journal-importer.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "main-func.h"
|
||||
#include "missing.h"
|
||||
#include "mkdir.h"
|
||||
#include "parse-util.h"
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1+ */
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "parse-util.h"
|
||||
#include "proc-cmdline.h"
|
||||
|
@ -16,6 +17,10 @@ static char **arg_mask = NULL;
|
|||
static char **arg_wants = NULL;
|
||||
static bool arg_debug_shell = false;
|
||||
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_default_unit, freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_mask, strv_freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_wants, strv_freep);
|
||||
|
||||
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
|
||||
int r;
|
||||
|
||||
|
@ -136,12 +141,12 @@ static int generate_wants_symlinks(void) {
|
|||
return r;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
static int run(int argc, char *argv[]) {
|
||||
int r, q;
|
||||
|
||||
if (argc > 1 && argc != 4) {
|
||||
log_error("This program takes three or no arguments.");
|
||||
return EXIT_FAILURE;
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
if (argc > 1)
|
||||
|
@ -160,22 +165,14 @@ int main(int argc, char *argv[]) {
|
|||
|
||||
if (arg_debug_shell) {
|
||||
r = strv_extend(&arg_wants, "debug-shell.service");
|
||||
if (r < 0) {
|
||||
r = log_oom();
|
||||
goto finish;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
r = generate_mask_symlinks();
|
||||
|
||||
q = generate_wants_symlinks();
|
||||
if (q < 0)
|
||||
r = q;
|
||||
|
||||
finish:
|
||||
arg_default_unit = mfree(arg_default_unit);
|
||||
strv_free(arg_wants);
|
||||
strv_free(arg_mask);
|
||||
|
||||
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
||||
return r < 0 ? r : q;
|
||||
}
|
||||
|
||||
DEFINE_MAIN_FUNCTION(run);
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "main-func.h"
|
||||
#include "string-table.h"
|
||||
#include "terminal-util.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
#include "terminal-util.h"
|
||||
|
|
|
@ -22,6 +22,7 @@
|
|||
#include "device-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "proc-cmdline.h"
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "bus-error.h"
|
||||
#include "bus-util.h"
|
||||
#include "hostname-util.h"
|
||||
#include "main-func.h"
|
||||
#include "spawn-polkit-agent.h"
|
||||
#include "terminal-util.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "hwdb-util.h"
|
||||
#include "main-func.h"
|
||||
#include "selinux-util.h"
|
||||
#include "terminal-util.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "id128-print.h"
|
||||
#include "main-func.h"
|
||||
#include "terminal-util.h"
|
||||
#include "util.h"
|
||||
#include "verbs.h"
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "hostname-util.h"
|
||||
#include "import-util.h"
|
||||
#include "machine-image.h"
|
||||
#include "main-func.h"
|
||||
#include "signal-util.h"
|
||||
#include "string-util.h"
|
||||
#include "verbs.h"
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "import-tar.h"
|
||||
#include "import-util.h"
|
||||
#include "machine-image.h"
|
||||
#include "main-func.h"
|
||||
#include "signal-util.h"
|
||||
#include "string-util.h"
|
||||
#include "verbs.h"
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include "hostname-util.h"
|
||||
#include "import-util.h"
|
||||
#include "machine-pool.h"
|
||||
#include "main-func.h"
|
||||
#include "missing.h"
|
||||
#include "mkdir.h"
|
||||
#include "parse-util.h"
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "hostname-util.h"
|
||||
#include "import-util.h"
|
||||
#include "machine-image.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "pull-raw.h"
|
||||
#include "pull-tar.h"
|
||||
|
|
|
@ -11,11 +11,12 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "fd-util.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "string-util.h"
|
||||
#include "syslog-util.h"
|
||||
#include "util.h"
|
||||
#include "terminal-util.h"
|
||||
#include "util.h"
|
||||
|
||||
static const char *arg_identifier = NULL;
|
||||
static int arg_priority = LOG_INFO;
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "fd-util.h"
|
||||
#include "format-table.h"
|
||||
#include "format-util.h"
|
||||
#include "main-func.h"
|
||||
#include "pager.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "format-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "logind.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "process-util.h"
|
||||
#include "selinux-util.h"
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include "bus-error.h"
|
||||
#include "fs-util.h"
|
||||
#include "label.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "mount-util.h"
|
||||
#include "path-util.h"
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
#include "label.h"
|
||||
#include "machine-image.h"
|
||||
#include "machined.h"
|
||||
#include "main-func.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
#include "special.h"
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "env-util.h"
|
||||
#include "format-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "string-util.h"
|
||||
#include "strv.h"
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "dissect-image.h"
|
||||
#include "main-func.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
#include "string-util.h"
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "alloc-util.h"
|
||||
#include "log.h"
|
||||
#include "macro.h"
|
||||
#include "main-func.h"
|
||||
#include "string-util.h"
|
||||
#include "terminal-util.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include "alloc-util.h"
|
||||
#include "bus-util.h"
|
||||
#include "def.h"
|
||||
#include "main-func.h"
|
||||
#include "portabled-bus.h"
|
||||
#include "portabled-image-bus.h"
|
||||
#include "portabled.h"
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
#include <sys/prctl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "main-func.h"
|
||||
#include "proc-cmdline.h"
|
||||
#include "process-util.h"
|
||||
#include "signal-util.h"
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
#include "fd-util.h"
|
||||
#include "io-util.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "string-util.h"
|
||||
#include "util.h"
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
|
||||
#include "exit-status.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "mount-setup.h"
|
||||
#include "mount-util.h"
|
||||
#include "path-util.h"
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
#include "fd-util.h"
|
||||
#include "fileio.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "parse-util.h"
|
||||
#include "sleep-config.h"
|
||||
#include "stdio-util.h"
|
||||
|
|
|
@ -11,10 +11,11 @@
|
|||
#include "sd-daemon.h"
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "build.h"
|
||||
#include "bus-internal.h"
|
||||
#include "bus-util.h"
|
||||
#include "build.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "util.h"
|
||||
|
||||
#define DEFAULT_BUS_PATH "unix:path=/run/dbus/system_bus_socket"
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
#include "hexdecoct.h"
|
||||
#include "install.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "path-lookup.h"
|
||||
#include "path-util.h"
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
#include "hashmap.h"
|
||||
#include "io-util.h"
|
||||
#include "macro.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "path-util.h"
|
||||
#include "process-util.h"
|
||||
|
|
|
@ -4,12 +4,13 @@
|
|||
|
||||
#include "alloc-util.h"
|
||||
#include "fs-util.h"
|
||||
#include "main-func.h"
|
||||
#include "mkdir.h"
|
||||
#include "mount-util.h"
|
||||
#include "stat-util.h"
|
||||
#include "volatile-util.h"
|
||||
#include "string-util.h"
|
||||
#include "path-util.h"
|
||||
#include "stat-util.h"
|
||||
#include "string-util.h"
|
||||
#include "volatile-util.h"
|
||||
|
||||
static int make_volatile(const char *path) {
|
||||
_cleanup_free_ char *old_usr = NULL;
|
||||
|
|
Loading…
Reference in a new issue