2007-04-05 11:18:42 +00:00
|
|
|
/*
|
2015-12-04 16:52:19 -06:00
|
|
|
* rc-depend
|
|
|
|
* rc service dependency and ordering
|
|
|
|
*/
|
2007-04-05 11:18:42 +00:00
|
|
|
|
2008-01-14 05:05:22 +00:00
|
|
|
/*
|
2015-12-04 16:52:19 -06:00
|
|
|
* Copyright (c) 2007-2015 The OpenRC Authors.
|
|
|
|
* See the Authors file at the top-level directory of this distribution and
|
2021-12-20 20:07:00 -05:00
|
|
|
* https://github.com/OpenRC/openrc/blob/HEAD/AUTHORS
|
2007-11-14 15:22:04 +00:00
|
|
|
*
|
2015-12-04 16:52:19 -06:00
|
|
|
* This file is part of OpenRC. It is subject to the license terms in
|
|
|
|
* the LICENSE file found in the top-level directory of this
|
2021-12-20 20:07:00 -05:00
|
|
|
* distribution and at https://github.com/OpenRC/openrc/blob/HEAD/LICENSE
|
2015-12-04 16:52:19 -06:00
|
|
|
* This file may not be copied, modified, propagated, or distributed
|
|
|
|
* except according to the terms contained in the LICENSE file.
|
2007-11-14 15:22:04 +00:00
|
|
|
*/
|
|
|
|
|
2007-04-05 11:18:42 +00:00
|
|
|
#include <sys/stat.h>
|
2009-01-12 23:53:13 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
2007-04-05 11:18:42 +00:00
|
|
|
|
2007-10-04 13:38:47 +00:00
|
|
|
#include <errno.h>
|
2008-02-20 10:52:57 +00:00
|
|
|
#include <fcntl.h>
|
2009-01-12 23:53:13 +00:00
|
|
|
#include <getopt.h>
|
|
|
|
#include <limits.h>
|
2007-04-05 11:18:42 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-01-12 23:53:13 +00:00
|
|
|
#include <time.h>
|
2008-02-20 10:52:57 +00:00
|
|
|
#include <unistd.h>
|
2009-01-12 23:53:13 +00:00
|
|
|
#include <utime.h>
|
2007-04-05 11:18:42 +00:00
|
|
|
|
2008-01-06 13:47:39 +00:00
|
|
|
#include "einfo.h"
|
2014-10-23 18:33:27 -04:00
|
|
|
#include "queue.h"
|
2007-04-05 11:18:42 +00:00
|
|
|
#include "rc.h"
|
2022-04-06 10:51:55 -05:00
|
|
|
#include "misc.h"
|
2007-10-04 13:38:47 +00:00
|
|
|
#include "_usage.h"
|
2015-12-23 14:06:31 -06:00
|
|
|
|
|
|
|
const char *applet = NULL;
|
|
|
|
const char *extraopts = NULL;
|
2021-12-20 20:03:35 -05:00
|
|
|
const char getoptstring[] = "aot:suTF:" getoptstring_COMMON;
|
2015-12-23 14:06:31 -06:00
|
|
|
const struct option longopts[] = {
|
2008-02-20 10:52:57 +00:00
|
|
|
{ "starting", 0, NULL, 'a'},
|
|
|
|
{ "stopping", 0, NULL, 'o'},
|
2007-10-12 00:01:33 +00:00
|
|
|
{ "type", 1, NULL, 't'},
|
2007-10-04 13:38:47 +00:00
|
|
|
{ "notrace", 0, NULL, 'T'},
|
|
|
|
{ "strict", 0, NULL, 's'},
|
|
|
|
{ "update", 0, NULL, 'u'},
|
2011-01-17 06:48:26 +00:00
|
|
|
{ "deptree-file", 1, NULL, 'F'},
|
2007-10-04 13:38:47 +00:00
|
|
|
longopts_COMMON
|
|
|
|
};
|
2015-12-23 14:06:31 -06:00
|
|
|
const char * const longopts_help[] = {
|
2008-02-20 10:52:57 +00:00
|
|
|
"Order services as if runlevel is starting",
|
|
|
|
"Order services as if runlevel is stopping",
|
2007-10-04 13:38:47 +00:00
|
|
|
"Type(s) of dependency to list",
|
|
|
|
"Don't trace service dependencies",
|
|
|
|
"Only use what is in the runlevels",
|
|
|
|
"Force an update of the dependency tree",
|
2011-01-17 06:48:26 +00:00
|
|
|
"File to load cached deptree from",
|
2007-10-04 13:38:47 +00:00
|
|
|
longopts_help_COMMON
|
|
|
|
};
|
2015-12-23 14:06:31 -06:00
|
|
|
const char *usagestring = NULL;
|
2007-10-04 13:38:47 +00:00
|
|
|
|
2015-12-23 14:06:31 -06:00
|
|
|
int main(int argc, char **argv)
|
2007-04-05 11:18:42 +00:00
|
|
|
{
|
2008-03-16 17:00:56 +00:00
|
|
|
RC_STRINGLIST *list;
|
|
|
|
RC_STRINGLIST *types;
|
|
|
|
RC_STRINGLIST *services;
|
|
|
|
RC_STRINGLIST *depends;
|
|
|
|
RC_STRING *s;
|
|
|
|
RC_DEPTREE *deptree = NULL;
|
2009-01-12 23:53:13 +00:00
|
|
|
int options = RC_DEP_TRACE, update = 0;
|
2007-04-11 12:44:47 +00:00
|
|
|
bool first = true;
|
2008-03-19 17:11:50 +00:00
|
|
|
char *runlevel = xstrdup(getenv("RC_RUNLEVEL"));
|
2007-10-04 13:38:47 +00:00
|
|
|
int opt;
|
|
|
|
char *token;
|
2011-01-17 06:48:26 +00:00
|
|
|
char *deptree_file = NULL;
|
2007-10-04 13:38:47 +00:00
|
|
|
|
2015-12-23 14:06:31 -06:00
|
|
|
applet = basename_c(argv[0]);
|
2008-03-16 17:00:56 +00:00
|
|
|
types = rc_stringlist_new();
|
|
|
|
while ((opt = getopt_long(argc, argv, getoptstring,
|
2009-04-23 21:31:22 +00:00
|
|
|
longopts, (int *) 0)) != -1)
|
2007-10-04 13:38:47 +00:00
|
|
|
{
|
|
|
|
switch (opt) {
|
2008-03-16 17:00:56 +00:00
|
|
|
case 'a':
|
|
|
|
options |= RC_DEP_START;
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
options |= RC_DEP_STOP;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
options |= RC_DEP_STRICT;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
while ((token = strsep(&optarg, ",")))
|
|
|
|
rc_stringlist_add(types, token);
|
|
|
|
break;
|
|
|
|
case 'u':
|
2009-01-12 23:53:13 +00:00
|
|
|
update = 1;
|
2008-03-16 17:00:56 +00:00
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
options &= RC_DEP_TRACE;
|
|
|
|
break;
|
2011-01-17 06:48:26 +00:00
|
|
|
case 'F':
|
2011-01-17 08:34:03 +00:00
|
|
|
deptree_file = xstrdup(optarg);
|
2011-01-17 06:48:26 +00:00
|
|
|
break;
|
2008-03-16 17:00:56 +00:00
|
|
|
|
2011-01-17 06:48:26 +00:00
|
|
|
case_RC_COMMON_GETOPT
|
2011-11-19 02:33:11 -05:00
|
|
|
}
|
2007-10-04 13:38:47 +00:00
|
|
|
}
|
2011-01-17 08:28:43 +00:00
|
|
|
|
2011-01-17 08:41:02 +00:00
|
|
|
if (deptree_file) {
|
|
|
|
if (!(deptree = rc_deptree_load_file(deptree_file)))
|
2011-01-17 06:48:26 +00:00
|
|
|
eerrorx("failed to load deptree");
|
|
|
|
} else {
|
|
|
|
if (!(deptree = _rc_deptree_load(update, NULL)))
|
|
|
|
eerrorx("failed to load deptree");
|
|
|
|
}
|
2007-10-04 13:38:47 +00:00
|
|
|
|
2008-10-10 08:37:21 +00:00
|
|
|
if (!runlevel)
|
2008-03-16 17:00:56 +00:00
|
|
|
runlevel = rc_runlevel_get();
|
2007-10-12 00:01:33 +00:00
|
|
|
|
2008-03-16 17:00:56 +00:00
|
|
|
services = rc_stringlist_new();
|
2007-10-04 13:38:47 +00:00
|
|
|
while (optind < argc) {
|
2008-03-16 17:00:56 +00:00
|
|
|
list = rc_stringlist_new();
|
|
|
|
rc_stringlist_add(list, argv[optind]);
|
2007-10-04 13:38:47 +00:00
|
|
|
errno = 0;
|
2008-03-16 17:00:56 +00:00
|
|
|
depends = rc_deptree_depends(deptree, NULL, list, runlevel, 0);
|
2008-10-10 08:37:21 +00:00
|
|
|
if (!depends && errno == ENOENT)
|
|
|
|
eerror("no dependency info for service `%s'",
|
2009-04-23 21:31:22 +00:00
|
|
|
argv[optind]);
|
2007-10-04 13:38:47 +00:00
|
|
|
else
|
2008-03-16 17:00:56 +00:00
|
|
|
rc_stringlist_add(services, argv[optind]);
|
2007-10-04 13:38:47 +00:00
|
|
|
|
2008-03-16 17:00:56 +00:00
|
|
|
rc_stringlist_free(depends);
|
|
|
|
rc_stringlist_free(list);
|
2007-10-04 13:38:47 +00:00
|
|
|
optind++;
|
2007-04-11 12:44:47 +00:00
|
|
|
}
|
2008-10-10 08:37:21 +00:00
|
|
|
if (!TAILQ_FIRST(services)) {
|
2008-03-16 17:00:56 +00:00
|
|
|
rc_stringlist_free(services);
|
|
|
|
rc_stringlist_free(types);
|
|
|
|
rc_deptree_free(deptree);
|
|
|
|
free(runlevel);
|
2007-04-11 12:44:47 +00:00
|
|
|
if (update)
|
2008-03-16 17:00:56 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
eerrorx("no services specified");
|
2007-04-11 12:44:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we don't have any types, then supply some defaults */
|
2008-10-10 08:37:21 +00:00
|
|
|
if (!TAILQ_FIRST(types)) {
|
2008-03-16 17:00:56 +00:00
|
|
|
rc_stringlist_add(types, "ineed");
|
|
|
|
rc_stringlist_add(types, "iuse");
|
2007-04-11 12:44:47 +00:00
|
|
|
}
|
|
|
|
|
2008-10-10 08:37:21 +00:00
|
|
|
depends = rc_deptree_depends(deptree, types, services,
|
2009-04-23 21:31:22 +00:00
|
|
|
runlevel, options);
|
2007-04-11 12:44:47 +00:00
|
|
|
|
2008-03-16 17:00:56 +00:00
|
|
|
if (TAILQ_FIRST(depends)) {
|
|
|
|
TAILQ_FOREACH(s, depends, entries) {
|
2007-04-11 12:44:47 +00:00
|
|
|
if (first)
|
|
|
|
first = false;
|
|
|
|
else
|
|
|
|
printf (" ");
|
2008-03-16 17:00:56 +00:00
|
|
|
printf ("%s", s->value);
|
2007-04-11 12:44:47 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
|
2008-03-16 17:00:56 +00:00
|
|
|
rc_stringlist_free(types);
|
|
|
|
rc_stringlist_free(services);
|
|
|
|
rc_stringlist_free(depends);
|
|
|
|
rc_deptree_free(deptree);
|
|
|
|
free(runlevel);
|
|
|
|
return EXIT_SUCCESS;
|
2007-04-05 11:18:42 +00:00
|
|
|
}
|