]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
machine: properly remove unit name symlink on removal
authorLennart Poettering <lennart@poettering.net>
Mon, 19 May 2025 09:05:51 +0000 (11:05 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 27 May 2025 08:23:36 +0000 (10:23 +0200)
src/machine/machine.c

index 989d6a5c22ddcf0de68efc56706d9ccf7de31174..fb397362b15142d99dd181d09135b3b168be0d38 100644 (file)
@@ -155,6 +155,13 @@ int machine_save(Machine *m) {
         if (!m->started)
                 return 0;
 
+        _cleanup_(unlink_and_freep) char *sl = NULL; /* auto-unlink! */
+        if (m->unit) {
+                sl = strjoin("/run/systemd/machines/unit:", m->unit);
+                if (!sl)
+                        return log_oom();
+        }
+
         r = mkdir_safe_label("/run/systemd/machines", 0755, 0, 0, MKDIR_WARN_MODE);
         if (r < 0)
                 return log_error_errno(r, "Failed to create /run/systemd/machines/: %m");
@@ -241,14 +248,13 @@ int machine_save(Machine *m) {
 
         temp_path = mfree(temp_path); /* disarm auto-destroy: temporary file does not exist anymore */
 
-        if (m->unit) {
-                char *sl;
-
-                /* Create a symlink from the unit name to the machine
-                 * name, so that we can quickly find the machine for
-                 * each given unit. Ignore error. */
-                sl = strjoina("/run/systemd/machines/unit:", m->unit);
+        if (sl) {
+                /* Create a symlink from the unit name to the machine name, so that we can quickly find the machine
+                 * for each given unit. Ignore error. */
                 (void) symlink(m->name, sl);
+
+                /* disarm auto-removal */
+                sl = mfree(sl);
         }
 
         return 0;
@@ -258,9 +264,7 @@ static void machine_unlink(Machine *m) {
         assert(m);
 
         if (m->unit) {
-                char *sl;
-
-                sl = strjoina("/run/systemd/machines/unit:", m->unit);
+                const char *sl = strjoina("/run/systemd/machines/unit:", m->unit);
                 (void) unlink(sl);
         }