2007-04-05 16:48:42 +05:30
|
|
|
/*
|
|
|
|
env-update
|
|
|
|
Create /etc/profile.env (sh), /etc/csh.env from /etc/env.d
|
|
|
|
Run ldconfig as required
|
|
|
|
|
|
|
|
Copyright 2007 Gentoo Foundation
|
|
|
|
Released under the GPLv2
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
*/
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-06-27 18:05:43 +05:30
|
|
|
#define APPLET "env-update"
|
|
|
|
|
2007-07-16 12:49:32 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <errno.h>
|
2007-07-16 12:49:32 +05:30
|
|
|
#include <fcntl.h>
|
2007-06-27 18:05:43 +05:30
|
|
|
#include <getopt.h>
|
2007-04-05 16:48:42 +05:30
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "einfo.h"
|
|
|
|
#include "rc.h"
|
|
|
|
#include "rc-misc.h"
|
|
|
|
#include "strlist.h"
|
|
|
|
|
2007-04-12 18:48:52 +05:30
|
|
|
#define ENVDIR "/etc/env.d"
|
|
|
|
#define PROFILE_ENV "/etc/profile.env"
|
|
|
|
#define CSH_ENV "/etc/csh.env"
|
|
|
|
#define LDSOCONF "/etc/ld.so.conf"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-12 18:48:52 +05:30
|
|
|
#define NOTICE "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n" \
|
|
|
|
"# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n" \
|
|
|
|
"# GO INTO %s NOT %s\n\n"
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-12 18:48:52 +05:30
|
|
|
#define LDNOTICE "# ld.so.conf autogenerated by env-update; make all\n" \
|
|
|
|
"# changes to contents of /etc/env.d directory\n"
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-07-16 12:49:32 +05:30
|
|
|
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
|
|
|
|
#define LD_MESSAGE "Regenerating /var/run/ld-elf.so.hints"
|
|
|
|
#define LD_SYSTEM "/sbin/ldconfig -elf -i '" LDSOCONF "'";
|
|
|
|
#else
|
|
|
|
#define LD_MESSAGE "Regenerating /etc/ld.so.cache"
|
|
|
|
#define LD_SYSTEM "/sbin/ldconfig"
|
|
|
|
#endif
|
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
static const char *colon_separated[] = {
|
2007-04-11 18:14:47 +05:30
|
|
|
"ADA_INCLUDE_PATH",
|
|
|
|
"ADA_OBJECTS_PATH",
|
|
|
|
"CLASSPATH",
|
|
|
|
"INFOPATH",
|
|
|
|
"KDEDIRS",
|
|
|
|
"LDPATH",
|
|
|
|
"MANPATH",
|
|
|
|
"PATH",
|
|
|
|
"PKG_CONFIG_PATH",
|
|
|
|
"PRELINK_PATH",
|
|
|
|
"PRELINK_PATH_MASK",
|
|
|
|
"PYTHONPATH",
|
|
|
|
"ROOTPATH",
|
|
|
|
NULL
|
2007-04-05 16:48:42 +05:30
|
|
|
};
|
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
static const char *space_separated[] = {
|
2007-04-11 18:14:47 +05:30
|
|
|
"CONFIG_PROTECT",
|
|
|
|
"CONFIG_PROTECT_MASK",
|
|
|
|
NULL,
|
2007-04-05 16:48:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
static char *applet = NULL;
|
|
|
|
|
2007-06-28 21:14:14 +05:30
|
|
|
#include "_usage.h"
|
2007-07-16 12:49:32 +05:30
|
|
|
#define getoptstring "lL" getoptstring_COMMON
|
2007-06-27 18:05:43 +05:30
|
|
|
static struct option longopts[] = {
|
2007-07-09 15:56:02 +05:30
|
|
|
{ "fork-ldconfig", 0, NULL, 'l'},
|
2007-06-27 18:05:43 +05:30
|
|
|
{ "no-ldconfig", 0, NULL, 'L'},
|
2007-06-28 21:14:14 +05:30
|
|
|
longopts_COMMON
|
2007-06-27 18:05:43 +05:30
|
|
|
{ NULL, 0, NULL, 0}
|
|
|
|
};
|
|
|
|
#include "_usage.c"
|
|
|
|
|
2007-04-05 16:48:42 +05:30
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
char **files = rc_ls_dir (NULL, ENVDIR, 0);
|
|
|
|
char *file;
|
|
|
|
char **envs = NULL;
|
|
|
|
char *env;
|
|
|
|
int i = 0;
|
2007-04-27 16:54:05 +05:30
|
|
|
int j;
|
2007-04-11 18:14:47 +05:30
|
|
|
FILE *fp;
|
|
|
|
bool ld = true;
|
|
|
|
char *ldent;
|
|
|
|
char **ldents = NULL;
|
|
|
|
int nents = 0;
|
2007-04-27 16:54:05 +05:30
|
|
|
char **config = NULL;
|
|
|
|
char *entry;
|
|
|
|
char **mycolons = NULL;
|
|
|
|
char **myspaces = NULL;
|
2007-06-27 18:05:43 +05:30
|
|
|
int opt;
|
|
|
|
bool ldconfig = true;
|
2007-07-09 15:56:02 +05:30
|
|
|
bool fork_ldconfig = false;
|
2007-06-27 18:05:43 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
applet = argv[0];
|
|
|
|
|
2007-06-27 18:05:43 +05:30
|
|
|
while ((opt = getopt_long (argc, argv, getoptstring,
|
|
|
|
longopts, (int *) 0)) != -1)
|
|
|
|
{
|
|
|
|
switch (opt) {
|
2007-07-09 15:56:02 +05:30
|
|
|
case 'l':
|
|
|
|
fork_ldconfig = true;
|
|
|
|
break;
|
2007-06-27 18:05:43 +05:30
|
|
|
case 'L':
|
|
|
|
ldconfig = false;
|
|
|
|
break;
|
2007-06-28 21:14:14 +05:30
|
|
|
|
|
|
|
case_RC_COMMON_GETOPT
|
2007-06-27 18:05:43 +05:30
|
|
|
}
|
|
|
|
}
|
2007-06-28 21:14:14 +05:30
|
|
|
|
2007-04-11 18:14:47 +05:30
|
|
|
if (! files)
|
|
|
|
eerrorx ("%s: no files in " ENVDIR " to process", applet);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (files, file, i) {
|
|
|
|
char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
|
|
|
|
char **entries = NULL;
|
|
|
|
|
|
|
|
if (! rc_is_dir (path))
|
|
|
|
entries = rc_get_config (NULL, path);
|
|
|
|
free (path);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (entries, entry, j) {
|
|
|
|
char *tmpent = rc_xstrdup (entry);
|
|
|
|
char *value = tmpent;
|
|
|
|
char *var = strsep (&value, "=");
|
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
if (strcmp (var, "COLON_SEPARATED") == 0)
|
|
|
|
while ((var = strsep (&value, " ")))
|
|
|
|
mycolons = rc_strlist_addu (mycolons, var);
|
|
|
|
else if (strcmp (var, "SPACE_SEPARATED") == 0)
|
|
|
|
while ((var = strsep (&value, " ")))
|
|
|
|
myspaces = rc_strlist_addu (myspaces, var);
|
|
|
|
else
|
|
|
|
config = rc_strlist_add (config, entry);
|
|
|
|
free (tmpent);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
rc_strlist_free (entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
STRLIST_FOREACH (config, entry, i) {
|
|
|
|
char *tmpent = rc_xstrdup (entry);
|
|
|
|
char *value = tmpent;
|
|
|
|
char *var = strsep (&value, "=");
|
|
|
|
char *match;
|
|
|
|
bool colon = false;
|
|
|
|
bool space = false;
|
|
|
|
bool replaced = false;
|
|
|
|
|
|
|
|
for (j = 0; colon_separated[j]; j++)
|
|
|
|
if (strcmp (colon_separated[j], var) == 0) {
|
|
|
|
colon = true;
|
|
|
|
break;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
if (! colon)
|
|
|
|
STRLIST_FOREACH (mycolons, match, j) {
|
|
|
|
if (strcmp (match, var) == 0) {
|
|
|
|
colon = true;
|
|
|
|
break;
|
|
|
|
} }
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
if (! colon)
|
|
|
|
for (j = 0; space_separated[j]; j++)
|
|
|
|
if (strcmp (space_separated[j], var) == 0) {
|
|
|
|
space = true;
|
2007-04-11 18:14:47 +05:30
|
|
|
break;
|
2007-04-27 16:54:05 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
if (! colon && ! space)
|
|
|
|
STRLIST_FOREACH (myspaces, match, j)
|
|
|
|
if (strcmp (match, var) == 0) {
|
|
|
|
space = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
/* Skip blank vars */
|
|
|
|
if ((colon || space) &&
|
|
|
|
(! value || strlen (value)) == 0)
|
|
|
|
{
|
2007-04-11 18:14:47 +05:30
|
|
|
free (tmpent);
|
2007-04-27 16:54:05 +05:30
|
|
|
continue;
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-27 16:54:05 +05:30
|
|
|
|
|
|
|
STRLIST_FOREACH (envs, env, j) {
|
|
|
|
char *tmpenv = rc_xstrdup (env);
|
|
|
|
char *tmpvalue = tmpenv;
|
|
|
|
char *tmpentry = strsep (&tmpvalue, "=");
|
|
|
|
|
|
|
|
if (strcmp (tmpentry, var) == 0) {
|
|
|
|
if (colon || space) {
|
|
|
|
int len = strlen (envs[j - 1]) + strlen (entry) + 1;
|
|
|
|
envs[j - 1] = rc_xrealloc (envs[j - 1], len);
|
|
|
|
snprintf (envs[j - 1] + strlen (envs[j - 1]), len,
|
|
|
|
"%s%s", colon ? ":" : " ", value);
|
|
|
|
} else {
|
|
|
|
free (envs[j - 1]);
|
|
|
|
envs[j - 1] = rc_xstrdup (entry);
|
|
|
|
}
|
|
|
|
replaced = true;
|
|
|
|
}
|
|
|
|
free (tmpenv);
|
|
|
|
|
|
|
|
if (replaced)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! replaced)
|
|
|
|
envs = rc_strlist_addsort (envs, entry);
|
|
|
|
|
|
|
|
free (tmpent);
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-27 16:54:05 +05:30
|
|
|
rc_strlist_free (mycolons);
|
|
|
|
rc_strlist_free (myspaces);
|
|
|
|
rc_strlist_free (config);
|
|
|
|
rc_strlist_free (files);
|
2007-04-11 18:14:47 +05:30
|
|
|
|
|
|
|
if ((fp = fopen (PROFILE_ENV, "w")) == NULL)
|
|
|
|
eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
|
|
|
|
fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (envs, env, i) {
|
|
|
|
char *tmpent = rc_xstrdup (env);
|
|
|
|
char *value = tmpent;
|
|
|
|
char *var = strsep (&value, "=");
|
2007-07-09 15:56:02 +05:30
|
|
|
if (strcmp (var, "LDPATH") != 0) {
|
|
|
|
if (*value == '$')
|
|
|
|
fprintf (fp, "export %s=%s\n", var, value);
|
|
|
|
else
|
|
|
|
fprintf (fp, "export %s='%s'\n", var, value);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
free (tmpent);
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
if ((fp = fopen (CSH_ENV, "w")) == NULL)
|
|
|
|
eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
|
|
|
|
fprintf (fp, NOTICE, "/etc/csh.cshrc", PROFILE_ENV);
|
|
|
|
|
|
|
|
STRLIST_FOREACH (envs, env, i) {
|
|
|
|
char *tmpent = rc_xstrdup (env);
|
|
|
|
char *value = tmpent;
|
|
|
|
char *var = strsep (&value, "=");
|
2007-07-09 15:56:02 +05:30
|
|
|
if (strcmp (var, "LDPATH") != 0) {
|
|
|
|
if (*value == '$')
|
|
|
|
fprintf (fp, "setenv %s %s\n", var, value);
|
|
|
|
else
|
|
|
|
fprintf (fp, "setenv %s '%s'\n", var, value);
|
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
free (tmpent);
|
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
ldent = rc_get_config_entry (envs, "LDPATH");
|
|
|
|
|
|
|
|
if (! ldent ||
|
|
|
|
(argc > 1 && argv[1] && strcmp (argv[1], "--no-ldconfig") == 0))
|
|
|
|
{
|
2007-04-27 16:54:05 +05:30
|
|
|
rc_strlist_free (envs);
|
2007-04-11 18:14:47 +05:30
|
|
|
return (EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((file = strsep (&ldent, ":"))) {
|
|
|
|
if (strlen (file) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ldents = rc_strlist_add (ldents, file);
|
|
|
|
nents++;
|
|
|
|
}
|
|
|
|
|
2007-06-27 18:05:43 +05:30
|
|
|
if (ldconfig) {
|
|
|
|
/* Update ld.so.conf only if different */
|
|
|
|
if (rc_exists (LDSOCONF)) {
|
|
|
|
char **lines = rc_get_list (NULL, LDSOCONF);
|
|
|
|
char *line;
|
|
|
|
ld = false;
|
|
|
|
STRLIST_FOREACH (lines, line, i)
|
|
|
|
if (i > nents || strcmp (line, ldents[i - 1]) != 0)
|
|
|
|
{
|
|
|
|
ld = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rc_strlist_free (lines);
|
|
|
|
if (i - 1 != nents)
|
2007-04-11 18:14:47 +05:30
|
|
|
ld = true;
|
2007-06-27 18:05:43 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-06-27 18:05:43 +05:30
|
|
|
if (ld) {
|
|
|
|
int retval = 0;
|
2007-07-16 12:49:32 +05:30
|
|
|
pid_t pid = 0;
|
2007-04-11 18:14:47 +05:30
|
|
|
|
2007-06-27 18:05:43 +05:30
|
|
|
if ((fp = fopen (LDSOCONF, "w")) == NULL)
|
|
|
|
eerrorx ("%s: fopen `%s': %s", applet, LDSOCONF,
|
|
|
|
strerror (errno));
|
|
|
|
fprintf (fp, LDNOTICE);
|
|
|
|
STRLIST_FOREACH (ldents, ldent, i)
|
|
|
|
fprintf (fp, "%s\n", ldent);
|
|
|
|
fclose (fp);
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-07-16 12:49:32 +05:30
|
|
|
ebegin (LD_MESSAGE);
|
2007-07-09 15:56:02 +05:30
|
|
|
if (fork_ldconfig) {
|
|
|
|
if ((pid = fork ()) == -1)
|
|
|
|
eerror ("%s: failed to fork: %s", applet,
|
|
|
|
strerror (errno));
|
2007-07-16 12:49:32 +05:30
|
|
|
else if (pid == 0) {
|
|
|
|
/* Become a proper daemon for a little bit */
|
|
|
|
int fd = open ("/dev/null", O_RDWR);
|
|
|
|
setsid ();
|
|
|
|
dup2 (fd, fileno (stdin));
|
|
|
|
dup2 (fd, fileno (stdout));
|
|
|
|
dup2 (fd, fileno (stderr));
|
|
|
|
}
|
2007-07-09 15:56:02 +05:30
|
|
|
}
|
2007-07-16 12:49:32 +05:30
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
retval = system (LD_SYSTEM);
|
|
|
|
eend (retval, NULL);
|
2007-06-27 18:05:43 +05:30
|
|
|
}
|
2007-04-11 18:14:47 +05:30
|
|
|
}
|
2007-04-05 16:48:42 +05:30
|
|
|
|
2007-04-27 16:54:05 +05:30
|
|
|
rc_strlist_free (ldents);
|
|
|
|
rc_strlist_free (envs);
|
2007-04-11 18:14:47 +05:30
|
|
|
return(EXIT_SUCCESS);
|
2007-04-05 16:48:42 +05:30
|
|
|
}
|