Skip to content

Commit

Permalink
librc: Overlay user configs.
Browse files Browse the repository at this point in the history
Add a new function to librc, `rc_usrconfdir()`, whick returns the
location where user-made rc.conf exists.

The user-made configuration is loaded first over the global config, so
it takes priority when resolving variables, thus "overriding" global
configs.
  • Loading branch information
navi-desu committed Sep 21, 2024
1 parent bcc279d commit 55577e8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
33 changes: 26 additions & 7 deletions src/librc/librc-misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,28 +409,47 @@ _free_rc_conf(void)
rc_stringlist_free(rc_conf);
}

static void
rc_conf_append(const char *file)
{
RC_STRINGLIST *conf = rc_config_load(file);
TAILQ_CONCAT(rc_conf, conf, entries);
rc_stringlist_free(conf);
}

char *
rc_conf_value(const char *setting)
{
const char *sysconfdir = rc_sysconfdir();
const char *usrconfdir = rc_usrconfdir();
RC_STRING *s;
char *conf;

if (rc_conf)
return rc_config_value(rc_conf, setting);

xasprintf(&conf, "%s/%s", sysconfdir, "rc.conf");
rc_conf = rc_config_load(conf);
rc_conf = rc_stringlist_new();
atexit(_free_rc_conf);

/* Load user configurations first, as they should override
* system wide configs. */
if (usrconfdir) {
xasprintf(&conf, "%s/%s", usrconfdir, "rc.conf");
rc_conf_append(conf);
free(conf);

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

xasprintf(&conf, "%s/%s", sysconfdir, "rc.conf");
rc_conf_append(sysconfdir);
free(conf);

/* 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);
}
if (exists(RC_CONF_OLD))
rc_conf_append(RC_CONF_OLD);

xasprintf(&conf, "%s/%s", sysconfdir, "rc.conf.d");
rc_conf = rc_config_directory(rc_conf, conf);
Expand Down
9 changes: 9 additions & 0 deletions src/librc/librc.c
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,15 @@ rc_sysconfdir(void)
return RC_SYSCONFDIR;
}

const char *
rc_usrconfdir(void)
{
if (!is_user)
return NULL;

return RC_SYSCONFDIR "/user.d";
}

const char *
rc_runleveldir(void)
{
Expand Down
6 changes: 6 additions & 0 deletions src/librc/rc.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ const char * const *rc_scriptdirs(void);
* @return Path to the system configuration directory */
const char *rc_sysconfdir(void);

/*! The user configuration directory is where rc.conf
* will be located for user-mode openrc. It is meant to
* override the system wide configs where applicable.
* @return Path to the user configuration directory, or NULL if in system mode */
const char *rc_usrconfdir(void);

/*! @return Path to runlevel directory */
const char *rc_runleveldir(void);

Expand Down

0 comments on commit 55577e8

Please sign in to comment.