1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-31 16:21:26 +03:00

Merge pull request #7637 from yuwata/transient-path

core/path: implement transient path unit
This commit is contained in:
Lennart Poettering 2017-12-15 10:53:49 +01:00 committed by GitHub
commit 42521cd1ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 167 additions and 17 deletions

4
TODO
View File

@ -24,6 +24,10 @@ Janitorial Clean-ups:
Features:
* implement transient socket unit.
* make systemd-run create transient path and socket unit.
* make use of ethtool veth peer info in machined, for automatically finding out
host-side interface pointing to the container.

View File

@ -325,7 +325,7 @@ Most timer unit settings are available to transient units.
✓ OnStartupSec=
✓ OnUnitActiveSec=
✓ OnUnitInactiveSec=
Persistent=
Persistent=
✓ WakeSystem=
✓ RemainAfterElapse=
✓ AccuracySec=
@ -421,17 +421,17 @@ Swap units are currently not available at all as transient units:
## Path Unit Settings
Path units are currently not available at all as transient units:
Most path unit settings are available to transient units.
```
PathExists=
PathExistsGlob=
PathChanged=
PathModified=
DirectoryNotEmpty=
PathExists=
PathExistsGlob=
PathChanged=
PathModified=
DirectoryNotEmpty=
Unit=
MakeDirectory=
DirectoryMode=
MakeDirectory=
DirectoryMode=
```
## Install Section

View File

@ -18,9 +18,12 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "alloc-util.h"
#include "bus-util.h"
#include "dbus-path.h"
#include "list.h"
#include "path.h"
#include "path-util.h"
#include "string-util.h"
#include "unit.h"
@ -85,3 +88,119 @@ const sd_bus_vtable bus_path_vtable[] = {
SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Path, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_VTABLE_END
};
static int bus_path_set_transient_property(
Path *p,
const char *name,
sd_bus_message *message,
UnitWriteFlags flags,
sd_bus_error *error) {
Unit *u = UNIT(p);
int r;
assert(p);
assert(name);
assert(message);
flags |= UNIT_PRIVATE;
if (STR_IN_SET(name, "PathExists", "PathExistsGlob", "PathChanged", "PathModified", "DirectoryNotEmpty")) {
const char *str;
PathType b;
b = path_type_from_string(name);
if (b < 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown path type");
r = sd_bus_message_read(message, "s", &str);
if (r < 0)
return r;
if (!isempty(str) && !path_is_absolute(str))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Path is not absolute");
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
if (isempty(str)) {
path_free_specs(p);
unit_write_settingf(u, flags, name, "%s=", name);
} else {
_cleanup_free_ char *k;
PathSpec *s;
k = strdup(str);
if (!k)
return -ENOMEM;
s = new0(PathSpec, 1);
if (!s)
return -ENOMEM;
s->unit = u;
s->path = path_kill_slashes(k);
k = NULL;
s->type = b;
s->inotify_fd = -1;
LIST_PREPEND(spec, p->specs, s);
unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "%s=%s", name, str);
}
}
return 1;
} else if (streq(name, "MakeDirectory")) {
int b;
r = sd_bus_message_read(message, "b", &b);
if (r < 0)
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
p->make_directory = b;
unit_write_settingf(u, flags, name, "%s=%s", name, yes_no(b));
}
return 1;
} else if (streq(name, "DirectoryMode")) {
mode_t m;
r = sd_bus_message_read(message, "u", &m);
if (r < 0)
return r;
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
p->directory_mode = m;
unit_write_settingf(u, flags, name, "%s=%040o", name, m);
}
return 1;
} else if (streq(name, "Unit")) {
/* not implemented yet */
return 0;
}
return 0;
}
int bus_path_set_property(
Unit *u,
const char *name,
sd_bus_message *message,
UnitWriteFlags mode,
sd_bus_error *error) {
Path *p = PATH(u);
assert(p);
assert(name);
assert(message);
if (u->transient && u->load_state == UNIT_STUB)
return bus_path_set_transient_property(p, name, message, mode, error);
return 0;
}

View File

@ -20,6 +20,10 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "sd-bus.h"
#include "unit.h"
extern const sd_bus_vtable bus_path_vtable[];
int bus_path_set_property(Unit *u, const char *name, sd_bus_message *i, UnitWriteFlags flags, sd_bus_error *error);

View File

@ -200,7 +200,7 @@ static int bus_timer_set_transient_property(
b = timer_base_from_string(name);
if (b < 0)
return -EINVAL;
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unknown timer base");
r = sd_bus_message_read(message, "t", &u);
if (r < 0)
@ -235,6 +235,8 @@ static int bus_timer_set_transient_property(
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
r = calendar_spec_from_string(str, &c);
if (r == -EINVAL)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid calendar spec");
if (r < 0)
return r;

View File

@ -772,6 +772,9 @@ const UnitVTable path_vtable = {
"Unit\0"
"Path\0"
"Install\0",
.private_section = "Path",
.can_transient = true,
.init = path_init,
.done = path_done,
@ -794,5 +797,6 @@ const UnitVTable path_vtable = {
.reset_failed = path_reset_failed,
.bus_vtable = bus_path_vtable
.bus_vtable = bus_path_vtable,
.bus_set_property = bus_path_set_property,
};

View File

@ -153,8 +153,6 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
l = strlen(field);
n = newa(char, l + 2);
if (!n)
return log_oom();
/* Change suffix Sec → USec */
strcpy(mempcpy(n, field, l - 3), "USec");
@ -360,7 +358,7 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
"NoNewPrivileges", "SyslogLevelPrefix", "RemainAfterElapse", "Persistent",
"MemoryDenyWriteExecute", "RestrictRealtime", "DynamicUser", "RemoveIPC",
"ProtectKernelTunables", "ProtectKernelModules", "ProtectControlGroups", "MountAPIVFS",
"CPUSchedulingResetOnFork", "LockPersonality")) {
"CPUSchedulingResetOnFork", "LockPersonality", "MakeDirectory")) {
r = parse_boolean(eq);
if (r < 0)
@ -974,6 +972,7 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
}
r = sd_bus_message_append(m, "v", "i", oa);
} else if (STR_IN_SET(field, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories",
"ReadWritePaths", "ReadOnlyPaths", "InaccessiblePaths")) {
const char *p;
@ -1005,7 +1004,7 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
offset += word[offset] == '+';
if (!path_is_absolute(word + offset)) {
log_error("Failed to parse %s value %s", field, eq);
log_error("Path specified by %s is not absolute: %s", field, eq);
return -EINVAL;
}
@ -1043,7 +1042,7 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
break;
if (!valid_user_group_name_or_id(word)) {
log_error("Failed to parse %s value %s", field, eq);
log_error("Invalid group name or id is specified by %s: %s", field, eq);
return -EINVAL;
}
@ -1058,7 +1057,10 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
r = sd_bus_message_close_container(m);
} else if (STR_IN_SET(field, "RuntimeDirectoryMode", "StateDirectoryMode", "CacheDirectoryMode", "LogsDirectoryMode", "ConfigurationDirectoryMode", "UMask")) {
} else if (STR_IN_SET(field,
"RuntimeDirectoryMode", "StateDirectoryMode", "CacheDirectoryMode",
"LogsDirectoryMode", "ConfigurationDirectoryMode", "UMask",
"DirectoryMode")) {
mode_t mode;
r = parse_mode(eq, &mode);
@ -1124,8 +1126,11 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
flags = (~flags) & NAMESPACE_FLAGS_ALL;
r = sd_bus_message_append(m, "v", "t", (uint64_t) flags);
} else if ((dep = unit_dependency_from_string(field)) >= 0)
r = sd_bus_message_append(m, "v", "as", 1, eq);
else if (streq(field, "MountFlags")) {
unsigned long f;
@ -1134,6 +1139,7 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
return log_error_errno(r, "Failed to parse mount propagation flags: %s", eq);
r = sd_bus_message_append(m, "v", "t", (uint64_t) f);
} else if (STR_IN_SET(field, "BindPaths", "BindReadOnlyPaths")) {
const char *p = eq;
@ -1303,6 +1309,17 @@ int bus_append_unit_property_assignment(sd_bus_message *m, const char *assignmen
r = sd_bus_message_append(m, "v", "t", t);
} else if (STR_IN_SET(field,
"PathExists", "PathExistsGlob", "PathChanged",
"PathModified", "DirectoryNotEmpty")) {
if (!path_is_absolute(eq)) {
log_error("Path specified by %s= is not absolute: %s", field, eq);
return -EINVAL;
}
r = sd_bus_message_append(m, "v", "s", eq);
} else {
log_error("Unknown assignment: %s", assignment);
return -EINVAL;