Further fixes to universal variable server socket management

- Change fishd_path to std::string
- Warn, rather than exiting with an error, if the universal variable
  server path is not available, and provide more useful advice.
- Export the new __fishd_runtime_dir variable.
This commit is contained in:
David Adam 2014-08-04 13:26:14 +08:00
parent 209d8b7f2f
commit 2aac8e5dde
7 changed files with 34 additions and 22 deletions

View File

@ -2268,10 +2268,11 @@ static int check_runtime_path(const char * path)
} }
/** Return the path of an appropriate runtime data directory */ /** Return the path of an appropriate runtime data directory */
const char* common_get_runtime_path(void) std::string common_get_runtime_path()
{ {
const char *dir = getenv("XDG_RUNTIME_DIR"); const char *dir = getenv("XDG_RUNTIME_DIR");
const char *uname = getenv("USER"); const char *uname = getenv("USER");
std::string path;
if (uname == NULL) if (uname == NULL)
{ {
@ -2283,19 +2284,19 @@ const char* common_get_runtime_path(void)
{ {
// /tmp/fish.user // /tmp/fish.user
dir = "/tmp/fish."; dir = "/tmp/fish.";
std::string path;
path.reserve(strlen(dir) + strlen(uname)); path.reserve(strlen(dir) + strlen(uname));
path.append(dir); path.append(dir);
path.append(uname); path.append(uname);
if (check_runtime_path(path.c_str()) != 0) if (check_runtime_path(path.c_str()) != 0)
{ {
debug(0, L"Couldn't create secure runtime path: '%s'", path.c_str()); debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", path.c_str());
exit(EXIT_FAILURE); path.clear();
} }
return strdup(path.c_str());
} }
else else
{ {
return dir; path.reserve(strlen(dir));
path.append(dir);
} }
return path;
} }

View File

@ -743,6 +743,6 @@ extern "C" {
} }
/** Return the path of an appropriate runtime data directory */ /** Return the path of an appropriate runtime data directory */
const char* common_get_runtime_path(void); std::string common_get_runtime_path();
#endif #endif

View File

@ -674,8 +674,8 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
const env_var_t user_dir_wstr = env_get_string(L"USER"); const env_var_t user_dir_wstr = env_get_string(L"USER");
const char * fishd_dir = common_get_runtime_path(); std::string fishd_dir = common_get_runtime_path();
env_set(L"__fish_runtime_dir", str2wcstring(fishd_dir).c_str(), ENV_GLOBAL); env_set(L"__fish_runtime_dir", str2wcstring(fishd_dir).c_str(), ENV_GLOBAL | ENV_EXPORT);
wchar_t * user_dir = user_dir_wstr.missing()?NULL:const_cast<wchar_t*>(user_dir_wstr.c_str()); wchar_t * user_dir = user_dir_wstr.missing()?NULL:const_cast<wchar_t*>(user_dir_wstr.c_str());

View File

@ -242,23 +242,29 @@ static void reconnect()
} }
void env_universal_init(const char * p, void env_universal_init(std::string p,
wchar_t *u, wchar_t *u,
void (*sf)(), void (*sf)(),
void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val)) void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val))
{ {
path=p; path=p.c_str();
user=u; user=u;
start_fishd=sf; start_fishd=sf;
external_callback = cb; external_callback = cb;
env_universal_server.fd = get_socket(); if (p == "") {
env_universal_common_init(&callback); debug(1, L"Could not connect to universal variable server. You will not be able to share variable values between fish sessions.");
env_universal_read_all(); }
s_env_univeral_inited = true; else
if (env_universal_server.fd >= 0)
{ {
env_universal_barrier(); env_universal_server.fd = get_socket();
env_universal_common_init(&callback);
env_universal_read_all();
s_env_univeral_inited = true;
if (env_universal_server.fd >= 0)
{
env_universal_barrier();
}
} }
} }

View File

@ -17,7 +17,7 @@ extern connection_t env_universal_server;
/** /**
Initialize the envuni library Initialize the envuni library
*/ */
void env_universal_init(const char * p, void env_universal_init(std::string p,
wchar_t *u, wchar_t *u,
void (*sf)(), void (*sf)(),
void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val)); void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val));

View File

@ -1032,8 +1032,8 @@ static void init(int mangle_descriptors, int out)
exit(1); exit(1);
} }
std::string dir = common_get_runtime_path();
env_universal_init("", 0, 0, 0); env_universal_init(dir, 0, 0, 0);
input_common_init(&interrupt_handler); input_common_init(&interrupt_handler);
output_set_writer(&pager_buffered_writer); output_set_writer(&pager_buffered_writer);

View File

@ -159,10 +159,15 @@ static int quit=0;
*/ */
static std::string get_socket_filename(void) static std::string get_socket_filename(void)
{ {
const char *dir = common_get_runtime_path(); std::string dir = common_get_runtime_path();
if (dir == "") {
debug(0, L"Cannot access desired socket path.");
exit(EXIT_FAILURE);
}
std::string name; std::string name;
name.reserve(strlen(dir) + strlen(SOCK_FILENAME) + 1); name.reserve(dir.length() + strlen(SOCK_FILENAME) + 1);
name.append(dir); name.append(dir);
name.push_back('/'); name.push_back('/');
name.append(SOCK_FILENAME); name.append(SOCK_FILENAME);