summaryrefslogtreecommitdiff
path: root/src/gpt-auto-generator
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2018-04-25 15:46:57 +0200
committerGitHub <noreply@github.com>2018-04-25 15:46:57 +0200
commit2de23375184592091b2726e6794ff48c35da4930 (patch)
treecf4ad526291565b986dbc08e89d06c348338328a /src/gpt-auto-generator
parent22ce84de184fa8c51985b621f7d1bb99e44ea35f (diff)
parente137880b2a58bd899c7d0e15dc24aa20fe02ab31 (diff)
downloadsystemd-2de23375184592091b2726e6794ff48c35da4930.tar.gz
Merge pull request #8812 from keszybz/gpt-auto-memleak
gpt-auto-generator: use stack variables and fix minor memleak
Diffstat (limited to 'src/gpt-auto-generator')
-rw-r--r--src/gpt-auto-generator/gpt-auto-generator.c69
1 files changed, 31 insertions, 38 deletions
diff --git a/src/gpt-auto-generator/gpt-auto-generator.c b/src/gpt-auto-generator/gpt-auto-generator.c
index e66cfffbf2..38a27a4318 100644
--- a/src/gpt-auto-generator/gpt-auto-generator.c
+++ b/src/gpt-auto-generator/gpt-auto-generator.c
@@ -46,9 +46,9 @@ static bool arg_root_enabled = true;
static bool arg_root_rw = false;
static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) {
- _cleanup_free_ char *e = NULL, *n = NULL, *p = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL;
+ _cleanup_free_ char *e = NULL, *n = NULL, *d = NULL, *id_escaped = NULL, *what_escaped = NULL;
_cleanup_fclose_ FILE *f = NULL;
- char *ret;
+ const char *p;
int r;
assert(id);
@@ -74,10 +74,7 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
if (!what_escaped)
return log_oom();
- p = strjoin(arg_dest, "/", n);
- if (!p)
- return log_oom();
-
+ p = strjoina(arg_dest, "/", n);
f = fopen(p, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", p);
@@ -125,11 +122,7 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
return r;
}
- free(p);
- p = strjoin(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf");
- if (!p)
- return log_oom();
-
+ p = strjoina(arg_dest, "/dev-mapper-", e, ".device.d/50-job-timeout-sec-0.conf");
mkdir_parents_label(p, 0755);
r = write_string_file(p,
"# Automatically generated by systemd-gpt-auto-generator\n\n"
@@ -139,12 +132,16 @@ static int add_cryptsetup(const char *id, const char *what, bool rw, bool requir
if (r < 0)
return log_error_errno(r, "Failed to write device drop-in: %m");
- ret = strappend("/dev/mapper/", id);
- if (!ret)
- return log_oom();
+ if (device) {
+ char *ret;
+
+ ret = strappend("/dev/mapper/", id);
+ if (!ret)
+ return log_oom();
- if (device)
*device = ret;
+ }
+
return 0;
}
@@ -233,7 +230,7 @@ static int add_mount(
return 0;
}
-static bool path_is_busy(const char *where) {
+static int path_is_busy(const char *where) {
int r;
/* already a mountpoint; generators run during reload */
@@ -246,13 +243,17 @@ static bool path_is_busy(const char *where) {
return false;
if (r < 0)
- return true;
+ return log_warning_errno(r, "Cannot check if \"%s\" is a mount point: %m", where);
/* not a mountpoint but it contains files */
- if (dir_is_empty(where) <= 0)
- return true;
+ r = dir_is_empty(where);
+ if (r < 0)
+ return log_warning_errno(r, "Cannot check if \"%s\" is empty: %m", where);
+ if (r > 0)
+ return false;
- return false;
+ log_debug("\"%s\" already populated, ignoring.", where);
+ return true;
}
static int add_partition_mount(
@@ -261,12 +262,12 @@ static int add_partition_mount(
const char *where,
const char *description) {
+ int r;
assert(p);
- if (path_is_busy(where)) {
- log_debug("%s already populated, ignoring.", where);
- return 0;
- }
+ r = path_is_busy(where);
+ if (r != 0)
+ return r < 0 ? r : 0;
return add_mount(
id,
@@ -337,8 +338,8 @@ static int add_automount(
usec_t timeout) {
_cleanup_free_ char *unit = NULL;
- _cleanup_free_ char *opt, *p = NULL;
_cleanup_fclose_ FILE *f = NULL;
+ const char *opt = "noauto", *p;
int r;
assert(id);
@@ -346,11 +347,7 @@ static int add_automount(
assert(description);
if (options)
- opt = strjoin(options, ",noauto");
- else
- opt = strdup("noauto");
- if (!opt)
- return log_oom();
+ opt = strjoina(options, ",", opt);
r = add_mount(id,
what,
@@ -367,10 +364,7 @@ static int add_automount(
if (r < 0)
return log_error_errno(r, "Failed to generate unit name: %m");
- p = strjoin(arg_dest, "/", unit);
- if (!p)
- return log_oom();
-
+ p = strjoina(arg_dest, "/", unit);
f = fopen(p, "wxe");
if (!f)
return log_error_errno(errno, "Failed to create unit file %s: %m", unit);
@@ -417,10 +411,9 @@ static int add_esp(DissectedPartition *p) {
return 0;
}
- if (path_is_busy(esp)) {
- log_debug("%s already populated, ignoring.", esp);
- return 0;
- }
+ r = path_is_busy(esp);
+ if (r != 0)
+ return r < 0 ? r : 0;
if (is_efi_boot()) {
sd_id128_t loader_uuid;