const UnitVTable automount_vtable = {
.suffix = ".automount",
+ .object_size = sizeof(Automount),
.sections =
"Unit\0"
"Automount\0"
if (!u) {
delete = true;
- if (!(u = unit_new(m)))
+ u = unit_new(m, sizeof(Device));
+ if (!u)
return -ENOMEM;
- if ((r = device_add_escaped_name(u, path)) < 0)
+ r = device_add_escaped_name(u, path);
+ if (r < 0)
goto fail;
unit_add_to_load_queue(u);
const UnitVTable device_vtable = {
.suffix = ".device",
+ .object_size = sizeof(Device),
.sections =
"Unit\0"
"Device\0"
int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) {
Unit *ret;
+ UnitType t;
int r;
assert(m);
if (!name)
name = file_name_from_path(path);
- if (!unit_name_is_valid(name, false)) {
+ t = unit_name_to_type(name);
+
+ if (t == _UNIT_TYPE_INVALID || !unit_name_is_valid_no_type(name, false)) {
dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name);
return -EINVAL;
}
- if ((ret = manager_get_unit(m, name))) {
+ ret = manager_get_unit(m, name);
+ if (ret) {
*_ret = ret;
return 1;
}
- if (!(ret = unit_new(m)))
+ ret = unit_new(m, unit_vtable[t]->object_size);
+ if (!ret)
return -ENOMEM;
- if (path)
- if (!(ret->meta.fragment_path = strdup(path))) {
+ if (path) {
+ ret->meta.fragment_path = strdup(path);
+ if (!ret->meta.fragment_path) {
unit_free(ret);
return -ENOMEM;
}
+ }
if ((r = unit_add_name(ret, name)) < 0) {
unit_free(ret);
if (!is_path(where))
return 0;
- if (!(e = unit_name_from_path(where, ".mount")))
+ e = unit_name_from_path(where, ".mount");
+ if (!e)
return -ENOMEM;
- if (!(u = manager_get_unit(m, e))) {
+ u = manager_get_unit(m, e);
+ if (!u) {
delete = true;
- if (!(u = unit_new(m))) {
+ u = unit_new(m, sizeof(Mount));
+ if (!u) {
free(e);
return -ENOMEM;
}
if (r < 0)
goto fail;
- if (!(MOUNT(u)->where = strdup(where))) {
+ MOUNT(u)->where = strdup(where);
+ if (!MOUNT(u)->where) {
r = -ENOMEM;
goto fail;
}
const UnitVTable mount_vtable = {
.suffix = ".mount",
+ .object_size = sizeof(Mount),
.sections =
"Unit\0"
"Mount\0"
const UnitVTable path_vtable = {
.suffix = ".path",
+ .object_size = sizeof(Path),
.sections =
"Unit\0"
"Path\0"
const UnitVTable service_vtable = {
.suffix = ".service",
+ .object_size = sizeof(Service),
.sections =
"Unit\0"
"Service\0"
const UnitVTable snapshot_vtable = {
.suffix = ".snapshot",
+ .object_size = sizeof(Snapshot),
.no_alias = true,
.no_instances = true,
const UnitVTable socket_vtable = {
.suffix = ".socket",
+ .object_size = sizeof(Socket),
.sections =
"Unit\0"
"Socket\0"
assert(m);
assert(what);
- if (!(e = unit_name_from_path(what, ".swap")))
+ e = unit_name_from_path(what, ".swap");
+ if (!e)
return -ENOMEM;
u = manager_get_unit(m, e);
if (!u) {
delete = true;
- if (!(u = unit_new(m))) {
+ u = unit_new(m, sizeof(Swap));
+ if (!u) {
free(e);
return -ENOMEM;
}
- if ((r = unit_add_name(u, e)) < 0)
+ r = unit_add_name(u, e);
+ if (r < 0)
goto fail;
- if (!(SWAP(u)->what = strdup(what))) {
+ SWAP(u)->what = strdup(what);
+ if (!SWAP(u)->what) {
r = -ENOMEM;
goto fail;
}
const UnitVTable swap_vtable = {
.suffix = ".swap",
+ .object_size = sizeof(Swap),
.sections =
"Unit\0"
"Swap\0"
const UnitVTable target_vtable = {
.suffix = ".target",
+ .object_size = sizeof(Target),
.sections =
"Unit\0"
"Target\0"
const UnitVTable timer_vtable = {
.suffix = ".timer",
+ .object_size = sizeof(Timer),
.sections =
"Unit\0"
"Timer\0"
[UNIT_PATH] = &path_vtable
};
-Unit *unit_new(Manager *m) {
+Unit *unit_new(Manager *m, size_t size) {
Unit *u;
assert(m);
+ assert(size >= sizeof(Meta));
- if (!(u = new0(Unit, 1)))
+ u = malloc0(size);
+ if (!u)
return NULL;
- if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
+ u->meta.names = set_new(string_hash_func, string_compare_func);
+ if (!u->meta.names) {
free(u);
return NULL;
}
struct UnitVTable {
const char *suffix;
+ /* How much memory does an object of this unit type need */
+ size_t object_size;
+
/* Config file sections this unit type understands, separated
* by NUL chars */
const char *sections;
DEFINE_CAST(SWAP, Swap);
DEFINE_CAST(PATH, Path);
-Unit *unit_new(Manager *m);
+Unit *unit_new(Manager *m, size_t size);
void unit_free(Unit *u);
int unit_add_name(Unit *u, const char *name);