This commit is contained in:
Roy Marples 2008-06-04 12:44:45 +00:00
parent 587051ec67
commit 83ade5b701
2 changed files with 118 additions and 144 deletions

View File

@ -37,7 +37,8 @@
static const char *bootlevel = NULL;
static char *get_shell_value(char *string)
static char *
get_shell_value(char *string)
{
char *p = string;
char *e;
@ -60,7 +61,8 @@ static char *get_shell_value(char *string)
return NULL;
}
void rc_deptree_free(RC_DEPTREE *deptree)
void
rc_deptree_free(RC_DEPTREE *deptree)
{
RC_DEPINFO *di;
RC_DEPINFO *di2;
@ -71,12 +73,10 @@ void rc_deptree_free(RC_DEPTREE *deptree)
return;
di = STAILQ_FIRST(deptree);
while (di)
{
while (di) {
di2 = STAILQ_NEXT(di, entries);
dt = STAILQ_FIRST(&di->depends);
while (dt)
{
while (dt) {
dt2 = STAILQ_NEXT(dt, entries);
rc_stringlist_free(dt->services);
free(dt->type);
@ -91,31 +91,30 @@ void rc_deptree_free(RC_DEPTREE *deptree)
}
librc_hidden_def(rc_deptree_free)
static RC_DEPINFO *get_depinfo(const RC_DEPTREE *deptree,
const char *service)
static RC_DEPINFO *
get_depinfo(const RC_DEPTREE *deptree, const char *service)
{
RC_DEPINFO *di;
STAILQ_FOREACH(di, deptree, entries)
if (strcmp(di->service, service) == 0)
return di;
return NULL;
}
static RC_DEPTYPE *get_deptype(const RC_DEPINFO *depinfo,
const char *type)
static RC_DEPTYPE *
get_deptype(const RC_DEPINFO *depinfo, const char *type)
{
RC_DEPTYPE *dt;
STAILQ_FOREACH(dt, &depinfo->depends, entries)
if (strcmp(dt->type, type) == 0)
return dt;
return NULL;
}
RC_DEPTREE *rc_deptree_load(void)
RC_DEPTREE *
rc_deptree_load(void)
{
FILE *fp;
RC_DEPTREE *deptree;
@ -133,28 +132,22 @@ RC_DEPTREE *rc_deptree_load(void)
deptree = xmalloc(sizeof(*deptree));
STAILQ_INIT(deptree);
while ((rc_getline(&line, &len, fp)))
{
p = line;
e = strsep(&p, "_");
if (!e || strcmp(e, "depinfo") != 0)
continue;
e = strsep (&p, "_");
if (!e || sscanf(e, "%d", &i) != 1)
continue;
if (!(type = strsep(&p, "_=")))
continue;
if (strcmp(type, "service") == 0)
{
if (strcmp(type, "service") == 0) {
/* Sanity */
e = get_shell_value(p);
if (! e || *e == '\0')
continue;
depinfo = xmalloc(sizeof(*depinfo));
STAILQ_INIT(&depinfo->depends);
depinfo->service = xstrdup(e);
@ -162,23 +155,19 @@ RC_DEPTREE *rc_deptree_load(void)
deptype = NULL;
continue;
}
e = strsep(&p, "=");
if (!e || sscanf(e, "%d", &i) != 1)
continue;
/* Sanity */
e = get_shell_value(p);
if (!e || *e == '\0')
continue;
if (!deptype || strcmp(deptype->type, type) != 0) {
deptype = xmalloc(sizeof(*deptype));
deptype->services = rc_stringlist_new();
deptype->type = xstrdup(type);
STAILQ_INSERT_TAIL(&depinfo->depends, deptype, entries);
}
rc_stringlist_add(deptype->services, e);
}
fclose(fp);
@ -188,7 +177,8 @@ RC_DEPTREE *rc_deptree_load(void)
}
librc_hidden_def(rc_deptree_load)
static bool valid_service(const char *runlevel, const char *service)
static bool
valid_service(const char *runlevel, const char *service)
{
RC_SERVICE state = rc_service_state(service);
@ -199,10 +189,10 @@ static bool valid_service(const char *runlevel, const char *service)
state & RC_SERVICE_STARTED);
}
static bool get_provided1(const char *runlevel, RC_STRINGLIST *providers,
RC_DEPTYPE *deptype,
const char *level, bool coldplugged,
RC_SERVICE state)
static bool
get_provided1(const char *runlevel, RC_STRINGLIST *providers,
RC_DEPTYPE *deptype, const char *level,
bool coldplugged, RC_SERVICE state)
{
RC_STRING *service;
RC_SERVICE st;
@ -221,10 +211,8 @@ static bool get_provided1(const char *runlevel, RC_STRINGLIST *providers,
ok = (st & RC_SERVICE_COLDPLUGGED &&
!rc_service_in_runlevel(svc, runlevel) &&
!rc_service_in_runlevel(svc, bootlevel));
if (!ok)
continue;
switch (state) {
case RC_SERVICE_STARTED:
ok = (st & RC_SERVICE_STARTED);
@ -239,10 +227,8 @@ static bool get_provided1(const char *runlevel, RC_STRINGLIST *providers,
default:
break;
}
if (!ok)
continue;
retval = true;
rc_stringlist_add(providers, svc);
}
@ -259,8 +245,8 @@ static bool get_provided1(const char *runlevel, RC_STRINGLIST *providers,
If there are any bugs in rc-depend, they will probably be here as
provided dependancy can change depending on runlevel state.
*/
static RC_STRINGLIST *get_provided (const RC_DEPINFO *depinfo,
const char *runlevel, int options)
static RC_STRINGLIST *
get_provided (const RC_DEPINFO *depinfo, const char *runlevel, int options)
{
RC_DEPTYPE *dt;
RC_STRINGLIST *providers = rc_stringlist_new();
@ -273,8 +259,7 @@ static RC_STRINGLIST *get_provided (const RC_DEPINFO *depinfo,
/* If we are stopping then all depends are true, regardless of state.
This is especially true for net services as they could force a restart
of the local dns resolver which may depend on net. */
if (options & RC_DEP_STOP)
{
if (options & RC_DEP_STOP) {
TAILQ_FOREACH(service, dt->services, entries)
rc_stringlist_add(providers, service->value);
return providers;
@ -282,16 +267,13 @@ static RC_STRINGLIST *get_provided (const RC_DEPINFO *depinfo,
/* If we're strict or startng, then only use what we have in our
* runlevel and bootlevel. If we starting then check cold-plugged too. */
if (options & RC_DEP_STRICT || options & RC_DEP_START)
{
if (options & RC_DEP_STRICT || options & RC_DEP_START) {
TAILQ_FOREACH(service, dt->services, entries)
if (rc_service_in_runlevel(service->value, runlevel) ||
rc_service_in_runlevel(service->value, bootlevel) ||
(options & RC_DEP_START &&
rc_service_state(service->value) & RC_SERVICE_COLDPLUGGED))
rc_stringlist_add(providers, service->value);
if (TAILQ_FIRST(providers))
return providers;
}
@ -360,7 +342,8 @@ static RC_STRINGLIST *get_provided (const RC_DEPINFO *depinfo,
return providers;
}
static void visit_service(const RC_DEPTREE *deptree,
static void
visit_service(const RC_DEPTREE *deptree,
const RC_STRINGLIST *types,
RC_STRINGLIST **sorted,
RC_STRINGLIST *visited,
@ -379,7 +362,6 @@ static void visit_service(const RC_DEPTREE *deptree,
TAILQ_FOREACH(type, visited, entries)
if (strcmp(type->value, depinfo->service) == 0)
return;
/* Add ourselves as a visited service */
rc_stringlist_add(visited, depinfo->service);
@ -389,7 +371,7 @@ static void visit_service(const RC_DEPTREE *deptree,
continue;
TAILQ_FOREACH(service, dt->services, entries) {
if (! options & RC_DEP_TRACE ||
if (!(options & RC_DEP_TRACE) ||
strcmp(type->value, "iprovide") == 0)
{
if (!*sorted)
@ -453,7 +435,8 @@ static void visit_service(const RC_DEPTREE *deptree,
}
}
RC_STRINGLIST *rc_deptree_depend(const RC_DEPTREE *deptree,
RC_STRINGLIST *
rc_deptree_depend(const RC_DEPTREE *deptree,
const char *service, const char *type)
{
RC_DEPINFO *di;
@ -472,12 +455,12 @@ RC_STRINGLIST *rc_deptree_depend(const RC_DEPTREE *deptree,
svcs = rc_stringlist_new();
TAILQ_FOREACH(svc, dt->services, entries)
rc_stringlist_add(svcs, svc->value);
return svcs;
}
librc_hidden_def(rc_deptree_depend)
RC_STRINGLIST *rc_deptree_depends(const RC_DEPTREE *deptree,
RC_STRINGLIST *
rc_deptree_depends(const RC_DEPTREE *deptree,
const RC_STRINGLIST *types,
const RC_STRINGLIST *services,
const char *runlevel, int options)
@ -490,7 +473,6 @@ RC_STRINGLIST *rc_deptree_depends(const RC_DEPTREE *deptree,
bootlevel = getenv("RC_BOOTLEVEL");
if (!bootlevel)
bootlevel = RC_LEVEL_BOOT;
TAILQ_FOREACH(service, services, entries) {
if (!(di = get_depinfo(deptree, service->value))) {
errno = ENOENT;
@ -500,14 +482,13 @@ RC_STRINGLIST *rc_deptree_depends(const RC_DEPTREE *deptree,
visit_service(deptree, types, &sorted, visited,
di, runlevel, options);
}
rc_stringlist_free(visited);
return sorted;
}
librc_hidden_def(rc_deptree_depends)
RC_STRINGLIST *rc_deptree_order(const RC_DEPTREE *deptree,
const char *runlevel, int options)
RC_STRINGLIST *
rc_deptree_order(const RC_DEPTREE *deptree, const char *runlevel, int options)
{
RC_STRINGLIST *list;
RC_STRINGLIST *list2;
@ -571,17 +552,16 @@ RC_STRINGLIST *rc_deptree_order(const RC_DEPTREE *deptree,
rc_stringlist_add(types, "ineed");
rc_stringlist_add(types, "iuse");
rc_stringlist_add(types, "iafter");
services = rc_deptree_depends(deptree, types, list, runlevel,
RC_DEP_STRICT | RC_DEP_TRACE | options);
rc_stringlist_free(list);
rc_stringlist_free(types);
return services;
}
librc_hidden_def(rc_deptree_order)
bool rc_newer_than(const char *source, const char *target)
bool
rc_newer_than(const char *source, const char *target)
{
struct stat buf;
time_t mtime;
@ -600,10 +580,8 @@ bool rc_newer_than(const char *source, const char *target)
such as broken symlinks */
if (stat(target, &buf) != 0)
return true;
if (mtime < buf.st_mtime)
return false;
/* If not a dir then reset errno */
if (!(dp = opendir(target))) {
errno = serrno;
@ -614,14 +592,12 @@ bool rc_newer_than(const char *source, const char *target)
while ((d = readdir(dp))) {
if (d->d_name[0] == '.')
continue;
snprintf(path, sizeof(path), "%s/%s", target, d->d_name);
newer = rc_newer_than(source, path);
if (! newer)
break;
}
closedir(dp);
return newer;
}
librc_hidden_def(rc_newer_than)
@ -658,7 +634,8 @@ static const char *const depdirs[] =
NULL
};
bool rc_deptree_update_needed(void)
bool
rc_deptree_update_needed(void)
{
bool newer = false;
RC_STRINGLIST *config;
@ -702,7 +679,6 @@ bool rc_deptree_update_needed(void)
}
rc_stringlist_free(config);
}
return newer;
}
librc_hidden_def(rc_deptree_update_needed)
@ -715,7 +691,8 @@ librc_hidden_def(rc_deptree_update_needed)
Phase 4 scans that depinfo object and puts in backlinks
Phase 5 saves the depinfo object to disk
*/
bool rc_deptree_update(void)
bool
rc_deptree_update(void)
{
FILE *fp;
RC_DEPTREE *deptree;
@ -754,9 +731,7 @@ bool rc_deptree_update(void)
deptree = xmalloc(sizeof(*deptree));
STAILQ_INIT(deptree);
config = rc_stringlist_new();
while ((rc_getline(&line, &len, fp)))
{
depends = line;
@ -967,7 +942,6 @@ bool rc_deptree_update(void)
}
rc_deptree_free(deptree);
return retval;
}
librc_hidden_def(rc_deptree_update)

View File

@ -256,13 +256,13 @@ const char *rc_sys(void);
* These options can change the services found by the rc_get_depinfo and
* rc_get_depends functions. */
/*! Trace provided services */
#define RC_DEP_TRACE 0x01
#define RC_DEP_TRACE (1<<0)
/*! Only use services added to runlevels */
#define RC_DEP_STRICT 0x02
#define RC_DEP_STRICT (1<<1)
/*! Runlevel is starting */
#define RC_DEP_START 0x04
#define RC_DEP_START (1<<2)
/*! Runlevel is stopping */
#define RC_DEP_STOP 0x08
#define RC_DEP_STOP (1<<3)
/*! @name Dependencies
* We analyse each init script and cache the resultant dependency tree.