X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fmachine%2Fmachine.c;h=0b0d45bb266594410f7cf6c7b04be6e7118e6646;hb=ecde7065f7b11a7a226d2f7b0e90e998a6347a59;hp=4596a80838f6bd8f751110715f45c9e66696910c;hpb=39883f622f392d8579f4428fc5a789a102efbb10;p=elogind.git diff --git a/src/machine/machine.c b/src/machine/machine.c index 4596a8083..0b0d45bb2 100644 --- a/src/machine/machine.c +++ b/src/machine/machine.c @@ -123,17 +123,42 @@ int machine_save(Machine *m) { "NAME=%s\n", m->name); - if (m->unit) - fprintf(f, "SCOPE=%s\n", m->unit); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */ + if (m->unit) { + _cleanup_free_ char *escaped; + + escaped = cescape(m->unit); + if (!escaped) { + r = -ENOMEM; + goto finish; + } + + fprintf(f, "SCOPE=%s\n", escaped); /* We continue to call this "SCOPE=" because it is internal only, and we want to stay compatible with old files */ + } if (m->scope_job) fprintf(f, "SCOPE_JOB=%s\n", m->scope_job); - if (m->service) - fprintf(f, "SERVICE=%s\n", m->service); + if (m->service) { + _cleanup_free_ char *escaped; - if (m->root_directory) - fprintf(f, "ROOT=%s\n", m->root_directory); + escaped = cescape(m->service); + if (!escaped) { + r = -ENOMEM; + goto finish; + } + fprintf(f, "SERVICE=%s\n", escaped); + } + + if (m->root_directory) { + _cleanup_free_ char *escaped; + + escaped = cescape(m->root_directory); + if (!escaped) { + r = -ENOMEM; + goto finish; + } + fprintf(f, "ROOT=%s\n", escaped); + } if (!sd_id128_equal(m->id, SD_ID128_NULL)) fprintf(f, "ID=" SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->id)); @@ -151,12 +176,13 @@ int machine_save(Machine *m) { m->timestamp.realtime, m->timestamp.monotonic); - fflush(f); + r = fflush_and_check(f); + if (r < 0) + goto finish; - if (ferror(f) || rename(temp_path, m->state_file) < 0) { + if (rename(temp_path, m->state_file) < 0) { r = -errno; - unlink(m->state_file); - unlink(temp_path); + goto finish; } if (m->unit) { @@ -170,8 +196,12 @@ int machine_save(Machine *m) { } finish: - if (r < 0) + if (r < 0) { + if (temp_path) + unlink(temp_path); + log_error("Failed to save machine data %s: %s", m->state_file, strerror(-r)); + } return r; } @@ -303,7 +333,7 @@ int machine_start(Machine *m, sd_bus_message *properties, sd_bus_error *error) { log_struct(LOG_INFO, MESSAGE_ID(SD_MESSAGE_MACHINE_START), "NAME=%s", m->name, - "LEADER=%lu", (unsigned long) m->leader, + "LEADER="PID_FMT, m->leader, "MESSAGE=New machine %s.", m->name, NULL); @@ -350,7 +380,7 @@ int machine_stop(Machine *m) { log_struct(LOG_INFO, MESSAGE_ID(SD_MESSAGE_MACHINE_STOP), "NAME=%s", m->name, - "LEADER=%lu", (unsigned long) m->leader, + "LEADER="PID_FMT, m->leader, "MESSAGE=Machine %s terminated.", m->name, NULL); @@ -410,7 +440,15 @@ int machine_kill(Machine *m, KillWho who, int signo) { if (!m->unit) return -ESRCH; - return manager_kill_unit(m->manager, m->unit, who, signo, NULL); + if (who == KILL_LEADER) { + /* If we shall simply kill the leader, do so directly */ + + if (kill(m->leader, signo) < 0) + return -errno; + } + + /* Otherwise make PID 1 do it for us, for the entire cgroup */ + return manager_kill_unit(m->manager, m->unit, signo, NULL); } static const char* const machine_class_table[_MACHINE_CLASS_MAX] = {