aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar ridiculousfish <corydoras@ridiculousfish.com>2014-05-21 11:02:17 -0700
committerGravatar ridiculousfish <corydoras@ridiculousfish.com>2014-05-21 11:02:17 -0700
commit451c97f35a4083413370bd2272adc2529e1e12eb (patch)
tree9301218c1f25387c69221c4983fae2ca77ccdd56
parent3cbace98a76ba35af88f0a9234a7c6ebad10d68c (diff)
parent8a577a9d4d71dd5b37a0c643ac8f44713b95f000 (diff)
Merges changes to support running fish without fishd.
In the new mode (not yet enabled), universal variables are set by reading and writing the fishd file directly, with some file locking for synchronization. This enables forwards and backwards compatibility. However there is no compatibility with simultaneous edits. Changes may be lost if fishd and the new mechanisms both attempt writes. fishd is still enabled by default for now; it will be disabled in a future commit. You can opt into the new mechanism (disabling fishd) by setting the environment variable fish_use_fishd to 0 before starting fish. This cannot itself be a universal variable, because of bootstrapping: the value is needed to determine how we read universal variables in the first place. Universal variable change notifications (i.e. reacting immediately to live edits) are tricky. Checking for changes is simple and relatively inexpensive (just a stat()), but relying solely on that would require frequent wakeups, and show up in fs_usage. So how do we get change notifications into an fd that we can monitor via select()? We support a few strategies, expressed as universal_notifier_t::notifier_strategy_t. By default we use notifyd on OS X and a named pipe on Linux / everywhere else. This is also configurable at runtime via the fish_universal_notifier variable.
-rw-r--r--common.cpp5
-rw-r--r--common.h20
-rw-r--r--configure.ac3
-rw-r--r--env_universal.cpp56
-rw-r--r--env_universal_common.cpp1738
-rw-r--r--env_universal_common.h135
-rw-r--r--fish_tests.cpp316
-rw-r--r--fishd.cpp117
-rw-r--r--history.cpp20
-rw-r--r--input_common.cpp43
-rw-r--r--iothread.h7
-rw-r--r--wildcard.cpp2
-rw-r--r--wutil.cpp114
-rw-r--r--wutil.h28
14 files changed, 2262 insertions, 342 deletions
diff --git a/common.cpp b/common.cpp
index 9c0db257..a9c57949 100644
--- a/common.cpp
+++ b/common.cpp
@@ -2225,6 +2225,11 @@ scoped_lock::scoped_lock(pthread_mutex_t &mutex) : lock_obj(&mutex), locked(fals
this->lock();
}
+scoped_lock::scoped_lock(lock_t &lock) : lock_obj(&lock.mutex), locked(false)
+{
+ this->lock();
+}
+
scoped_lock::~scoped_lock()
{
if (locked) this->unlock();
diff --git a/common.h b/common.h
index b22720b0..18821509 100644
--- a/common.h
+++ b/common.h
@@ -537,6 +537,22 @@ public:
bool is_forked_child();
+
+class lock_t
+{
+ public:
+ pthread_mutex_t mutex;
+ lock_t()
+ {
+ pthread_mutex_init(&mutex, NULL);
+ }
+
+ ~lock_t()
+ {
+ pthread_mutex_destroy(&mutex);
+ }
+};
+
/* Basic scoped lock class */
class scoped_lock
{
@@ -551,6 +567,7 @@ public:
void lock(void);
void unlock(void);
scoped_lock(pthread_mutex_t &mutex);
+ scoped_lock(lock_t &lock);
~scoped_lock();
};
@@ -849,6 +866,9 @@ void assert_is_not_forked_child(const char *who);
#define ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(x) assert_is_not_forked_child(x)
#define ASSERT_IS_NOT_FORKED_CHILD() ASSERT_IS_NOT_FORKED_CHILD_TRAMPOLINE(__FUNCTION__)
+/** Macro to help suppress potentially unused variable warnings */
+#define USE(var) (void)(var)
+
extern "C" {
__attribute__((noinline)) void debug_thread_error(void);
}
diff --git a/configure.ac b/configure.ac
index 40693d6e..fefb2c46 100644
--- a/configure.ac
+++ b/configure.ac
@@ -376,6 +376,7 @@ AC_DEFINE(
# Check for os dependant libraries for all binaries.
AC_SEARCH_LIBS( connect, socket, , [AC_MSG_ERROR([Cannot find the socket library, needed to build this package.] )] )
AC_SEARCH_LIBS( nanosleep, rt, , [AC_MSG_ERROR([Cannot find the rt library, needed to build this package.] )] )
+AC_SEARCH_LIBS( shm_open, rt, , [AC_MSG_ERROR([Cannot find the rt library, needed to build this package.] )] )
AC_SEARCH_LIBS( pthread_create, pthread, , [AC_MSG_ERROR([Cannot find the pthread library, needed to build this package.] )] )
AC_SEARCH_LIBS( setupterm, [ncurses tinfo curses], , [AC_MSG_ERROR([Could not find a curses implementation, needed to build fish. If this is Linux, try running 'sudo apt-get install libncurses5-dev' or 'sudo yum install ncurses-devel'])] )
AC_SEARCH_LIBS( [nan], [m], [AC_DEFINE( [HAVE_NAN], [1], [Define to 1 if you have the nan function])] )
@@ -535,6 +536,7 @@ AC_CHECK_FUNCS( wcsdup wcsndup wcslen wcscasecmp wcsncasecmp fwprintf )
AC_CHECK_FUNCS( futimes wcwidth wcswidth wcstok fputwc fgetwc )
AC_CHECK_FUNCS( wcstol wcslcat wcslcpy lrand48_r killpg mkostemp )
AC_CHECK_FUNCS( backtrace backtrace_symbols sysconf getifaddrs getpeerucred getpeereid )
+AC_CHECK_FUNCS( inotify_init inotify_init1 )
if test x$local_gettext != xno; then
AC_CHECK_FUNCS( gettext dcgettext )
@@ -764,6 +766,7 @@ case $target_os in
;;
esac
+
# Tell the world what we know
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
diff --git a/env_universal.cpp b/env_universal.cpp
index 9c6165fe..fd04c478 100644
--- a/env_universal.cpp
+++ b/env_universal.cpp
@@ -248,11 +248,8 @@ static void check_connection(void)
static void env_universal_remove_all()
{
size_t i;
-
wcstring_list_t lst;
- env_universal_common_get_names(lst,
- 1,
- 1);
+ env_universal_common_get_names(lst, true, true);
for (i=0; i<lst.size(); i++)
{
const wcstring &key = lst.at(i);
@@ -269,6 +266,8 @@ static void env_universal_remove_all()
*/
static void reconnect()
{
+ assert(synchronizes_via_fishd());
+
if (get_socket_count >= RECONNECT_COUNT)
return;
@@ -286,24 +285,38 @@ static void reconnect()
}
}
+void env_universal_read_from_file()
+{
+
+}
void env_universal_init(wchar_t * p,
wchar_t *u,
void (*sf)(),
void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val))
{
- path=p;
- user=u;
- start_fishd=sf;
- external_callback = cb;
-
- 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)
+ if (! synchronizes_via_fishd())
{
- env_universal_barrier();
+ external_callback = cb;
+ env_universal_common_init(&callback);
+ env_universal_read_from_file();
+ s_env_univeral_inited = true;
+ }
+ else
+ {
+ path=p;
+ user=u;
+ start_fishd=sf;
+ external_callback = cb;
+
+ 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();
+ }
}
}
@@ -374,8 +387,15 @@ bool env_universal_get_export(const wcstring &name)
void env_universal_barrier()
{
ASSERT_IS_MAIN_THREAD();
+ UNIVERSAL_LOG("BARRIER");
message_t *msg;
fd_set fds;
+
+ if (! synchronizes_via_fishd())
+ {
+ env_universal_common_sync();
+ return;
+ }
if (!s_env_univeral_inited || is_dead())
return;
@@ -441,9 +461,10 @@ void env_universal_set(const wcstring &name, const wcstring &value, bool exportv
debug(3, L"env_universal_set( \"%ls\", \"%ls\" )", name.c_str(), value.c_str());
- if (is_dead())
+ if (! synchronizes_via_fishd() || is_dead())
{
env_universal_common_set(name.c_str(), value.c_str(), exportv);
+ env_universal_barrier();
}
else
{
@@ -478,7 +499,7 @@ int env_universal_remove(const wchar_t *name)
L"env_universal_remove( \"%ls\" )",
name);
- if (is_dead())
+ if (! synchronizes_via_fishd() || is_dead())
{
env_universal_common_remove(name_str);
}
@@ -504,3 +525,4 @@ void env_universal_get_names(wcstring_list_t &lst,
show_exported,
show_unexported);
}
+
diff --git a/env_universal_common.cpp b/env_universal_common.cpp
index eb0e15ae..cb45e29d 100644
--- a/env_universal_common.cpp
+++ b/env_universal_common.cpp
@@ -16,6 +16,7 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
+#include <arpa/inet.h>
#include <pwd.h>
#include <errno.h>
#include <sys/stat.h>
@@ -26,7 +27,11 @@
#include <locale.h>
#include <dirent.h>
#include <signal.h>
+#include <fcntl.h>
#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/file.h>
+#include <sys/mman.h>
#include <map>
#ifdef HAVE_SYS_SELECT_H
@@ -40,6 +45,18 @@
#include "wutil.h"
#include "utf8.h"
#include "env_universal_common.h"
+#include "path.h"
+#include "iothread.h"
+
+#if __APPLE__
+#define FISH_NOTIFYD_AVAILABLE 1
+#include <notify.h>
+#endif
+
+#if HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1
+#define FISH_INOTIFY_AVAILABLE 1
+#include <sys/inotify.h>
+#endif
/**
Non-wide version of the set command
@@ -86,21 +103,64 @@
*/
#define ENV_UNIVERSAL_EOF 0x102
-static void parse_message(wchar_t *msg,
- connection_t *src);
+/** 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"
+
+static wcstring fishd_get_config();
+static std::string get_variables_file_path(const std::string &dir, const std::string &identifier);
+
+static wcstring default_vars_path()
+{
+ wcstring wdir = fishd_get_config();
+ const std::string dir = wcs2string(wdir);
+ if (dir.empty())
+ return L"";
+
+ const std::string machine_id = get_machine_identifier();
+ const std::string machine_id_path = get_variables_file_path(dir, machine_id);
+ return str2wcstring(machine_id_path);
+}
/**
The table of all universal variables
*/
-static env_universal_t s_env_universal_var;
+static env_universal_t &default_universal_vars()
+{
+ static env_universal_t s_default_vars(L"");
+ return s_default_vars;
+}
/**
Callback function, should be called on all events
*/
+struct callback_data_t
+{
+ fish_message_type_t type;
+ wcstring key;
+ wcstring val;
+
+ callback_data_t(fish_message_type_t t, const wcstring &k, const wcstring &v) : type(t), key(k), val(v)
+ {
+ }
+};
+
static void (*callback)(fish_message_type_t type,
const wchar_t *key,
const wchar_t *val);
+/* Post callbacks that we have determined in this list. We do this here, instead of at the point where we determined that the values changed, because we determine those under a lock, and reentrancy would cause a deadlock */
+static void post_callbacks(const callback_data_list_t &callbacks)
+{
+ if (callback != NULL)
+ {
+ for (size_t i=0; i < callbacks.size(); i++)
+ {
+ const callback_data_t &data = callbacks.at(i);
+ callback(data.type, data.key.c_str(), data.val.c_str());
+ }
+ }
+}
+
/* UTF <-> wchar conversions. These return a string allocated with malloc. These call sites could be cleaned up substantially to eliminate the dependence on malloc. */
static wchar_t *utf2wcs(const char *input)
{
@@ -129,6 +189,13 @@ void env_universal_common_init(void (*cb)(fish_message_type_t type, const wchar_
callback = cb;
}
+void read_message(connection_t *conn)
+{
+ callback_data_list_t callbacks;
+ default_universal_vars().read_message(conn, &callbacks);
+ post_callbacks(callbacks);
+}
+
/**
Read one byte of date form the specified connection
*/
@@ -171,90 +238,12 @@ static int read_byte(connection_t *src)
}
-
-void read_message(connection_t *src)
-{
- while (1)
- {
-
- int ib = read_byte(src);
- char b;
-
- switch (ib)
- {
- case ENV_UNIVERSAL_AGAIN:
- {
- return;
- }
-
- case ENV_UNIVERSAL_ERROR:
- {
- debug(2, L"Read error on fd %d, set killme flag", src->fd);
- if (debug_level > 2)
- wperror(L"read");
- src->killme = 1;
- return;
- }
-
- case ENV_UNIVERSAL_EOF:
- {
- 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;
- }
- }
-
- b = (char)ib;
-
- if (b == '\n')
- {
- 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)
- {
- parse_message(msg, src);
- }
- else
- {
- debug(0, _(L"Could not convert message '%s' to wide character string"), &src->input.at(0));
- }
-
- free(msg);
-
- }
- else
- {
- src->input.push_back(b);
- }
- }
-}
-
/**
Remove variable with specified name
*/
void env_universal_common_remove(const wcstring &name)
{
- s_env_universal_var.remove(name);
+ default_universal_vars().remove(name);
}
/**
@@ -277,7 +266,7 @@ void env_universal_common_set(const wchar_t *key, const wchar_t *val, bool expor
CHECK(key,);
CHECK(val,);
- s_env_universal_var.set(key, val, exportv);
+ default_universal_vars().set(key, val, exportv);
if (callback)
{
@@ -285,87 +274,30 @@ void env_universal_common_set(const wchar_t *key, const wchar_t *val, bool expor
}
}
-
-/**
- Parse message msg
-*/
-static void parse_message(wchar_t *msg,
- connection_t *src)
+void env_universal_common_sync()
{
-// debug( 3, L"parse_message( %ls );", msg );
-
- if (msg[0] == L'#')
- return;
-
- if (match(msg, SET_STR) || match(msg, SET_EXPORT_STR))
+ assert(! synchronizes_via_fishd());
+ callback_data_list_t callbacks;
+ bool changed = default_universal_vars().sync(&callbacks);
+ if (changed)
{
- wchar_t *name, *tmp;
- bool exportv = match(msg, SET_EXPORT_STR);
-
- name = msg+(exportv?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
- while (wcschr(L"\t ", *name))
- name++;
-
- tmp = wcschr(name, L':');
- if (tmp)
- {
- const wcstring key(name, tmp - name);
-
- wcstring val;
- if (unescape_string(tmp + 1, &val, 0))
- {
- env_universal_common_set(key.c_str(), val.c_str(), exportv);
- }
- }
- else
- {
- debug(1, PARSE_ERR, msg);
- }
+ universal_notifier_t::default_notifier().post_notification();
}
- else if (match(msg, ERASE_STR))
- {
- wchar_t *name, *tmp;
-
- name = msg+wcslen(ERASE_STR);
- while (wcschr(L"\t ", *name))
- name++;
-
- tmp = name;
- while (iswalnum(*tmp) || *tmp == L'_')
- tmp++;
-
- *tmp = 0;
-
- if (!wcslen(name))
- {
- debug(1, PARSE_ERR, msg);
- }
-
- env_universal_common_remove(name);
+ post_callbacks(callbacks);
+}
- if (callback)
- {
- callback(ERASE, name, 0);
- }
- }
- else if (match(msg, BARRIER_STR))
- {
- message_t *msg = create_message(BARRIER_REPLY, 0, 0);
- msg->count = 1;
- src->unsent.push(msg);
- try_send_all(src);
- }
- else if (match(msg, BARRIER_REPLY_STR))
- {
- if (callback)
- {
- callback(BARRIER_REPLY, 0, 0);
- }
- }
- else
+static void report_error(int err_code, const wchar_t *err_format, ...)
+{
+ va_list va;
+ va_start(va, err_format);
+ const wcstring err_text = vformat_string(err_format, va);
+ va_end(va);
+
+ if (! err_text.empty())
{
- debug(1, PARSE_ERR, msg);
+ fwprintf(stderr, L"%ls: ", err_text.c_str());
}
+ fwprintf(stderr, L"%s\n", strerror(err_code));
}
/**
@@ -448,7 +380,7 @@ static bool is_universal_safe_to_encode_directly(wchar_t c)
if (c < 32 || c > 128)
return false;
- return iswalnum(c) || wcschr(L"/", c);
+ return iswalnum(c) || wcschr(L"/_", c);
}
/**
@@ -586,25 +518,24 @@ message_t *create_message(fish_message_type_t type,
*/
void env_universal_common_get_names(wcstring_list_t &lst, bool show_exported, bool show_unexported)
{
- wcstring_list_t names = s_env_universal_var.get_names(show_exported, show_unexported);
+ const wcstring_list_t names = default_universal_vars().get_names(show_exported, show_unexported);
lst.insert(lst.end(), names.begin(), names.end());
}
env_var_t env_universal_common_get(const wcstring &name)
{
- return s_env_universal_var.get(name);
+ return default_universal_vars().get(name);
}
bool env_universal_common_get_export(const wcstring &name)
{
- return s_env_universal_var.get_export(name);
+ return default_universal_vars().get_export(name);
}
void enqueue_all(connection_t *c)
{
- s_env_universal_var.enqueue_all(c);
- try_send_all(c);
+ default_universal_vars().enqueue_all(c);
}
connection_t::connection_t(int input_fd) :
@@ -629,7 +560,7 @@ void connection_destroy(connection_t *c)
}
}
-env_universal_t::env_universal_t()
+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));
}
@@ -661,18 +592,55 @@ bool env_universal_t::get_export(const wcstring &name) const
return result;
}
+
+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())
+ {
+ /* 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)
+ {
+ this->modified.insert(key);
+ }
+ }
+}
+
void env_universal_t::set(const wcstring &key, const wcstring &val, bool exportv)
{
scoped_lock locker(lock);
- var_entry_t *entry = &vars[key];
- entry->val = val;
- entry->exportv = exportv;
+ this->set_internal(key, val, exportv, true /* overwrite */);
+}
+
+void env_universal_t::remove_internal(const wcstring &key, bool overwrite)
+{
+ ASSERT_IS_LOCKED(lock);
+ if (! overwrite && this->modified.find(key) != modified.end())
+ {
+ /* This value has been modified and we're not overwriting it. Skip it. */
+ return;
+ }
+ size_t erased = this->vars.erase(key);
+ if (erased > 0 && overwrite)
+ {
+ this->modified.insert(key);
+ }
}
void env_universal_t::remove(const wcstring &key)
{
scoped_lock locker(lock);
- vars.erase(key);
+ this->remove_internal(key, true);
}
wcstring_list_t env_universal_t::get_names(bool show_exported, bool show_unexported) const
@@ -692,9 +660,9 @@ wcstring_list_t env_universal_t::get_names(bool show_exported, bool show_unexpor
return result;
}
-void env_universal_t::enqueue_all(connection_t *c) const
+void env_universal_t::enqueue_all_internal(connection_t *c) const
{
- scoped_lock locker(lock);
+ ASSERT_IS_LOCKED(lock);
var_table_t::const_iterator iter;
for (iter = vars.begin(); iter != vars.end(); ++iter)
{
@@ -705,4 +673,1432 @@ void env_universal_t::enqueue_all(connection_t *c) const
msg->count=1;
c->unsent.push(msg);
}
+ try_send_all(c);
+}
+
+void env_universal_t::enqueue_all(connection_t *c) const
+{
+ scoped_lock locker(lock);
+ enqueue_all_internal(c);
+}
+
+void env_universal_t::erase_unmodified_values()
+{
+ /* Delete all non-modified keys. */
+ var_table_t::iterator iter = vars.begin();
+ while (iter != vars.end())
+ {
+ const wcstring &key = iter->first;
+ if (modified.find(key) == modified.end())
+ {
+ // Unmodified key. Erase the old value.
+ vars.erase(iter++);
+ }
+ else
+ {
+ // Modified key, retain the value.
+ ++iter;
+ }
+ }
+}
+
+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
+ {
+ /* Unmodified values are sourced from the file. Since we are about to read a different file, erase them */
+ this->erase_unmodified_values();
+ connection_t c(fd);
+ /* Read from the file. Do not destroy the connection; the caller is responsible for closing the fd. */
+ this->read_message_internal(&c, callbacks);
+ last_read_file = current_file;
+ }
+}
+
+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)
+ {
+ UNIVERSAL_LOG("Sync elided based on fast stat()");
+ return true;
+ }
+
+ /* OK to not use CLO_EXEC here because fishd is single threaded */
+ bool result = false;
+ int fd = wopen_cloexec(path, O_RDONLY);
+ if (fd >= 0)
+ {
+ UNIVERSAL_LOG("Reading from file");
+ this->load_from_fd(fd, callbacks);
+ close(fd);
+ result = true;
+ }
+ return result;
+}
+
+void env_universal_t::write_to_fd(int fd)
+{
+ ASSERT_IS_LOCKED(lock);
+ assert(fd >= 0);
+ connection_t conn(fd);
+ write_loop(fd, SAVE_MSG, strlen(SAVE_MSG));
+ this->enqueue_all_internal(&conn);
+
+ /* 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);
+
+ /* Do not destroy the connection; we don't close the file */
+}
+
+bool env_universal_t::move_new_vars_file_into_place(const wcstring &src, const wcstring &dst)
+{
+ int ret = wrename(src, dst);
+ if (ret != 0)
+ {
+ int err = errno;
+ report_error(err, L"Unable to rename file from '%ls' to '%ls'", src.c_str(), dst.c_str());
+ }
+ return ret == 0;
+}
+
+/**
+ Get environment variable value.
+ */
+static env_var_t fishd_env_get(const char *key)
+{
+ const char *env = getenv(key);
+ if (env != NULL)
+ {
+ return env_var_t(str2wcstring(env));
+ }
+ else
+ {
+ const wcstring wkey = str2wcstring(key);
+ return env_universal_common_get(wkey);
+ }
+}
+
+static wcstring fishd_get_config()
+{
+ bool done = false;
+ wcstring result;
+
+ env_var_t xdg_dir = fishd_env_get("XDG_CONFIG_HOME");
+ if (! xdg_dir.missing_or_empty())
+ {
+ result = xdg_dir;
+ append_path_component(result, L"/fish");
+ if (!create_directory(result))
+ {
+ done = true;
+ }
+ }
+ else
+ {
+ env_var_t home = fishd_env_get("HOME");
+ if (! home.missing_or_empty())
+ {
+ result = home;
+ append_path_component(result, L"/.config/fish");
+ if (!create_directory(result))
+ {
+ 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;
+}
+
+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;
+ std::string hostname_id;
+ if (get_hostname_identifier(&hostname_id))
+ {
+ const wcstring hostname_path = wdirname(vars_path) + L'/' + str2wcstring(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_WRONLY | O_CREAT | O_EXCL | O_TRUNC | O_CLOEXEC);
+ if (result_fd >= 0)
+ {
+ tmp_name = str2wcstring(narrow_str);
+ }
+#else
+ if (mktemp(narrow_str))
+ {
+ /* 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);
+ }
+#endif
+
+ if (result_fd >= 0)
+ {
+ /* Success */
+ *out_fd = result_fd;
+ *out_path = str2wcstring(narrow_str);
+ success = true;
+ }
+ free(narrow_str);
+ }
+ if (! success)
+ {
+ int err = errno;
+ report_error(err, L"Unable to open file '%ls'", tmp_name.c_str());
+ }
+ return success;
+}
+
+bool env_universal_t::open_and_acquire_lock(const wcstring &path, int *out_fd)
+{
+ /* 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 fd = wopen_cloexec(path, flags, 0644);
+ if (fd < 0)
+ {
+ int err = errno;
+ if (err == EINTR)
+ {
+ /* Signal; try again */
+ continue;
+ }
+#ifdef O_EXLOCK
+ else if (err == EOPNOTSUPP)
+ {
+ /* 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;
+ }
+#endif
+ else
+ {
+ report_error(err, L"Unable to open universal variable file '%ls'", path.c_str());
+ break;
+ }
+ }
+
+ /* 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)
+ {
+ while (flock(fd, LOCK_EX) < 0)
+ {
+ /* error */
+ if (errno != EINTR)
+ {
+ int err = errno;
+ report_error(err, L"Unable to lock universal variable file '%ls'", path.c_str());
+ break;
+ }
+ }
+ }
+
+ /* 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))
+ {
+ /* Oops, it changed! Try again */
+ close(fd);
+ continue;
+ }
+
+
+ /* 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;
+}
+
+/* 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)
+{
+ 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;
+ }
+
+#if 0
+ for (std::set<wcstring>::iterator iter = modified.begin(); iter != modified.end(); ++iter)
+ {
+ fprintf(stderr, "Modified %ls\n", iter->c_str());
+ }
+#endif
+
+ 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);
+ }
+
+ /* Read from it */
+ if (success)
+ {
+ assert(vars_fd >= 0);
+ this->load_from_fd(vars_fd, callbacks);
+ }
+
+ /* 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);
+ this->write_to_fd(private_fd);
+ }
+
+ 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_t::read_message_internal(connection_t *src, callback_data_list_t *callbacks)
+{
+ ASSERT_IS_LOCKED(lock);
+ while (1)
+ {
+
+ int ib = read_byte(src);
+ char b;
+
+ switch (ib)
+ {
+ case ENV_UNIVERSAL_AGAIN:
+ {
+ return;
+ }
+
+ case ENV_UNIVERSAL_ERROR:
+ {
+ debug(2, L"Read error on fd %d, set killme flag", src->fd);
+ if (debug_level > 2)
+ wperror(L"read");
+ src->killme = 1;
+ return;
+ }
+
+ case ENV_UNIVERSAL_EOF:
+ {
+ 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;
+ }
+ }
+
+ b = (char)ib;
+
+ if (b == '\n')
+ {
+ 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)
+ {
+ this->parse_message_internal(msg, src, callbacks);
+ }
+ else
+ {
+ debug(0, _(L"Could not convert message '%s' to wide character string"), &src->input.at(0));
+ }
+
+ free(msg);
+
+ }
+ else
+ {
+ src->input.push_back(b);
+ }
+ }
+}
+
+void env_universal_t::read_message(connection_t *src, callback_data_list_t *callbacks)
+{
+ scoped_lock locker(lock);
+ return read_message_internal(src, callbacks);
+}
+
+/**
+ Parse message msg
+ */
+void env_universal_t::parse_message_internal(wchar_t *msg, connection_t *src, callback_data_list_t *callbacks)
+{
+ ASSERT_IS_LOCKED(lock);
+
+ // debug( 3, L"parse_message( %ls );", msg );
+
+ if (msg[0] == L'#')
+ return;
+
+ if (match(msg, SET_STR) || match(msg, SET_EXPORT_STR))
+ {
+ wchar_t *name, *tmp;
+ bool exportv = match(msg, SET_EXPORT_STR);
+
+ name = msg+(exportv?wcslen(SET_EXPORT_STR):wcslen(SET_STR));
+ while (wcschr(L"\t ", *name))
+ name++;
+
+ tmp = wcschr(name, L':');
+ if (tmp)
+ {
+ const wcstring key(name, tmp - name);
+
+ wcstring val;
+ if (unescape_string(tmp + 1, &val, 0))
+ {
+ this->set_internal(key, val, exportv, false);
+ if (callbacks != NULL)
+ {
+ callbacks->push_back(callback_data_t(exportv ? SET_EXPORT:SET, key, val));
+ }
+ }
+ }
+ else
+ {
+ debug(1, PARSE_ERR, msg);
+ }
+ }
+ else if (match(msg, ERASE_STR))
+ {
+ wchar_t *name, *tmp;
+
+ name = msg+wcslen(ERASE_STR);
+ while (wcschr(L"\t ", *name))
+ name++;
+
+ tmp = name;
+ while (iswalnum(*tmp) || *tmp == L'_')
+ tmp++;
+
+ *tmp = 0;
+
+ if (!wcslen(name))
+ {
+ debug(1, PARSE_ERR, msg);
+ }
+
+ this->remove_internal(name, false);
+ if (callbacks != NULL)
+ {
+ callbacks->push_back(callback_data_t(ERASE, name, wcstring()));
+ }
+ }
+ else if (match(msg, BARRIER_STR))
+ {
+ message_t *msg = create_message(BARRIER_REPLY, 0, 0);
+ msg->count = 1;
+ src->unsent.push(msg);
+ try_send_all(src);
+ }
+ else if (match(msg, BARRIER_REPLY_STR))
+ {
+ if (callbacks != NULL)
+ {
+ callbacks->push_back(callback_data_t(BARRIER_REPLY, wcstring(), wcstring()));
+ }
+ }
+ else
+ {
+ debug(1, PARSE_ERR, msg);
+ }
+}
+
+static std::string get_variables_file_path(const std::string &dir, const std::string &identifier)
+{
+ std::string name;
+ name.append(dir);
+ name.append("/");
+ name.append("fishd.");
+ name.append(identifier);
+ return name;
+}
+
+/**
+ Maximum length of hostname. Longer hostnames are truncated
+ */
+#define HOSTNAME_LEN 32
+
+/* Length of a MAC address */
+#define MAC_ADDRESS_MAX_LEN 6
+
+
+/* Thanks to Jan Brittenson
+ http://lists.apple.com/archives/xcode-users/2009/May/msg00062.html
+ */
+#ifdef SIOCGIFHWADDR
+
+/* 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)
+ {
+ memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN);
+ result = true;
+ }
+ close(dummy);
+ }
+ return result;
+}
+
+#elif defined(HAVE_GETIFADDRS)
+
+/* 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)
+ {
+ 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);
+ }
+ 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 */
+bool get_hostname_identifier(std::string *result)
+{
+ bool success = false;
+ char hostname[HOSTNAME_LEN + 1] = {};
+ if (gethostname(hostname, HOSTNAME_LEN) == 0)
+ {
+ result->assign(hostname);
+ success = true;
+ }
+ 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. */
+std::string get_machine_identifier(void)
+{
+ std::string result;
+ unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {};
+ if (get_mac_address(mac_addr))
+ {
+ result.reserve(2 * MAC_ADDRESS_MAX_LEN);
+ for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++)
+ {
+ char buff[3];
+ snprintf(buff, sizeof buff, "%02x", mac_addr[i]);
+ result.append(buff);
+ }
+ }
+ else if (get_hostname_identifier(&result))
+ {
+ /* Hooray */
+ }
+ else
+ {
+ /* Fallback */
+ result.assign("nohost");
+ }
+ return result;
+}
+
+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;
+ }
+
+ /* 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()
+ {
+ 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);
+ }
+ }
+
+ universal_notifier_shmem_poller_t() : last_change_time(0), last_seed(0), region(NULL)
+ {
+ open_shmem();
+ }
+
+ ~universal_notifier_shmem_poller_t()
+ {
+ if (region != NULL)
+ {
+ // 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");
+ }
+ }
+ }
+
+ 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
+ {
+ // 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
+ }
+ }
+};
+
+/* A notifyd-based notifier. Very straightforward. */
+class universal_notifier_notifyd_t : public universal_notifier_t
+{
+ int notify_fd;
+ int token;
+ std::string name;
+
+ void setup_notifyd()
+ {
+#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)
+ {
+ 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, with CLO_EXEC
+ int flags = fcntl(this->notify_fd, F_GETFL, 0);
+ fcntl(this->notify_fd, F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC);
+ }
+#endif
+ }
+
+public:
+ universal_notifier_notifyd_t() : notify_fd(-1), token(0)
+ {
+ setup_notifyd();
+ }
+
+ ~universal_notifier_notifyd_t()
+ {
+ if (token != 0)
+ {
+#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
+ }
+};
+
+/* inotify-based notifier. This attempts to watch the uvars file directly. Since the file is never modified (only replaced), it has to watch for deletions. If the file is not present, this will probably break horribly. Needs love before it can be competitive. */
+class universal_notifier_inotify_t : public universal_notifier_t
+{
+ int watch_fd;
+ int watch_descriptor;
+ const std::string narrow_path;
+
+ void reestablish_watch()
+ {
+#if FISH_INOTIFY_AVAILABLE
+ if (this->watch_fd >= 0)
+ {
+ if (this->watch_descriptor >= 0)
+ {
+ inotify_rm_watch(this->watch_fd, this->watch_descriptor);
+ }
+ this->watch_descriptor = inotify_add_watch(this->watch_fd, narrow_path.c_str(), IN_MODIFY | IN_MOVE_SELF | IN_DELETE_SELF | IN_EXCL_UNLINK);
+ if (this->watch_descriptor < 0)
+ {
+ wperror(L"inotify_add_watch");
+ }
+ }
+#endif
+ }
+
+ void setup_inotify(const wchar_t *test_path)
+ {
+#if FISH_INOTIFY_AVAILABLE
+ // Construct the watchfd
+
+#if HAVE_INOTIFY_INIT1
+ this->watch_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
+#else
+ this->watch_fd = inotify_init();
+#endif
+
+ if (this->watch_fd < 0)
+ {
+ wperror(L"inotify_init");
+ }
+ else
+ {
+ int flags = fcntl(this->watch_fd, F_GETFL, 0);
+ fcntl(this->watch_fd, F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC);
+ }
+ reestablish_watch();
+#endif
+ }
+
+public:
+
+ universal_notifier_inotify_t(const wchar_t *test_path) : watch_fd(-1), watch_descriptor(-1), narrow_path(wcs2string(test_path ? test_path : default_vars_path()))
+ {
+ setup_inotify(test_path);
+ }
+
+ ~universal_notifier_inotify_t()
+ {
+ if (watch_fd >= 0)
+ {
+ close(watch_fd);
+ }
+ USE(watch_descriptor);
+ }
+
+ int notification_fd()
+ {
+ return watch_fd;
+ }
+
+ bool notification_fd_became_readable(int fd)
+ {
+ assert(fd == watch_fd);
+ bool result = false;
+
+#if FISH_INOTIFY_AVAILABLE
+ for (;;)
+ {
+ struct inotify_event evt = {};
+ ssize_t read_amt = read(watch_fd, &evt, sizeof evt);
+ if (read_amt >= (ssize_t)sizeof evt)
+ {
+ if (evt.mask & (IN_DELETE_SELF | IN_MOVE_SELF))
+ {
+ // When a file is deleted, the watch is lost. Recreate it.
+ reestablish_watch();
+ result = true;
+ }
+ if (evt.mask & IN_MODIFY)
+ {
+ result = true;
+ }
+ }
+ else
+ {
+ break;
+ }
+ }
+#endif
+ return result;
+ }
+};
+
+#define NAMED_PIPE_FLASH_DURATION_USEC (1000000 / 10)
+#define SUSTAINED_READABILITY_CLEANUP_DURATION_USEC (1000000 * 5)
+
+/* 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.
+
+ 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
+{
+ 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)
+ {
+ wcstring vars_path = test_path ? wcstring(test_path) : default_vars_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)
+ {
+ /* 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);
+ }
+ }
+ if (fd < 0)
+ {
+ // 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
+ {
+ 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
+ {
+ // We are not in polling mode
+ // Return the fd so it can be watched
+ return pipe_fd;
+ }
+ }
+
+ bool notification_fd_became_readable(int fd)
+ {
+ // 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)
+ {
+ polling_due_to_readable_fd = true;
+ drain_if_still_readable_time_usec = get_time() + SUSTAINED_READABILITY_CLEANUP_DURATION_USEC;
+ should_sync = true;
+ }
+ return should_sync;
+ }
+
+ void post_notification()
+ {
+ if (pipe_fd >= 0)
+ {
+ // 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;
+ }
+ }
+
+ unsigned long usec_delay_between_polls() const
+ {
+ unsigned long readback_delay = ULONG_MAX;
+ if (this->readback_time_usec > 0)
+ {
+ // How long until the readback?
+ long long now = get_time();
+ if (now >= this->readback_time_usec)
+ {
+ // Oops, it already passed! Return something tiny.
+ readback_delay = 1000;
+ }
+ else
+ {
+ readback_delay = (unsigned long)(this->readback_time_usec - now);
+ }
+ }
+
+ unsigned long polling_delay = ULONG_MAX;
+ if (polling_due_to_readable_fd)
+ {
+ // We're in polling mode
+ // Don't return a value less than our polling interval
+ polling_delay = NAMED_PIPE_FLASH_DURATION_USEC;
+ }
+
+ // 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)
+ {
+ result = 0;
+ }
+ 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)
+ {
+ // 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;
+ }
+
+ // Check to see if we are doing readability polling
+ if (polling_due_to_readable_fd && pipe_fd >= 0)
+ {
+ // 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;
+ }
+};
+
+class universal_notifier_null_t : public universal_notifier_t
+{
+ /* Does nothing! */
+};
+
+static universal_notifier_t::notifier_strategy_t fetch_default_strategy_from_environment()
+{
+ if (synchronizes_via_fishd())
+ {
+ return universal_notifier_t::strategy_null;
+ }
+
+ universal_notifier_t::notifier_strategy_t result = universal_notifier_t::strategy_default;
+
+ const struct
+ {
+ 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},
+ {"inotify", universal_notifier_t::strategy_inotify},
+ {"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++)
+ {
+ 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;
}
+
+universal_notifier_t::notifier_strategy_t universal_notifier_t::resolve_default_strategy()
+{
+ static universal_notifier_t::notifier_strategy_t s_explicit_strategy = fetch_default_strategy_from_environment();
+ if (s_explicit_strategy != strategy_default)
+ {
+ return s_explicit_strategy;
+ }
+#if FISH_NOTIFYD_AVAILABLE
+ return strategy_notifyd;
+#else
+ return strategy_named_pipe;
+#endif
+}
+
+universal_notifier_t &universal_notifier_t::default_notifier()
+{
+ static universal_notifier_t *result = new_notifier_for_strategy(strategy_default);
+ return *result;
+}
+
+universal_notifier_t *universal_notifier_t::new_notifier_for_strategy(universal_notifier_t::notifier_strategy_t strat, const wchar_t *test_path)
+{
+ if (strat == strategy_default)
+ {
+ 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_inotify:
+ return new universal_notifier_inotify_t(test_path);
+
+ 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;
+ }
+}
+
+/* Default implementations. */
+universal_notifier_t::universal_notifier_t()
+{
+}
+
+universal_notifier_t::~universal_notifier_t()
+{
+}
+
+int universal_notifier_t::notification_fd()
+{
+ return -1;
+}
+
+void universal_notifier_t::post_notification()
+{
+}
+
+bool universal_notifier_t::poll()
+{
+ 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;
+}
+
+static bool initialize_synchronizes_via_fishd()
+{
+ if (program_name && ! wcscmp(program_name, L"fishd"))
+ {
+ /* fishd always wants to use fishd */
+ return true;
+ }
+
+ return bool_from_env_var(UNIVERSAL_USE_FISHD, true);
+}
+
+bool synchronizes_via_fishd()
+{
+ /* Note that in general we can't change this once it's been set, so we only load it once */
+ static bool result = initialize_synchronizes_via_fishd();
+ return result;
+}
+
+bool universal_log_enabled()
+{
+ return bool_from_env_var(UNIVERSAL_LOGGING_ENV_NAME, false);
+}
+
diff --git a/env_universal_common.h b/env_universal_common.h
index 100cbfc4..c18f7e97 100644
--- a/env_universal_common.h
+++ b/env_universal_common.h
@@ -4,6 +4,7 @@
#include <wchar.h>
#include <queue>
#include <string>
+#include <set>
#include "util.h"
#include "env.h"
@@ -188,6 +189,9 @@ env_var_t env_universal_common_get(const wcstring &name);
*/
bool env_universal_common_get_export(const wcstring &name);
+/** Synchronizes all changse: writes everything out, reads stuff in */
+void env_universal_common_sync();
+
/**
Add messages about all existing variables to the specified connection
*/
@@ -200,13 +204,45 @@ void enqueue_all(connection_t *c);
*/
void connection_destroy(connection_t *c);
+typedef std::vector<struct callback_data_t> callback_data_list_t;
+
/** Class representing universal variables */
class env_universal_t
{
+ /* Current values */
var_table_t vars;
+
+ /* Keys that have been modified, and need to be written. A value here that is not present in vars indicates a deleted value. */
+ std::set<wcstring> modified;
+
+ /* Path that we save to. If empty, use the default */
+ const wcstring explicit_vars_path;
+
mutable pthread_mutex_t lock;
+ bool tried_renaming;
+ bool load_from_path(const wcstring &path, callback_data_list_t *callbacks);
+ void load_from_fd(int fd, callback_data_list_t *callbacks);
+ void erase_unmodified_values();
+
+ void parse_message_internal(wchar_t *msg, connection_t *src, callback_data_list_t *callbacks);
+
+ void set_internal(const wcstring &key, const wcstring &val, bool exportv, bool overwrite);
+ void remove_internal(const wcstring &name, bool overwrite);
+
+ /* Functions concerned with saving */
+ bool open_and_acquire_lock(const wcstring &path, int *out_fd);
+ bool open_temporary_file(const wcstring &directory, wcstring *out_path, int *out_fd);
+ void write_to_fd(int fd);
+ bool move_new_vars_file_into_place(const wcstring &src, const wcstring &dst);
+
+ /* File id from which we last read */
+ file_id_t last_read_file;
+
+ void read_message_internal(connection_t *src, callback_data_list_t *callbacks);
+ void enqueue_all_internal(connection_t *c) const;
+
public:
- env_universal_t();
+ env_universal_t(const wcstring &path);
~env_universal_t();
/* Get the value of the variable with the specified name */
@@ -226,6 +262,103 @@ public:
/* Writes variables to the connection */
void enqueue_all(connection_t *c) const;
+
+ /** Loads variables at the correct path */
+ bool load();
+
+ /** Reads and writes variables at the correct path. Returns true if modified variables were written. */
+ bool sync(callback_data_list_t *callbacks);
+
+ /* Internal use */
+ void read_message(connection_t *src, callback_data_list_t *callbacks);
+};
+
+/** The "universal notifier" is an object responsible for broadcasting and receiving universal variable change notifications. These notifications do not contain the change, but merely indicate that the uvar file has changed. It is up to the uvar subsystem to re-read the file.
+
+ We support a few notificatins strategies. Not all strategies are supported on all platforms.
+
+ Notifiers may request polling, and/or provide a file descriptor to be watched for readability in select().
+
+ To request polling, the notifier overrides usec_delay_between_polls() to return a positive value. That value will be used as the timeout in select(). When select returns, the loop invokes poll(). poll() should return true to indicate that the file may have changed.
+
+ To provide a file descriptor, the notifier overrides notification_fd() to return a non-negative fd. This will be added to the "read" file descriptor list in select(). If the fd is readable, notification_fd_became_readable() will be called; that function should be overridden to return true if the file may have changed.
+
+*/
+class universal_notifier_t
+{
+public:
+ enum notifier_strategy_t
+ {
+ // Default meta-strategy to use the 'best' notifier for the system
+ strategy_default,
+
+ // Use a value in shared memory. Simple, but requires polling and therefore semi-frequent wakeups.
+ strategy_shmem_polling,
+
+ // Strategy that uses a named pipe. Somewhat complex, but portable and doesn't require polling most of the time.
+ strategy_named_pipe,
+
+ // Strategy that attempts to detect file changes directly via inotify. Doesn't handle certain edge cases (like the file missing); not yet recommended. Linux only.
+ strategy_inotify,
+
+ // Strategy that uses notify(3). Simple and efficient, but OS X only.
+ strategy_notifyd,
+
+ // Null notifier, does nothing
+ strategy_null
+ };
+
+ protected:
+ universal_notifier_t();
+
+ private:
+ /* No copying */
+ universal_notifier_t &operator=(const universal_notifier_t &);
+ universal_notifier_t(const universal_notifier_t &x);
+ static notifier_strategy_t resolve_default_strategy();
+
+ public:
+
+ virtual ~universal_notifier_t();
+
+ /* Factory constructor. Free with delete */
+ static universal_notifier_t *new_notifier_for_strategy(notifier_strategy_t strat, const wchar_t *test_path = NULL);
+
+ /* Default instance. Other instances are possible for testing. */
+ static universal_notifier_t &default_notifier();
+
+ /* Does a fast poll(). Returns true if changed. */
+ virtual bool poll();
+
+ /* Triggers a notification */
+ virtual void post_notification();
+
+ /* Recommended delay between polls. A value of 0 means no polling required (so no timeout) */
+ virtual unsigned long usec_delay_between_polls() const;
+
+ /* Returns the fd from which to watch for events, or -1 if none */
+ virtual int notification_fd();
+
+ /* The notification_fd is readable; drain it. Returns true if a notification is considered to have been posted. */
+ virtual bool notification_fd_became_readable(int fd);
};
+std::string get_machine_identifier();
+bool get_hostname_identifier(std::string *result);
+
+/* Temporary */
+bool synchronizes_via_fishd();
+
+bool universal_log_enabled();
+#define UNIVERSAL_LOG(x) if (universal_log_enabled()) fprintf(stderr, "UNIVERSAL LOG: %s\n", x)
+
+/* Environment variable for requesting a particular universal notifier. See fetch_default_strategy_from_environment for names. */
+#define UNIVERSAL_NOTIFIER_ENV_NAME "fish_universal_notifier"
+
+/* Environment variable for enabling universal variable logging (to stderr) */
+#define UNIVERSAL_LOGGING_ENV_NAME "fish_universal_log"
+
+/* Environment variable for enabling fishd */
+#define UNIVERSAL_USE_FISHD "fish_use_fishd"
+
#endif
diff --git a/fish_tests.cpp b/fish_tests.cpp
index c7b2222b..4c53bf73 100644
--- a/fish_tests.cpp
+++ b/fish_tests.cpp
@@ -63,6 +63,13 @@
#include "pager.h"
#include "input.h"
#include "utf8.h"
+#include "env_universal_common.h"
+
+#if HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1
+#include <sys/inotify.h>
+#include <sys/utsname.h>
+#endif
+
static const char * const * s_arguments;
static int s_test_run_count = 0;
@@ -2162,6 +2169,313 @@ static void test_input()
}
}
+#define UVARS_PER_THREAD 8
+#define UVARS_TEST_PATH L"/tmp/fish_uvars_test/varsfile.txt"
+
+static int test_universal_helper(int *x)
+{
+ env_universal_t uvars(UVARS_TEST_PATH);
+ for (int j=0; j < UVARS_PER_THREAD; j++)
+ {
+ const wcstring key = format_string(L"key_%d_%d", *x, j);
+ const wcstring val = format_string(L"val_%d_%d", *x, j);
+ uvars.set(key, val, false);
+ bool synced = uvars.sync(NULL);
+ if (! synced)
+ {
+ err(L"Failed to sync universal variables");
+ }
+ fputc('.', stderr);
+ }
+
+ /* Last step is to delete the first key */
+ uvars.remove(format_string(L"key_%d_%d", *x, 0));
+ bool synced = uvars.sync(NULL);
+ if (! synced)
+ {
+ err(L"Failed to sync universal variables");
+ }
+ fputc('.', stderr);
+
+ return 0;
+}
+
+static void test_universal()
+{
+ say(L"Testing universal variables");
+ if (system("mkdir -p /tmp/fish_uvars_test/")) err(L"mkdir failed");
+
+ const int threads = 16;
+ for (int i=0; i < threads; i++)
+ {
+ iothread_perform(test_universal_helper, (void (*)(int *, int))NULL, new int(i));
+ }
+ iothread_drain_all();
+
+ env_universal_t uvars(UVARS_TEST_PATH);
+ bool loaded = uvars.load();
+ if (! loaded)
+ {
+ err(L"Failed to load universal variables");
+ }
+ for (int i=0; i < threads; i++)
+ {
+ for (int j=0; j < UVARS_PER_THREAD; j++)
+ {
+ const wcstring key = format_string(L"key_%d_%d", i, j);
+ env_var_t expected_val;
+ if (j == 0)
+ {
+ expected_val = env_var_t::missing_var();
+ }
+ else
+ {
+ expected_val = format_string(L"val_%d_%d", i, j);
+ }
+ const env_var_t var = uvars.get(key);
+ if (j == 0)
+ {
+ assert(expected_val.missing());
+ }
+ if (var != expected_val)
+ {
+ const wchar_t *missing_desc = L"<missing>";
+ err(L"Wrong value for key %ls: expected %ls, got %ls\n", key.c_str(), (expected_val.missing() ? missing_desc : expected_val.c_str()), (var.missing() ? missing_desc : var.c_str()));
+ }
+ }
+ }
+
+ if (system("rm -Rf /tmp/fish_uvars_test")) err(L"rrm failed");
+ putc('\n', stderr);
+}
+
+bool poll_notifier(universal_notifier_t *note)
+{
+ bool result = false;
+ if (note->usec_delay_between_polls() > 0)
+ {
+ result = note->poll();
+ }
+
+ int fd = note->notification_fd();
+ if (! result && fd >= 0)
+ {
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ struct timeval tv = {0, 0};
+ if (select(fd + 1, &fds, NULL, NULL, &tv) > 0 && FD_ISSET(fd, &fds))
+ {
+ result = note->notification_fd_became_readable(fd);
+ }
+ }
+ return result;
+}
+
+static void trigger_or_wait_for_notification(universal_notifier_t *notifier, universal_notifier_t::notifier_strategy_t strategy)
+{
+ switch (strategy)
+ {
+ case universal_notifier_t::strategy_default:
+ assert(0 && "strategy_default should be passed");
+ break;
+
+ case universal_notifier_t::strategy_shmem_polling:
+ // nothing required
+ break;
+
+ case universal_notifier_t::strategy_notifyd:
+ // notifyd requires a round trip to the notifyd server, which means we have to wait a little bit to receive it
+ // In practice, this seems to be enough
+ usleep(1000000 / 25);
+ break;
+
+ case universal_notifier_t::strategy_named_pipe:
+ case universal_notifier_t::strategy_null:
+ break;
+
+ case universal_notifier_t::strategy_inotify:
+ {
+ // Hacktastic. Replace the file, then wait
+ char cmd[512];
+ sprintf(cmd, "touch %ls ; mv %ls %ls", UVARS_TEST_PATH L".tmp", UVARS_TEST_PATH ".tmp", UVARS_TEST_PATH);
+ if (system(cmd)) err(L"Command failed: %s", cmd);
+ usleep(1000000 / 25);
+ break;
+ }
+ }
+}
+
+static void test_notifiers_with_strategy(universal_notifier_t::notifier_strategy_t strategy)
+{
+ assert(strategy != universal_notifier_t::strategy_default);
+ say(L"Testing universal notifiers with strategy %d", (int)strategy);
+ universal_notifier_t *notifiers[16];
+ size_t notifier_count = sizeof notifiers / sizeof *notifiers;
+
+ // Populate array of notifiers
+ for (size_t i=0; i < notifier_count; i++)
+ {
+ notifiers[i] = universal_notifier_t::new_notifier_for_strategy(strategy, UVARS_TEST_PATH);
+ }
+
+ // Nobody should poll yet
+ for (size_t i=0; i < notifier_count; i++)
+ {
+ if (poll_notifier(notifiers[i]))
+ {
+ err(L"Universal variable notifier polled true before any changes, with strategy %d", (int)strategy);
+ }
+ }
+
+ // Tweak each notifier. Verify that others see it.
+ for (size_t post_idx=0; post_idx < notifier_count; post_idx++)
+ {
+ notifiers[post_idx]->post_notification();
+
+ // Do special stuff to "trigger" a notification for testing
+ trigger_or_wait_for_notification(notifiers[post_idx], strategy);
+
+ for (size_t i=0; i < notifier_count; i++)
+ {
+ // We aren't concerned with the one who posted
+ // Poll from it (to drain it), and then skip it
+ if (i == post_idx)
+ {
+ poll_notifier(notifiers[i]);
+ continue;
+ }
+
+ if (! poll_notifier(notifiers[i]))
+ {
+ err(L"Universal variable notifier (%lu) %p polled failed to notice changes, with strategy %d", i, notifiers[i], (int)strategy);
+ }
+ }
+
+ // Named pipes have special cleanup requirements
+ if (strategy == universal_notifier_t::strategy_named_pipe)
+ {
+ usleep(1000000 / 10); //corresponds to NAMED_PIPE_FLASH_DURATION_USEC
+ // Have to clean up the posted one first, so that the others see the pipe become no longer readable
+ poll_notifier(notifiers[post_idx]);
+ for (size_t i=0; i < notifier_count; i++)
+ {
+ poll_notifier(notifiers[i]);
+ }
+ }
+ }
+
+ // Nobody should poll now
+ for (size_t i=0; i < notifier_count; i++)
+ {
+ if (poll_notifier(notifiers[i]))
+ {
+ err(L"Universal variable notifier polled true after all changes, with strategy %d", (int)strategy);
+ }
+ }
+
+ // Clean up
+ for (size_t i=0; i < notifier_count; i++)
+ {
+ delete notifiers[i];
+ }
+}
+
+#if HAVE_INOTIFY_INIT
+#define INOTIFY_TEST_PATH "/tmp/inotify_test.tmp"
+static bool test_basic_inotify_support()
+{
+ bool inotify_works = true;
+ int fd = inotify_init();
+ if (fd < 0)
+ {
+ err(L"inotify_init failed");
+ }
+
+ /* Mark fd as nonblocking */
+ int flags = fcntl(fd, F_GETFL, 0);
+ if (flags == -1)
+ {
+ err(L"fcntl GETFL failed");
+ }
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
+ {
+ err(L"fcntl SETFL failed");
+ }
+
+ if (system("touch " INOTIFY_TEST_PATH))
+ {
+ err(L"touch failed");
+ }
+
+ /* Add file to watch list */
+ int wd = inotify_add_watch(fd, INOTIFY_TEST_PATH, IN_DELETE | IN_DELETE_SELF);
+ if (wd < 0)
+ {
+ err(L"inotify_add_watch failed: %s", strerror(errno));
+ }
+
+ /* Delete file */
+ if (system("rm " INOTIFY_TEST_PATH))
+ {
+ err(L"rm failed");
+ }
+
+ /* Verify that file is deleted */
+ struct stat statbuf;
+ if (stat(INOTIFY_TEST_PATH, &statbuf) != -1 || errno != ENOENT)
+ {
+ err(L"File at path " INOTIFY_TEST_PATH " still exists after deleting it");
+ }
+
+ /* The fd should be readable now or very shortly */
+ struct timeval tv = {1, 0};
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ int count = select(fd + 1, &fds, NULL, NULL, &tv);
+ if (count == 0 || ! FD_ISSET(fd, &fds))
+ {
+ inotify_works = false;
+ err(L"inotify file descriptor not readable. Is inotify busted?");
+ struct utsname version = {};
+ uname(&version);
+ fprintf(stderr, "kernel version %s - %s\n", version.release, version.version);
+ }
+ else
+ {
+ fprintf(stderr, "inotify seems OK\n");
+ }
+
+ if (fd >= 0)
+ {
+ close(fd);
+ }
+
+ return inotify_works;
+}
+#endif
+
+static void test_universal_notifiers()
+{
+ if (system("mkdir -p /tmp/fish_uvars_test/ && touch /tmp/fish_uvars_test/varsfile.txt")) err(L"mkdir failed");
+ test_notifiers_with_strategy(universal_notifier_t::strategy_shmem_polling);
+ test_notifiers_with_strategy(universal_notifier_t::strategy_named_pipe);
+#if __APPLE__
+ test_notifiers_with_strategy(universal_notifier_t::strategy_notifyd);
+#endif
+
+ // inotify test disabled pending investigation into why this fails on travis-ci
+ // https://github.com/travis-ci/travis-ci/issues/2342
+#if 0
+#if HAVE_INOTIFY_INIT
+ test_basic_inotify_support();
+ test_notifiers_with_strategy(universal_notifier_t::strategy_inotify);
+#endif
+#endif
+ if (system("rm -Rf /tmp/fish_uvars_test/")) err(L"rm failed");
+}
+
class history_tests_t
{
public:
@@ -3214,6 +3528,8 @@ int main(int argc, char **argv)
if (should_test_function("colors")) test_colors();
if (should_test_function("complete")) test_complete();
if (should_test_function("input")) test_input();
+ if (should_test_function("universal")) test_universal();
+ if (should_test_function("universal_notifiers")) test_universal_notifiers();
if (should_test_function("completion_insertions")) test_completion_insertions();
if (should_test_function("autosuggestion_combining")) test_autosuggestion_combining();
if (should_test_function("autosuggest_suggest_special")) test_autosuggest_suggest_special();
diff --git a/fishd.cpp b/fishd.cpp
index 95926373..4bb80b50 100644
--- a/fishd.cpp
+++ b/fishd.cpp
@@ -101,9 +101,6 @@ time the original barrier request was sent have been received.
#define MSG_DONTWAIT 0
#endif
-/* Length of a MAC address */
-#define MAC_ADDRESS_MAX_LEN 6
-
/**
Small greeting to show that fishd is running
*/
@@ -270,119 +267,6 @@ static std::string gen_unique_nfs_filename(const std::string &filename)
return newname;
}
-
-/* Thanks to Jan Brittenson
- http://lists.apple.com/archives/xcode-users/2009/May/msg00062.html
-*/
-#ifdef SIOCGIFHWADDR
-
-/* 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)
- {
- memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN);
- result = true;
- }
- close(dummy);
- }
- return result;
-}
-
-#elif defined(HAVE_GETIFADDRS)
-
-/* 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)
- {
- 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);
- }
- 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(std::string *result)
-{
- bool success = false;
- char hostname[HOSTNAME_LEN + 1] = {};
- if (gethostname(hostname, HOSTNAME_LEN) == 0)
- {
- result->assign(hostname);
- success = true;
- }
- 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. */
-static std::string get_machine_identifier(void)
-{
- std::string result;
- unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {};
- if (get_mac_address(mac_addr))
- {
- result.reserve(2 * MAC_ADDRESS_MAX_LEN);
- for (size_t i=0; i < MAC_ADDRESS_MAX_LEN; i++)
- {
- char buff[3];
- snprintf(buff, sizeof buff, "%02x", mac_addr[i]);
- result.append(buff);
- }
- }
- else if (get_hostname_identifier(&result))
- {
- /* Hooray */
- }
- else
- {
- /* Fallback */
- result.assign("nohost");
- }
- return result;
-}
-
/**
The number of milliseconds to wait between polls when attempting to acquire
a lockfile
@@ -817,6 +701,7 @@ static std::string get_variables_file_path(const std::string &dir, const std::st
return name;
}
+
static bool load_or_save_variables(bool save)
{
const wcstring wdir = fishd_get_config();
diff --git a/history.cpp b/history.cpp
index 77414ff7..fdbc2359 100644
--- a/history.cpp
+++ b/history.cpp
@@ -149,8 +149,6 @@ public:
}
};
-static const file_id_t kInvalidFileID((dev_t)(-1), (ino_t)(-1));
-
/* Lock a file via fcntl; returns true on success, false on failure. */
static bool history_file_lock(int fd, short type)
{
@@ -162,20 +160,6 @@ static bool history_file_lock(int fd, short type)
return ret != -1;
}
-/* Get a file_id_t corresponding to the given fd */
-static file_id_t history_file_identify(int fd)
-{
- file_id_t result = kInvalidFileID;
- struct stat buf = {};
- if (0 == fstat(fd, &buf))
- {
- result.first = buf.st_dev;
- result.second = buf.st_ino;
- }
- return result;
-}
-
-
/* Our LRU cache is used for restricting the amount of history we have, and limiting how long we order it. */
class history_lru_node_t : public lru_node_t
{
@@ -1047,7 +1031,7 @@ bool history_t::map_file(const wcstring &name, const char **out_map_start, size_
/* Get the file ID if requested */
if (file_id != NULL)
- *file_id = history_file_identify(fd);
+ *file_id = file_id_for_fd(fd);
/* Take a read lock to guard against someone else appending. This is released when the file is closed (below). We will read the file after releasing the lock, but that's not a problem, because we never modify already written data. In short, the purpose of this lock is to ensure we don't see the file size change mid-update.
@@ -1479,7 +1463,7 @@ bool history_t::save_internal_via_appending()
if (out_fd >= 0)
{
/* Check to see if the file changed */
- if (history_file_identify(out_fd) != mmap_file_id)
+ if (file_id_for_fd(out_fd) != mmap_file_id)
file_changed = true;
/* Exclusive lock on the entire file. This is released when we close the file (below). This may fail on (e.g.) lockless NFS. If so, proceed as if it did not fail; the risk is that we may get interleaved history items, which is considered better than no history, or forcing everything through the slow copy-move mode. We try to minimize this possibility by writing with O_APPEND.
diff --git a/input_common.cpp b/input_common.cpp
index d26f30b9..1252dc0d 100644
--- a/input_common.cpp
+++ b/input_common.cpp
@@ -96,7 +96,7 @@ static wint_t readb()
input_flush_callbacks();
fd_set fdset;
- int fd_max=0;
+ int fd_max = 0;
int ioport = iothread_port();
int res;
@@ -105,15 +105,36 @@ static wint_t readb()
if (env_universal_server.fd > 0)
{
FD_SET(env_universal_server.fd, &fdset);
- if (fd_max < env_universal_server.fd) fd_max = env_universal_server.fd;
+ fd_max = maxi(fd_max, env_universal_server.fd);
}
if (ioport > 0)
{
FD_SET(ioport, &fdset);
- if (fd_max < ioport) fd_max = ioport;
+ fd_max = maxi(fd_max, ioport);
}
-
- res = select(fd_max + 1, &fdset, 0, 0, 0);
+
+ /* Get our uvar notifier */
+ universal_notifier_t &notifier = universal_notifier_t::default_notifier();
+
+ /* Get the notification fd (possibly none) */
+ int notifier_fd = notifier.notification_fd();
+ if (notifier_fd > 0)
+ {
+ FD_SET(notifier_fd, &fdset);
+ fd_max = maxi(fd_max, notifier_fd);
+ }
+
+ /* Get its suggested delay (possibly none) */
+ struct timeval tv = {};
+ const unsigned long usecs_delay = notifier.usec_delay_between_polls();
+ if (usecs_delay > 0)
+ {
+ unsigned long usecs_per_sec = 1000000;
+ tv.tv_sec = (int)(usecs_delay / usecs_per_sec);
+ tv.tv_usec = (int)(usecs_delay % usecs_per_sec);
+ }
+
+ res = select(fd_max + 1, &fdset, 0, 0, usecs_delay > 0 ? &tv : NULL);
if (res==-1)
{
switch (errno)
@@ -162,6 +183,18 @@ static wint_t readb()
return lookahead_pop();
}
}
+
+ /* Check to see if we want a barrier */
+ bool barrier_from_poll = notifier.poll();
+ bool barrier_from_readability = false;
+ if (notifier_fd > 0 && FD_ISSET(notifier_fd, &fdset))
+ {
+ barrier_from_readability = notifier.notification_fd_became_readable(notifier_fd);
+ }
+ if (barrier_from_poll || barrier_from_readability)
+ {
+ env_universal_barrier();
+ }
if (ioport > 0 && FD_ISSET(ioport, &fdset))
{
diff --git a/iothread.h b/iothread.h
index 8ac3bd9d..3c07b771 100644
--- a/iothread.h
+++ b/iothread.h
@@ -38,7 +38,12 @@ int iothread_perform(int (*handler)(T *), void (*completionCallback)(T *, int),
return iothread_perform_base((int (*)(void *))handler, (void (*)(void *, int))completionCallback, static_cast<void *>(context));
}
-/** Helper templates */
+template<typename T>
+int iothread_perform(int (*handler)(T *), T *context)
+{
+ return iothread_perform_base((int (*)(void *))handler, (void (*)(void *, int))0, static_cast<void *>(context));
+}
+
template<typename T>
int iothread_perform_on_main(int (*handler)(T *), T *context)
{
diff --git a/wildcard.cpp b/wildcard.cpp
index 2b93561b..3ec1a8e5 100644
--- a/wildcard.cpp
+++ b/wildcard.cpp
@@ -971,7 +971,7 @@ static int wildcard_expand_internal(const wchar_t *wc,
// Insert a "file ID" into visited_files
// If the insertion fails, we've already visited this file (i.e. a symlink loop)
// If we're not recursive, insert anyways (in case we loop back around in a future recursive segment), but continue on; the idea being that literal path components should still work
- const file_id_t file_id(buf.st_dev, buf.st_ino);
+ const file_id_t file_id = file_id_t::file_id_from_stat(&buf);
if (S_ISDIR(buf.st_mode) && (visited_files.insert(file_id).second || ! is_recursive))
{
new_dir.push_back(L'/');
diff --git a/wutil.cpp b/wutil.cpp
index 7750a3e4..3b1bba79 100644
--- a/wutil.cpp
+++ b/wutil.cpp
@@ -35,6 +35,8 @@
typedef std::string cstring;
+const file_id_t kInvalidFileID = {-1, -1, -1, -1, -1, -1};
+
/**
Minimum length of the internal covnersion buffers
*/
@@ -246,46 +248,46 @@ int wopen_cloexec(const wcstring &pathname, int flags, mode_t mode)
int wcreat(const wcstring &pathname, mode_t mode)
{
- cstring tmp = wcs2string(pathname);
+ const cstring tmp = wcs2string(pathname);
return creat(tmp.c_str(), mode);
}
DIR *wopendir(const wcstring &name)
{
- cstring tmp = wcs2string(name);
+ const cstring tmp = wcs2string(name);
return opendir(tmp.c_str());
}
int wstat(const wcstring &file_name, struct stat *buf)
{
- cstring tmp = wcs2string(file_name);
+ const cstring tmp = wcs2string(file_name);
return stat(tmp.c_str(), buf);
}
int lwstat(const wcstring &file_name, struct stat *buf)
{
- cstring tmp = wcs2string(file_name);
+ const cstring tmp = wcs2string(file_name);
return lstat(tmp.c_str(), buf);
}
int waccess(const wcstring &file_name, int mode)
{
- cstring tmp = wcs2string(file_name);
+ const cstring tmp = wcs2string(file_name);
return access(tmp.c_str(), mode);
}
int wunlink(const wcstring &file_name)
{
- cstring tmp = wcs2string(file_name);
+ const cstring tmp = wcs2string(file_name);
return unlink(tmp.c_str());
}
-void wperror(const wcstring &s)
+void wperror(const wchar_t *s)
{
int e = errno;
- if (!s.empty())
+ if (s[0] != L'\0')
{
- fwprintf(stderr, L"%ls: ", s.c_str());
+ fwprintf(stderr, L"%ls: ", s);
}
fwprintf(stderr, L"%s\n", strerror(e));
}
@@ -525,3 +527,97 @@ int fish_wcstoi(const wchar_t *str, wchar_t ** endptr, int base)
}
return (int)ret;
}
+
+file_id_t file_id_t::file_id_from_stat(const struct stat *buf)
+{
+ assert(buf != NULL);
+
+ file_id_t result = {};
+ result.device = buf->st_dev;
+ result.inode = buf->st_ino;
+ result.size = buf->st_size;
+ result.change_seconds = buf->st_ctime;
+
+#if STAT_HAVE_NSEC
+ result.change_nanoseconds = buf->st_ctime_nsec;
+#elif defined(__APPLE__)
+ result.change_nanoseconds = buf->st_ctimespec.tv_nsec;
+#elif defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || defined(_XOPEN_SOURCE)
+ result.change_nanoseconds = buf->st_ctim.tv_nsec;
+#else
+ result.change_nanoseconds = 0;
+#endif
+
+#if defined(__APPLE__) || defined(__DragonFly__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
+ result.generation = buf->st_gen;
+#else
+ result.generation = 0;
+#endif
+ return result;
+}
+
+
+file_id_t file_id_for_fd(int fd)
+{
+ file_id_t result = kInvalidFileID;
+ struct stat buf = {};
+ if (0 == fstat(fd, &buf))
+ {
+ result = file_id_t::file_id_from_stat(&buf);
+ }
+ return result;
+}
+
+file_id_t file_id_for_path(const wcstring &path)
+{
+ file_id_t result = kInvalidFileID;
+ struct stat buf = {};
+ if (0 == wstat(path, &buf))
+ {
+ result = file_id_t::file_id_from_stat(&buf);
+ }
+ return result;
+
+}
+
+bool file_id_t::operator==(const file_id_t &rhs) const
+{
+ return device == rhs.device &&
+ inode == rhs.inode &&
+ size == rhs.size &&
+ change_seconds == rhs.change_seconds &&
+ change_nanoseconds == rhs.change_nanoseconds &&
+ generation == rhs.generation;
+}
+
+bool file_id_t::operator!=(const file_id_t &rhs) const
+{
+ return ! (*this == rhs);
+}
+
+template<typename T>
+int compare(T a, T b)
+{
+ if (a < b)
+ {
+ return -1;
+ }
+ else if (a > b)
+ {
+ return 1;
+ }
+ return 0;
+}
+
+bool file_id_t::operator<(const file_id_t &rhs) const
+{
+ /* Compare each field, stopping when we get to a non-equal field */
+ int ret = 0;
+ if (! ret) ret = compare(device, rhs.device);
+ if (! ret) ret = compare(inode, rhs.inode);
+ if (! ret) ret = compare(size, rhs.size);
+ if (! ret) ret = compare(generation, rhs.generation);
+ if (! ret) ret = compare(change_seconds, rhs.change_seconds);
+ if (! ret) ret = compare(change_nanoseconds, rhs.change_nanoseconds);
+ return ret < 0;
+}
diff --git a/wutil.h b/wutil.h
index 347e7313..36d6d075 100644
--- a/wutil.h
+++ b/wutil.h
@@ -16,6 +16,7 @@
#include <stdarg.h>
#include <string>
#include <utility>
+#include <stdint.h>
#include "common.h"
/**
@@ -84,7 +85,7 @@ int wunlink(const wcstring &pathname);
/**
Wide character version of perror().
*/
-void wperror(const wcstring &s);
+void wperror(const wchar_t *s);
/**
Async-safe version of perror().
@@ -158,8 +159,29 @@ int wrename(const wcstring &oldName, const wcstring &newName);
/** Like wcstol(), but fails on a value outside the range of an int */
int fish_wcstoi(const wchar_t *str, wchar_t ** endptr, int base);
-/** Class for representing a file's inode. We use this to detect and avoid symlink loops, among other things. */
-typedef std::pair<dev_t, ino_t> file_id_t;
+/** Class for representing a file's inode. We use this to detect and avoid symlink loops, among other things. While an inode / dev pair is sufficient to distinguish co-existing files, Linux seems to aggressively re-use inodes, so it cannot determine if a file has been deleted (ABA problem). Therefore we include richer information. */
+struct file_id_t
+{
+ dev_t device;
+ ino_t inode;
+ uint64_t size;
+ time_t change_seconds;
+ long change_nanoseconds;
+ uint32_t generation;
+
+ bool operator==(const file_id_t &rhs) const;
+ bool operator!=(const file_id_t &rhs) const;
+
+ // Used to permit these as keys in std::map
+ bool operator<(const file_id_t &rhs) const;
+
+ static file_id_t file_id_from_stat(const struct stat *buf);
+};
+
+file_id_t file_id_for_fd(int fd);
+file_id_t file_id_for_path(const wcstring &path);
+
+extern const file_id_t kInvalidFileID;
#endif