Skip to content

Commit

Permalink
TS-2401: Remove global system_runtime_dir
Browse files Browse the repository at this point in the history
Introduce RecConfigReadRuntimeDir() to centralize overriding
Layout::runtimedir with proxy.config.local_state_dir. Use this to
replace all the instances of system_runtime_dir.
  • Loading branch information
jpeach committed Dec 6, 2013
1 parent 88f5176 commit f438ab4
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 73 deletions.
16 changes: 15 additions & 1 deletion cmd/traffic_cop/traffic_cop.cc
Expand Up @@ -542,6 +542,20 @@ config_read_int(const char *name, int *val, bool miss_ok = false)
exit(1);
}

static const char *
config_read_runtime_dir()
{
char state_dir[PATH_NAME_MAX + 1];

state_dir[0] = '\0';
config_read_string("proxy.config.local_state_dir", state_dir, sizeof(state_dir), true);
if (strlen(state_dir) > 0) {
return Layout::get()->relative(state_dir);
} else {
return ats_strdup(Layout::get()->runtimedir);
}
}

static void
config_reload_records()
{
Expand Down Expand Up @@ -1700,7 +1714,7 @@ init_config_dir()
cop_log_trace("Entering init_config_dir()\n");

root_dir = Layout::get()->prefix;
runtime_dir = Layout::get()->runtimedir;
runtime_dir = config_read_runtime_dir();
config_dir = Layout::get()->sysconfdir;

if (chdir(root_dir) < 0) {
Expand Down
2 changes: 0 additions & 2 deletions example/app-template/app-template.cc
Expand Up @@ -53,7 +53,6 @@ int system_num_of_processors = ink_number_of_processors();
int system_num_of_net_threads = DEFAULT_NUMBER_OF_THREADS;
int system_num_of_udp_threads = DEFAULT_NUMBER_OF_UDP_THREADS;

char system_runtime_dir[PATH_NAME_MAX + 1];
char system_config_directory[PATH_NAME_MAX + 1];

//int system_remote_management_flag = DEFAULT_REMOTE_MANAGEMENT_FLAG;
Expand Down Expand Up @@ -157,7 +156,6 @@ int main(int argc, char * argv[])

// Get TS directories
ink_strlcpy(system_config_directory, Layout::get()->sysconfdir, sizeof(system_config_directory));
ink_strlcpy(system_runtime_dir, Layout::get()->runtimedir, sizeof(system_runtime_dir));

if (chdir(Layout::get()->prefix) < 0) {
fprintf(stderr,"unable to change to root directory \"%s\" [%d '%s']\n", Layout::get()->prefix, errno, strerror(errno));
Expand Down
31 changes: 18 additions & 13 deletions iocore/hostdb/HostDB.cc
Expand Up @@ -380,6 +380,8 @@ HostDBCache::start(int flags)
bool reconfigure = ((flags & PROCESSOR_RECONFIGURE) ? true : false);
bool fix = ((flags & PROCESSOR_FIX) ? true : false);

storage_path[0] = '\0';

// Read configuration
// Command line overrides manager configuration.
//
Expand All @@ -390,33 +392,36 @@ HostDBCache::start(int flags)
REC_ReadConfigString(storage_path, "proxy.config.hostdb.storage_path", PATH_NAME_MAX);
REC_ReadConfigInt32(storage_size, "proxy.config.hostdb.storage_size");

if (storage_path[0] != '/') {
Layout::relative_to(storage_path, PATH_NAME_MAX, Layout::get()->prefix, storage_path);
// If proxy.config.hostdb.storage_path is not set, use the local state dir. If it is set to
// a relative path, make it relative to the prefix.
if (storage_path[0] == '\0') {
xptr<char> rundir(RecConfigReadRuntimeDir());
ink_strlcpy(storage_path, rundir, sizeof(storage_path));
} else if (storage_path[0] != '/') {
Layout::relative_to(storage_path, sizeof(storage_path), Layout::get()->prefix, storage_path);
}

Debug("hostdb", "Storage path is %s", storage_path);

// XXX: Should this be W_OK?
if (access(storage_path, R_OK) == -1) {
ink_strlcpy(storage_path, system_runtime_dir, sizeof(storage_path));
if (access(storage_path, R_OK) == -1) {
Warning("Unable to access() directory '%s': %d, %s", storage_path, errno, strerror(errno));
Warning(" Please set 'proxy.config.hostdb.storage_path' or 'proxy.config.local_state_dir' ");
}
if (access(storage_path, W_OK | R_OK) == -1) {
Warning("Unable to access() directory '%s': %d, %s", storage_path, errno, strerror(errno));
Warning("Please set 'proxy.config.hostdb.storage_path' or 'proxy.config.local_state_dir'");
}

hostDBStore = NEW(new Store);
hostDBSpan = NEW(new Span);
hostDBSpan->init(storage_path, storage_size);
hostDBStore->add(hostDBSpan);

Debug("hostdb", "Opening %s, size=%d", hostdb_filename, hostdb_size);
if (open(hostDBStore, "hostdb.config", hostdb_filename, hostdb_size, reconfigure, fix, false /* slient */ ) < 0) {
xptr<char> rundir(RecConfigReadRuntimeDir());
xptr<char> config(Layout::relative_to(rundir, "hostdb.config"));

Note("reconfiguring host database");

char p[PATH_NAME_MAX + 1];
Layout::relative_to(p, PATH_NAME_MAX, system_runtime_dir, "hostdb.config");
if (unlink(p) < 0)
Debug("hostdb", "unable to unlink %s", p);
if (unlink(config) < 0)
Debug("hostdb", "unable to unlink %s", (const char *)config);

delete hostDBStore;
hostDBStore = NEW(new Store);
Expand Down
6 changes: 4 additions & 2 deletions iocore/hostdb/MultiCache.cc
Expand Up @@ -501,9 +501,10 @@ int
MultiCacheBase::read_config(const char *config_filename, Store & s, char *fn, int *pi, int *pbuck)
{
int scratch;
xptr<char> rundir(RecConfigReadRuntimeDir());
char p[PATH_NAME_MAX + 1], buf[256];

Layout::relative_to(p, sizeof(p), system_runtime_dir, config_filename);
Layout::relative_to(p, sizeof(p), rundir, config_filename);

int fd =::open(p, O_RDONLY);
if (fd < 0)
Expand Down Expand Up @@ -537,10 +538,11 @@ MultiCacheBase::read_config(const char *config_filename, Store & s, char *fn, in
int
MultiCacheBase::write_config(const char *config_filename, int nominal_size, int abuckets)
{
xptr<char> rundir(RecConfigReadRuntimeDir());
char p[PATH_NAME_MAX + 1], buf[256];
int fd, retcode = -1;

Layout::relative_to(p, sizeof(p), system_runtime_dir, config_filename);
Layout::relative_to(p, sizeof(p), rundir, config_filename);

// XXX: Shouldn't that be 0664?
//
Expand Down
4 changes: 4 additions & 0 deletions lib/records/I_RecCore.h
Expand Up @@ -45,6 +45,10 @@ typedef void (*RecConfigEntryCallback)(RecT rec_type, RecDataT data_type, const
void RecConfigFileInit(void);
int RecConfigFileParse(const char * path, RecConfigEntryCallback handler, bool inc_version);

// Return a copy of the system's local state directory, taking proxy.config.local_state_dir into account. The
// caller MUST releease the result with ats_free().
char * RecConfigReadRuntimeDir();

// Test whether the named configuration value is overridden by an environment variable. Return either
// the overridden value, or the original value. Caller MUST NOT free the result.
const char * RecConfigOverrideFromEnvironment(const char * name, const char * value);
Expand Down
2 changes: 0 additions & 2 deletions lib/records/I_RecDefs.h
Expand Up @@ -175,9 +175,7 @@ typedef int (*RecRawStatSyncCb) (const char *name, RecDataT data_type, RecData *
#define REC_VAR_NAME_DELIMITOR '.'
#define REC_VAR_NAME_WILDCARD '*'


// System Defaults
extern char system_runtime_dir[PATH_NAME_MAX + 1];
extern char system_config_directory[PATH_NAME_MAX + 1];

#endif
1 change: 1 addition & 0 deletions lib/records/RecConfigParse.cc
Expand Up @@ -30,6 +30,7 @@
#include "P_RecUtils.h"
#include "P_RecMessage.h"
#include "P_RecCore.h"
#include "I_Layout.h"

const char *g_rec_config_fpath = NULL;
LLQ *g_rec_config_contents_llq = NULL;
Expand Down
16 changes: 16 additions & 0 deletions lib/records/RecCore.cc
Expand Up @@ -1053,6 +1053,22 @@ REC_readString(const char *name, bool * found, bool lock)
return _tmp;
}

//-------------------------------------------------------------------------
// RecConfigReadRuntimeDir
//-------------------------------------------------------------------------
char *
RecConfigReadRuntimeDir()
{
char buf[PATH_NAME_MAX + 1];

buf[0] = '\0';
RecGetRecordString("proxy.config.local_state_dir", buf, PATH_NAME_MAX);
if (strlen(buf) > 0) {
return Layout::get()->relative(buf);
} else {
return ats_strdup(Layout::get()->runtimedir);
}
}

//-------------------------------------------------------------------------
// REC_SignalManager (TS)
Expand Down
5 changes: 4 additions & 1 deletion mgmt/LocalManager.cc
Expand Up @@ -204,6 +204,8 @@ LocalManager::LocalManager(char * /* mpath ATS_UNUSED */, bool proxy_on)
: BaseManager(), run_proxy(proxy_on)
{
bool found;
xptr<char> rundir(RecConfigReadRuntimeDir());

#ifdef MGMT_USE_SYSLOG
syslog_facility = 0;
#endif
Expand All @@ -221,7 +223,8 @@ LocalManager::LocalManager(char * /* mpath ATS_UNUSED */, bool proxy_on)
mgmt_log("Bad or missing proxy.config.lm.sem_id value; using default id %d\n", MGMT_SEMID_DEFAULT);
mgmt_sync_key = MGMT_SEMID_DEFAULT;
}
ink_strlcpy(pserver_path, system_runtime_dir, sizeof(pserver_path));

ink_strlcpy(pserver_path, rundir, sizeof(pserver_path));

virt_map = NULL;

Expand Down
8 changes: 3 additions & 5 deletions mgmt/Main.cc
Expand Up @@ -78,7 +78,6 @@ static char action_tags[1024] = "";
static bool proxy_on = true;

// TODO: Check if really need those
char system_runtime_dir[PATH_NAME_MAX + 1];
char system_config_directory[PATH_NAME_MAX + 1];

char mgmt_path[PATH_NAME_MAX + 1];
Expand Down Expand Up @@ -267,6 +266,7 @@ setup_coredump()
static void
init_dirs()
{
xptr<char> rundir(RecConfigReadRuntimeDir());
char buf[PATH_NAME_MAX + 1];

REC_ReadConfigString(buf, "proxy.config.config_dir", PATH_NAME_MAX);
Expand All @@ -279,10 +279,8 @@ init_dirs()

ink_strlcpy(mgmt_path, system_config_directory, sizeof(mgmt_path));

REC_ReadConfigString(buf, "proxy.config.local_state_dir", PATH_NAME_MAX);
Layout::get()->relative(system_runtime_dir, PATH_NAME_MAX, buf);
if (access(system_runtime_dir, R_OK) == -1) {
mgmt_elog(0, "unable to access() local state dir '%s': %d, %s\n", system_runtime_dir, errno, strerror(errno));
if (access(rundir, R_OK) == -1) {
mgmt_elog(0, "unable to access() local state dir '%s': %d, %s\n", (const char *)rundir, errno, strerror(errno));
mgmt_elog(0, "please set 'proxy.config.local_state_dir'\n");
_exit(1);
}
Expand Down
4 changes: 3 additions & 1 deletion mgmt/ProcessManager.cc
Expand Up @@ -69,7 +69,9 @@ startProcessManager(void *arg)
ProcessManager::ProcessManager(bool rlm):
BaseManager(), require_lm(rlm), mgmt_sync_key(0), local_manager_sockfd(0), cbtable(NULL)
{
ink_strlcpy(pserver_path, Layout::get()->runtimedir, sizeof(pserver_path));
xptr<char> rundir(RecConfigReadRuntimeDir());

ink_strlcpy(pserver_path, rundir, sizeof(pserver_path));
mgmt_signal_queue = create_queue();

// Set temp. process/manager timeout. Will be reconfigure later.
Expand Down
5 changes: 3 additions & 2 deletions mgmt/web2/WebIntrMain.cc
Expand Up @@ -432,11 +432,12 @@ webIntr_main(void *)
// set up socket paths;
char api_sock_path[1024];
char event_sock_path[1024];
xptr<char> rundir(RecConfigReadRuntimeDir());

bzero(api_sock_path, 1024);
bzero(event_sock_path, 1024);
snprintf(api_sock_path, sizeof(api_sock_path), "%s/mgmtapisocket", system_runtime_dir);
snprintf(event_sock_path, sizeof(event_sock_path), "%s/eventapisocket", system_runtime_dir);
snprintf(api_sock_path, sizeof(api_sock_path), "%s/mgmtapisocket", (const char *)rundir);
snprintf(event_sock_path, sizeof(event_sock_path), "%s/eventapisocket", (const char *)rundir);

// INKqa12562: MgmtAPI sockets should be created with 775 permission
mode_t oldmask = umask(S_IWOTH);
Expand Down
49 changes: 21 additions & 28 deletions proxy/Main.cc
Expand Up @@ -144,7 +144,6 @@ char cluster_host[MAXDNAME + 1] = DEFAULT_CLUSTER_HOST;
static char command_string[512] = "";
int remote_management_flag = DEFAULT_REMOTE_MANAGEMENT_FLAG;

char system_runtime_dir[PATH_NAME_MAX + 1]; // Layout->runtimedir
char system_config_directory[PATH_NAME_MAX + 1]; // Layout->sysconfdir

static char error_tags[1024] = "";
Expand Down Expand Up @@ -247,24 +246,19 @@ init_system()
static void
check_lockfile()
{
char *lockfile = NULL;
xptr<char> rundir(RecConfigReadRuntimeDir());
xptr<char> lockfile;
pid_t holding_pid;
int err;

if (access(Layout::get()->runtimedir, R_OK | W_OK) == -1) {
fprintf(stderr,"unable to access() dir'%s': %d, %s\n",
Layout::get()->runtimedir, errno, strerror(errno));
fprintf(stderr," please set correct path in env variable TS_ROOT \n");
_exit(1);
}
lockfile = Layout::relative_to(Layout::get()->runtimedir, SERVER_LOCK);
lockfile = Layout::relative_to(rundir, SERVER_LOCK);

Lockfile server_lockfile(lockfile);
err = server_lockfile.Get(&holding_pid);

if (err != 1) {
char *reason = strerror(-err);
fprintf(stderr, "WARNING: Can't acquire lockfile '%s'", lockfile);
fprintf(stderr, "WARNING: Can't acquire lockfile '%s'", (const char *)lockfile);

if ((err == 0) && (holding_pid != -1)) {
fprintf(stderr, " (Lock file held by process ID %ld)\n", (long)holding_pid);
Expand All @@ -277,16 +271,16 @@ check_lockfile()
}
_exit(1);
}
ats_free(lockfile);
}

static void
init_dirs(void)
{
xptr<char> rundir(RecConfigReadRuntimeDir());

char buf[PATH_NAME_MAX + 1];

ink_strlcpy(system_config_directory, Layout::get()->sysconfdir, PATH_NAME_MAX);
ink_strlcpy(system_runtime_dir, Layout::get()->runtimedir, PATH_NAME_MAX);

/*
* XXX: There is not much sense in the following code
Expand All @@ -305,15 +299,11 @@ init_dirs(void)
}
}

if (access(system_runtime_dir, R_OK | W_OK) == -1) {
REC_ReadConfigString(buf, "proxy.config.local_state_dir", PATH_NAME_MAX);
Layout::get()->relative(system_runtime_dir, PATH_NAME_MAX, buf);
if (access(system_runtime_dir, R_OK | W_OK) == -1) {
fprintf(stderr,"unable to access() local state dir '%s': %d, %s\n",
system_runtime_dir, errno, strerror(errno));
fprintf(stderr,"please set 'proxy.config.local_state_dir'\n");
_exit(1);
}
if (access(rundir, R_OK | W_OK) == -1) {
fprintf(stderr,"unable to access() local state dir '%s': %d, %s\n",
(const char *)rundir, errno, strerror(errno));
fprintf(stderr,"please set 'proxy.config.local_state_dir'\n");
_exit(1);
}

}
Expand Down Expand Up @@ -553,12 +543,13 @@ cmd_clear(char *cmd)
//bool c_adb = !strcmp(cmd, "clear_authdb");
bool c_cache = !strcmp(cmd, "clear_cache");

char p[PATH_NAME_MAX];
if (c_all || c_hdb) {
Note("Clearing Configuration");
Layout::relative_to(p, sizeof(p), system_runtime_dir, "hostdb.config");
if (unlink(p) < 0)
Note("unable to unlink %s", p);
xptr<char> rundir(RecConfigReadRuntimeDir());
xptr<char> config(Layout::relative_to(rundir, "hostdb.config"));

Note("Clearing HostDB Configuration");
if (unlink(config) < 0)
Note("unable to unlink %s", (const char *)config);
}

if (c_all || c_cache) {
Expand All @@ -569,6 +560,7 @@ cmd_clear(char *cmd)
return CMD_FAILED;
}
}

if (c_hdb || c_all) {
Note("Clearing Host Database");
if (hostDBProcessor.cache()->start(PROCESSOR_RECONFIGURE) < 0) {
Expand Down Expand Up @@ -1288,8 +1280,6 @@ main(int /* argc ATS_UNUSED */, char **argv)
fprintf(stderr, "%s\n", appVersionInfo.FullVersionInfoStr);
_exit(0);
}
// Ensure only one copy of traffic server is running
check_lockfile();

// Set stdout/stdin to be unbuffered
setbuf(stdout, NULL);
Expand Down Expand Up @@ -1321,6 +1311,9 @@ main(int /* argc ATS_UNUSED */, char **argv)
// Local process manager
initialize_process_manager();

// Ensure only one copy of traffic server is running
check_lockfile();

// Set the core limit for the process
init_core_size();
init_system();
Expand Down

0 comments on commit f438ab4

Please sign in to comment.