aboutsummaryrefslogtreecommitdiffhomepage
path: root/env_universal_common.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'env_universal_common.cpp')
-rw-r--r--env_universal_common.cpp2070
1 files changed, 1400 insertions, 670 deletions
diff --git a/env_universal_common.cpp b/env_universal_common.cpp
index 2b12cf10..a8d45bcd 100644
--- a/env_universal_common.cpp
+++ b/env_universal_common.cpp
@@ -7,27 +7,16 @@
*/
#include "config.h"
+#include "env_universal_common.h"
-#include <stdlib.h>
-#include <stdio.h>
-#include <wchar.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
+#include <fcntl.h>
#include <sys/un.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <sys/file.h>
+#include <sys/socket.h>
+#include <arpa/inet.h>
#include <pwd.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <dirent.h>
-#include <wctype.h>
-#include <iconv.h>
-
-#include <errno.h>
-#include <locale.h>
-#include <dirent.h>
-#include <signal.h>
-#include <map>
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h>
@@ -38,924 +27,1665 @@
#include "common.h"
#include "wutil.h"
-#include "env_universal_common.h"
+#include "utf8.h"
+#include "path.h"
+#include "iothread.h"
-/**
- Non-wide version of the set command
-*/
-#define SET_MBS "SET"
+#if __APPLE__
+#define FISH_NOTIFYD_AVAILABLE 1
+#include <notify.h>
+#endif
+
+// NAME_MAX is not defined on Solaris and suggests the use of pathconf()
+// There is no obvious sensible pathconf() for shared memory and _XPG_NAME_MAX
+// seems a reasonable choice.
+#if !defined(NAME_MAX) && defined(_XOPEN_NAME_MAX)
+#define NAME_MAX _XOPEN_NAME_MAX
+#endif
/**
- Non-wide version of the set_export command
+ The set command
*/
-#define SET_EXPORT_MBS "SET_EXPORT"
+#define SET_STR L"SET"
/**
- Non-wide version of the erase command
+ The set_export command
*/
-#define ERASE_MBS "ERASE"
+#define SET_EXPORT_STR L"SET_EXPORT"
+
/**
- Non-wide version of the barrier command
+ Non-wide version of the set command
*/
-#define BARRIER_MBS "BARRIER"
+#define SET_MBS "SET"
/**
- Non-wide version of the barrier_reply command
+ Non-wide version of the set_export command
*/
-#define BARRIER_REPLY_MBS "BARRIER_REPLY"
+#define SET_EXPORT_MBS "SET_EXPORT"
/**
Error message
*/
#define PARSE_ERR L"Unable to parse universal variable message: '%ls'"
-/**
- ERROR string for internal buffered reader
-*/
-#define ENV_UNIVERSAL_ERROR 0x100
+/** Small note about not editing ~/.fishd manually. Inserted at the top of all .fishd files. */
+#define SAVE_MSG "# This file is automatically generated by the fish.\n# Do NOT edit it directly, your changes will be overwritten.\n"
-/**
- EAGAIN string for internal buffered reader
-*/
-#define ENV_UNIVERSAL_AGAIN 0x101
+static wcstring fishd_get_config();
+static wcstring get_machine_identifier();
+static bool get_hostname_identifier(wcstring *result);
-/**
- EOF string for internal buffered reader
-*/
-#define ENV_UNIVERSAL_EOF 0x102
+static wcstring vars_filename_in_directory(const wcstring &wdir)
+{
+ if (wdir.empty())
+ return L"";
+
+ wcstring result = wdir;
+ result.append(L"/fishd.");
+ result.append(get_machine_identifier());
+ return result;
+}
-/**
- Maximum length of socket filename
-*/
-#ifndef UNIX_PATH_MAX
-#define UNIX_PATH_MAX 100
-#endif
+static const wcstring &default_vars_path()
+{
+ static wcstring cached_result = vars_filename_in_directory(fishd_get_config());
+ return cached_result;
+}
/**
- A variable entry. Stores the value of a variable and whether it
- should be exported. Obviously, it needs to be allocated large
- enough to fit the value string.
+ Check, and create if necessary, a secure runtime path
+ Derived from tmux.c in tmux (http://tmux.sourceforge.net/)
*/
-typedef struct var_uni_entry
+static int check_runtime_path(const char * path)
{
- bool exportv; /**< Whether the variable should be exported */
- wcstring val; /**< The value of the variable */
- var_uni_entry():exportv(false), val() { }
-}
-var_uni_entry_t;
+ /*
+ * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
+ * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
+ * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+ struct stat statpath;
+ u_int uid = geteuid();
+
+ if (mkdir(path, S_IRWXU) != 0 && errno != EEXIST)
+ return errno;
+ if (lstat(path, &statpath) != 0)
+ return errno;
+ if (!S_ISDIR(statpath.st_mode)
+ || statpath.st_uid != uid
+ || (statpath.st_mode & (S_IRWXG|S_IRWXO)) != 0)
+ return EACCES;
+ return 0;
+}
-static void parse_message(wchar_t *msg,
- connection_t *src);
+/** Return the path of an appropriate runtime data directory */
+static wcstring get_runtime_path()
+{
+ wcstring result;
+ const char *dir = getenv("XDG_RUNTIME_DIR");
+ if (dir != NULL)
+ {
+ result = str2wcstring(dir);
+ }
+ else
+ {
+ const char *uname = getenv("USER");
+ if (uname == NULL)
+ {
+ const struct passwd *pw = getpwuid(getuid());
+ uname = pw->pw_name;
+ }
-/**
- The table of all universal variables
-*/
-typedef std::map<wcstring, var_uni_entry_t> env_var_table_t;
-env_var_table_t env_universal_var;
+ // /tmp/fish.user
+ std::string tmpdir = "/tmp/fish.";
+ tmpdir.append(uname);
+ if (check_runtime_path(tmpdir.c_str()) != 0)
+ {
+ debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", tmpdir.c_str());
+ }
+ else
+ {
+ result = str2wcstring(tmpdir);
+ }
+ }
+ return result;
+}
-/**
- Callback function, should be called on all events
-*/
-static void (*callback)(fish_message_type_t type,
- const wchar_t *key,
- const wchar_t *val);
-/**
- List of names for the UTF-8 character set.
- */
-static const char *iconv_utf8_names[]=
+/* Returns a "variables" file in the appropriate runtime directory. This is called infrequently and so does not need to be cached. */
+static wcstring default_named_pipe_path()
{
- "utf-8", "UTF-8",
- "utf8", "UTF8",
- 0
+ // Note that vars_filename_in_directory returns empty string when passed the empty string
+ return vars_filename_in_directory(get_runtime_path());
}
-;
/**
- List of wide character names, undefined byte length.
- */
-static const char *iconv_wide_names_unknown[]=
+ Test if the message msg contains the command cmd
+*/
+static bool match(const wchar_t *msg, const wchar_t *cmd)
{
- "wchar_t", "WCHAR_T",
- "wchar", "WCHAR",
- 0
+ size_t len = wcslen(cmd);
+ if (wcsncasecmp(msg, cmd, len) != 0)
+ return false;
+
+ if (msg[len] && msg[len]!= L' ' && msg[len] != L'\t')
+ return false;
+
+ return true;
}
-;
-/**
- List of wide character names, 4 bytes long.
- */
-static const char *iconv_wide_names_4[]=
+static void report_error(int err_code, const wchar_t *err_format, ...)
{
- "wchar_t", "WCHAR_T",
- "wchar", "WCHAR",
- "ucs-4", "UCS-4",
- "ucs4", "UCS4",
- "utf-32", "UTF-32",
- "utf32", "UTF32",
- 0
+ va_list va;
+ va_start(va, err_format);
+ const wcstring err_text = vformat_string(err_format, va);
+ va_end(va);
+
+ if (! err_text.empty())
+ {
+ fwprintf(stderr, L"%ls: ", err_text.c_str());
+ }
+ fwprintf(stderr, L"%s\n", strerror(err_code));
}
-;
-/**
- List of wide character names, 2 bytes long.
- */
-static const char *iconv_wide_names_2[]=
+/* The universal variable format has some funny escaping requirements; here we try to be safe */
+static bool is_universal_safe_to_encode_directly(wchar_t c)
{
- "wchar_t", "WCHAR_T",
- "wchar", "WCHAR",
- "ucs-2", "UCS-2",
- "ucs2", "UCS2",
- "utf-16", "UTF-16",
- "utf16", "UTF16",
- 0
-}
-;
+ if (c < 32 || c > 128)
+ return false;
-template<class T>
-class sloppy {};
+ return iswalnum(c) || wcschr(L"/_", c);
+}
-static size_t hack_iconv(iconv_t cd, const char * const* inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
+/**
+ Escape specified string
+ */
+static wcstring full_escape(const wchar_t *in)
{
- /* FreeBSD has this prototype: size_t iconv (iconv_t, const char **...)
- OS X and Linux this one: size_t iconv (iconv_t, char **...)
- AFAIK there's no single type that can be passed as both char ** and const char **.
- Therefore, we let C++ figure it out, by providing a struct with an implicit conversion to both char** and const char **.
- */
- struct sloppy_char
+ wcstring out;
+ for (; *in; in++)
{
- const char * const * t;
- operator char** () const
+ wchar_t c = *in;
+ if (is_universal_safe_to_encode_directly(c))
{
- return (char **)t;
+ out.push_back(c);
}
- operator const char** () const
+ else if (c <= (wchar_t)ASCII_MAX)
{
- return (const char**)t;
+ // See #1225 for discussion of use of ASCII_MAX here
+ append_format(out, L"\\x%.2x", c);
}
- } slop_inbuf = {inbuf};
-
- return iconv(cd, slop_inbuf, inbytesleft, outbuf, outbytesleft);
+ else if (c < 65536)
+ {
+ append_format(out, L"\\u%.4x", c);
+ }
+ else
+ {
+ append_format(out, L"\\U%.8x", c);
+ }
+ }
+ return out;
}
-/**
- Convert utf-8 string to wide string
- */
-static wchar_t *utf2wcs(const char *in)
+/* Converts input to UTF-8 and appends it to receiver, using storage as temp storage */
+static bool append_utf8(const wcstring &input, std::string *receiver, std::string *storage)
{
- iconv_t cd=(iconv_t) -1;
- int i,j;
-
- wchar_t *out;
-
- /*
- Try to convert to wchar_t. If that is not a valid character set,
- try various names for ucs-4. We can't be sure that ucs-4 is
- really the character set used by wchar_t, but it is the best
- assumption we can make.
- */
- const char **to_name=0;
-
- switch (sizeof(wchar_t))
+ bool result = false;
+ if (wchar_to_utf8_string(input, storage))
{
+ receiver->append(*storage);
+ result = true;
+ }
+ return result;
+}
- case 2:
- to_name = iconv_wide_names_2;
- break;
-
- case 4:
- to_name = iconv_wide_names_4;
- break;
-
- default:
- to_name = iconv_wide_names_unknown;
- break;
+/* Creates a file entry like "SET fish_color_cwd:FF0". Appends the result to *result (as UTF8). Returns true on success. storage may be used for temporary storage, to avoid allocations */
+static bool append_file_entry(fish_message_type_t type, const wcstring &key_in, const wcstring &val_in, std::string *result, std::string *storage)
+{
+ assert(storage != NULL);
+ assert(result != NULL);
+
+ // Record the length on entry, in case we need to back up
+ bool success = true;
+ const size_t result_length_on_entry = result->size();
+
+ // Append header like "SET "
+ result->append(type==SET ? SET_MBS : SET_EXPORT_MBS);
+ result->push_back(' ');
+
+ // Append variable name like "fish_color_cwd"
+ if (wcsvarname(key_in.c_str()))
+ {
+ debug(0, L"Illegal variable name: '%ls'", key_in.c_str());
+ success = false;
+ }
+ if (success && ! append_utf8(key_in, result, storage))
+ {
+ debug(0, L"Could not convert %ls to narrow character string", key_in.c_str());
+ success = false;
}
+
+ // Append ":"
+ if (success)
+ {
+ result->push_back(':');
+ }
+
+ // Append value
+ if (success && ! append_utf8(full_escape(val_in.c_str()), result, storage))
+ {
+ debug(0, L"Could not convert %ls to narrow character string", val_in.c_str());
+ success = false;
+ }
+
+ // Append newline
+ if (success)
+ {
+ result->push_back('\n');
+ }
+
+ // Don't modify result on failure. It's sufficient to simply resize it since all we ever did was append to it.
+ if (! success)
+ {
+ result->resize(result_length_on_entry);
+ }
+
+ return success;
+}
+env_universal_t::env_universal_t(const wcstring &path) : explicit_vars_path(path), tried_renaming(false), last_read_file(kInvalidFileID)
+{
+ VOMIT_ON_FAILURE(pthread_mutex_init(&lock, NULL));
+}
- /*
- The line protocol fish uses is always utf-8.
- */
- const char **from_name = iconv_utf8_names;
+env_universal_t::~env_universal_t()
+{
+ pthread_mutex_destroy(&lock);
+}
- size_t in_len = strlen(in);
- size_t out_len = sizeof(wchar_t)*(in_len+2);
- size_t nconv;
- char *nout;
+env_var_t env_universal_t::get(const wcstring &name) const
+{
+ env_var_t result = env_var_t::missing_var();
+ var_table_t::const_iterator where = vars.find(name);
+ if (where != vars.end())
+ {
+ result = where->second.val;
+ }
+ return result;
+}
- out = (wchar_t *)malloc(out_len);
- nout = (char *)out;
+bool env_universal_t::get_export(const wcstring &name) const
+{
+ bool result = false;
+ var_table_t::const_iterator where = vars.find(name);
+ if (where != vars.end())
+ {
+ result = where->second.exportv;
+ }
+ return result;
+}
- if (!out)
- return 0;
- for (i=0; to_name[i]; i++)
+void env_universal_t::set_internal(const wcstring &key, const wcstring &val, bool exportv, bool overwrite)
+{
+ ASSERT_IS_LOCKED(lock);
+ if (! overwrite && this->modified.find(key) != this->modified.end())
{
- for (j=0; from_name[j]; j++)
+ /* This value has been modified and we're not overwriting it. Skip it. */
+ return;
+ }
+
+ var_entry_t *entry = &vars[key];
+ if (entry->exportv != exportv || entry->val != val)
+ {
+ entry->val = val;
+ entry->exportv = exportv;
+
+ /* If we are overwriting, then this is now modified */
+ if (overwrite)
{
- cd = iconv_open(to_name[i], from_name[j]);
-
- if (cd != (iconv_t) -1)
- {
- goto start_conversion;
-
- }
+ this->modified.insert(key);
}
}
+}
-start_conversion:
+void env_universal_t::set(const wcstring &key, const wcstring &val, bool exportv)
+{
+ scoped_lock locker(lock);
+ this->set_internal(key, val, exportv, true /* overwrite */);
+}
- if (cd == (iconv_t) -1)
+bool env_universal_t::remove_internal(const wcstring &key)
+{
+ ASSERT_IS_LOCKED(lock);
+ size_t erased = this->vars.erase(key);
+ if (erased > 0)
{
- /* Something went wrong. */
- debug(0, L"Could not perform utf-8 conversion");
- if (errno != EINVAL)
- wperror(L"iconv_open");
-
- /* Terminate the output string. */
- free(out);
- return 0;
+ this->modified.insert(key);
}
+ return erased > 0;
+}
- /* FreeBSD has this prototype: size_t iconv (iconv_t, const char **...)
- OS X and Linux this one: size_t iconv (iconv_t, char **...)
- AFAIK there's no single type that can be passed as both char ** and const char **.
- Hence this hack.
- */
- nconv = hack_iconv(cd, &in, &in_len, &nout, &out_len);
+bool env_universal_t::remove(const wcstring &key)
+{
+ scoped_lock locker(lock);
+ return this->remove_internal(key);
+}
- if (nconv == (size_t) -1)
+wcstring_list_t env_universal_t::get_names(bool show_exported, bool show_unexported) const
+{
+ wcstring_list_t result;
+ scoped_lock locker(lock);
+ var_table_t::const_iterator iter;
+ for (iter = vars.begin(); iter != vars.end(); ++iter)
{
- debug(0, L"Error while converting from utf string");
- return 0;
+ const wcstring &key = iter->first;
+ const var_entry_t &e = iter->second;
+ if ((e.exportv && show_exported) || (! e.exportv && show_unexported))
+ {
+ result.push_back(key);
+ }
}
+ return result;
+}
- *((wchar_t *) nout) = L'\0';
-
- /*
- Check for silly iconv behaviour inserting an bytemark in the output
- string.
- */
- if (*out == L'\xfeff' || *out == L'\xffef' || *out == L'\xefbbbf')
+/* Given a variable table, generate callbacks representing the difference between our vars and the new vars */
+void env_universal_t::generate_callbacks(const var_table_t &new_vars, callback_data_list_t *callbacks) const
+{
+ assert(callbacks != NULL);
+
+ /* Construct callbacks for erased values */
+ for (var_table_t::const_iterator iter = this->vars.begin(); iter != this->vars.end(); ++iter)
{
- wchar_t *out_old = out;
- out = wcsdup(out+1);
- if (! out)
+ const wcstring &key = iter->first;
+
+ /* Skip modified values */
+ if (this->modified.find(key) != this->modified.end())
+ {
+ continue;
+ }
+
+ /* If the value is not present in new_vars, it has been erased */
+ if (new_vars.find(key) == new_vars.end())
{
- debug(0, L"FNORD!!!!");
- free(out_old);
- return 0;
+ callbacks->push_back(callback_data_t(ERASE, key, L""));
+ }
+ }
+
+ /* Construct callbacks for newly inserted or changed values */
+ for (var_table_t::const_iterator iter = new_vars.begin(); iter != new_vars.end(); ++iter)
+ {
+ const wcstring &key = iter->first;
+
+ /* Skip modified values */
+ if (this->modified.find(key) != this->modified.end())
+ {
+ continue;
+ }
+
+ /* See if the value has changed */
+ const var_entry_t &new_entry = iter->second;
+ var_table_t::const_iterator existing = this->vars.find(key);
+ if (existing == this->vars.end() || existing->second.exportv != new_entry.exportv || existing->second.val != new_entry.val)
+ {
+ /* Value has changed */
+ callbacks->push_back(callback_data_t(new_entry.exportv ? SET_EXPORT : SET, key, new_entry.val));
}
- free(out_old);
}
-
-
- if (iconv_close(cd) != 0)
- wperror(L"iconv_close");
-
- return out;
}
-
-/**
- Convert wide string to utf-8
- */
-static char *wcs2utf(const wchar_t *in)
+void env_universal_t::acquire_variables(var_table_t *vars_to_acquire)
{
- iconv_t cd=(iconv_t) -1;
- int i,j;
-
- char *char_in = (char *)in;
- char *out;
-
- /*
- Try to convert to wchar_t. If that is not a valid character set,
- try various names for ucs-4. We can't be sure that ucs-4 is
- really the character set used by wchar_t, but it is the best
- assumption we can make.
- */
- const char **from_name=0;
-
- switch (sizeof(wchar_t))
+ /* Copy modified values from existing vars to vars_to_acquire */
+ for (std::set<wcstring>::iterator iter = this->modified.begin(); iter != this->modified.end(); ++iter)
{
-
- case 2:
- from_name = iconv_wide_names_2;
- break;
-
- case 4:
- from_name = iconv_wide_names_4;
- break;
-
- default:
- from_name = iconv_wide_names_unknown;
- break;
+ const wcstring &key = *iter;
+ var_table_t::iterator src_iter = this->vars.find(key);
+ if (src_iter == this->vars.end())
+ {
+ /* The value has been deleted. */
+ vars_to_acquire->erase(key);
+ }
+ else
+ {
+ /* The value has been modified. Copy it over. Note we can destructively modify the source entry in vars since we are about to get rid of this->vars entirely. */
+ var_entry_t &src = src_iter->second;
+ var_entry_t &dst = (*vars_to_acquire)[key];
+ dst.val.swap(src.val);
+ dst.exportv = src.exportv;
+ }
}
+
+ /* We have constructed all the callbacks and updated vars_to_acquire. Acquire it! */
+ this->vars.swap(*vars_to_acquire);
+}
- const char **to_name = iconv_utf8_names;
-
- size_t in_len = wcslen(in);
- size_t out_len = sizeof(char)*((MAX_UTF8_BYTES*in_len)+1);
- size_t nconv;
- char *nout;
-
- out = (char *)malloc(out_len);
- nout = (char *)out;
- in_len *= sizeof(wchar_t);
-
- if (!out)
- return 0;
-
- for (i=0; to_name[i]; i++)
+void env_universal_t::load_from_fd(int fd, callback_data_list_t *callbacks)
+{
+ ASSERT_IS_LOCKED(lock);
+ assert(fd >= 0);
+ /* Get the dev / inode */
+ const file_id_t current_file = file_id_for_fd(fd);
+ if (current_file == last_read_file)
+ {
+ UNIVERSAL_LOG("Sync elided based on fstat()");
+ }
+ else
{
- for (j=0; from_name[j]; j++)
+ /* Read a variables table from the file. */
+ var_table_t new_vars = this->read_message_internal(fd);
+
+ /* Announce changes */
+ if (callbacks != NULL)
{
- cd = iconv_open(to_name[i], from_name[j]);
-
- if (cd != (iconv_t) -1)
- {
- goto start_conversion;
-
- }
+ this->generate_callbacks(new_vars, callbacks);
}
+
+ /* Acquire the new variables */
+ this->acquire_variables(&new_vars);
+
+ last_read_file = current_file;
}
+}
-start_conversion:
-
- if (cd == (iconv_t) -1)
+bool env_universal_t::load_from_path(const wcstring &path, callback_data_list_t *callbacks)
+{
+ ASSERT_IS_LOCKED(lock);
+
+ /* Check to see if the file is unchanged. We do this again in load_from_fd, but this avoids opening the file unnecessarily. */
+ if (last_read_file != kInvalidFileID && file_id_for_path(path) == last_read_file)
{
- /* Something went wrong. */
- debug(0, L"Could not perform utf-8 conversion");
- if (errno != EINVAL)
- wperror(L"iconv_open");
-
- /* Terminate the output string. */
- free(out);
- return 0;
+ UNIVERSAL_LOG("Sync elided based on fast stat()");
+ return true;
}
-
- nconv = hack_iconv(cd, &char_in, &in_len, &nout, &out_len);
-
-
- if (nconv == (size_t) -1)
+
+ bool result = false;
+ int fd = wopen_cloexec(path, O_RDONLY);
+ if (fd >= 0)
{
- debug(0, L"%d %d", in_len, out_len);
- debug(0, L"Error while converting from to string");
- return 0;
+ UNIVERSAL_LOG("Reading from file");
+ this->load_from_fd(fd, callbacks);
+ close(fd);
+ result = true;
}
-
- *nout = '\0';
-
- if (iconv_close(cd) != 0)
- wperror(L"iconv_close");
-
- return out;
+ return result;
}
-
-
-void env_universal_common_init(void (*cb)(fish_message_type_t type, const wchar_t *key, const wchar_t *val))
+/* Writes our state to the fd. path is provided only for error reporting */
+bool env_universal_t::write_to_fd(int fd, const wcstring &path)
{
- callback = cb;
+ ASSERT_IS_LOCKED(lock);
+ assert(fd >= 0);
+ bool success = true;
+
+ // Stuff we output to fd
+ std::string contents;
+
+ // Temporary storage
+ std::string storage;
+
+ // Write the save message. If this fails, we don't bother complaining.
+ write_loop(fd, SAVE_MSG, strlen(SAVE_MSG));
+
+ var_table_t::const_iterator iter = vars.begin();
+ while (iter != vars.end())
+ {
+ // Append the entry. Note that append_file_entry may fail, but that only affects one variable; soldier on.
+ const wcstring &key = iter->first;
+ const var_entry_t &entry = iter->second;
+ append_file_entry(entry.exportv ? SET_EXPORT : SET, key, entry.val, &contents, &storage);
+
+ // Go to next
+ ++iter;
+
+ // Flush if this is the last iteration or we exceed a page
+ if (iter == vars.end() || contents.size() >= 4096)
+ {
+ if (write_loop(fd, contents.data(), contents.size()) < 0)
+ {
+ int err = errno;
+ report_error(err, L"Unable to write to universal variables file '%ls'", path.c_str());
+ success = false;
+ break;
+ }
+ contents.clear();
+ }
+ }
+
+ /* Since we just wrote out this file, it matches our internal state; pretend we read from it */
+ this->last_read_file = file_id_for_fd(fd);
+
+ /* We don't close the file */
+ return success;
}
-/**
- Read one byte of date from the specified connection
- */
-static int read_byte(connection_t *src)
+bool env_universal_t::move_new_vars_file_into_place(const wcstring &src, const wcstring &dst)
{
-
- if (src->buffer_consumed >= src->read_buffer.size())
+ int ret = wrename(src, dst);
+ if (ret != 0)
{
- char local[ENV_UNIVERSAL_BUFFER_SIZE];
-
- ssize_t res = read(src->fd, local, sizeof local);
+ int err = errno;
+ report_error(err, L"Unable to rename file from '%ls' to '%ls'", src.c_str(), dst.c_str());
+ }
+ return ret == 0;
+}
-// debug(4, L"Read chunk '%.*s'", res, src->buffer );
+static wcstring fishd_get_config()
+{
+ bool done = false;
+ wcstring result;
- if (res < 0)
+ env_var_t xdg_dir = env_get_string(L"XDG_CONFIG_HOME", ENV_GLOBAL | ENV_EXPORT);
+ if (! xdg_dir.missing_or_empty())
+ {
+ result = xdg_dir;
+ append_path_component(result, L"/fish");
+ if (!create_directory(result))
{
-
- if (errno == EAGAIN ||
- errno == EINTR)
+ done = true;
+ }
+ }
+ else
+ {
+ env_var_t home = env_get_string(L"HOME", ENV_GLOBAL | ENV_EXPORT);
+ if (! home.missing_or_empty())
+ {
+ result = home;
+ append_path_component(result, L"/.config/fish");
+ if (!create_directory(result))
{
- return ENV_UNIVERSAL_AGAIN;
+ done = 1;
}
+ }
+ }
+
+ if (! done)
+ {
+ /* Bad juju */
+ debug(0, _(L"Unable to create a configuration directory for fish. Your personal settings will not be saved. Please set the $XDG_CONFIG_HOME variable to a directory where the current user has write access."));
+ result.clear();
+ }
+
+ return result;
+}
- return ENV_UNIVERSAL_ERROR;
+bool env_universal_t::load()
+{
+ scoped_lock locker(lock);
+ callback_data_list_t callbacks;
+ const wcstring vars_path = explicit_vars_path.empty() ? default_vars_path() : explicit_vars_path;
+ bool success = load_from_path(vars_path, &callbacks);
+ if (! success && ! tried_renaming && errno == ENOENT)
+ {
+ /* We failed to load, because the file was not found. Older fish used the hostname only. Try *moving* the filename based on the hostname into place; if that succeeds try again. Silently "upgraded." */
+ tried_renaming = true;
+ wcstring hostname_id;
+ if (get_hostname_identifier(&hostname_id))
+ {
+ const wcstring hostname_path = wdirname(vars_path) + L'/' + hostname_id;
+ if (0 == wrename(hostname_path, vars_path))
+ {
+ /* We renamed - try again */
+ success = this->load();
+ }
+ }
+ }
+ return success;
+}
+bool env_universal_t::open_temporary_file(const wcstring &directory, wcstring *out_path, int *out_fd)
+{
+ /* Create and open a temporary file for writing within the given directory */
+ /* Try to create a temporary file, up to 10 times. We don't use mkstemps because we want to open it CLO_EXEC. This should almost always succeed on the first try. */
+ assert(! string_suffixes_string(L"/", directory));
+
+ bool success = false;
+ const wcstring tmp_name_template = directory + L"/fishd.tmp.XXXXXX";
+ wcstring tmp_name;
+ for (size_t attempt = 0; attempt < 10 && ! success; attempt++)
+ {
+ int result_fd = -1;
+ char *narrow_str = wcs2str(tmp_name_template.c_str());
+#if HAVE_MKOSTEMP
+ result_fd = mkostemp(narrow_str, O_CLOEXEC);
+ if (result_fd >= 0)
+ {
+ tmp_name = str2wcstring(narrow_str);
}
- else if (res == 0)
+#else
+ if (mktemp(narrow_str))
{
- return ENV_UNIVERSAL_EOF;
+ /* It was successfully templated; try opening it atomically */
+ tmp_name = str2wcstring(narrow_str);
+ result_fd = wopen_cloexec(tmp_name, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0644);
}
- else
+#endif
+
+ if (result_fd >= 0)
{
- src->read_buffer.clear();
- src->read_buffer.insert(src->read_buffer.begin(), local, local + res);
- src->buffer_consumed = 0;
+ /* Success */
+ *out_fd = result_fd;
+ *out_path = str2wcstring(narrow_str);
+ success = true;
}
+ free(narrow_str);
}
-
- return src->read_buffer.at(src->buffer_consumed++);
-
+ if (! success)
+ {
+ int err = errno;
+ report_error(err, L"Unable to open file '%ls'", tmp_name.c_str());
+ }
+ return success;
}
-
-void read_message(connection_t *src)
+bool env_universal_t::open_and_acquire_lock(const wcstring &path, int *out_fd)
{
- while (1)
+ /* Attempt to open the file for reading at the given path, atomically acquiring a lock. On BSD, we can use O_EXLOCK. On Linux, we open the file, take a lock, and then compare fstat() to stat(); if they match, it means that the file was not replaced before we acquired the lock.
+
+ We pass O_RDONLY with O_CREAT; this creates a potentially empty file. We do this so that we have something to lock on.
+ */
+ int result_fd = -1;
+ bool needs_lock = true;
+ int flags = O_RDONLY | O_CREAT;
+#ifdef O_EXLOCK
+ flags |= O_EXLOCK;
+ needs_lock = false;
+#endif
+ for (;;)
{
-
- int ib = read_byte(src);
- char b;
-
- switch (ib)
+ int fd = wopen_cloexec(path, flags, 0644);
+ if (fd < 0)
{
- case ENV_UNIVERSAL_AGAIN:
+ int err = errno;
+ if (err == EINTR)
{
- return;
+ /* Signal; try again */
+ continue;
}
-
- case ENV_UNIVERSAL_ERROR:
+#ifdef O_EXLOCK
+ else if (err == EOPNOTSUPP)
{
- debug(2, L"Read error on fd %d, set killme flag", src->fd);
- if (debug_level > 2)
- wperror(L"read");
- src->killme = 1;
- return;
+ /* Filesystem probably does not support locking. Clear the flag and try again. Note that we try taking the lock via flock anyways. */
+ flags &= ~O_EXLOCK;
+ needs_lock = true;
+ continue;
}
-
- case ENV_UNIVERSAL_EOF:
+#endif
+ else
{
- src->killme = 1;
- debug(3, L"Fd %d has reached eof, set killme flag", src->fd);
- if (! src->input.empty())
- {
- char c = 0;
- src->input.push_back(c);
- debug(1,
- L"Universal variable connection closed while reading command. Partial command recieved: '%s'",
- &src->input.at(0));
- }
- return;
+ report_error(err, L"Unable to open universal variable file '%ls'", path.c_str());
+ break;
}
}
-
- b = (char)ib;
-
- if (b == '\n')
+
+ /* If we get here, we must have a valid fd */
+ assert(fd >= 0);
+
+ /* Try taking the lock, if necessary. If we failed, we may be on lockless NFS, etc.; in that case we pretend we succeeded. See the comment in save_to_path for the rationale. */
+ if (needs_lock)
{
- wchar_t *msg;
-
- b = 0;
- src->input.push_back(b);
-
- msg = utf2wcs(&src->input.at(0));
-
- /*
- Before calling parse_message, we must empty reset
- everything, since the callback function could
- potentially call read_message.
- */
- src->input.clear();
-
- if (msg)
+ while (flock(fd, LOCK_EX) < 0)
{
- parse_message(msg, src);
- }
- else
- {
- debug(0, _(L"Could not convert message '%s' to wide character string"), &src->input.at(0));
+ /* error */
+ if (errno != EINTR)
+ {
+ int err = errno;
+ report_error(err, L"Unable to lock universal variable file '%ls'", path.c_str());
+ break;
+ }
}
-
- free(msg);
-
}
- else
+
+ /* Hopefully we got the lock. However, it's possible the file changed out from under us while we were waiting for the lock. Make sure that didn't happen. */
+ if (file_id_for_fd(fd) != file_id_for_path(path))
{
- src->input.push_back(b);
+ /* Oops, it changed! Try again */
+ close(fd);
+ continue;
}
- }
-}
-/**
- Remove variable with specified name
-*/
-void env_universal_common_remove(const wcstring &name)
-{
- env_universal_var.erase(name);
+
+ /* Finally, we have an fd that's valid and hopefully locked. We're done */
+ assert(fd >= 0);
+ result_fd = fd;
+ break;
+ }
+
+ *out_fd = result_fd;
+
+ return result_fd >= 0;
}
-/**
- Test if the message msg contains the command cmd
-*/
-static bool match(const wchar_t *msg, const wchar_t *cmd)
+/* Returns true if modified variables were written, false if not. (There may still be variable changes due to other processes on a false return). */
+bool env_universal_t::sync(callback_data_list_t *callbacks)
{
- size_t len = wcslen(cmd);
- if (wcsncasecmp(msg, cmd, len) != 0)
+ UNIVERSAL_LOG("sync");
+ scoped_lock locker(lock);
+ /* Our saving strategy:
+
+ 1. Open the file, producing an fd.
+ 2. Lock the file (may be combined with step 1 on systems with O_EXLOCK)
+ 3. After taking the lock, check if the file at the given path is different from what we opened. If so, start over.
+ 4. Read from the file. This can be elided if its dev/inode is unchanged since the last read
+ 5. Open an adjacent temporary file
+ 6. Write our changes to an adjacent file
+ 7. Move the adjacent file into place via rename. This is assumed to be atomic.
+ 8. Release the lock and close the file
+
+ Consider what happens if Process 1 and 2 both do this simultaneously. Can there be data loss? Process 1 opens the file and then attempts to take the lock. Now, either process 1 will see the original file, or process 2's new file. If it sees the new file, we're OK: it's going to read from the new file, and so there's no data loss. If it sees the old file, then process 2 must have locked it (if process 1 locks it, switch their roles). The lock will block until process 2 reaches step 7; at that point process 1 will reach step 2, notice that the file has changed, and then start over.
+
+ It's possible that the underlying filesystem does not support locks (lockless NFS). In this case, we risk data loss if two shells try to write their universal variables simultaneously. In practice this is unlikely, since uvars are usually written interactively.
+
+ Prior versions of fish used a hard link scheme to support file locking on lockless NFS. The risk here is that if the process crashes or is killed while holding the lock, future instances of fish will not be able to obtain it. This seems to be a greater risk than that of data loss on lockless NFS. Users who put their home directory on lockless NFS are playing with fire anyways.
+ */
+ const wcstring &vars_path = explicit_vars_path.empty() ? default_vars_path() : explicit_vars_path;
+
+ /* If we have no changes, just load */
+ if (modified.empty())
+ {
+ this->load_from_path(vars_path, callbacks);
return false;
+ }
+
+ const wcstring directory = wdirname(vars_path);
+ bool success = true;
+ int vars_fd = -1;
+ int private_fd = -1;
+ wcstring private_file_path;
+
+ UNIVERSAL_LOG("Performing full sync");
+
+ /* Open the file */
+ if (success)
+ {
+ success = this->open_and_acquire_lock(vars_path, &vars_fd);
+ }
- if (msg[len] && msg[len]!= L' ' && msg[len] != L'\t')
- return false;
+ /* Read from it */
+ if (success)
+ {
+ assert(vars_fd >= 0);
+ this->load_from_fd(vars_fd, callbacks);
+ }
- return true;
+ /* Open adjacent temporary file */
+ if (success)
+ {
+ success = this->open_temporary_file(directory, &private_file_path, &private_fd);
+ }
+
+ /* Write to it */
+ if (success)
+ {
+ assert(private_fd >= 0);
+ success = this->write_to_fd(private_fd, private_file_path);
+ }
+
+ if (success)
+ {
+ /* Apply new file */
+ success = this->move_new_vars_file_into_place(private_file_path, vars_path);
+ }
+
+ if (success)
+ {
+ /* Since we moved the new file into place, clear the path so we don't try to unlink it */
+ private_file_path.clear();
+ }
+
+ /* Clean up */
+ if (vars_fd >= 0)
+ {
+ close(vars_fd);
+ }
+ if (private_fd >= 0)
+ {
+ close(private_fd);
+ }
+ if (! private_file_path.empty())
+ {
+ wunlink(private_file_path);
+ }
+
+ if (success)
+ {
+ /* All of our modified variables have now been written out. */
+ modified.clear();
+ }
+
+ return success;
}
-void env_universal_common_set(const wchar_t *key, const wchar_t *val, bool exportv)
+var_table_t env_universal_t::read_message_internal(int fd)
{
- CHECK(key,);
- CHECK(val,);
-
- var_uni_entry_t &entry = env_universal_var[key];
- entry.exportv=exportv;
- entry.val = val;
-
- if (callback)
+ var_table_t result;
+
+ // Temp value used to avoid repeated allocations
+ wcstring storage;
+
+ // The line we construct (and then parse)
+ std::string line;
+ wcstring wide_line;
+ for (;;)
{
- callback(exportv?SET_EXPORT:SET, key, val);
+ // Read into a buffer. Note this is NOT null-terminated!
+ char buffer[1024];
+ ssize_t amt = read_loop(fd, buffer, sizeof buffer);
+ if (amt <= 0)
+ {
+ break;
+ }
+ const size_t bufflen = (size_t)amt;
+
+ // Walk over it by lines. The contents of an unterminated line will be left in 'line' for the next iteration.
+ size_t line_start = 0;
+ while (line_start < amt)
+ {
+ // Run until we hit a newline
+ size_t cursor = line_start;
+ while (cursor < bufflen && buffer[cursor] != '\n')
+ {
+ cursor++;
+ }
+
+ // Copy over what we read
+ line.append(buffer + line_start, cursor - line_start);
+
+ // Process it if it's a newline (which is true if we are before the end of the buffer)
+ if (cursor < bufflen && ! line.empty())
+ {
+ if (utf8_to_wchar_string(line, &wide_line))
+ {
+ env_universal_t::parse_message_internal(wide_line, &result, &storage);
+ }
+ line.clear();
+ }
+
+ // Skip over the newline (or skip past the end)
+ line_start = cursor + 1;
+ }
}
+
+ // We make no effort to handle an unterminated last line
+ return result;
}
-
/**
- Parse message msg
-*/
-static void parse_message(wchar_t *msg,
- connection_t *src)
+ Parse message msg
+ */
+void env_universal_t::parse_message_internal(const wcstring &msgstr, var_table_t *vars, wcstring *storage)
{
-// debug( 3, L"parse_message( %ls );", msg );
-
+ const wchar_t *msg = msgstr.c_str();
+
+ // debug( 3, L"parse_message( %ls );", msg );
+
if (msg[0] == L'#')
return;
-
- if (match(msg, SET_STR) || match(msg, SET_EXPORT_STR))
+
+ bool is_set_export = match(msg, SET_EXPORT_STR);
+ bool is_set = ! is_set_export && match(msg, SET_STR);
+ if (is_set || is_set_export)
{
- wchar_t *name, *tmp;
- bool exportv = match(msg, SET_EXPORT_STR);
-
+ const wchar_t *name, *tmp;
+ const bool exportv = is_set_export;
+
name = msg+(exportv?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
- while (wcschr(L"\t ", *name))
+ while (name[0] == L'\t' || name[0] == L' ')
name++;
-
+
tmp = wcschr(name, L':');
if (tmp)
{
- wchar_t *val;
- const wcstring key(name, tmp - name);
-
- val = tmp+1;
- val = unescape(val, 0);
-
- if (val != NULL)
- env_universal_common_set(key.c_str(), val, exportv);
-
- free(val);
+ /* Use 'storage' to hold our key to avoid allocations */
+ storage->assign(name, tmp - name);
+ const wcstring &key = *storage;
+
+ wcstring val;
+ if (unescape_string(tmp + 1, &val, 0))
+ {
+ var_entry_t &entry = (*vars)[key];
+ entry.exportv = exportv;
+ entry.val.swap(val); //acquire the value
+ }
}
else
{
debug(1, PARSE_ERR, msg);
}
}
- else if (match(msg, ERASE_STR))
+ else
{
- wchar_t *name, *tmp;
+ debug(1, PARSE_ERR, msg);
+ }
+}
- name = msg+wcslen(ERASE_STR);
- while (wcschr(L"\t ", *name))
- name++;
+/**
+ Maximum length of hostname. Longer hostnames are truncated
+ */
+#define HOSTNAME_LEN 32
+
+/* Length of a MAC address */
+#define MAC_ADDRESS_MAX_LEN 6
- tmp = name;
- while (iswalnum(*tmp) || *tmp == L'_')
- tmp++;
- *tmp = 0;
+/* Thanks to Jan Brittenson
+ http://lists.apple.com/archives/xcode-users/2009/May/msg00062.html
+ */
+#ifdef SIOCGIFHWADDR
- if (!wcslen(name))
+/* Linux */
+#include <net/if.h>
+static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN], const char *interface = "eth0")
+{
+ bool result = false;
+ const int dummy = socket(AF_INET, SOCK_STREAM, 0);
+ if (dummy >= 0)
+ {
+ struct ifreq r;
+ strncpy((char *)r.ifr_name, interface, sizeof r.ifr_name - 1);
+ r.ifr_name[sizeof r.ifr_name - 1] = 0;
+ if (ioctl(dummy, SIOCGIFHWADDR, &r) >= 0)
{
- debug(1, PARSE_ERR, msg);
+ memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN);
+ result = true;
}
+ close(dummy);
+ }
+ return result;
+}
- env_universal_common_remove(name);
+#elif defined(HAVE_GETIFADDRS)
- if (callback)
+/* OS X and BSD */
+#include <ifaddrs.h>
+#include <net/if_dl.h>
+static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN], const char *interface = "en0")
+{
+ // BSD, Mac OS X
+ struct ifaddrs *ifap;
+ bool ok = false;
+
+ if (getifaddrs(&ifap) == 0)
+ {
+ for (const ifaddrs *p = ifap; p; p = p->ifa_next)
{
- callback(ERASE, name, 0);
+ if (p->ifa_addr->sa_family == AF_LINK)
+ {
+ if (p->ifa_name && p->ifa_name[0] &&
+ ! strcmp((const char*)p->ifa_name, interface))
+ {
+
+ const sockaddr_dl& sdl = *(sockaddr_dl*)p->ifa_addr;
+
+ size_t alen = sdl.sdl_alen;
+ if (alen > MAC_ADDRESS_MAX_LEN) alen = MAC_ADDRESS_MAX_LEN;
+ memcpy(macaddr, sdl.sdl_data + sdl.sdl_nlen, alen);
+ ok = true;
+ break;
+ }
+ }
}
+ freeifaddrs(ifap);
}
- else if (match(msg, BARRIER_STR))
+ return ok;
+}
+
+#else
+
+/* Unsupported */
+static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN])
+{
+ return false;
+}
+
+#endif
+
+/* Function to get an identifier based on the hostname */
+static bool get_hostname_identifier(wcstring *result)
+{
+ bool success = false;
+ char hostname[HOSTNAME_LEN + 1] = {};
+ if (gethostname(hostname, HOSTNAME_LEN) == 0)
{
- message_t *msg = create_message(BARRIER_REPLY, 0, 0);
- msg->count = 1;
- src->unsent.push(msg);
- try_send_all(src);
+ result->assign(str2wcstring(hostname));
+ success = true;
}
- else if (match(msg, BARRIER_REPLY_STR))
+ return success;
+}
+
+/* Get a sort of unique machine identifier. Prefer the MAC address; if that fails, fall back to the hostname; if that fails, pick something. */
+wcstring get_machine_identifier()
+{
+ wcstring result;
+ unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {};
+ if (get_mac_address(mac_addr))
{
- if (callback)
+ result.reserve(2 * MAC_ADDRESS_MAX_LEN);
+ for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++)
{
- callback(BARRIER_REPLY, 0, 0);
+ append_format(result, L"%02x", mac_addr[i]);
}
}
+ else if (get_hostname_identifier(&result))
+ {
+ /* Hooray */
+ }
else
{
- debug(1, PARSE_ERR, msg);
+ /* Fallback */
+ result.assign(L"nohost");
}
+ return result;
}
-/**
- Attempt to send the specified message to the specified file descriptor
-
- \return 1 on sucess, 0 if the message could not be sent without blocking and -1 on error
-*/
-static int try_send(message_t *msg,
- int fd)
+class universal_notifier_shmem_poller_t : public universal_notifier_t
{
+ /* This is what our shared memory looks like. Everything here is stored in network byte order (big-endian) */
+ struct universal_notifier_shmem_t
+ {
+ uint32_t magic;
+ uint32_t version;
+ uint32_t universal_variable_seed;
+ };
+
+#define SHMEM_MAGIC_NUMBER 0xF154
+#define SHMEM_VERSION_CURRENT 1000
+
+ private:
+ long long last_change_time;
+ uint32_t last_seed;
+ volatile universal_notifier_shmem_t *region;
+
+ void open_shmem()
+ {
+ assert(region == NULL);
+
+ // Use a path based on our uid to avoid collisions
+ char path[NAME_MAX];
+ snprintf(path, sizeof path, "/%ls_shmem_%d", program_name ? program_name : L"fish", getuid());
+
+ bool errored = false;
+ int fd = shm_open(path, O_RDWR | O_CREAT, 0600);
+ if (fd < 0)
+ {
+ int err = errno;
+ report_error(err, L"Unable to open shared memory with path '%s'", path);
+ errored = true;
+ }
- debug(3,
- L"before write of %d chars to fd %d", msg->body.size(), fd);
-
- ssize_t res = write(fd, msg->body.c_str(), msg->body.size());
-
- if (res != -1)
+ /* Get the size */
+ size_t size = 0;
+ if (! errored)
+ {
+ struct stat buf = {};
+ if (fstat(fd, &buf) < 0)
+ {
+ int err = errno;
+ report_error(err, L"Unable to fstat shared memory object with path '%s'", path);
+ errored = true;
+ }
+ size = buf.st_size;
+ }
+
+ /* Set the size, if it's too small */
+ if (! errored && size < sizeof(universal_notifier_shmem_t))
+ {
+ if (ftruncate(fd, sizeof(universal_notifier_shmem_t)) < 0)
+ {
+ int err = errno;
+ report_error(err, L"Unable to truncate shared memory object with path '%s'", path);
+ errored = true;
+ }
+ }
+
+ /* Memory map the region */
+ if (! errored)
+ {
+ void *addr = mmap(NULL, sizeof(universal_notifier_shmem_t), PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
+ if (addr == MAP_FAILED)
+ {
+ int err = errno;
+ report_error(err, L"Unable to memory map shared memory object with path '%s'", path);
+ region = NULL;
+ }
+ else
+ {
+ region = static_cast<universal_notifier_shmem_t*>(addr);
+ }
+ }
+
+ /* Close the fd, even if the mapping succeeded */
+ if (fd >= 0)
+ {
+ close(fd);
+ }
+
+ /* Read the current seed */
+ this->poll();
+ }
+
+ public:
+
+ /* Our notification involves changing the value in our shared memory. In practice, all clients will be in separate processes, so it suffices to set the value to a pid. For testing purposes, however, it's useful to keep them in the same process, so we increment the value. This isn't "safe" in the sense that multiple simultaneous increments may result in one being lost, but it should always result in the value being changed, which is sufficient. */
+ void post_notification()
{
- debug(4, L"Wrote message '%s'", msg->body.c_str());
+ if (region != NULL)
+ {
+ /* Read off the seed */
+ uint32_t seed = ntohl(region->universal_variable_seed);
+
+ /* Increment it. Don't let it wrap to zero. */
+ do
+ {
+ seed++;
+ }
+ while (seed == 0);
+ last_seed = seed;
+
+ /* Write out our data */
+ region->magic = htonl(SHMEM_MAGIC_NUMBER);
+ region->version = htonl(SHMEM_VERSION_CURRENT);
+ region->universal_variable_seed = htonl(seed);
+ }
}
- else
+
+ universal_notifier_shmem_poller_t() : last_change_time(0), last_seed(0), region(NULL)
{
- debug(4, L"Failed to write message '%s'", msg->body.c_str());
+ open_shmem();
}
-
- if (res == -1)
+
+ ~universal_notifier_shmem_poller_t()
{
- switch (errno)
+ if (region != NULL)
{
- case EAGAIN:
- return 0;
-
- default:
- debug(2,
- L"Error while sending universal variable message to fd %d. Closing connection",
- fd);
- if (debug_level > 2)
- wperror(L"write");
-
- return -1;
+ // Behold: C++ in all its glory!
+ void *address = const_cast<void *>(static_cast<volatile void *>(region));
+ if (munmap(address, sizeof(universal_notifier_shmem_t)) < 0)
+ {
+ wperror(L"munmap");
+ }
}
}
- msg->count--;
-
- if (!msg->count)
+
+ bool poll()
+ {
+ bool result = false;
+ if (region != NULL)
+ {
+ uint32_t seed = ntohl(region->universal_variable_seed);
+ if (seed != last_seed)
+ {
+ result = true;
+ last_seed = seed;
+ last_change_time = get_time();
+ }
+ }
+ return result;
+ }
+
+ unsigned long usec_delay_between_polls() const
{
- delete msg;
+ // If it's been less than five seconds since the last change, we poll quickly
+ // Otherwise we poll more slowly
+ // Note that a poll is a very cheap shmem read. The bad part about making this high
+ // is the process scheduling/wakeups it produces
+ unsigned long usec_per_sec = 1000000;
+ if (get_time() - last_change_time < 5LL * usec_per_sec)
+ {
+ return usec_per_sec / 10; //10 times a second
+ }
+ else
+ {
+ return usec_per_sec / 3; //3 times a second
+ }
}
- return 1;
-}
+};
-void try_send_all(connection_t *c)
+/* A notifyd-based notifier. Very straightforward. */
+class universal_notifier_notifyd_t : public universal_notifier_t
{
- /* debug( 3,
- L"Send all updates to connection on fd %d",
- c->fd );*/
- while (!c->unsent.empty())
+ int notify_fd;
+ int token;
+ std::string name;
+
+ void setup_notifyd()
{
- switch (try_send(c->unsent.front(), c->fd))
+#if FISH_NOTIFYD_AVAILABLE
+ // per notify(3), the user.uid.%d style is only accessible to processes with that uid
+ char local_name[256];
+ snprintf(local_name, sizeof local_name, "user.uid.%d.%ls.uvars", getuid(), program_name ? program_name : L"fish");
+ name.assign(local_name);
+
+ uint32_t status = notify_register_file_descriptor(name.c_str(), &this->notify_fd, 0, &this->token);
+ if (status != NOTIFY_STATUS_OK)
{
- case 1:
- c->unsent.pop();
- break;
-
- case 0:
- debug(4,
- L"Socket full, send rest later");
- return;
-
- case -1:
- c->killme = 1;
- return;
+ fprintf(stderr, "Warning: notify_register_file_descriptor() failed with status %u. Universal variable notifications may not be received.", status);
}
+ if (this->notify_fd >= 0)
+ {
+ // Mark us for non-blocking reads, and CLO_EXEC
+ int flags = fcntl(this->notify_fd, F_GETFL, 0);
+ if (flags >= 0 && ! (flags & O_NONBLOCK))
+ {
+ fcntl(this->notify_fd, F_SETFL, flags | O_NONBLOCK);
+ }
+
+ set_cloexec(this->notify_fd);
+ // Serious hack: notify_fd is likely the read end of a pipe. The other end is owned by libnotify, which does not mark it as CLO_EXEC (it should!)
+ // The next fd is probably notify_fd + 1
+ // Do it ourselves. If the implementation changes and some other FD gets marked as CLO_EXEC, that's probably a good thing.
+ set_cloexec(this->notify_fd + 1);
+ }
+#endif
}
-}
+
+public:
+ universal_notifier_notifyd_t() : notify_fd(-1), token(-1 /* NOTIFY_TOKEN_INVALID */)
+ {
+ setup_notifyd();
+ }
+
+ ~universal_notifier_notifyd_t()
+ {
+ if (token != -1 /* NOTIFY_TOKEN_INVALID */)
+ {
+#if FISH_NOTIFYD_AVAILABLE
+ notify_cancel(token);
+#endif
+ }
+ }
+
+ int notification_fd()
+ {
+ return notify_fd;
+ }
+
+ bool notification_fd_became_readable(int fd)
+ {
+ /* notifyd notifications come in as 32 bit values. We don't care about the value. We set ourselves as non-blocking, so just read until we can't read any more. */
+ assert(fd == notify_fd);
+ bool read_something = false;
+ unsigned char buff[64];
+ ssize_t amt_read;
+ do
+ {
+ amt_read = read(notify_fd, buff, sizeof buff);
+ read_something = (read_something || amt_read > 0);
+ } while (amt_read == sizeof buff);
+ return read_something;
+ }
+
+ void post_notification()
+ {
+#if FISH_NOTIFYD_AVAILABLE
+ uint32_t status = notify_post(name.c_str());
+ if (status != NOTIFY_STATUS_OK)
+ {
+ fprintf(stderr, "Warning: notify_post() failed with status %u. Universal variable notifications may not be sent.", status);
+ }
+#endif
+ }
+};
-/* The universal variable format has some funny escaping requirements; here we try to be safe */
-static bool is_universal_safe_to_encode_directly(wchar_t c)
-{
- if (c < 32 || c > 128)
- return false;
+#define NAMED_PIPE_FLASH_DURATION_USEC (1000000 / 10)
+#define SUSTAINED_READABILITY_CLEANUP_DURATION_USEC (1000000 * 5)
- return iswalnum(c) || wcschr(L"/", c);
-}
+/* Named-pipe based notifier. All clients open the same named pipe for reading and writing. The pipe's readability status is a trigger to enter polling mode.
-/**
- Escape specified string
- */
-static wcstring full_escape(const wchar_t *in)
+ To post a notification, write some data to the pipe, wait a little while, and then read it back.
+
+ To receive a notification, watch for the pipe to become readable. When it does, enter a polling mode until the pipe is no longer readable. To guard against the possibility of a shell exiting when there is data remaining in the pipe, if the pipe is kept readable too long, clients will attempt to read data out of it (to render it no longer readable).
+*/
+class universal_notifier_named_pipe_t : public universal_notifier_t
{
- wcstring out;
- for (; *in; in++)
+ int pipe_fd;
+ long long readback_time_usec;
+ size_t readback_amount;
+
+ bool polling_due_to_readable_fd;
+ long long drain_if_still_readable_time_usec;
+
+ void make_pipe(const wchar_t *test_path)
{
- wchar_t c = *in;
- if (is_universal_safe_to_encode_directly(c))
+ wcstring vars_path = test_path ? wcstring(test_path) : default_named_pipe_path();
+ vars_path.append(L".notifier");
+ const std::string narrow_path = wcs2string(vars_path);
+
+ int fd = wopen_cloexec(vars_path, O_RDWR | O_NONBLOCK, 0600);
+ if (fd < 0 && errno == ENOENT)
{
- out.push_back(c);
+ /* File doesn't exist, try creating it */
+ if (mkfifo(narrow_path.c_str(), 0600) >= 0)
+ {
+ fd = wopen_cloexec(vars_path, O_RDWR | O_NONBLOCK, 0600);
+ }
}
- else if (c < 256)
+ if (fd < 0)
{
- append_format(out, L"\\x%.2x", c);
+ // Maybe open failed, maybe mkfifo failed
+ int err = errno;
+ report_error(err, L"Unable to make or open a FIFO for universal variables with path '%ls'", vars_path.c_str());
}
- else if (c < 65536)
+ else
{
- append_format(out, L"\\u%.4x", c);
+ pipe_fd = fd;
+ }
+ }
+
+ void drain_excessive_data()
+ {
+ // The pipe seems to have data on it, that won't go away
+ // Read a big chunk out of it.
+ // We don't read until it's exhausted, because if someone were to pipe say /dev/null, that would cause us to hang!
+ size_t read_amt = 64 * 1024;
+ void *buff = malloc(read_amt);
+ read_ignore(this->pipe_fd, buff, read_amt);
+ free(buff);
+ }
+
+ public:
+ universal_notifier_named_pipe_t(const wchar_t *test_path) : pipe_fd(-1), readback_time_usec(0), readback_amount(0), polling_due_to_readable_fd(false), drain_if_still_readable_time_usec(0)
+ {
+ make_pipe(test_path);
+ }
+
+ ~universal_notifier_named_pipe_t()
+ {
+ if (pipe_fd >= 0)
+ {
+ close(pipe_fd);
+ }
+ }
+
+ int notification_fd()
+ {
+ if (polling_due_to_readable_fd)
+ {
+ // We are in polling mode because we think our fd is readable
+ // This means that, if we return it to be select()'d on, we'll be called back immediately
+ // So don't return it
+ return -1;
}
else
{
- append_format(out, L"\\U%.8x", c);
+ // We are not in polling mode
+ // Return the fd so it can be watched
+ return pipe_fd;
}
}
- return out;
-}
-
-/* Sets the body of a message to the null-terminated list of null terminated const char *. */
-void set_body(message_t *msg, ...)
-{
- /* Start by counting the length of all the strings */
- size_t body_len = 0;
- const char *arg;
- va_list arg_list;
- va_start(arg_list, msg);
- while ((arg = va_arg(arg_list, const char *)) != NULL)
- body_len += strlen(arg);
- va_end(arg_list);
-
- /* Reserve that length in the string */
- msg->body.reserve(body_len + 1); //+1 for trailing NULL? Do I need that?
-
- /* Set the string contents */
- va_start(arg_list, msg);
- while ((arg = va_arg(arg_list, const char *)) != NULL)
- msg->body.append(arg);
- va_end(arg_list);
-}
-
-/* Returns an instance of message_t allocated via new */
-message_t *create_message(fish_message_type_t type,
- const wchar_t *key_in,
- const wchar_t *val_in)
-{
- char *key = NULL;
-
- // debug( 4, L"Crete message of type %d", type );
-
- if (key_in)
+
+ bool notification_fd_became_readable(int fd)
{
- if (wcsvarname(key_in))
+ // Our fd is readable. We deliberately do not read anything out of it: if we did, other sessions may miss the notification.
+ // Instead, we go into "polling mode:" we do not select() on our fd for a while, and sync periodically until the fd is no longer readable.
+ // However, if we are the one who posted the notification, we don't sync (until we clean up!)
+ bool should_sync = false;
+ if (readback_time_usec == 0)
{
- debug(0, L"Illegal variable name: '%ls'", key_in);
- return NULL;
+ polling_due_to_readable_fd = true;
+ drain_if_still_readable_time_usec = get_time() + SUSTAINED_READABILITY_CLEANUP_DURATION_USEC;
+ should_sync = true;
}
-
- key = wcs2utf(key_in);
- if (!key)
+ return should_sync;
+ }
+
+ void post_notification()
+ {
+ if (pipe_fd >= 0)
{
- debug(0,
- L"Could not convert %ls to narrow character string",
- key_in);
- return NULL;
+ // We need to write some data (any data) to the pipe, then wait for a while, then read it back.
+ // Nobody is expected to read it except us.
+ int pid_nbo = htonl(getpid());
+ ssize_t amt_written = write(this->pipe_fd, &pid_nbo, sizeof pid_nbo);
+ if (amt_written < 0)
+ {
+ if (errno == EWOULDBLOCK || errno == EAGAIN)
+ {
+ // Very unsual: the pipe is full!
+ drain_excessive_data();
+ }
+ }
+
+ // Now schedule a read for some time in the future
+ this->readback_time_usec = get_time() + NAMED_PIPE_FLASH_DURATION_USEC;
+ this->readback_amount += sizeof pid_nbo;
}
}
-
- message_t *msg = new message_t;
- msg->count = 0;
-
- switch (type)
+
+ unsigned long usec_delay_between_polls() const
{
- case SET:
- case SET_EXPORT:
+ unsigned long readback_delay = ULONG_MAX;
+ if (this->readback_time_usec > 0)
{
- if (!val_in)
+ // How long until the readback?
+ long long now = get_time();
+ if (now >= this->readback_time_usec)
{
- val_in=L"";
+ // Oops, it already passed! Return something tiny.
+ readback_delay = 1000;
+ }
+ else
+ {
+ readback_delay = (unsigned long)(this->readback_time_usec - now);
}
-
- wcstring esc = full_escape(val_in);
- char *val = wcs2utf(esc.c_str());
- set_body(msg, (type==SET?SET_MBS:SET_EXPORT_MBS), " ", key, ":", val, "\n", NULL);
- free(val);
- break;
}
-
- case ERASE:
+
+ unsigned long polling_delay = ULONG_MAX;
+ if (polling_due_to_readable_fd)
{
- set_body(msg, ERASE_MBS, " ", key, "\n", NULL);
- break;
+ // We're in polling mode
+ // Don't return a value less than our polling interval
+ polling_delay = NAMED_PIPE_FLASH_DURATION_USEC;
}
-
- case BARRIER:
+
+ // Now return the smaller of the two values
+ // If we get ULONG_MAX, it means there's no more need to poll; in that case return 0
+ unsigned long result = mini(readback_delay, polling_delay);
+ if (result == ULLONG_MAX)
{
- set_body(msg, BARRIER_MBS, "\n", NULL);
- break;
+ result = 0;
}
-
- case BARRIER_REPLY:
+ return result;
+ }
+
+ bool poll()
+ {
+ bool result = false;
+
+ // Check if we are past the readback time
+ if (this->readback_time_usec > 0 && get_time() >= this->readback_time_usec)
{
- set_body(msg, BARRIER_REPLY_MBS, "\n", NULL);
- break;
+ // Read back what we wrote. We do nothing with the value.
+ while (this->readback_amount > 0)
+ {
+ char buff[64];
+ size_t amt_to_read = mini(this->readback_amount, sizeof buff);
+ read_ignore(this->pipe_fd, buff, amt_to_read);
+ this->readback_amount -= amt_to_read;
+ }
+ assert(this->readback_amount == 0);
+ this->readback_time_usec = 0;
}
-
- default:
+
+ // Check to see if we are doing readability polling
+ if (polling_due_to_readable_fd && pipe_fd >= 0)
{
- debug(0, L"create_message: Unknown message type");
+ // We are polling, so we are definitely going to sync
+ result = true;
+
+ // See if this is still readable
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(this->pipe_fd, &fds);
+ struct timeval timeout = {};
+ select(this->pipe_fd + 1, &fds, NULL, NULL, &timeout);
+ if (! FD_ISSET(this->pipe_fd, &fds))
+ {
+ // No longer readable, no longer polling
+ polling_due_to_readable_fd = false;
+ drain_if_still_readable_time_usec = 0;
+ }
+ else
+ {
+ // Still readable. If it's been readable for a long time, there is probably lingering data on the pipe
+ if (get_time() >= drain_if_still_readable_time_usec)
+ {
+ drain_excessive_data();
+ }
+ }
}
+
+ return result;
}
+};
- free(key);
-
- // debug( 4, L"Message body is '%s'", msg->body );
-
- return msg;
-}
+class universal_notifier_null_t : public universal_notifier_t
+{
+ /* Does nothing! */
+};
-/**
- Put exported or unexported variables in a string list
-*/
-void env_universal_common_get_names(wcstring_list_t &lst,
- bool show_exported,
- bool show_unexported)
+static universal_notifier_t::notifier_strategy_t fetch_default_strategy_from_environment()
{
- env_var_table_t::const_iterator iter;
- for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
+ universal_notifier_t::notifier_strategy_t result = universal_notifier_t::strategy_default;
+
+ const struct
{
- const wcstring &key = iter->first;
- const var_uni_entry_t &e = iter->second;
- if ((e.exportv && show_exported) || (! e.exportv && show_unexported))
+ const char *name;
+ universal_notifier_t::notifier_strategy_t strat;
+ } options[] =
+ {
+ {"default", universal_notifier_t::strategy_default},
+ {"shmem", universal_notifier_t::strategy_shmem_polling},
+ {"pipe", universal_notifier_t::strategy_named_pipe},
+ {"notifyd", universal_notifier_t::strategy_notifyd}
+ };
+ const size_t opt_count = sizeof options / sizeof *options;
+
+ const char *var = getenv(UNIVERSAL_NOTIFIER_ENV_NAME);
+ if (var != NULL && var[0] != '\0')
+ {
+ size_t i;
+ for (i=0; i < opt_count; i++)
{
- lst.push_back(key);
+ if (! strcmp(var, options[i].name))
+ {
+ result = options[i].strat;
+ break;
+ }
+ }
+ if (i >= opt_count)
+ {
+ fprintf(stderr, "Warning: unrecognized value for %s: '%s'\n", UNIVERSAL_NOTIFIER_ENV_NAME, var);
+ fprintf(stderr, "Warning: valid values are ");
+ for (size_t j=0; j < opt_count; j++)
+ {
+ fprintf(stderr, "%s%s", j > 0 ? ", " : "", options[j].name);
+ }
+ fputc('\n', stderr);
}
}
-
+ return result;
}
-
-const wchar_t *env_universal_common_get(const wcstring &name)
+universal_notifier_t::notifier_strategy_t universal_notifier_t::resolve_default_strategy()
{
- env_var_table_t::const_iterator result = env_universal_var.find(name);
- if (result != env_universal_var.end())
+ static universal_notifier_t::notifier_strategy_t s_explicit_strategy = fetch_default_strategy_from_environment();
+ if (s_explicit_strategy != strategy_default)
{
- const var_uni_entry_t &e = result->second;
- return const_cast<wchar_t*>(e.val.c_str());
+ return s_explicit_strategy;
}
+#if FISH_NOTIFYD_AVAILABLE
+ return strategy_notifyd;
+#else
+ return strategy_named_pipe;
+#endif
+}
- return NULL;
+universal_notifier_t &universal_notifier_t::default_notifier()
+{
+ static universal_notifier_t *result = new_notifier_for_strategy(strategy_default);
+ return *result;
}
-bool env_universal_common_get_export(const wcstring &name)
+universal_notifier_t *universal_notifier_t::new_notifier_for_strategy(universal_notifier_t::notifier_strategy_t strat, const wchar_t *test_path)
{
- env_var_table_t::const_iterator result = env_universal_var.find(name);
- if (result != env_universal_var.end())
+ if (strat == strategy_default)
{
- const var_uni_entry_t &e = result->second;
- return e.exportv;
+ strat = resolve_default_strategy();
+ }
+ switch (strat)
+ {
+ case strategy_shmem_polling:
+ return new universal_notifier_shmem_poller_t();
+
+ case strategy_notifyd:
+ return new universal_notifier_notifyd_t();
+
+ case strategy_named_pipe:
+ return new universal_notifier_named_pipe_t(test_path);
+
+ case strategy_null:
+ return new universal_notifier_null_t();
+
+ default:
+ fprintf(stderr, "Unsupported strategy %d\n", strat);
+ return NULL;
}
- return false;
}
-void enqueue_all(connection_t *c)
+/* Default implementations. */
+universal_notifier_t::universal_notifier_t()
{
- env_var_table_t::const_iterator iter;
- for (iter = env_universal_var.begin(); iter != env_universal_var.end(); ++iter)
- {
- const wcstring &key = iter->first;
- const var_uni_entry_t &entry = iter->second;
+}
- message_t *msg = create_message(entry.exportv ? SET_EXPORT : SET, key.c_str(), entry.val.c_str());
- msg->count=1;
- c->unsent.push(msg);
- }
+universal_notifier_t::~universal_notifier_t()
+{
+}
- try_send_all(c);
+int universal_notifier_t::notification_fd()
+{
+ return -1;
}
-connection_t::connection_t(int input_fd) :
- fd(input_fd),
- killme(false),
- buffer_consumed(0)
+void universal_notifier_t::post_notification()
{
}
-void connection_destroy(connection_t *c)
+bool universal_notifier_t::poll()
{
- /*
- A connection need not always be open - we only try to close it
- if it is open.
- */
- if (c->fd >= 0)
- {
- if (close(c->fd))
- {
- wperror(L"close");
- }
- }
+ return false;
}
+
+unsigned long universal_notifier_t::usec_delay_between_polls() const
+{
+ return 0;
+}
+
+bool universal_notifier_t::notification_fd_became_readable(int fd)
+{
+ return false;
+}
+
+static bool bool_from_env_var(const char *name, bool default_value)
+{
+ const char *var = getenv(name);
+ return var ? from_string<bool>(var) : default_value;
+}
+
+bool universal_log_enabled()
+{
+ return bool_from_env_var(UNIVERSAL_LOGGING_ENV_NAME, false);
+}
+