1
0
mirror of https://github.com/systemd/systemd synced 2024-07-09 04:26:06 +00:00

Merge pull request #27424 from dtardon/auto-cleanup

More automatic cleanup
This commit is contained in:
Yu Watanabe 2023-04-28 18:46:36 +09:00 committed by GitHub
commit 75fd8ad008
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 106 additions and 112 deletions

View File

@ -136,20 +136,20 @@ bool strv_env_name_or_assignment_is_valid(char **l) {
return true;
}
static int env_append(char **r, char ***k, char **a) {
assert(r);
static int env_append(char **e, char ***k, char **a) {
assert(e);
assert(k);
assert(*k >= r);
assert(*k >= e);
if (!a)
return 0;
/* Expects the following arguments: 'r' shall point to the beginning of an strv we are going to append to, 'k'
/* Expects the following arguments: 'e' shall point to the beginning of an strv we are going to append to, 'k'
* to a pointer pointing to the NULL entry at the end of the same array. 'a' shall point to another strv.
*
* This call adds every entry of 'a' to 'r', either overriding an existing matching entry, or appending to it.
* This call adds every entry of 'a' to 'e', either overriding an existing matching entry, or appending to it.
*
* This call assumes 'r' has enough pre-allocated space to grow by all of 'a''s items. */
* This call assumes 'e' has enough pre-allocated space to grow by all of 'a''s items. */
for (; *a; a++) {
char **j, *c;
@ -159,7 +159,7 @@ static int env_append(char **r, char ***k, char **a) {
if ((*a)[n] == '=')
n++;
for (j = r; j < *k; j++)
for (j = e; j < *k; j++)
if (strneq(*j, *a, n))
break;
@ -266,7 +266,7 @@ static bool env_entry_has_name(const char *entry, const char *name) {
char **strv_env_delete(char **x, size_t n_lists, ...) {
size_t n, i = 0;
char **r;
_cleanup_strv_free_ char **t = NULL;
va_list ap;
/* Deletes every entry from x that is mentioned in the other
@ -274,8 +274,8 @@ char **strv_env_delete(char **x, size_t n_lists, ...) {
n = strv_length(x);
r = new(char*, n+1);
if (!r)
t = new(char*, n+1);
if (!t)
return NULL;
STRV_FOREACH(k, x) {
@ -290,11 +290,9 @@ char **strv_env_delete(char **x, size_t n_lists, ...) {
}
va_end(ap);
r[i] = strdup(*k);
if (!r[i]) {
strv_free(r);
t[i] = strdup(*k);
if (!t[i])
return NULL;
}
i++;
continue;
@ -303,11 +301,11 @@ char **strv_env_delete(char **x, size_t n_lists, ...) {
va_end(ap);
}
r[i] = NULL;
t[i] = NULL;
assert(i <= n);
return r;
return TAKE_PTR(t);
}
char **strv_env_unset(char **l, const char *p) {
@ -588,7 +586,7 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
const char *e, *word = format, *test_value = NULL; /* test_value is initialized to appease gcc */
char *k;
_cleanup_free_ char *r = NULL;
_cleanup_free_ char *s = NULL;
size_t i, len = 0; /* len is initialized to appease gcc */
int nest = 0;
@ -604,31 +602,31 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
case CURLY:
if (*e == '{') {
k = strnappend(r, word, e-word-1);
k = strnappend(s, word, e-word-1);
if (!k)
return NULL;
free_and_replace(r, k);
free_and_replace(s, k);
word = e-1;
state = VARIABLE;
nest++;
} else if (*e == '$') {
k = strnappend(r, word, e-word);
k = strnappend(s, word, e-word);
if (!k)
return NULL;
free_and_replace(r, k);
free_and_replace(s, k);
word = e+1;
state = WORD;
} else if (flags & REPLACE_ENV_ALLOW_BRACELESS && strchr(VALID_BASH_ENV_NAME_CHARS, *e)) {
k = strnappend(r, word, e-word-1);
k = strnappend(s, word, e-word-1);
if (!k)
return NULL;
free_and_replace(r, k);
free_and_replace(s, k);
word = e-1;
state = VARIABLE_RAW;
@ -643,7 +641,7 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
t = strv_env_get_n(env, word+2, e-word-2, flags);
if (!strextend(&r, t))
if (!strextend(&s, t))
return NULL;
word = e+1;
@ -696,7 +694,7 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
else if (!t && state == DEFAULT_VALUE)
t = v = replace_env_n(test_value, e-test_value, env, flags);
if (!strextend(&r, t))
if (!strextend(&s, t))
return NULL;
word = e+1;
@ -712,7 +710,7 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
t = strv_env_get_n(env, word+1, e-word-1, flags);
if (!strextend(&r, t))
if (!strextend(&s, t))
return NULL;
word = e--;
@ -728,13 +726,13 @@ char *replace_env_n(const char *format, size_t n, char **env, unsigned flags) {
assert(flags & REPLACE_ENV_ALLOW_BRACELESS);
t = strv_env_get_n(env, word+1, e-word-1, flags);
return strjoin(r, t);
return strjoin(s, t);
} else
return strnappend(r, word, e-word);
return strnappend(s, word, e-word);
}
char **replace_env_argv(char **argv, char **env) {
char **ret;
_cleanup_strv_free_ char **ret = NULL;
size_t k = 0, l = 0;
l = strv_length(argv);
@ -748,7 +746,8 @@ char **replace_env_argv(char **argv, char **env) {
/* If $FOO appears as single word, replace it by the split up variable */
if ((*i)[0] == '$' && !IN_SET((*i)[1], '{', '$')) {
char *e;
char **w, **m = NULL;
char **w;
_cleanup_strv_free_ char **m = NULL;
size_t q;
e = strv_env_get(env, *i+1);
@ -758,11 +757,9 @@ char **replace_env_argv(char **argv, char **env) {
r = strv_split_full(&m, e, WHITESPACE, EXTRACT_RELAX|EXTRACT_UNQUOTE);
if (r < 0) {
ret[k] = NULL;
strv_free(ret);
return NULL;
}
} else
m = NULL;
}
q = strv_length(m);
l = l + q - 1;
@ -770,15 +767,13 @@ char **replace_env_argv(char **argv, char **env) {
w = reallocarray(ret, l + 1, sizeof(char *));
if (!w) {
ret[k] = NULL;
strv_free(ret);
strv_free(m);
return NULL;
}
ret = w;
if (m) {
memcpy(ret + k, m, q * sizeof(char*));
free(m);
m = mfree(m);
}
k += q;
@ -787,15 +782,13 @@ char **replace_env_argv(char **argv, char **env) {
/* If ${FOO} appears as part of a word, replace it by the variable as-is */
ret[k] = replace_env(*i, env, 0);
if (!ret[k]) {
strv_free(ret);
if (!ret[k])
return NULL;
}
k++;
}
ret[k] = NULL;
return ret;
return TAKE_PTR(ret);
}
int getenv_bool(const char *p) {

View File

@ -22,7 +22,7 @@
#include "time-util.h"
int path_split_and_make_absolute(const char *p, char ***ret) {
char **l;
_cleanup_strv_free_ char **l = NULL;
int r;
assert(p);
@ -33,12 +33,10 @@ int path_split_and_make_absolute(const char *p, char ***ret) {
return -ENOMEM;
r = path_strv_make_absolute_cwd(l);
if (r < 0) {
strv_free(l);
if (r < 0)
return r;
}
*ret = l;
*ret = TAKE_PTR(l);
return r;
}

View File

@ -2019,7 +2019,7 @@ int manager_add_job(
sd_bus_error *error,
Job **ret) {
Transaction *tr;
_cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
int r;
assert(m);
@ -2048,23 +2048,23 @@ int manager_add_job(
IN_SET(mode, JOB_IGNORE_DEPENDENCIES, JOB_IGNORE_REQUIREMENTS),
mode == JOB_IGNORE_DEPENDENCIES, error);
if (r < 0)
goto tr_abort;
return r;
if (mode == JOB_ISOLATE) {
r = transaction_add_isolate_jobs(tr, m);
if (r < 0)
goto tr_abort;
return r;
}
if (mode == JOB_TRIGGERING) {
r = transaction_add_triggering_jobs(tr, unit);
if (r < 0)
goto tr_abort;
return r;
}
r = transaction_activate(tr, m, mode, affected_jobs, error);
if (r < 0)
goto tr_abort;
return r;
log_unit_debug(unit,
"Enqueued job %s/%s as %u", unit->id,
@ -2073,13 +2073,8 @@ int manager_add_job(
if (ret)
*ret = tr->anchor_job;
transaction_free(tr);
tr = transaction_free(tr);
return 0;
tr_abort:
transaction_abort(tr);
transaction_free(tr);
return r;
}
int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, Set *affected_jobs, sd_bus_error *e, Job **ret) {
@ -2117,7 +2112,7 @@ int manager_add_job_by_name_and_warn(Manager *m, JobType type, const char *name,
int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error *e) {
int r;
Transaction *tr;
_cleanup_(transaction_abort_and_freep) Transaction *tr = NULL;
assert(m);
assert(unit);
@ -2131,22 +2126,17 @@ int manager_propagate_reload(Manager *m, Unit *unit, JobMode mode, sd_bus_error
/* We need an anchor job */
r = transaction_add_job_and_dependencies(tr, JOB_NOP, unit, NULL, false, false, true, true, e);
if (r < 0)
goto tr_abort;
return r;
/* Failure in adding individual dependencies is ignored, so this always succeeds. */
transaction_add_propagate_reload_jobs(tr, unit, tr->anchor_job, mode == JOB_IGNORE_DEPENDENCIES, e);
r = transaction_activate(tr, m, mode, NULL, e);
if (r < 0)
goto tr_abort;
return r;
transaction_free(tr);
tr = transaction_free(tr);
return 0;
tr_abort:
transaction_abort(tr);
transaction_free(tr);
return r;
}
Job *manager_get_job(Manager *m, uint32_t id) {

View File

@ -34,7 +34,7 @@ static void transaction_delete_unit(Transaction *tr, Unit *u) {
transaction_delete_job(tr, j, true);
}
void transaction_abort(Transaction *tr) {
static void transaction_abort(Transaction *tr) {
Job *j;
assert(tr);
@ -1199,8 +1199,21 @@ Transaction *transaction_new(bool irreversible) {
return tr;
}
void transaction_free(Transaction *tr) {
Transaction *transaction_free(Transaction *tr) {
if (!tr)
return NULL;
assert(hashmap_isempty(tr->jobs));
hashmap_free(tr->jobs);
free(tr);
return mfree(tr);
}
Transaction *transaction_abort_and_free(Transaction *tr) {
if (!tr)
return NULL;
transaction_abort(tr);
return transaction_free(tr);
}

View File

@ -16,7 +16,9 @@ struct Transaction {
};
Transaction *transaction_new(bool irreversible);
void transaction_free(Transaction *tr);
Transaction *transaction_free(Transaction *tr);
Transaction *transaction_abort_and_free(Transaction *tr);
DEFINE_TRIVIAL_CLEANUP_FUNC(Transaction*, transaction_abort_and_free);
void transaction_add_propagate_reload_jobs(Transaction *tr, Unit *unit, Job *by, bool ignore_order, sd_bus_error *e);
int transaction_add_job_and_dependencies(
@ -32,4 +34,3 @@ int transaction_add_job_and_dependencies(
int transaction_activate(Transaction *tr, Manager *m, JobMode mode, Set *affected, sd_bus_error *e);
int transaction_add_isolate_jobs(Transaction *tr, Manager *m);
int transaction_add_triggering_jobs(Transaction *tr, Unit *u);
void transaction_abort(Transaction *tr);

View File

@ -385,7 +385,7 @@ static int method_register_home(
_cleanup_(user_record_unrefp) UserRecord *hr = NULL;
Manager *m = ASSERT_PTR(userdata);
Home *h;
_cleanup_(home_freep) Home *h = NULL;
int r;
assert(message);
@ -413,10 +413,10 @@ static int method_register_home(
return r;
r = home_save_record(h);
if (r < 0) {
home_free(h);
if (r < 0)
return r;
}
TAKE_PTR(h);
return sd_bus_reply_method_return(message, NULL);
}

View File

@ -284,9 +284,10 @@ static int server_init(Server *s, unsigned n_sockets) {
static int process_event(Server *s, struct epoll_event *ev) {
int r;
Fifo *f;
_cleanup_(fifo_freep) Fifo *f = NULL;
assert(s);
assert(ev);
if (!(ev->events & EPOLLIN))
return log_info_errno(SYNTHETIC_ERRNO(EIO),
@ -294,11 +295,10 @@ static int process_event(Server *s, struct epoll_event *ev) {
f = (Fifo*) ev->data.ptr;
r = fifo_process(f);
if (r < 0) {
log_info_errno(r, "Got error on fifo: %m");
fifo_free(f);
return r;
}
if (r < 0)
return log_info_errno(r, "Got error on fifo: %m");
TAKE_PTR(f);
return 0;
}

View File

@ -53,8 +53,9 @@ Button* button_new(Manager *m, const char *name) {
return b;
}
void button_free(Button *b) {
assert(b);
Button *button_free(Button *b) {
if (!b)
return NULL;
hashmap_remove(b->manager->buttons, b->name);
@ -65,7 +66,8 @@ void button_free(Button *b) {
free(b->name);
free(b->seat);
free(b);
return mfree(b);
}
int button_set_seat(Button *b, const char *sn) {

View File

@ -20,7 +20,7 @@ struct Button {
};
Button* button_new(Manager *m, const char *name);
void button_free(Button *b);
Button *button_free(Button *b);
int button_open(Button *b);
int button_set_seat(Button *b, const char *sn);
int button_check_switches(Button *b);

View File

@ -323,15 +323,11 @@ int manager_process_button_device(Manager *m, sd_device *d) {
return r;
if (device_for_action(d, SD_DEVICE_REMOVE) ||
sd_device_has_current_tag(d, "power-switch") <= 0) {
sd_device_has_current_tag(d, "power-switch") <= 0)
b = hashmap_get(m->buttons, sysname);
if (!b)
return 0;
button_free(hashmap_get(m->buttons, sysname));
button_free(b);
} else {
else {
const char *sn;
r = manager_add_button(m, sysname, &b);

View File

@ -460,7 +460,7 @@ static int method_set_tty(sd_bus_message *message, void *userdata, sd_bus_error
static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {
Session *s = ASSERT_PTR(userdata);
uint32_t major, minor;
SessionDevice *sd;
_cleanup_(session_device_freep) SessionDevice *sd = NULL;
dev_t dev;
int r;
@ -492,18 +492,16 @@ static int method_take_device(sd_bus_message *message, void *userdata, sd_bus_er
r = session_device_save(sd);
if (r < 0)
goto error;
return r;
r = sd_bus_reply_method_return(message, "hb", sd->fd, !sd->active);
if (r < 0)
goto error;
return r;
session_save(s);
return 1;
TAKE_PTR(sd);
error:
session_device_free(sd);
return r;
return 1;
}
static int method_release_device(sd_bus_message *message, void *userdata, sd_bus_error *error) {

View File

@ -375,8 +375,9 @@ error:
return r;
}
void session_device_free(SessionDevice *sd) {
assert(sd);
SessionDevice *session_device_free(SessionDevice *sd) {
if (!sd)
return NULL;
/* Make sure to remove the pushed fd. */
if (sd->pushed_fd)
@ -391,7 +392,8 @@ void session_device_free(SessionDevice *sd) {
hashmap_remove(sd->session->devices, &sd->dev);
free(sd->node);
free(sd);
return mfree(sd);
}
void session_device_complete_pause(SessionDevice *sd) {

View File

@ -28,7 +28,9 @@ struct SessionDevice {
};
int session_device_new(Session *s, dev_t dev, bool open_device, SessionDevice **out);
void session_device_free(SessionDevice *sd);
SessionDevice *session_device_free(SessionDevice *sd);
DEFINE_TRIVIAL_CLEANUP_FUNC(SessionDevice*, session_device_free);
void session_device_complete_pause(SessionDevice *sd);
void session_device_resume_all(Session *s);

View File

@ -40,11 +40,11 @@
#include "udev-util.h"
#include "user-util.h"
static Manager* manager_unref(Manager *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref);
static Manager* manager_free(Manager *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
static int manager_new(Manager **ret) {
_cleanup_(manager_unrefp) Manager *m = NULL;
_cleanup_(manager_freep) Manager *m = NULL;
int r;
assert(ret);
@ -102,7 +102,7 @@ static int manager_new(Manager **ret) {
return 0;
}
static Manager* manager_unref(Manager *m) {
static Manager* manager_free(Manager *m) {
Session *session;
User *u;
Device *d;
@ -1177,7 +1177,7 @@ static int manager_run(Manager *m) {
}
static int run(int argc, char *argv[]) {
_cleanup_(manager_unrefp) Manager *m = NULL;
_cleanup_(manager_freep) Manager *m = NULL;
_unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
int r;

View File

@ -464,7 +464,8 @@ int specifier_var_tmp_dir(char specifier, const void *data, const char *root, co
}
int specifier_escape_strv(char **l, char ***ret) {
char **z, **p, **q;
_cleanup_strv_free_ char **z = NULL;
char **p, **q;
assert(ret);
@ -480,14 +481,12 @@ int specifier_escape_strv(char **l, char ***ret) {
for (p = l, q = z; *p; p++, q++) {
*q = specifier_escape(*p);
if (!*q) {
strv_free(z);
if (!*q)
return -ENOMEM;
}
}
*q = NULL;
*ret = z;
*ret = TAKE_PTR(z);
return 0;
}