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