2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
librc-depend
|
|
|
|
rc service dependency and ordering
|
|
|
|
*/
|
|
|
|
|
2007-11-14 20:52:04 +05:30
|
|
|
/*
|
|
|
|
* Copyright 2007 Gentoo Foundation
|
|
|
|
* Copyright 2007 Roy Marples
|
|
|
|
* All rights reserved
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2007-04-13 19:38:16 +05:30
|
|
|
#include "librc.h"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-08-15 20:19:41 +05:30
|
|
|
#define GENDEP RC_LIBDIR "/sh/gendepends.sh"
|
|
|
|
|
|
|
|
#define RC_DEPCONFIG RC_SVCDIR "/depconfig"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
static const char *bootlevel = NULL;
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
/* We use this so we can pass our char array through many functions */
|
|
|
|
struct lhead
|
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
char **list;
|
2007-04-05 16:48:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static char *get_shell_value (char *string)
|
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
char *p = string;
|
|
|
|
char *e;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
if (! string)
|
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
if (*p == '\'')
|
|
|
|
p++;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
e = p + strlen (p) - 1;
|
|
|
|
if (*e == '\n')
|
|
|
|
*e-- = 0;
|
|
|
|
if (*e == '\'')
|
|
|
|
*e-- = 0;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
if (*p != 0)
|
|
|
|
return p;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
void rc_deptree_free (rc_depinfo_t *deptree)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_depinfo_t *di = deptree;
|
|
|
|
while (di)
|
|
|
|
{
|
|
|
|
rc_depinfo_t *dip = di->next;
|
|
|
|
rc_deptype_t *dt = di->depends;
|
|
|
|
free (di->service);
|
|
|
|
while (dt)
|
|
|
|
{
|
|
|
|
rc_deptype_t *dtp = dt->next;
|
|
|
|
free (dt->type);
|
|
|
|
rc_strlist_free (dt->services);
|
|
|
|
free (dt);
|
|
|
|
dt = dtp;
|
|
|
|
}
|
|
|
|
free (di);
|
|
|
|
di = dip;
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-29 22:12:08 +05:30
|
|
|
librc_hidden_def(rc_deptree_free)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
rc_depinfo_t *rc_deptree_load (void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
FILE *fp;
|
|
|
|
rc_depinfo_t *deptree = NULL;
|
|
|
|
rc_depinfo_t *depinfo = NULL;
|
|
|
|
rc_deptype_t *deptype = NULL;
|
|
|
|
char buffer [RC_LINEBUFFER];
|
|
|
|
char *type;
|
|
|
|
char *p;
|
|
|
|
char *e;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (! (fp = fopen (RC_DEPTREE, "r")))
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp))
|
|
|
|
{
|
|
|
|
p = buffer;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
/* Sanity */
|
|
|
|
e = get_shell_value (p);
|
|
|
|
if (! e || strlen (e) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (! deptree)
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
deptree = xmalloc (sizeof (rc_depinfo_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
depinfo = deptree;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
depinfo->next = xmalloc (sizeof (rc_depinfo_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
depinfo = depinfo->next;
|
|
|
|
}
|
|
|
|
memset (depinfo, 0, sizeof (rc_depinfo_t));
|
2007-10-08 16:46:22 +05:30
|
|
|
depinfo->service = xstrdup (e);
|
2007-07-21 18:19:51 +05:30
|
|
|
deptype = NULL;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
e = strsep (&p, "=");
|
|
|
|
if (! e || sscanf (e, "%d", &i) != 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Sanity */
|
|
|
|
e = get_shell_value (p);
|
|
|
|
if (! e || strlen (e) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (! deptype)
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
depinfo->depends = xmalloc (sizeof (rc_deptype_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
deptype = depinfo->depends;
|
|
|
|
memset (deptype, 0, sizeof (rc_deptype_t));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (strcmp (deptype->type, type) != 0)
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
deptype->next = xmalloc (sizeof (rc_deptype_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
deptype = deptype->next;
|
|
|
|
memset (deptype, 0, sizeof (rc_deptype_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! deptype->type)
|
2007-10-08 16:46:22 +05:30
|
|
|
deptype->type = xstrdup (type);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&deptype->services, e);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return (deptree);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-29 22:12:08 +05:30
|
|
|
librc_hidden_def(rc_deptree_load)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-10-04 19:08:47 +05:30
|
|
|
static rc_depinfo_t *get_depinfo (rc_depinfo_t *deptree, const char *service)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_depinfo_t *di;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
if (! deptree || ! service)
|
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
for (di = deptree; di; di = di->next)
|
|
|
|
if (strcmp (di->service, service) == 0)
|
|
|
|
return (di);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-10-04 19:08:47 +05:30
|
|
|
static rc_deptype_t *get_deptype (rc_depinfo_t *depinfo, const char *type)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_deptype_t *dt;
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
if (! depinfo || !type)
|
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
for (dt = depinfo->depends; dt; dt = dt->next)
|
|
|
|
if (strcmp (dt->type, type) == 0)
|
|
|
|
return (dt);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
return (NULL);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static bool valid_service (const char *runlevel, const char *service)
|
|
|
|
{
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t state = rc_service_state (service);
|
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
return ((strcmp (runlevel, bootlevel) != 0 &&
|
2007-09-25 23:00:07 +05:30
|
|
|
rc_service_in_runlevel (service, bootlevel)) ||
|
|
|
|
rc_service_in_runlevel (service, runlevel) ||
|
2007-09-28 20:23:38 +05:30
|
|
|
state & RC_SERVICE_COLDPLUGGED ||
|
|
|
|
state & RC_SERVICE_STARTED);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static bool get_provided1 (const char *runlevel, struct lhead *providers,
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_deptype_t *deptype,
|
|
|
|
const char *level, bool coldplugged,
|
2007-07-03 03:35:38 +05:30
|
|
|
rc_service_state_t state)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
char *service;
|
|
|
|
int i;
|
|
|
|
bool retval = false;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (deptype->services, service, i)
|
|
|
|
{
|
|
|
|
bool ok = true;
|
2007-09-28 20:23:38 +05:30
|
|
|
rc_service_state_t s = rc_service_state (service);
|
2007-07-21 18:19:51 +05:30
|
|
|
if (level)
|
2007-09-25 23:00:07 +05:30
|
|
|
ok = rc_service_in_runlevel (service, level);
|
2007-07-21 18:19:51 +05:30
|
|
|
else if (coldplugged)
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (s & RC_SERVICE_COLDPLUGGED &&
|
2007-09-25 23:00:07 +05:30
|
|
|
! rc_service_in_runlevel (service, runlevel) &&
|
|
|
|
! rc_service_in_runlevel (service, bootlevel));
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
if (! ok)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (state) {
|
2007-09-28 17:59:23 +05:30
|
|
|
case RC_SERVICE_STARTED:
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (s & RC_SERVICE_STARTED);
|
2007-07-21 18:19:51 +05:30
|
|
|
break;
|
2007-09-28 17:59:23 +05:30
|
|
|
case RC_SERVICE_INACTIVE:
|
|
|
|
case RC_SERVICE_STARTING:
|
|
|
|
case RC_SERVICE_STOPPING:
|
2007-09-28 20:23:38 +05:30
|
|
|
ok = (s & RC_SERVICE_STARTING ||
|
|
|
|
s & RC_SERVICE_STOPPING ||
|
|
|
|
s & RC_SERVICE_INACTIVE);
|
2007-07-21 18:19:51 +05:30
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! ok)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
retval = true;
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&providers->list, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* Work out if a service is provided by another service.
|
|
|
|
For example metalog provides logger.
|
|
|
|
We need to be able to handle syslogd providing logger too.
|
|
|
|
We do this by checking whats running, then what's starting/stopping,
|
|
|
|
then what's run in the runlevels and finally alphabetical order.
|
|
|
|
|
|
|
|
If there are any bugs in rc-depend, they will probably be here as
|
|
|
|
provided dependancy can change depending on runlevel state.
|
|
|
|
*/
|
|
|
|
static char **get_provided (rc_depinfo_t *deptree, rc_depinfo_t *depinfo,
|
2007-07-21 18:19:51 +05:30
|
|
|
const char *runlevel, int options)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_deptype_t *dt;
|
|
|
|
struct lhead providers;
|
|
|
|
char *service;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (! deptree || ! depinfo)
|
|
|
|
return (NULL);
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_service_exists (depinfo->service))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (NULL);
|
|
|
|
|
2007-10-04 19:08:47 +05:30
|
|
|
dt = get_deptype (depinfo, "providedby");
|
2007-07-21 18:19:51 +05:30
|
|
|
if (! dt)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
memset (&providers, 0, sizeof (struct lhead));
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
STRLIST_FOREACH (dt->services, service, i)
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&providers.list, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
return (providers.list);
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
/* If we're strict, then only use what we have in our runlevel
|
|
|
|
* and bootlevel */
|
|
|
|
if (options & RC_DEP_STRICT)
|
|
|
|
{
|
|
|
|
STRLIST_FOREACH (dt->services, service, i)
|
2007-09-25 23:00:07 +05:30
|
|
|
if (rc_service_in_runlevel (service, runlevel) ||
|
|
|
|
rc_service_in_runlevel (service, bootlevel))
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&providers.list, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
if (providers.list)
|
|
|
|
return (providers.list);
|
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
/* OK, we're not strict or there were no services in our runlevel.
|
|
|
|
This is now where the logic gets a little fuzzy :)
|
|
|
|
If there is >1 running service then we return NULL.
|
|
|
|
We do this so we don't hang around waiting for inactive services and
|
|
|
|
our need has already been satisfied as it's not strict.
|
|
|
|
We apply this to our runlevel, coldplugged services, then bootlevel
|
|
|
|
and finally any running.*/
|
|
|
|
#define DO \
|
|
|
|
if (providers.list && providers.list[0] && providers.list[1]) \
|
|
|
|
{ \
|
|
|
|
rc_strlist_free (providers.list); \
|
|
|
|
return (NULL); \
|
|
|
|
} \
|
|
|
|
else if (providers.list) \
|
|
|
|
return providers.list; \
|
|
|
|
|
|
|
|
/* Anything in the runlevel has to come first */
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, runlevel, false, RC_SERVICE_STARTED))
|
2007-07-21 18:19:51 +05:30
|
|
|
{ DO }
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, runlevel, false, RC_SERVICE_STARTING))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, runlevel, false, RC_SERVICE_STOPPED))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
|
|
|
|
|
|
|
/* Check coldplugged services */
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, NULL, true, RC_SERVICE_STARTED))
|
2007-07-21 18:19:51 +05:30
|
|
|
{ DO }
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, NULL, true, RC_SERVICE_STARTING))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
|
|
|
|
|
|
|
/* Check bootlevel if we're not in it */
|
|
|
|
if (bootlevel && strcmp (runlevel, bootlevel) != 0)
|
|
|
|
{
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, bootlevel, false, RC_SERVICE_STARTED))
|
2007-07-21 18:19:51 +05:30
|
|
|
{ DO }
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, bootlevel, false, RC_SERVICE_STARTING))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
2007-07-03 03:35:38 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
/* Check coldplugged services */
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, NULL, true, RC_SERVICE_STOPPED))
|
2007-08-16 22:23:20 +05:30
|
|
|
{ DO }
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-08-16 22:23:20 +05:30
|
|
|
/* Check manually started */
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, NULL, false, RC_SERVICE_STARTED))
|
2007-08-16 22:23:20 +05:30
|
|
|
{ DO }
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, NULL, false, RC_SERVICE_STARTING))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
/* Nothing started then. OK, lets get the stopped services */
|
2007-09-28 17:59:23 +05:30
|
|
|
if (get_provided1 (runlevel, &providers, dt, runlevel, false, RC_SERVICE_STOPPED))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
2007-07-03 03:35:38 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
if (bootlevel && (strcmp (runlevel, bootlevel) != 0)
|
2007-09-28 17:59:23 +05:30
|
|
|
&& (get_provided1 (runlevel, &providers, dt, bootlevel, false, RC_SERVICE_STOPPED)))
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
/* Still nothing? OK, list all services */
|
|
|
|
STRLIST_FOREACH (dt->services, service, i)
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&providers.list, service);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
return (providers.list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-10-15 20:10:53 +05:30
|
|
|
static void visit_service (rc_depinfo_t *deptree, const char * const *types,
|
2007-07-21 18:19:51 +05:30
|
|
|
struct lhead *sorted, struct lhead *visited,
|
|
|
|
rc_depinfo_t *depinfo,
|
|
|
|
const char *runlevel, int options)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
int i, j, k;
|
2007-10-15 20:10:53 +05:30
|
|
|
char *lp;
|
|
|
|
const char *item;
|
2007-07-21 18:19:51 +05:30
|
|
|
char *service;
|
|
|
|
rc_depinfo_t *di;
|
|
|
|
rc_deptype_t *dt;
|
|
|
|
char **provides;
|
|
|
|
char *svcname;
|
|
|
|
|
|
|
|
if (! deptree || !sorted || !visited || !depinfo)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Check if we have already visited this service or not */
|
|
|
|
STRLIST_FOREACH (visited->list, item, i)
|
|
|
|
if (strcmp (item, depinfo->service) == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Add ourselves as a visited service */
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&visited->list, depinfo->service);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
STRLIST_FOREACH (types, item, i)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
if ((dt = get_deptype (depinfo, item)))
|
2007-07-21 18:19:51 +05:30
|
|
|
{
|
|
|
|
STRLIST_FOREACH (dt->services, service, j)
|
|
|
|
{
|
|
|
|
if (! options & RC_DEP_TRACE || strcmp (item, "iprovide") == 0)
|
|
|
|
{
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&sorted->list, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-04 19:08:47 +05:30
|
|
|
di = get_depinfo (deptree, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
if ((provides = get_provided (deptree, di, runlevel, options)))
|
|
|
|
{
|
|
|
|
STRLIST_FOREACH (provides, lp, k)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
di = get_depinfo (deptree, lp);
|
2007-07-21 18:19:51 +05:30
|
|
|
if (di && (strcmp (item, "ineed") == 0 ||
|
2007-08-17 18:53:02 +05:30
|
|
|
strcmp (item, "needsme") == 0 ||
|
2007-07-21 18:19:51 +05:30
|
|
|
valid_service (runlevel, di->service)))
|
|
|
|
visit_service (deptree, types, sorted, visited, di,
|
|
|
|
runlevel, options | RC_DEP_TRACE);
|
|
|
|
}
|
|
|
|
rc_strlist_free (provides);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (di && (strcmp (item, "ineed") == 0 ||
|
2007-08-17 18:53:02 +05:30
|
|
|
strcmp (item, "needsme") == 0 ||
|
2007-07-21 18:19:51 +05:30
|
|
|
valid_service (runlevel, service)))
|
|
|
|
visit_service (deptree, types, sorted, visited, di,
|
|
|
|
runlevel, options | RC_DEP_TRACE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now visit the stuff we provide for */
|
2007-09-29 22:12:08 +05:30
|
|
|
if (options & RC_DEP_TRACE &&
|
2007-10-04 19:08:47 +05:30
|
|
|
(dt = get_deptype (depinfo, "iprovide")))
|
2007-07-21 18:19:51 +05:30
|
|
|
{
|
|
|
|
STRLIST_FOREACH (dt->services, service, i)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
if ((di = get_depinfo (deptree, service)))
|
2007-07-21 18:19:51 +05:30
|
|
|
if ((provides = get_provided (deptree, di, runlevel, options)))
|
|
|
|
{
|
|
|
|
STRLIST_FOREACH (provides, lp, j)
|
|
|
|
if (strcmp (lp, depinfo->service) == 0)
|
|
|
|
{
|
|
|
|
visit_service (deptree, types, sorted, visited, di,
|
|
|
|
runlevel, options | RC_DEP_TRACE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc_strlist_free (provides);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We've visited everything we need, so add ourselves unless we
|
|
|
|
are also the service calling us or we are provided by something */
|
|
|
|
svcname = getenv("SVCNAME");
|
|
|
|
if (! svcname || strcmp (svcname, depinfo->service) != 0)
|
2007-10-04 19:08:47 +05:30
|
|
|
if (! get_deptype (depinfo, "providedby"))
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_add (&sorted->list, depinfo->service);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
char **rc_deptree_depends (rc_depinfo_t *deptree,
|
2007-10-15 21:33:49 +05:30
|
|
|
const char **types, const char **services,
|
2007-09-29 22:12:08 +05:30
|
|
|
const char *runlevel, int options)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
struct lhead sorted;
|
|
|
|
struct lhead visited;
|
|
|
|
rc_depinfo_t *di;
|
2007-10-15 20:10:53 +05:30
|
|
|
const char *service;
|
2007-07-21 18:19:51 +05:30
|
|
|
int i;
|
|
|
|
|
2007-10-04 19:08:47 +05:30
|
|
|
if (! deptree || ! services)
|
2007-07-21 18:19:51 +05:30
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
memset (&sorted, 0, sizeof (struct lhead));
|
|
|
|
memset (&visited, 0, sizeof (struct lhead));
|
|
|
|
|
|
|
|
bootlevel = getenv ("RC_BOOTLEVEL");
|
|
|
|
if (! bootlevel)
|
|
|
|
bootlevel = RC_LEVEL_BOOT;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (services, service, i)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
if (! (di = get_depinfo (deptree, service))) {
|
|
|
|
errno = ENOENT;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (types)
|
|
|
|
visit_service (deptree, types, &sorted, &visited,
|
|
|
|
di, runlevel, options);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
rc_strlist_free (visited.list);
|
|
|
|
return (sorted.list);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-29 22:12:08 +05:30
|
|
|
librc_hidden_def(rc_deptree_depends)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-10-15 20:10:53 +05:30
|
|
|
static const const char *order_types[] = { "ineed", "iuse", "iafter", NULL };
|
2007-10-10 18:41:35 +05:30
|
|
|
char **rc_deptree_order (rc_depinfo_t *deptree, const char *runlevel,
|
|
|
|
int options)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
char **list = NULL;
|
|
|
|
char **services = NULL;
|
|
|
|
bool reverse = false;
|
2007-09-18 21:13:19 +05:30
|
|
|
char **tmp = NULL;
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
if (! runlevel)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
bootlevel = getenv ("RC_BOOTLEVEL");
|
|
|
|
if (! bootlevel)
|
|
|
|
bootlevel = RC_LEVEL_BOOT;
|
|
|
|
|
|
|
|
/* When shutting down, list all running services */
|
|
|
|
if (strcmp (runlevel, RC_LEVEL_SINGLE) == 0 ||
|
|
|
|
strcmp (runlevel, RC_LEVEL_SHUTDOWN) == 0 ||
|
|
|
|
strcmp (runlevel, RC_LEVEL_REBOOT) == 0)
|
|
|
|
{
|
2007-10-05 15:46:14 +05:30
|
|
|
list = rc_services_in_state (RC_SERVICE_STARTED);
|
2007-09-18 21:13:19 +05:30
|
|
|
|
2007-10-05 15:46:14 +05:30
|
|
|
tmp = rc_services_in_state (RC_SERVICE_INACTIVE);
|
2007-09-18 21:13:19 +05:30
|
|
|
rc_strlist_join (&list, tmp);
|
|
|
|
rc_strlist_free (tmp);
|
|
|
|
|
2007-10-05 15:46:14 +05:30
|
|
|
tmp = rc_services_in_state (RC_SERVICE_STARTING);
|
2007-09-18 21:13:19 +05:30
|
|
|
rc_strlist_join (&list, tmp);
|
|
|
|
rc_strlist_free (tmp);
|
2007-07-21 18:19:51 +05:30
|
|
|
reverse = true;
|
2007-09-18 17:34:51 +05:30
|
|
|
} else {
|
2007-07-21 18:19:51 +05:30
|
|
|
list = rc_services_in_runlevel (runlevel);
|
|
|
|
|
|
|
|
/* Add coldplugged services */
|
2007-10-05 15:46:14 +05:30
|
|
|
tmp = rc_services_in_state (RC_SERVICE_COLDPLUGGED);
|
2007-09-18 21:13:19 +05:30
|
|
|
rc_strlist_join (&list, tmp);
|
|
|
|
rc_strlist_free (tmp);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
/* If we're not the boot runlevel then add that too */
|
2007-09-18 17:34:51 +05:30
|
|
|
if (strcmp (runlevel, bootlevel) != 0) {
|
2007-10-05 15:46:14 +05:30
|
|
|
tmp = rc_services_in_runlevel (bootlevel);
|
2007-09-18 21:13:19 +05:30
|
|
|
rc_strlist_join (&list, tmp);
|
|
|
|
rc_strlist_free (tmp);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
2007-09-18 21:13:19 +05:30
|
|
|
}
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
/* Now we have our lists, we need to pull in any dependencies
|
|
|
|
and order them */
|
2007-10-15 20:10:53 +05:30
|
|
|
services = rc_deptree_depends (deptree, order_types, (const char **) list,
|
|
|
|
runlevel,
|
2007-09-29 22:12:08 +05:30
|
|
|
RC_DEP_STRICT | RC_DEP_TRACE | options);
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_strlist_free (list);
|
|
|
|
|
|
|
|
if (reverse)
|
|
|
|
rc_strlist_reverse (services);
|
|
|
|
|
|
|
|
return (services);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-10-10 18:41:35 +05:30
|
|
|
librc_hidden_def(rc_deptree_order)
|
2007-04-05 16:48:42 +05:30
|
|
|
|
|
|
|
static bool is_newer_than (const char *file, const char *target)
|
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
struct stat buf;
|
|
|
|
time_t mtime;
|
2007-10-04 22:24:29 +05:30
|
|
|
bool newer = true;
|
2007-10-05 15:46:14 +05:30
|
|
|
DIR *dp;
|
|
|
|
struct dirent *d;
|
|
|
|
char *path;
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
if (stat (file, &buf) != 0 || buf.st_size == 0)
|
|
|
|
return (false);
|
|
|
|
mtime = buf.st_mtime;
|
|
|
|
|
|
|
|
/* Of course we are newever than targets that don't exist
|
|
|
|
Such as broken symlinks */
|
|
|
|
if (stat (target, &buf) != 0)
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
if (mtime < buf.st_mtime)
|
|
|
|
return (false);
|
|
|
|
|
2007-10-05 15:46:14 +05:30
|
|
|
if (! (dp = opendir (target)))
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
while ((d = readdir (dp))) {
|
|
|
|
if (d->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path = rc_strcatpaths (target, d->d_name, (char *) NULL);
|
2007-10-04 22:24:29 +05:30
|
|
|
newer = is_newer_than (file, path);
|
|
|
|
free (path);
|
|
|
|
if (! newer)
|
|
|
|
break;
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
2007-10-05 15:46:14 +05:30
|
|
|
closedir (dp);
|
|
|
|
|
2007-10-04 22:24:29 +05:30
|
|
|
return (newer);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct deppair
|
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
const char *depend;
|
|
|
|
const char *addto;
|
2007-04-05 16:48:42 +05:30
|
|
|
} deppair_t;
|
|
|
|
|
|
|
|
static const deppair_t deppairs[] = {
|
2007-07-21 18:19:51 +05:30
|
|
|
{ "ineed", "needsme" },
|
|
|
|
{ "iuse", "usesme" },
|
|
|
|
{ "iafter", "ibefore" },
|
|
|
|
{ "ibefore", "iafter" },
|
|
|
|
{ "iprovide", "providedby" },
|
|
|
|
{ NULL, NULL }
|
2007-04-05 16:48:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static const char *depdirs[] =
|
|
|
|
{
|
2007-08-09 20:03:20 +05:30
|
|
|
RC_SVCDIR "/starting",
|
|
|
|
RC_SVCDIR "/started",
|
|
|
|
RC_SVCDIR "/stopping",
|
|
|
|
RC_SVCDIR "/inactive",
|
|
|
|
RC_SVCDIR "/wasinactive",
|
|
|
|
RC_SVCDIR "/failed",
|
|
|
|
RC_SVCDIR "/coldplugged",
|
|
|
|
RC_SVCDIR "/daemons",
|
|
|
|
RC_SVCDIR "/options",
|
|
|
|
RC_SVCDIR "/exclusive",
|
|
|
|
RC_SVCDIR "/scheduled",
|
2007-07-21 18:19:51 +05:30
|
|
|
NULL
|
2007-04-05 16:48:42 +05:30
|
|
|
};
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
bool rc_deptree_update_needed (void)
|
|
|
|
{
|
|
|
|
bool newer = false;
|
|
|
|
char **config;
|
|
|
|
char *service;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Create base directories if needed */
|
|
|
|
for (i = 0; depdirs[i]; i++)
|
|
|
|
if (mkdir (depdirs[i], 0755) != 0 && errno != EEXIST)
|
2007-10-23 15:36:15 +05:30
|
|
|
fprintf (stderr, "mkdir `%s': %s\n", depdirs[i], strerror (errno));
|
2007-09-29 22:12:08 +05:30
|
|
|
|
|
|
|
/* Quick test to see if anything we use has changed */
|
|
|
|
if (! is_newer_than (RC_DEPTREE, RC_INITDIR) ||
|
|
|
|
! is_newer_than (RC_DEPTREE, RC_CONFDIR) ||
|
|
|
|
! is_newer_than (RC_DEPTREE, "/etc/rc.conf"))
|
|
|
|
return (true);
|
|
|
|
|
|
|
|
/* Some init scripts dependencies change depending on config files
|
|
|
|
* outside of baselayout, like syslog-ng, so we check those too. */
|
2007-10-03 20:13:05 +05:30
|
|
|
config = rc_config_list (RC_DEPCONFIG);
|
2007-09-29 22:12:08 +05:30
|
|
|
STRLIST_FOREACH (config, service, i) {
|
|
|
|
if (! is_newer_than (RC_DEPTREE, service)) {
|
|
|
|
newer = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rc_strlist_free (config);
|
|
|
|
|
|
|
|
return (newer);
|
|
|
|
}
|
|
|
|
librc_hidden_def(rc_deptree_update_needed)
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
/* This is a 5 phase operation
|
|
|
|
Phase 1 is a shell script which loads each init script and config in turn
|
|
|
|
and echos their dependency info to stdout
|
|
|
|
Phase 2 takes that and populates a depinfo object with that data
|
|
|
|
Phase 3 adds any provided services to the depinfo object
|
|
|
|
Phase 4 scans that depinfo object and puts in backlinks
|
|
|
|
Phase 5 saves the depinfo object to disk
|
|
|
|
*/
|
2007-10-04 19:08:47 +05:30
|
|
|
bool rc_deptree_update (void)
|
2007-04-05 16:48:42 +05:30
|
|
|
{
|
2007-07-21 18:19:51 +05:30
|
|
|
char *depends;
|
|
|
|
char *service;
|
|
|
|
char *type;
|
|
|
|
char *depend;
|
2007-08-15 20:19:41 +05:30
|
|
|
char **config = NULL;
|
2007-10-04 19:08:47 +05:30
|
|
|
int retval = true;
|
2007-07-21 18:19:51 +05:30
|
|
|
FILE *fp;
|
|
|
|
rc_depinfo_t *deptree;
|
|
|
|
rc_depinfo_t *depinfo;
|
|
|
|
rc_depinfo_t *di;
|
|
|
|
rc_depinfo_t *last_depinfo = NULL;
|
2007-08-15 20:19:41 +05:30
|
|
|
rc_deptype_t *deptype = NULL;
|
2007-07-21 18:19:51 +05:30
|
|
|
rc_deptype_t *dt;
|
|
|
|
rc_deptype_t *last_deptype = NULL;
|
2007-10-12 05:31:33 +05:30
|
|
|
char *buffer = NULL;
|
2007-07-21 18:19:51 +05:30
|
|
|
int len;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
int k;
|
|
|
|
bool already_added;
|
|
|
|
|
|
|
|
/* Some init scripts need RC_LIBDIR to source stuff
|
|
|
|
Ideally we should be setting our full env instead */
|
|
|
|
if (! getenv ("RC_LIBDIR"))
|
|
|
|
setenv ("RC_LIBDIR", RC_LIBDIR, 0);
|
|
|
|
|
|
|
|
/* Phase 1 */
|
|
|
|
if (! (fp = popen (GENDEP, "r")))
|
2007-10-04 19:08:47 +05:30
|
|
|
return (false);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
2007-10-08 16:41:21 +05:30
|
|
|
deptree = xmalloc (sizeof (rc_depinfo_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
memset (deptree, 0, sizeof (rc_depinfo_t));
|
2007-10-12 05:31:33 +05:30
|
|
|
buffer = xmalloc (sizeof (char) * RC_LINEBUFFER);
|
2007-07-21 18:19:51 +05:30
|
|
|
memset (buffer, 0, RC_LINEBUFFER);
|
|
|
|
|
|
|
|
/* Phase 2 */
|
|
|
|
while (fgets (buffer, RC_LINEBUFFER, fp))
|
|
|
|
{
|
|
|
|
/* Trim the newline */
|
|
|
|
if (buffer[strlen (buffer) - 1] == '\n')
|
|
|
|
buffer[strlen(buffer) -1] = 0;
|
|
|
|
|
|
|
|
depends = buffer;
|
|
|
|
service = strsep (&depends, " ");
|
|
|
|
if (! service)
|
|
|
|
continue;
|
|
|
|
type = strsep (&depends, " ");
|
|
|
|
|
|
|
|
for (depinfo = deptree; depinfo; depinfo = depinfo->next)
|
|
|
|
{
|
|
|
|
last_depinfo = depinfo;
|
|
|
|
if (depinfo->service && strcmp (depinfo->service, service) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! depinfo)
|
|
|
|
{
|
|
|
|
if (! last_depinfo->service)
|
|
|
|
depinfo = last_depinfo;
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
last_depinfo->next = xmalloc (sizeof (rc_depinfo_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
depinfo = last_depinfo->next;
|
|
|
|
}
|
|
|
|
memset (depinfo, 0, sizeof (rc_depinfo_t));
|
2007-10-08 16:46:22 +05:30
|
|
|
depinfo->service = xstrdup (service);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/* We may not have any depends */
|
|
|
|
if (! type || ! depends)
|
|
|
|
continue;
|
|
|
|
|
2007-08-15 20:19:41 +05:30
|
|
|
/* Get the type */
|
|
|
|
if (strcmp (type, "config") != 0) {
|
|
|
|
last_deptype = NULL;
|
|
|
|
for (deptype = depinfo->depends; deptype; deptype = deptype->next)
|
2007-07-21 18:19:51 +05:30
|
|
|
{
|
2007-08-15 20:19:41 +05:30
|
|
|
last_deptype = deptype;
|
|
|
|
if (strcmp (deptype->type, type) == 0)
|
|
|
|
break;
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
2007-08-15 20:19:41 +05:30
|
|
|
|
|
|
|
if (! deptype)
|
2007-07-21 18:19:51 +05:30
|
|
|
{
|
2007-08-15 20:19:41 +05:30
|
|
|
if (! last_deptype)
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
depinfo->depends = xmalloc (sizeof (rc_deptype_t));
|
2007-08-15 20:19:41 +05:30
|
|
|
deptype = depinfo->depends;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
last_deptype->next = xmalloc (sizeof (rc_deptype_t));
|
2007-08-15 20:19:41 +05:30
|
|
|
deptype = last_deptype->next;
|
|
|
|
}
|
|
|
|
memset (deptype, 0, sizeof (rc_deptype_t));
|
2007-10-08 16:46:22 +05:30
|
|
|
deptype->type = xstrdup (type);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now add each depend to our type.
|
|
|
|
We do this individually so we handle multiple spaces gracefully */
|
|
|
|
while ((depend = strsep (&depends, " ")))
|
|
|
|
{
|
|
|
|
if (depend[0] == 0)
|
|
|
|
continue;
|
|
|
|
|
2007-08-15 20:19:41 +05:30
|
|
|
if (strcmp (type, "config") == 0) {
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&config, depend);
|
2007-08-15 20:19:41 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-07-21 18:19:51 +05:30
|
|
|
/* .sh files are not init scripts */
|
|
|
|
len = strlen (depend);
|
|
|
|
if (len > 2 &&
|
|
|
|
depend[len - 3] == '.' &&
|
|
|
|
depend[len - 2] == 's' &&
|
|
|
|
depend[len - 1] == 'h')
|
|
|
|
continue;
|
|
|
|
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&deptype->services, depend);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
pclose (fp);
|
2007-10-12 05:31:33 +05:30
|
|
|
free (buffer);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
/* Phase 3 - add our providors to the tree */
|
|
|
|
for (depinfo = deptree; depinfo; depinfo = depinfo->next)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
if ((deptype = get_deptype (depinfo, "iprovide")))
|
2007-07-21 18:19:51 +05:30
|
|
|
STRLIST_FOREACH (deptype->services, service, i)
|
|
|
|
{
|
|
|
|
for (di = deptree; di; di = di->next)
|
|
|
|
{
|
|
|
|
last_depinfo = di;
|
|
|
|
if (strcmp (di->service, service) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! di)
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
last_depinfo->next = xmalloc (sizeof (rc_depinfo_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
di = last_depinfo->next;
|
|
|
|
memset (di, 0, sizeof (rc_depinfo_t));
|
2007-10-08 16:46:22 +05:30
|
|
|
di->service = xstrdup (service);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Phase 4 - backreference our depends */
|
|
|
|
for (depinfo = deptree; depinfo; depinfo = depinfo->next)
|
|
|
|
{
|
|
|
|
for (i = 0; deppairs[i].depend; i++)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
deptype = get_deptype (depinfo, deppairs[i].depend);
|
2007-07-21 18:19:51 +05:30
|
|
|
if (! deptype)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
STRLIST_FOREACH (deptype->services, service, j)
|
|
|
|
{
|
2007-10-04 19:08:47 +05:30
|
|
|
di = get_depinfo (deptree, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
if (! di)
|
|
|
|
{
|
|
|
|
if (strcmp (deptype->type, "ineed") == 0)
|
|
|
|
{
|
2007-09-29 22:12:08 +05:30
|
|
|
fprintf (stderr,
|
2007-10-23 15:36:15 +05:30
|
|
|
"Service `%s' needs non existant service `%s'\n",
|
2007-09-29 22:12:08 +05:30
|
|
|
depinfo->service, service);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add our deptype now */
|
|
|
|
last_deptype = NULL;
|
|
|
|
for (dt = di->depends; dt; dt = dt->next)
|
|
|
|
{
|
|
|
|
last_deptype = dt;
|
|
|
|
if (strcmp (dt->type, deppairs[i].addto) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (! dt)
|
|
|
|
{
|
|
|
|
if (! last_deptype)
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
di->depends = xmalloc (sizeof (rc_deptype_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
dt = di->depends;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-08 16:41:21 +05:30
|
|
|
last_deptype->next = xmalloc (sizeof (rc_deptype_t));
|
2007-07-21 18:19:51 +05:30
|
|
|
dt = last_deptype->next;
|
|
|
|
}
|
|
|
|
memset (dt, 0, sizeof (rc_deptype_t));
|
2007-10-08 16:46:22 +05:30
|
|
|
dt->type = xstrdup (deppairs[i].addto);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
already_added = false;
|
|
|
|
STRLIST_FOREACH (dt->services, service, k)
|
|
|
|
if (strcmp (service, depinfo->service) == 0)
|
|
|
|
{
|
|
|
|
already_added = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! already_added)
|
2007-09-18 17:06:55 +05:30
|
|
|
rc_strlist_addsort (&dt->services, depinfo->service);
|
2007-07-21 18:19:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Phase 5 - save to disk
|
|
|
|
Now that we're purely in C, do we need to keep a shell parseable file?
|
|
|
|
I think yes as then it stays human readable
|
|
|
|
This works and should be entirely shell parseable provided that depend
|
|
|
|
names don't have any non shell variable characters in
|
|
|
|
*/
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((fp = fopen (RC_DEPTREE, "w"))) {
|
2007-07-21 18:19:51 +05:30
|
|
|
i = 0;
|
|
|
|
for (depinfo = deptree; depinfo; depinfo = depinfo->next)
|
|
|
|
{
|
|
|
|
fprintf (fp, "depinfo_%d_service='%s'\n", i, depinfo->service);
|
|
|
|
for (deptype = depinfo->depends; deptype; deptype = deptype->next)
|
|
|
|
{
|
|
|
|
k = 0;
|
|
|
|
STRLIST_FOREACH (deptype->services, service, j)
|
|
|
|
{
|
|
|
|
fprintf (fp, "depinfo_%d_%s_%d='%s'\n", i, deptype->type,
|
|
|
|
k, service);
|
|
|
|
k++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
fclose (fp);
|
2007-10-04 19:08:47 +05:30
|
|
|
} else {
|
|
|
|
fprintf (stderr, "fopen `%s': %s\n", RC_DEPTREE, strerror (errno));
|
|
|
|
retval = false;
|
|
|
|
}
|
2007-07-21 18:19:51 +05:30
|
|
|
|
2007-08-15 20:19:41 +05:30
|
|
|
/* Save our external config files to disk */
|
|
|
|
if (config) {
|
2007-09-29 22:12:08 +05:30
|
|
|
if ((fp = fopen (RC_DEPCONFIG, "w"))) {
|
2007-08-15 20:19:41 +05:30
|
|
|
STRLIST_FOREACH (config, service, i)
|
|
|
|
fprintf (fp, "%s\n", service);
|
|
|
|
fclose (fp);
|
2007-10-04 19:08:47 +05:30
|
|
|
} else {
|
2007-09-29 22:12:08 +05:30
|
|
|
fprintf (stderr, "fopen `%s': %s\n", RC_DEPCONFIG, strerror (errno));
|
2007-10-04 19:08:47 +05:30
|
|
|
retval = false;
|
|
|
|
}
|
2007-08-15 20:19:41 +05:30
|
|
|
rc_strlist_free (config);
|
|
|
|
}
|
|
|
|
|
2007-09-29 22:12:08 +05:30
|
|
|
rc_deptree_free (deptree);
|
2007-07-21 18:19:51 +05:30
|
|
|
|
|
|
|
return (retval);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|
2007-09-29 22:12:08 +05:30
|
|
|
librc_hidden_def(rc_deptree_update)
|