Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

librc, rc, supervise-daemon: Repace hardcoded paths with function calls. #752

Merged
merged 2 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions sh/gendepends.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ depend() {
}

_done_dirs=
for _dir in \
@SYSCONFDIR@/init.d \
@PKG_PREFIX@/etc/init.d \
@LOCAL_PREFIX@/etc/init.d
for _dir in $RC_SCRIPTDIRS
do
_dir="$_dir/init.d"
[ -d "$_dir" ] || continue

# Don't do the same dir twice
Expand Down
8 changes: 4 additions & 4 deletions src/librc/librc-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ rc_service_daemon_set(const char *service, const char *exec,
return false;
}

xasprintf(&dirpath, RC_SVCDIR "/daemons/%s", basename_c(service));
xasprintf(&dirpath, "%s/daemons/%s", rc_svcdir(), basename_c(service));

/* Regardless, erase any existing daemon info */
if ((dp = opendir(dirpath))) {
Expand Down Expand Up @@ -489,7 +489,7 @@ rc_service_started_daemon(const char *service,
if (!service || !exec)
return false;

xasprintf(&dirpath, RC_SVCDIR "/daemons/%s", basename_c(service));
xasprintf(&dirpath, "%s/daemons/%s", rc_svcdir(), basename_c(service));
match = _match_list(exec, argv, NULL);

if (indx > 0) {
Expand Down Expand Up @@ -541,8 +541,8 @@ rc_service_daemons_crashed(const char *service)
char *ch_root;
char *spidfile;

path += snprintf(dirpath, sizeof(dirpath), RC_SVCDIR "/daemons/%s",
basename_c(service));
path += snprintf(dirpath, sizeof(dirpath),
"%s/daemons/%s", rc_svcdir(), basename_c(service));

if (!(dp = opendir(dirpath)))
return false;
Expand Down
136 changes: 87 additions & 49 deletions src/librc/librc-depend.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@

#define GENDEP RC_LIBEXECDIR "/sh/gendepends.sh"

#define RC_DEPCONFIG RC_SVCDIR "/depconfig"

static const char *bootlevel = NULL;

static char *
Expand Down Expand Up @@ -149,7 +147,14 @@ make_deptree(void) {

RC_DEPTREE *
rc_deptree_load(void) {
return rc_deptree_load_file(RC_DEPTREE_CACHE);
char *deptree_cache;
RC_DEPTREE *deptree;

xasprintf(&deptree_cache, "%s/deptree", rc_svcdir());
deptree = rc_deptree_load_file(deptree_cache);
free(deptree_cache);

return deptree;
}

RC_DEPTREE *
Expand Down Expand Up @@ -687,19 +692,19 @@ static const DEPPAIR deppairs[] = {

static const char *const depdirs[] =
{
RC_SVCDIR,
RC_SVCDIR "/starting",
RC_SVCDIR "/started",
RC_SVCDIR "/stopping",
RC_SVCDIR "/inactive",
RC_SVCDIR "/wasinactive",
RC_SVCDIR "/failed",
RC_SVCDIR "/hotplugged",
RC_SVCDIR "/daemons",
RC_SVCDIR "/options",
RC_SVCDIR "/exclusive",
RC_SVCDIR "/scheduled",
RC_SVCDIR "/tmp",
"",
"starting",
"started",
"stopping",
"inactive",
"wasinactive",
"failed",
"hotplugged",
"daemons",
"options",
"exclusive",
"scheduled",
"tmp",
NULL
};

Expand All @@ -712,16 +717,23 @@ rc_deptree_update_needed(time_t *newest, char *file)
int i;
struct stat buf;
time_t mtime;
char *path;
char *deptree_cache, *depconfig;
const char *service_dir = rc_svcdir();

/* Create base directories if needed */
for (i = 0; depdirs[i]; i++)
if (mkdir(depdirs[i], 0755) != 0 && errno != EEXIST)
for (i = 0; depdirs[i]; i++) {
xasprintf(&path, "%s/%s", service_dir, depdirs[i]);
if (mkdir(path, 0755) != 0 && errno != EEXIST)
fprintf(stderr, "mkdir `%s': %s\n", depdirs[i], strerror(errno));
free(path);
}

/* Quick test to see if anything we use has changed and we have
* data in our deptree. */

if (stat(RC_DEPTREE_CACHE, &buf) == 0) {
xasprintf(&deptree_cache, "%s/deptree", service_dir);
if (stat(deptree_cache, &buf) == 0) {
mtime = buf.st_mtime;
} else {
/* No previous cache found.
Expand All @@ -731,30 +743,30 @@ rc_deptree_update_needed(time_t *newest, char *file)
newer = true;
mtime = time(NULL);
}
free(deptree_cache);

for (const char * const *dirs = rc_scriptdirs(); *dirs; dirs++) {
static const char *subdirs[] = { "init.d", "conf.d", NULL };
for (const char **subdir = subdirs; *subdir; subdir++) {
xasprintf(&path, "%s/%s", *dirs, *subdir);
newer |= !deep_mtime_check(path, true, &mtime, file);
free(path);
}
}

newer |= !deep_mtime_check(RC_INITDIR,true,&mtime,file);
newer |= !deep_mtime_check(RC_CONFDIR,true,&mtime,file);
#ifdef RC_PKG_INITDIR
newer |= !deep_mtime_check(RC_PKG_INITDIR,true,&mtime,file);
#endif
#ifdef RC_PKG_CONFDIR
newer |= !deep_mtime_check(RC_PKG_CONFDIR,true,&mtime,file);
#endif
#ifdef RC_LOCAL_INITDIRs
newer |= !deep_mtime_check(RC_LOCAL_INITDIR,true,&mtime,file);
#endif
#ifdef RC_LOCAL_CONFDIR
newer |= !deep_mtime_check(RC_LOCAL_CONFDIR,true,&mtime,file);
#endif
newer |= !deep_mtime_check(RC_CONF,true,&mtime,file);
xasprintf(&path, "%s/rc.conf", rc_sysconfdir());
newer |= !deep_mtime_check(path, true, &mtime, file);
free(path);

/* Some init scripts dependencies change depending on config files
* outside of baselayout, like syslog-ng, so we check those too. */
config = rc_config_list(RC_DEPCONFIG);
xasprintf(&depconfig, "%s/depconfig", service_dir);
config = rc_config_list(depconfig);
TAILQ_FOREACH(s, config, entries) {
newer |= !deep_mtime_check(s->value, true, &mtime, file);
}
rc_stringlist_free(config);
free(depconfig);

/* Return newest file time, if requested */
if ((newer) && (newest != NULL)) {
Expand All @@ -764,6 +776,35 @@ rc_deptree_update_needed(time_t *newest, char *file)
return newer;
}

static void
setup_environment(void) {
char *scriptdirs, *env;
size_t env_size = 0;
struct utsname uts;

for (const char * const *dirs = rc_scriptdirs(); *dirs; dirs++)
env_size += strlen(*dirs) + sizeof(' ');

env = scriptdirs = xmalloc(env_size);

for (const char * const *dirs = rc_scriptdirs(); *dirs; dirs++) {
int len = snprintf(scriptdirs, env_size, "%s ", *dirs);
scriptdirs += len;
env_size -= len;
}

setenv("RC_SCRIPTDIRS", env, 1);
free(env);

/* Some init scripts need RC_LIBEXECDIR to source stuff
Ideally we should be setting our full env instead */
if (!getenv("RC_LIBEXECDIR"))
setenv("RC_LIBEXECDIR", RC_LIBEXECDIR, 0);

if (uname(&uts) == 0)
setenv("RC_UNAME", uts.sysname, 1);
}

/* This is a 7 phase operation
Phase 1 is a shell script which loads each init script and config in turn
and echos their dependency info to stdout
Expand All @@ -788,20 +829,14 @@ rc_deptree_update(void)
char *line = NULL;
size_t size;
char *depend, *depends, *service, *type;
char *deptree_cache, *depconfig;
size_t i, l;
bool retval = true;
const char *sys = rc_sys();
struct utsname uts;
int serrno;

/* Some init scripts need RC_LIBEXECDIR to source stuff
Ideally we should be setting our full env instead */
if (!getenv("RC_LIBEXECDIR"))
setenv("RC_LIBEXECDIR", RC_LIBEXECDIR, 0);

if (uname(&uts) == 0)
setenv("RC_UNAME", uts.sysname, 1);
/* Phase 1 - source all init scripts and print dependencies */
setup_environment();
if (!(fp = popen(GENDEP, "r")))
return false;

Expand Down Expand Up @@ -1047,7 +1082,8 @@ rc_deptree_update(void)
This works and should be entirely shell parseable provided that depend
names don't have any non shell variable characters in
*/
if ((fp = fopen(RC_DEPTREE_CACHE, "w"))) {
xasprintf(&deptree_cache, "%s/deptree", rc_svcdir());
if ((fp = fopen(deptree_cache, "w"))) {
i = 0;
TAILQ_FOREACH(depinfo, deptree, entries) {
fprintf(fp, "depinfo_%zu_service='%s'\n", i, depinfo->service);
Expand All @@ -1062,24 +1098,26 @@ rc_deptree_update(void)
}
fclose(fp);
} else {
fprintf(stderr, "fopen `%s': %s\n",
RC_DEPTREE_CACHE, strerror(errno));
fprintf(stderr, "fopen '%s': %s\n", deptree_cache, strerror(errno));
retval = false;
}
free(deptree_cache);

/* Save our external config files to disk */
xasprintf(&depconfig, "%s/depconfig", rc_svcdir());
if (TAILQ_FIRST(config)) {
if ((fp = fopen(RC_DEPCONFIG, "w"))) {
if ((fp = fopen(depconfig, "w"))) {
TAILQ_FOREACH(s, config, entries)
fprintf(fp, "%s\n", s->value);
fclose(fp);
} else {
fprintf(stderr, "fopen `%s': %s\n", RC_DEPCONFIG, strerror(errno));
fprintf(stderr, "fopen '%s': %s\n", depconfig, strerror(errno));
retval = false;
}
} else {
unlink(RC_DEPCONFIG);
unlink(depconfig);
}
free(depconfig);

rc_stringlist_free(config);
rc_deptree_free(deptree);
Expand Down
55 changes: 31 additions & 24 deletions src/librc/librc-misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ static RC_STRINGLIST *rc_config_kcl(RC_STRINGLIST *config)
return config;
}

static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config)
static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config, char *dir)
{
DIR *dp;
struct dirent *d;
Expand All @@ -311,7 +311,7 @@ static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config)
char path[PATH_MAX];
RC_STRING *line;

if ((dp = opendir(RC_CONF_D)) != NULL) {
if ((dp = opendir(dir)) != NULL) {
rc_conf_d_files = rc_stringlist_new();
while ((d = readdir(dp)) != NULL) {
if (fnmatch("*.conf", d->d_name, FNM_PATHNAME) == 0) {
Expand All @@ -324,7 +324,7 @@ static RC_STRINGLIST * rc_config_directory(RC_STRINGLIST *config)
TAILQ_FOREACH(fname, rc_conf_d_files, entries) {
if (!fname->value)
continue;
sprintf(path, "%s/%s", RC_CONF_D, fname->value);
sprintf(path, "%s/%s", dir, fname->value);
rc_conf_d_list = rc_config_list(path);
TAILQ_FOREACH(line, rc_conf_d_list, entries)
if (line->value)
Expand Down Expand Up @@ -386,32 +386,39 @@ _free_rc_conf(void)
char *
rc_conf_value(const char *setting)
{
RC_STRINGLIST *old;
const char *sysconfdir = rc_sysconfdir();
RC_STRING *s;
char *p;
char *conf;

if (!rc_conf) {
rc_conf = rc_config_load(RC_CONF);
atexit(_free_rc_conf);
if (rc_conf)
return rc_config_value(rc_conf, setting);

/* Support old configs. */
if (exists(RC_CONF_OLD)) {
old = rc_config_load(RC_CONF_OLD);
TAILQ_CONCAT(rc_conf, old, entries);
rc_stringlist_free(old);
}
xasprintf(&conf, "%s/%s", sysconfdir, "rc.conf");
rc_conf = rc_config_load(conf);
atexit(_free_rc_conf);

rc_conf = rc_config_directory(rc_conf);
rc_conf = rc_config_kcl(rc_conf);
free(conf);

/* Convert old uppercase to lowercase */
TAILQ_FOREACH(s, rc_conf, entries) {
p = s->value;
while (p && *p && *p != '=') {
if (isupper((unsigned char)*p))
*p = tolower((unsigned char)*p);
p++;
}
/* Support old configs. */
if (exists(RC_CONF_OLD)) {
RC_STRINGLIST *old_conf = rc_config_load(RC_CONF_OLD);
TAILQ_CONCAT(rc_conf, old_conf, entries);
rc_stringlist_free(old_conf);
}

xasprintf(&conf, "%s/%s", sysconfdir, "rc.conf.d");
rc_conf = rc_config_directory(rc_conf, conf);
free(conf);

rc_conf = rc_config_kcl(rc_conf);

/* Convert old uppercase to lowercase */
TAILQ_FOREACH(s, rc_conf, entries) {
char *p = s->value;
while (p && *p && *p != '=') {
if (isupper((unsigned char)*p))
*p = tolower((unsigned char)*p);
p++;
}
}

Expand Down
Loading
Loading