1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

start implementing a test suite for the engine

This commit is contained in:
Lennart Poettering 2010-01-20 05:03:52 +01:00
parent 986b9910d3
commit 7fad411cb5
7 changed files with 245 additions and 176 deletions

View File

@ -1,8 +1,15 @@
CFLAGS=-Wall -Wextra -O0 -g -pipe -D_GNU_SOURCE -fdiagnostics-show-option -Wno-unused-parameter
LIBS=-lrt
systemd: main.o name.o util.o set.o hashmap.o strv.o job.o manager.o conf-parser.o load-fragment.o socket-util.c
COMMON=name.o util.o set.o hashmap.o strv.o job.o manager.o conf-parser.o load-fragment.o socket-util.c
all: systemd test-engine
systemd: main.o $(COMMON)
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
test-engine: test-engine.o $(COMMON)
$(CC) $(CFLAGS) -o $@ $^ $(LIBS)
clean:
rm -f *.o systemd
rm -f *.o systemd test-engine

View File

@ -93,7 +93,7 @@ static int config_parse_names(
return -EEXIST;
}
if ((r = name_merge(name, other) < 0)) {
if ((r = name_merge(name, other)) < 0) {
free(t);
return r;
}

4
main.c
View File

@ -18,12 +18,12 @@ int main(int argc, char *argv[]) {
}
if ((r = manager_load_name(m, "default.milestone", &milestone) < 0)) {
if ((r = manager_load_name(m, "default.milestone", &milestone)) < 0) {
fprintf(stderr, "Failed to load default milestone: %s\n", strerror(-r));
goto finish;
}
if ((r = manager_load_name(m, "syslog.socket", &syslog) < 0)) {
if ((r = manager_load_name(m, "syslog.socket", &syslog)) < 0) {
fprintf(stderr, "Failed to load syslog socket: %s\n", strerror(-r));
goto finish;
}

145
manager.c
View File

@ -8,7 +8,6 @@
#include "hashmap.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"
Manager* manager_new(void) {
Manager *m;
@ -122,7 +121,7 @@ static int types_merge(JobType *a, JobType b) {
return -EEXIST;
}
static void manager_merge_and_delete_prospective_job(Manager *m, Job *j, Job *other, JobType t) {
static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) {
JobDependency *l, *last;
assert(j);
@ -191,17 +190,17 @@ static int transaction_merge_jobs(Manager *m) {
while ((k = j->transaction_next)) {
if (j->linked) {
manager_merge_and_delete_prospective_job(m, k, j, t);
transaction_merge_and_delete_job(m, k, j, t);
j = k;
} else
manager_merge_and_delete_prospective_job(m, j, k, t);
transaction_merge_and_delete_job(m, j, k, t);
}
assert(!j->transaction_next);
assert(!j->transaction_prev);
}
return r;
return 0;
}
static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation) {
@ -212,12 +211,12 @@ static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned
assert(m);
assert(j);
/* Did we find a loop? */
/* Did we find a cycle? */
if (j->marker && j->generation == generation) {
Job *k;
/* So, we already have been here. We have a
* loop. Let's try to break it. We go backwards in our
* cycle. Let's try to break it. We go backwards in our
* path and try to find a suitable job to remove. */
for (k = from; k; k = (k->generation == generation ? k->marker : NULL)) {
@ -227,7 +226,7 @@ static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned
}
/* Check if this in fact was the beginning of
* the loop */
* the cycle */
if (k == j)
break;
}
@ -275,7 +274,7 @@ static int transaction_verify_order(Manager *m, unsigned *generation) {
if ((r = transaction_verify_order_one(m, j, NULL, (*generation)++)) < 0) {
/* There was a loop, but it was fixed,
/* There was a cycleq, but it was fixed,
* we need to restart our algorithm */
if (r == -EAGAIN) {
again = true;
@ -584,121 +583,6 @@ Name *manager_get_name(Manager *m, const char *name) {
return hashmap_get(m->names, name);
}
static int verify_type(Name *name) {
char *n;
void *state;
assert(name);
/* Checks that all aliases of this name have the same and valid type */
SET_FOREACH(n, name->meta.names, state) {
NameType t;
if ((t = name_type_from_string(n)) == _NAME_TYPE_INVALID)
return -EINVAL;
if (name->meta.type == _NAME_TYPE_INVALID) {
name->meta.type = t;
continue;
}
if (name->meta.type != t)
return -EINVAL;
}
if (name->meta.type == _NAME_TYPE_INVALID)
return -EINVAL;
return 0;
}
static int service_load_sysv(Service *s) {
assert(s);
/* Load service data from SysV init scripts, preferably with
* LSB headers ... */
return 0;
}
static int name_load_fstab(Name *n) {
assert(n);
assert(n->meta.type == NAME_MOUNT || n->meta.type == NAME_AUTOMOUNT);
/* Load mount data from /etc/fstab */
return 0;
}
static int snapshot_load(Snapshot *s) {
assert(s);
/* Load snapshots from disk */
return 0;
}
static int name_load_dropin(Name *n) {
assert(n);
/* Load dependencies from drop-in directories */
return 0;
}
static int load(Name *name) {
int r;
assert(name);
if (name->meta.state != NAME_STUB)
return 0;
if ((r = verify_type(name)) < 0)
return r;
if (name->meta.type == NAME_SERVICE) {
/* Load a .service file */
if ((r = name_load_fragment(name)) == 0)
goto finish;
/* Load a classic init script */
if (r == -ENOENT)
if ((r = service_load_sysv(SERVICE(name))) == 0)
goto finish;
} else if (name->meta.type == NAME_MOUNT ||
name->meta.type == NAME_AUTOMOUNT) {
if ((r = name_load_fstab(name)) == 0)
goto finish;
} else if (name->meta.type == NAME_SNAPSHOT) {
if ((r = snapshot_load(SNAPSHOT(name))) == 0)
goto finish;
} else {
if ((r = name_load_fragment(name)) == 0)
goto finish;
}
name->meta.state = NAME_FAILED;
return r;
finish:
if ((r = name_load_dropin(name)) < 0)
return r;
if ((r = name_link_names(name, true)) < 0)
return r;
name->meta.state = NAME_LOADED;
return 0;
}
static int dispatch_load_queue(Manager *m) {
Meta *meta;
@ -714,7 +598,7 @@ static int dispatch_load_queue(Manager *m) {
* tries to load its data until the queue is empty */
while ((meta = m->load_queue)) {
load(NAME(meta));
name_load(NAME(meta));
LIST_REMOVE(Meta, m->load_queue, meta);
}
@ -801,3 +685,14 @@ void manager_dump_names(Manager *s, FILE *f, const char *prefix) {
if (name_id(n) == t)
name_dump(n, f, prefix);
}
void manager_clear_jobs(Manager *m) {
Job *j;
assert(m);
transaction_abort(m);
while ((j = hashmap_first(m->jobs)))
job_free(j);
}

View File

@ -50,4 +50,6 @@ void manager_dump_jobs(Manager *s, FILE *f, const char *prefix);
void manager_transaction_delete_job(Manager *m, Job *j);
void manager_clear_jobs(Manager *m);
#endif

254
name.c
View File

@ -8,6 +8,7 @@
#include "name.h"
#include "macro.h"
#include "strv.h"
#include "load-fragment.h"
NameType name_type_from_string(const char *n) {
NameType t;
@ -112,9 +113,12 @@ int name_link(Name *n) {
assert(!set_isempty(n->meta.names));
assert(!n->meta.linked);
if ((r = name_sanitize(n)) < 0)
return r;
n->meta.linked = true;
if ((r = name_link_names(n, false) < 0)) {
if ((r = name_link_names(n, false)) < 0) {
char *t;
void *state;
@ -297,55 +301,13 @@ static int ensure_in_set(Set **s, void *data) {
if (!(*s = set_new(trivial_hash_func, trivial_compare_func)))
return -ENOMEM;
if ((r = set_put(*s, data) < 0))
if ((r = set_put(*s, data)) < 0)
if (r != -EEXIST)
return r;
return 0;
}
/* FIXME: Does not rollback on failure! */
int name_augment(Name *n) {
int r;
void* state;
Name *other;
assert(n);
/* Adds in the missing links to make all dependencies
* bidirectional. */
SET_FOREACH(other, n->meta.dependencies[NAME_BEFORE], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_AFTER], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_AFTER], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_BEFORE], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_CONFLICTS], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_CONFLICTS], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_REQUIRES], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_REQUIRED_BY], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_REQUISITE], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_REQUIRED_BY], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_WANTS], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUIRES], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n) < 0))
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUISITE], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n) < 0))
return r;
return r;
}
static int ensure_merge(Set **s, Set *other) {
if (!other)
@ -389,7 +351,7 @@ int name_merge(Name *name, Name *other) {
/* Hookup new deps and names */
if (name->meta.linked) {
if ((r = name_augment(name)) < 0)
if ((r = name_sanitize(name)) < 0)
return r;
if ((r = name_link_names(name, true)) < 0)
@ -399,6 +361,60 @@ int name_merge(Name *name, Name *other) {
return 0;
}
/* FIXME: Does not rollback on failure! */
static int augment(Name *n) {
int r;
void* state;
Name *other;
assert(n);
/* Adds in the missing links to make all dependencies
* bidirectional. */
SET_FOREACH(other, n->meta.dependencies[NAME_BEFORE], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_AFTER], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_AFTER], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_BEFORE], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_CONFLICTS], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_CONFLICTS], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_REQUIRES], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_REQUIRED_BY], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_REQUISITE], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_REQUIRED_BY], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_WANTS], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUIRES], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n)) < 0)
return r;
SET_FOREACH(other, n->meta.dependencies[NAME_SOFT_REQUISITE], state)
if ((r = ensure_in_set(&other->meta.dependencies[NAME_WANTED_BY], n)) < 0)
return r;
return 0;
}
int name_sanitize(Name *n) {
NameDependency d;
assert(n);
/* Remove loops */
for (d = 0; d < _NAME_DEPENDENCY_MAX; d++)
set_remove(n->meta.dependencies[d], n);
return augment(n);
}
const char* name_id(Name *n) {
assert(n);
@ -427,8 +443,22 @@ void name_dump(Name *n, FILE *f, const char *prefix) {
[SOCKET_MAINTAINANCE] = "maintainance"
};
static const char* const dependency_table[_NAME_DEPENDENCY_MAX] = {
[NAME_REQUIRES] = "Requires",
[NAME_SOFT_REQUIRES] = "SoftRequires",
[NAME_WANTS] = "Wants",
[NAME_REQUISITE] = "Requisite",
[NAME_SOFT_REQUISITE] = "SoftRequisite",
[NAME_REQUIRED_BY] = "RequiredBy",
[NAME_WANTED_BY] = "WantedBy",
[NAME_CONFLICTS] = "Conflicts",
[NAME_BEFORE] = "Before",
[NAME_AFTER] = "After",
};
void *state;
char *t;
NameDependency d;
assert(n);
@ -448,6 +478,22 @@ void name_dump(Name *n, FILE *f, const char *prefix) {
fprintf(f, "%s ", t);
fprintf(f, "\n");
for (d = 0; d < _NAME_DEPENDENCY_MAX; d++) {
void *state;
Name *other;
if (set_isempty(n->meta.dependencies[d]))
continue;
fprintf(f, "%s\t%s: ", prefix, dependency_table[d]);
SET_FOREACH(other, n->meta.dependencies[d], state)
fprintf(f, "%s ", name_id(other));
fprintf(f, "\n");
}
switch (n->meta.type) {
case NAME_SOCKET: {
int r;
@ -485,3 +531,121 @@ void name_dump(Name *n, FILE *f, const char *prefix) {
free(p);
}
}
static int verify_type(Name *name) {
char *n;
void *state;
assert(name);
/* Checks that all aliases of this name have the same and valid type */
SET_FOREACH(n, name->meta.names, state) {
NameType t;
if ((t = name_type_from_string(n)) == _NAME_TYPE_INVALID)
return -EINVAL;
if (name->meta.type == _NAME_TYPE_INVALID) {
name->meta.type = t;
continue;
}
if (name->meta.type != t)
return -EINVAL;
}
if (name->meta.type == _NAME_TYPE_INVALID)
return -EINVAL;
return 0;
}
static int service_load_sysv(Service *s) {
assert(s);
/* Load service data from SysV init scripts, preferably with
* LSB headers ... */
return -ENOENT;
}
static int name_load_fstab(Name *n) {
assert(n);
assert(n->meta.type == NAME_MOUNT || n->meta.type == NAME_AUTOMOUNT);
/* Load mount data from /etc/fstab */
return 0;
}
static int snapshot_load(Snapshot *s) {
assert(s);
/* Load snapshots from disk */
return 0;
}
static int name_load_dropin(Name *n) {
assert(n);
/* Load dependencies from drop-in directories */
return 0;
}
int name_load(Name *name) {
int r;
assert(name);
if (name->meta.state != NAME_STUB)
return 0;
if ((r = verify_type(name)) < 0)
return r;
if (name->meta.type == NAME_SERVICE) {
/* Load a .service file */
if ((r = name_load_fragment(name)) == 0)
goto finish;
/* Load a classic init script */
if (r == -ENOENT)
if ((r = service_load_sysv(SERVICE(name))) == 0)
goto finish;
} else if (name->meta.type == NAME_MOUNT ||
name->meta.type == NAME_AUTOMOUNT) {
if ((r = name_load_fstab(name)) == 0)
goto finish;
} else if (name->meta.type == NAME_SNAPSHOT) {
if ((r = snapshot_load(SNAPSHOT(name))) == 0)
goto finish;
} else {
if ((r = name_load_fragment(name)) == 0)
goto finish;
}
name->meta.state = NAME_FAILED;
return r;
finish:
if ((r = name_load_dropin(name)) < 0)
return r;
if ((r = name_sanitize(name)) < 0)
return r;
if ((r = name_link_names(name, false)) < 0)
return r;
name->meta.state = NAME_LOADED;
return 0;
}

3
name.h
View File

@ -281,7 +281,8 @@ void name_free(Name *name);
int name_link(Name *name);
int name_link_names(Name *name, bool replace);
int name_merge(Name *name, Name *other);
int name_augment(Name *n);
int name_sanitize(Name *n);
int name_load(Name *name);
const char* name_id(Name *n);
void name_dump(Name *n, FILE *f, const char *prefix);