2016-04-29 09:55:41 +08:00
|
|
|
// The utility library for universal variables. Used both by the client library and by the daemon.
|
2016-12-26 12:53:59 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
2006-01-31 00:58:00 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
#include <arpa/inet.h> // IWYU pragma: keep
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <errno.h>
|
2016-04-29 09:55:41 +08:00
|
|
|
#include <fcntl.h>
|
2017-02-11 10:47:02 +08:00
|
|
|
// We need the sys/file.h for the flock() declaration on Linux but not OS X.
|
|
|
|
#include <sys/file.h> // IWYU pragma: keep
|
|
|
|
// We need the ioctl.h header so we can check if SIOCGIFHWADDR is defined by it so we know if we're
|
|
|
|
// on a Linux system.
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <limits.h>
|
2016-12-26 12:53:59 +08:00
|
|
|
#include <netinet/in.h> // IWYU pragma: keep
|
2017-02-14 12:37:27 +08:00
|
|
|
#include <sys/ioctl.h> // IWYU pragma: keep
|
2017-02-11 10:47:02 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(__CYGWIN__)
|
2016-04-29 09:55:41 +08:00
|
|
|
#include <pwd.h>
|
2017-02-11 10:47:02 +08:00
|
|
|
#endif
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2016-12-26 12:53:59 +08:00
|
|
|
#ifdef __CYGWIN__
|
2016-04-29 09:55:41 +08:00
|
|
|
#include <sys/mman.h>
|
2016-12-26 12:53:59 +08:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_SELECT_H
|
|
|
|
#include <sys/select.h> // IWYU pragma: keep
|
|
|
|
#endif
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <sys/stat.h>
|
2017-02-14 12:37:27 +08:00
|
|
|
#include <sys/time.h> // IWYU pragma: keep
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <sys/types.h> // IWYU pragma: keep
|
2016-12-16 13:50:27 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wchar.h>
|
2017-02-13 12:24:22 +08:00
|
|
|
|
2017-01-31 05:41:17 +08:00
|
|
|
#include <atomic>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <map>
|
2016-12-16 13:50:27 +08:00
|
|
|
#include <string>
|
2017-02-11 10:47:02 +08:00
|
|
|
#include <type_traits>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <utility>
|
2006-08-10 06:26:05 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
#include "common.h"
|
|
|
|
#include "env.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "env_universal_common.h"
|
2016-04-29 09:55:41 +08:00
|
|
|
#include "fallback.h" // IWYU pragma: keep
|
2016-11-27 08:35:48 +08:00
|
|
|
#include "path.h"
|
2016-04-29 09:55:41 +08:00
|
|
|
#include "utf8.h"
|
2016-12-26 12:53:59 +08:00
|
|
|
#include "util.h" // IWYU pragma: keep
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "wutil.h"
|
|
|
|
|
2014-05-01 06:50:03 +08:00
|
|
|
#if __APPLE__
|
|
|
|
#define FISH_NOTIFYD_AVAILABLE 1
|
|
|
|
#include <notify.h>
|
|
|
|
#endif
|
|
|
|
|
2016-12-24 20:09:34 +08:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#define _BSD_SOURCE
|
|
|
|
#include <bsd/ifaddrs.h>
|
2017-01-27 12:00:43 +08:00
|
|
|
#endif // Haiku
|
2016-12-24 20:09:34 +08:00
|
|
|
|
2014-12-08 11:48:28 +08:00
|
|
|
// 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
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// The set command.
|
2014-06-16 08:30:50 +08:00
|
|
|
#define SET_STR L"SET"
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// The set_export command.
|
2014-06-16 08:30:50 +08:00
|
|
|
#define SET_EXPORT_STR L"SET_EXPORT"
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Non-wide version of the set command.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define SET_MBS "SET"
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Non-wide version of the set_export command.
|
2005-09-23 04:16:52 +08:00
|
|
|
#define SET_EXPORT_MBS "SET_EXPORT"
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Error message.
|
2005-09-20 21:26:39 +08:00
|
|
|
#define PARSE_ERR L"Unable to parse universal variable message: '%ls'"
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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"
|
2014-04-28 04:34:51 +08:00
|
|
|
|
2014-11-10 16:06:25 +08:00
|
|
|
static wcstring get_machine_identifier();
|
|
|
|
static bool get_hostname_identifier(wcstring *result);
|
2014-04-28 04:34:51 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
static wcstring vars_filename_in_directory(const wcstring &wdir) {
|
|
|
|
if (wdir.empty()) return L"";
|
|
|
|
|
2014-11-10 16:06:25 +08:00
|
|
|
wcstring result = wdir;
|
|
|
|
result.append(L"/fishd.");
|
|
|
|
result.append(get_machine_identifier());
|
|
|
|
return result;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
|
2016-11-27 08:35:48 +08:00
|
|
|
static const wcstring default_vars_path() {
|
|
|
|
wcstring path;
|
|
|
|
path_get_config(path);
|
|
|
|
return vars_filename_in_directory(path);
|
2014-11-10 08:55:02 +08:00
|
|
|
}
|
|
|
|
|
2017-01-03 13:11:53 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(__CYGWIN__)
|
|
|
|
/// Check, and create if necessary, a secure runtime path. Derived from tmux.c in tmux
|
2016-04-29 09:55:41 +08:00
|
|
|
/// (http://tmux.sourceforge.net/).
|
|
|
|
static int check_runtime_path(const char *path) {
|
|
|
|
// 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.
|
2014-11-10 14:57:19 +08:00
|
|
|
struct stat statpath;
|
2016-03-18 11:45:48 +08:00
|
|
|
uid_t uid = geteuid();
|
2014-11-10 14:57:19 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
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)
|
2014-11-10 14:57:19 +08:00
|
|
|
return EACCES;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Return the path of an appropriate runtime data directory.
|
|
|
|
static wcstring get_runtime_path() {
|
2014-11-10 16:06:25 +08:00
|
|
|
wcstring result;
|
2014-11-10 14:57:19 +08:00
|
|
|
const char *dir = getenv("XDG_RUNTIME_DIR");
|
2015-10-09 10:44:38 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Check that the path is actually usable. Technically this is guaranteed by the fdo spec but in
|
|
|
|
// practice it is not always the case: see #1828 and #2222.
|
2015-10-09 10:44:38 +08:00
|
|
|
int mode = R_OK | W_OK | X_OK;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (dir != NULL && access(dir, mode) == 0 && check_runtime_path(dir) == 0) {
|
2014-11-10 16:06:25 +08:00
|
|
|
result = str2wcstring(dir);
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2014-11-10 14:57:19 +08:00
|
|
|
const char *uname = getenv("USER");
|
2017-07-21 02:18:07 +08:00
|
|
|
// $USER should already have been set (in setup_path()).
|
|
|
|
// If it still isn't, there's something wrong.
|
|
|
|
if (!uname) {
|
|
|
|
return result;
|
2014-11-10 14:57:19 +08:00
|
|
|
}
|
|
|
|
// /tmp/fish.user
|
2014-11-10 16:06:25 +08:00
|
|
|
std::string tmpdir = "/tmp/fish.";
|
|
|
|
tmpdir.append(uname);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (check_runtime_path(tmpdir.c_str()) != 0) {
|
2017-01-03 13:11:53 +08:00
|
|
|
debug(0, L"Runtime path not available.");
|
|
|
|
debug(0, L"Try deleting the directory %s and restarting fish.", tmpdir.c_str());
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2014-11-10 16:06:25 +08:00
|
|
|
result = str2wcstring(tmpdir);
|
2014-11-10 14:57:19 +08:00
|
|
|
}
|
2014-11-10 09:56:38 +08:00
|
|
|
}
|
2014-11-10 16:06:25 +08:00
|
|
|
return result;
|
2014-11-10 14:57:19 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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() {
|
|
|
|
// Note that vars_filename_in_directory returns empty string when passed the empty string.
|
2014-11-10 16:06:25 +08:00
|
|
|
return vars_filename_in_directory(get_runtime_path());
|
2014-11-10 09:56:38 +08:00
|
|
|
}
|
2017-01-03 13:11:53 +08:00
|
|
|
#endif
|
2014-11-10 09:56:38 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Test if the message msg contains the command cmd.
|
|
|
|
static bool match(const wchar_t *msg, const wchar_t *cmd) {
|
2012-11-19 08:30:30 +08:00
|
|
|
size_t len = wcslen(cmd);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (wcsncasecmp(msg, cmd, len) != 0) return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
if (msg[len] && msg[len] != L' ' && msg[len] != L'\t') return false;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2013-01-20 05:16:21 +08:00
|
|
|
return true;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
return iswalnum(c) || wcschr(L"/_", c);
|
2012-10-09 05:47:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Escape specified string.
|
|
|
|
static wcstring full_escape(const wchar_t *in) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring out;
|
2016-04-29 09:55:41 +08:00
|
|
|
for (; *in; in++) {
|
2012-10-09 05:47:25 +08:00
|
|
|
wchar_t c = *in;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (is_universal_safe_to_encode_directly(c)) {
|
2012-10-09 05:47:25 +08:00
|
|
|
out.push_back(c);
|
2016-04-29 09:55:41 +08:00
|
|
|
} else if (c <= (wchar_t)ASCII_MAX) {
|
|
|
|
// See #1225 for discussion of use of ASCII_MAX here.
|
2012-11-19 08:30:30 +08:00
|
|
|
append_format(out, L"\\x%.2x", c);
|
2016-04-29 09:55:41 +08:00
|
|
|
} else if (c < 65536) {
|
2012-11-19 08:30:30 +08:00
|
|
|
append_format(out, L"\\u%.4x", c);
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
append_format(out, L"\\U%.8x", c);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
return out;
|
2006-10-18 05:11:29 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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) {
|
2014-06-10 03:57:44 +08:00
|
|
|
bool result = false;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (wchar_to_utf8_string(input, storage)) {
|
2014-06-10 03:57:44 +08:00
|
|
|
receiver->append(*storage);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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) {
|
2014-06-10 03:57:44 +08:00
|
|
|
assert(storage != NULL);
|
|
|
|
assert(result != NULL);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Record the length on entry, in case we need to back up.
|
2014-06-10 03:57:44 +08:00
|
|
|
bool success = true;
|
|
|
|
const size_t result_length_on_entry = result->size();
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-06-10 03:57:44 +08:00
|
|
|
// Append header like "SET "
|
2016-04-29 09:55:41 +08:00
|
|
|
result->append(type == SET ? SET_MBS : SET_EXPORT_MBS);
|
2014-06-10 03:57:44 +08:00
|
|
|
result->push_back(' ');
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Append variable name like "fish_color_cwd".
|
2017-04-20 14:43:02 +08:00
|
|
|
if (!valid_var_name(key_in)) {
|
2014-06-10 03:57:44 +08:00
|
|
|
debug(0, L"Illegal variable name: '%ls'", key_in.c_str());
|
|
|
|
success = false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
if (success && !append_utf8(key_in, result, storage)) {
|
2014-06-10 03:57:44 +08:00
|
|
|
debug(0, L"Could not convert %ls to narrow character string", key_in.c_str());
|
|
|
|
success = false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Append ":".
|
|
|
|
if (success) {
|
2014-06-10 03:57:44 +08:00
|
|
|
result->push_back(':');
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Append value.
|
|
|
|
if (success && !append_utf8(full_escape(val_in.c_str()), result, storage)) {
|
2014-06-10 03:57:44 +08:00
|
|
|
debug(0, L"Could not convert %ls to narrow character string", val_in.c_str());
|
|
|
|
success = false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Append newline.
|
|
|
|
if (success) {
|
2014-06-10 03:57:44 +08:00
|
|
|
result->push_back('\n');
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Don't modify result on failure. It's sufficient to simply resize it since all we ever did was
|
|
|
|
// append to it.
|
|
|
|
if (!success) {
|
2014-06-10 03:57:44 +08:00
|
|
|
result->resize(result_length_on_entry);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-06-10 03:57:44 +08:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
env_universal_t::env_universal_t(const wcstring &path)
|
|
|
|
: explicit_vars_path(path), tried_renaming(false), last_read_file(kInvalidFileID) {
|
2017-02-15 13:09:15 +08:00
|
|
|
DIE_ON_FAILURE(pthread_mutex_init(&lock, NULL));
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
env_universal_t::~env_universal_t() { pthread_mutex_destroy(&lock); }
|
2014-04-26 07:09:26 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
env_var_t env_universal_t::get(const wcstring &name) const {
|
2014-04-26 07:09:26 +08:00
|
|
|
env_var_t result = env_var_t::missing_var();
|
|
|
|
var_table_t::const_iterator where = vars.find(name);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (where != vars.end()) {
|
2016-02-28 11:38:15 +08:00
|
|
|
result = env_var_t(where->second.val);
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
bool env_universal_t::get_export(const wcstring &name) const {
|
2014-04-26 07:09:26 +08:00
|
|
|
bool result = false;
|
|
|
|
var_table_t::const_iterator where = vars.find(name);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (where != vars.end()) {
|
2014-04-26 07:09:26 +08:00
|
|
|
result = where->second.exportv;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
void env_universal_t::set_internal(const wcstring &key, const wcstring &val, bool exportv,
|
|
|
|
bool overwrite) {
|
2014-04-27 02:41:34 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!overwrite && this->modified.find(key) != this->modified.end()) {
|
|
|
|
// This value has been modified and we're not overwriting it. Skip it.
|
2014-04-28 04:34:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-04-26 07:09:26 +08:00
|
|
|
var_entry_t *entry = &vars[key];
|
2016-04-29 09:55:41 +08:00
|
|
|
if (entry->exportv != exportv || entry->val != val) {
|
2014-04-30 08:03:00 +08:00
|
|
|
entry->val = val;
|
|
|
|
entry->exportv = exportv;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// If we are overwriting, then this is now modified.
|
|
|
|
if (overwrite) {
|
2014-04-30 08:03:00 +08:00
|
|
|
this->modified.insert(key);
|
|
|
|
}
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
void env_universal_t::set(const wcstring &key, const wcstring &val, bool exportv) {
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2014-04-28 04:34:51 +08:00
|
|
|
this->set_internal(key, val, exportv, true /* overwrite */);
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
bool env_universal_t::remove_internal(const wcstring &key) {
|
2014-04-28 04:34:51 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2014-04-30 08:03:00 +08:00
|
|
|
size_t erased = this->vars.erase(key);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (erased > 0) {
|
2014-04-28 04:34:51 +08:00
|
|
|
this->modified.insert(key);
|
|
|
|
}
|
2014-06-14 06:15:11 +08:00
|
|
|
return erased > 0;
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
bool env_universal_t::remove(const wcstring &key) {
|
2014-04-28 04:34:51 +08:00
|
|
|
scoped_lock locker(lock);
|
2014-06-14 06:15:11 +08:00
|
|
|
return this->remove_internal(key);
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
wcstring_list_t env_universal_t::get_names(bool show_exported, bool show_unexported) const {
|
2014-04-26 07:09:26 +08:00
|
|
|
wcstring_list_t result;
|
2016-07-21 13:30:58 +08:00
|
|
|
scoped_lock locker(lock);
|
2014-04-26 07:09:26 +08:00
|
|
|
var_table_t::const_iterator iter;
|
2016-04-29 09:55:41 +08:00
|
|
|
for (iter = vars.begin(); iter != vars.end(); ++iter) {
|
2014-04-26 07:09:26 +08:00
|
|
|
const wcstring &key = iter->first;
|
|
|
|
const var_entry_t &e = iter->second;
|
2016-04-29 09:55:41 +08:00
|
|
|
if ((e.exportv && show_exported) || (!e.exportv && show_unexported)) {
|
2014-04-26 07:09:26 +08:00
|
|
|
result.push_back(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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,
|
2017-07-15 01:45:31 +08:00
|
|
|
callback_data_list_t &callbacks) const {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Construct callbacks for erased values.
|
|
|
|
for (var_table_t::const_iterator iter = this->vars.begin(); iter != this->vars.end(); ++iter) {
|
2014-06-17 03:25:33 +08:00
|
|
|
const wcstring &key = iter->first;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Skip modified values.
|
|
|
|
if (this->modified.find(key) != this->modified.end()) {
|
2014-06-17 03:25:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// If the value is not present in new_vars, it has been erased.
|
|
|
|
if (new_vars.find(key) == new_vars.end()) {
|
2017-07-15 01:45:31 +08:00
|
|
|
callbacks.push_back(callback_data_t(ERASE, key, L""));
|
2014-06-17 03:25:33 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Construct callbacks for newly inserted or changed values.
|
|
|
|
for (var_table_t::const_iterator iter = new_vars.begin(); iter != new_vars.end(); ++iter) {
|
2014-04-30 08:03:00 +08:00
|
|
|
const wcstring &key = iter->first;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Skip modified values.
|
|
|
|
if (this->modified.find(key) != this->modified.end()) {
|
2014-06-17 03:25:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// See if the value has changed.
|
2014-06-17 03:25:33 +08:00
|
|
|
const var_entry_t &new_entry = iter->second;
|
|
|
|
var_table_t::const_iterator existing = this->vars.find(key);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (existing == this->vars.end() || existing->second.exportv != new_entry.exportv ||
|
|
|
|
existing->second.val != new_entry.val) {
|
|
|
|
// Value has changed.
|
2017-07-15 01:45:31 +08:00
|
|
|
callbacks.push_back(
|
2016-04-29 09:55:41 +08:00
|
|
|
callback_data_t(new_entry.exportv ? SET_EXPORT : SET, key, new_entry.val));
|
2014-06-17 03:25:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
void env_universal_t::acquire_variables(var_table_t *vars_to_acquire) {
|
|
|
|
// Copy modified values from existing vars to vars_to_acquire.
|
|
|
|
for (std::set<wcstring>::iterator iter = this->modified.begin(); iter != this->modified.end();
|
|
|
|
++iter) {
|
2014-06-17 03:25:33 +08:00
|
|
|
const wcstring &key = *iter;
|
|
|
|
var_table_t::iterator src_iter = this->vars.find(key);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (src_iter == this->vars.end()) {
|
2014-06-17 03:25:33 +08:00
|
|
|
/* The value has been deleted. */
|
|
|
|
vars_to_acquire->erase(key);
|
2016-04-29 09:55:41 +08:00
|
|
|
} 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.
|
2014-06-17 03:25:33 +08:00
|
|
|
var_entry_t &src = src_iter->second;
|
|
|
|
var_entry_t &dst = (*vars_to_acquire)[key];
|
2017-01-27 08:14:50 +08:00
|
|
|
dst.val = std::move(src.val);
|
2014-06-17 03:25:33 +08:00
|
|
|
dst.exportv = src.exportv;
|
2014-04-30 08:03:00 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// We have constructed all the callbacks and updated vars_to_acquire. Acquire it!
|
2017-01-27 08:14:50 +08:00
|
|
|
this->vars = std::move(*vars_to_acquire);
|
2014-04-30 08:03:00 +08:00
|
|
|
}
|
|
|
|
|
2017-07-15 01:45:31 +08:00
|
|
|
void env_universal_t::load_from_fd(int fd, callback_data_list_t &callbacks) {
|
2014-04-28 04:34:51 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
|
|
|
assert(fd >= 0);
|
2016-04-29 09:55:41 +08:00
|
|
|
// Get the dev / inode.
|
2014-04-29 06:14:33 +08:00
|
|
|
const file_id_t current_file = file_id_for_fd(fd);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (current_file == last_read_file) {
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log sync elided based on fstat()");
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
|
|
|
// Read a variables table from the file.
|
2014-06-17 03:25:33 +08:00
|
|
|
var_table_t new_vars = this->read_message_internal(fd);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Announce changes.
|
2017-07-15 01:45:31 +08:00
|
|
|
this->generate_callbacks(new_vars, callbacks);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Acquire the new variables.
|
2014-06-17 03:25:33 +08:00
|
|
|
this->acquire_variables(&new_vars);
|
2014-04-28 04:34:51 +08:00
|
|
|
last_read_file = current_file;
|
|
|
|
}
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
2014-04-26 08:44:49 +08:00
|
|
|
|
2017-07-15 01:45:31 +08:00
|
|
|
bool env_universal_t::load_from_path(const wcstring &path, callback_data_list_t &callbacks) {
|
2014-04-27 02:41:34 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// 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) {
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log sync elided based on fast stat()");
|
2014-04-30 08:03:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-04-27 02:41:34 +08:00
|
|
|
bool result = false;
|
2014-04-28 04:34:51 +08:00
|
|
|
int fd = wopen_cloexec(path, O_RDONLY);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (fd >= 0) {
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log reading from file");
|
2014-04-30 02:28:00 +08:00
|
|
|
this->load_from_fd(fd, callbacks);
|
2014-04-28 04:34:51 +08:00
|
|
|
close(fd);
|
2014-04-27 02:41:34 +08:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
return result;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2014-04-27 02:41:34 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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) {
|
2014-04-28 04:34:51 +08:00
|
|
|
ASSERT_IS_LOCKED(lock);
|
|
|
|
assert(fd >= 0);
|
2014-06-10 03:57:44 +08:00
|
|
|
bool success = true;
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Stuff we output to fd.
|
2014-06-10 03:57:44 +08:00
|
|
|
std::string contents;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Temporary storage.
|
2014-06-10 03:57:44 +08:00
|
|
|
std::string storage;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-06-10 03:57:44 +08:00
|
|
|
// Write the save message. If this fails, we don't bother complaining.
|
2014-04-28 04:34:51 +08:00
|
|
|
write_loop(fd, SAVE_MSG, strlen(SAVE_MSG));
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-06-10 03:57:44 +08:00
|
|
|
var_table_t::const_iterator iter = vars.begin();
|
2016-04-29 09:55:41 +08:00
|
|
|
while (iter != vars.end()) {
|
|
|
|
// Append the entry. Note that append_file_entry may fail, but that only affects one
|
|
|
|
// variable; soldier on.
|
2014-06-10 03:57:44 +08:00
|
|
|
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);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Go to next.
|
2014-06-10 03:57:44 +08:00
|
|
|
++iter;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// 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) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to write to universal variables file '%ls': %s"), path.c_str(),
|
|
|
|
error);
|
2014-06-10 03:57:44 +08:00
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
contents.clear();
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Since we just wrote out this file, it matches our internal state; pretend we read from it.
|
2014-04-28 04:34:51 +08:00
|
|
|
this->last_read_file = file_id_for_fd(fd);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// We don't close the file.
|
2014-06-10 03:57:44 +08:00
|
|
|
return success;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
bool env_universal_t::move_new_vars_file_into_place(const wcstring &src, const wcstring &dst) {
|
2014-04-28 04:34:51 +08:00
|
|
|
int ret = wrename(src, dst);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (ret != 0) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to rename file from '%ls' to '%ls': %s"), src.c_str(), dst.c_str(),
|
|
|
|
error);
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
return ret == 0;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2017-07-15 01:45:31 +08:00
|
|
|
bool env_universal_t::load(callback_data_list_t &callbacks) {
|
2016-04-29 09:55:41 +08:00
|
|
|
const wcstring vars_path =
|
|
|
|
explicit_vars_path.empty() ? default_vars_path() : explicit_vars_path;
|
2017-03-27 04:22:23 +08:00
|
|
|
scoped_lock locker(lock);
|
2017-07-15 01:45:31 +08:00
|
|
|
bool success = load_from_path(vars_path, callbacks);
|
2016-04-29 09:55:41 +08:00
|
|
|
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."
|
2014-04-27 02:41:34 +08:00
|
|
|
tried_renaming = true;
|
2014-11-10 16:06:25 +08:00
|
|
|
wcstring hostname_id;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (get_hostname_identifier(&hostname_id)) {
|
2014-11-10 16:06:25 +08:00
|
|
|
const wcstring hostname_path = wdirname(vars_path) + L'/' + hostname_id;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (0 == wrename(hostname_path, vars_path)) {
|
|
|
|
// We renamed - try again.
|
2017-07-15 01:45:31 +08:00
|
|
|
success = this->load(callbacks);
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-15 01:45:31 +08:00
|
|
|
|
2014-04-27 02:41:34 +08:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-04-05 05:34:28 +08:00
|
|
|
bool env_universal_t::open_temporary_file(const wcstring &directory, wcstring *out_path,
|
2016-04-29 09:55:41 +08:00
|
|
|
int *out_fd) {
|
2016-04-05 05:34:28 +08:00
|
|
|
// 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.
|
2016-11-27 08:35:48 +08:00
|
|
|
assert(!string_suffixes_string(L"/", directory)); //!OCLINT(multiple unary operator)
|
2016-04-05 05:34:28 +08:00
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
bool success = false;
|
2016-11-27 08:35:48 +08:00
|
|
|
int saved_errno;
|
2014-04-28 04:34:51 +08:00
|
|
|
const wcstring tmp_name_template = directory + L"/fishd.tmp.XXXXXX";
|
2016-04-05 05:34:28 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
for (size_t attempt = 0; attempt < 10 && !success; attempt++) {
|
2014-04-28 04:34:51 +08:00
|
|
|
char *narrow_str = wcs2str(tmp_name_template.c_str());
|
2017-01-08 17:32:49 +08:00
|
|
|
int result_fd = fish_mkstemp_cloexec(narrow_str);
|
2016-07-31 10:32:05 +08:00
|
|
|
saved_errno = errno;
|
2016-04-05 05:34:28 +08:00
|
|
|
success = result_fd != -1;
|
|
|
|
*out_fd = result_fd;
|
|
|
|
*out_path = str2wcstring(narrow_str);
|
2014-04-28 04:34:51 +08:00
|
|
|
free(narrow_str);
|
|
|
|
}
|
2016-07-31 10:32:05 +08:00
|
|
|
|
|
|
|
if (!success) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(saved_errno);
|
|
|
|
debug(0, _(L"Unable to open temporary file '%ls': %s"), out_path->c_str(), error);
|
2016-07-31 10:32:05 +08:00
|
|
|
}
|
2014-04-28 04:34:51 +08:00
|
|
|
return success;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2016-12-16 13:50:27 +08:00
|
|
|
/// Check how long the operation took and print a message if it took too long.
|
|
|
|
/// Returns false if it took too long else true.
|
|
|
|
static bool check_duration(double start_time) {
|
|
|
|
double duration = timef() - start_time;
|
|
|
|
if (duration > 0.25) {
|
|
|
|
debug(1, _(L"Locking the universal var file took too long (%.3f seconds)."), duration);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try locking the file. Return true if we succeeded else false. This is safe in terms of the
|
|
|
|
/// fallback function implemented in terms of fcntl: only ever run on the main thread, and protected
|
|
|
|
/// by the universal variable lock.
|
|
|
|
static bool lock_uvar_file(int fd) {
|
|
|
|
double start_time = timef();
|
|
|
|
while (flock(fd, LOCK_EX) == -1) {
|
|
|
|
if (errno != EINTR) return false; // do nothing per issue #2149
|
|
|
|
}
|
|
|
|
return check_duration(start_time);
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
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.
|
2017-01-30 13:42:33 +08:00
|
|
|
static std::atomic<bool> do_locking(true);
|
2014-04-28 04:34:51 +08:00
|
|
|
bool needs_lock = true;
|
2015-07-03 15:31:03 +08:00
|
|
|
int flags = O_RDWR | O_CREAT;
|
2016-12-16 13:50:27 +08:00
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
#ifdef O_EXLOCK
|
2016-12-16 13:50:27 +08:00
|
|
|
if (do_locking) {
|
|
|
|
flags |= O_EXLOCK;
|
|
|
|
needs_lock = false;
|
|
|
|
}
|
2014-04-28 04:34:51 +08:00
|
|
|
#endif
|
2016-12-16 13:50:27 +08:00
|
|
|
|
|
|
|
int fd = -1;
|
|
|
|
while (fd == -1) {
|
|
|
|
double start_time = timef();
|
|
|
|
fd = wopen_cloexec(path, flags, 0644);
|
|
|
|
if (fd == -1) {
|
|
|
|
if (errno == EINTR) continue; // signaled; try again
|
2014-04-28 04:34:51 +08:00
|
|
|
#ifdef O_EXLOCK
|
2016-12-16 13:50:27 +08:00
|
|
|
if (do_locking && (errno == ENOTSUP || errno == EOPNOTSUPP)) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Filesystem probably does not support locking. Clear the flag and try again. Note
|
2016-09-09 08:35:31 +08:00
|
|
|
// that we try taking the lock via flock anyways. Note that on Linux the two errno
|
|
|
|
// symbols have the same value but on BSD they're different.
|
2014-04-28 04:34:51 +08:00
|
|
|
flags &= ~O_EXLOCK;
|
|
|
|
needs_lock = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2016-12-16 13:50:27 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to open universal variable file '%ls': %s"), path.c_str(), error);
|
|
|
|
break;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-12-16 13:50:27 +08:00
|
|
|
assert(fd >= 0); // if we get here, we must have a valid fd
|
|
|
|
if (!needs_lock && do_locking) {
|
|
|
|
do_locking = check_duration(start_time);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// 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.
|
2016-12-16 13:50:27 +08:00
|
|
|
if (needs_lock && do_locking) {
|
|
|
|
do_locking = lock_uvar_file(fd);
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// 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.
|
2014-04-28 04:34:51 +08:00
|
|
|
close(fd);
|
2016-12-16 13:50:27 +08:00
|
|
|
fd = -1;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-12-16 13:50:27 +08:00
|
|
|
*out_fd = fd;
|
|
|
|
return fd >= 0;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Returns true if modified variables were written, false if not. (There may still be variable
|
|
|
|
// changes due to other processes on a false return).
|
2017-07-15 01:45:31 +08:00
|
|
|
bool env_universal_t::sync(callback_data_list_t &callbacks) {
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log sync");
|
2014-04-27 02:41:34 +08:00
|
|
|
scoped_lock locker(lock);
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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;
|
2015-11-18 16:58:45 +08:00
|
|
|
|
|
|
|
if (vars_path.empty()) {
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(2, L"No universal variable path available");
|
2015-11-18 16:58:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// If we have no changes, just load.
|
|
|
|
if (modified.empty()) {
|
2014-04-30 08:03:00 +08:00
|
|
|
this->load_from_path(vars_path, callbacks);
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log no modifications");
|
2014-04-30 08:03:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
const wcstring directory = wdirname(vars_path);
|
2014-05-15 10:49:06 +08:00
|
|
|
bool success = true;
|
2014-04-28 04:34:51 +08:00
|
|
|
int vars_fd = -1;
|
|
|
|
int private_fd = -1;
|
|
|
|
wcstring private_file_path;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log performing full sync");
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Open the file.
|
|
|
|
if (success) {
|
2014-05-15 10:49:06 +08:00
|
|
|
success = this->open_and_acquire_lock(vars_path, &vars_fd);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!success) debug(5, L"universal log open_and_acquire_lock() failed");
|
2014-05-15 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Read from it.
|
|
|
|
if (success) {
|
2014-04-28 04:34:51 +08:00
|
|
|
assert(vars_fd >= 0);
|
2014-04-30 02:28:00 +08:00
|
|
|
this->load_from_fd(vars_fd, callbacks);
|
2014-05-15 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Open adjacent temporary file.
|
|
|
|
if (success) {
|
2014-05-15 10:49:06 +08:00
|
|
|
success = this->open_temporary_file(directory, &private_file_path, &private_fd);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!success) debug(5, L"universal log open_temporary_file() failed");
|
2014-05-15 10:49:06 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Write to it.
|
|
|
|
if (success) {
|
2014-04-28 04:34:51 +08:00
|
|
|
assert(private_fd >= 0);
|
2014-06-10 03:57:44 +08:00
|
|
|
success = this->write_to_fd(private_fd, private_file_path);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!success) debug(5, L"universal log write_to_fd() failed");
|
2014-05-15 10:49:06 +08:00
|
|
|
}
|
2016-03-13 03:21:52 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
if (success) {
|
|
|
|
// Ensure we maintain ownership and permissions (#2176).
|
2015-09-27 06:24:55 +08:00
|
|
|
struct stat sbuf;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (wstat(vars_path, &sbuf) >= 0) {
|
2016-03-13 04:26:01 +08:00
|
|
|
if (fchown(private_fd, sbuf.st_uid, sbuf.st_gid) == -1)
|
2016-04-04 11:23:43 +08:00
|
|
|
debug(5, L"universal log fchown() failed");
|
2016-04-29 09:55:41 +08:00
|
|
|
if (fchmod(private_fd, sbuf.st_mode) == -1) debug(5, L"universal log fchmod() failed");
|
2015-09-27 06:24:55 +08:00
|
|
|
}
|
2016-03-13 03:21:52 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Linux by default stores the mtime with low precision, low enough that updates that occur in quick
|
|
|
|
// succession may result in the same mtime (even the nanoseconds field). So manually set the mtime
|
|
|
|
// of the new file to a high-precision clock. Note that this is only necessary because Linux
|
|
|
|
// aggressively reuses inodes, causing the ABA problem; on other platforms we tend to notice the
|
|
|
|
// file has changed due to a different inode (or file size!)
|
|
|
|
//
|
|
|
|
// It's probably worth finding a simpler solution to this. The tests ran into this, but it's
|
|
|
|
// unlikely to affect users.
|
Attempt to fix the sporadic uvar test failures on Linux
We identify when the universal variable file has changed out from under us by
comparing a bunch of fields from its stat: inode, device, size, high-precision
timestamp, generation. Linux aggressively reuses inodes, and the size may be
the same by coincidence (which is the case in the tests). Also, Linux
officially has nanosecond precision, but in practice it seems to only uses
millisecond precision for storing mtimes. Thus if there are three or more
updates within a millisecond, every field we check may be the same, and we are
vulnerable to the ABA problem. I believe this explains the occasional test
failures.
The solution is to manually set the nanosecond field of the mtime timestamp to
something unlikely to be duplicated, like a random number, or better yet, the
current time (with nanosecond precision). This is more in the spirit of the
timestamp, and it means we're around a million times less likely to collide.
This seems to fix the tests.
2015-11-09 15:48:32 +08:00
|
|
|
#if HAVE_CLOCK_GETTIME && HAVE_FUTIMENS
|
|
|
|
struct timespec times[2] = {};
|
2016-04-29 09:55:41 +08:00
|
|
|
times[0].tv_nsec = UTIME_OMIT; // don't change ctime
|
|
|
|
if (0 == clock_gettime(CLOCK_REALTIME, ×[1])) {
|
Attempt to fix the sporadic uvar test failures on Linux
We identify when the universal variable file has changed out from under us by
comparing a bunch of fields from its stat: inode, device, size, high-precision
timestamp, generation. Linux aggressively reuses inodes, and the size may be
the same by coincidence (which is the case in the tests). Also, Linux
officially has nanosecond precision, but in practice it seems to only uses
millisecond precision for storing mtimes. Thus if there are three or more
updates within a millisecond, every field we check may be the same, and we are
vulnerable to the ABA problem. I believe this explains the occasional test
failures.
The solution is to manually set the nanosecond field of the mtime timestamp to
something unlikely to be duplicated, like a random number, or better yet, the
current time (with nanosecond precision). This is more in the spirit of the
timestamp, and it means we're around a million times less likely to collide.
This seems to fix the tests.
2015-11-09 15:48:32 +08:00
|
|
|
futimens(private_fd, times);
|
|
|
|
}
|
|
|
|
#endif
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Apply new file.
|
2014-05-15 10:49:06 +08:00
|
|
|
success = this->move_new_vars_file_into_place(private_file_path, vars_path);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!success) debug(5, L"universal log move_new_vars_file_into_place() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2014-04-28 04:34:51 +08:00
|
|
|
close(vars_fd);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
if (private_fd >= 0) {
|
2014-04-28 04:34:51 +08:00
|
|
|
close(private_fd);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!private_file_path.empty()) {
|
2014-04-28 04:34:51 +08:00
|
|
|
wunlink(private_file_path);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
if (success) {
|
|
|
|
// All of our modified variables have now been written out.
|
2014-04-28 04:34:51 +08:00
|
|
|
modified.clear();
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
var_table_t env_universal_t::read_message_internal(int fd) {
|
2014-06-17 03:25:33 +08:00
|
|
|
var_table_t result;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Temp value used to avoid repeated allocations.
|
2014-06-17 03:25:33 +08:00
|
|
|
wcstring storage;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// The line we construct (and then parse).
|
2014-06-10 07:41:10 +08:00
|
|
|
std::string line;
|
|
|
|
wcstring wide_line;
|
2016-04-29 09:55:41 +08:00
|
|
|
for (;;) {
|
2014-06-10 07:41:10 +08:00
|
|
|
// Read into a buffer. Note this is NOT null-terminated!
|
|
|
|
char buffer[1024];
|
|
|
|
ssize_t amt = read_loop(fd, buffer, sizeof buffer);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (amt <= 0) {
|
2014-06-10 07:41:10 +08:00
|
|
|
break;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
2014-06-16 08:30:50 +08:00
|
|
|
const size_t bufflen = (size_t)amt;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Walk over it by lines. The contents of an unterminated line will be left in 'line' for
|
|
|
|
// the next iteration.
|
2016-10-10 05:36:08 +08:00
|
|
|
ssize_t line_start = 0;
|
2016-04-29 09:55:41 +08:00
|
|
|
while (line_start < amt) {
|
|
|
|
// Run until we hit a newline.
|
2014-06-10 07:41:10 +08:00
|
|
|
size_t cursor = line_start;
|
2016-04-29 09:55:41 +08:00
|
|
|
while (cursor < bufflen && buffer[cursor] != '\n') {
|
2014-06-10 07:41:10 +08:00
|
|
|
cursor++;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Copy over what we read.
|
2014-06-10 07:41:10 +08:00
|
|
|
line.append(buffer + line_start, cursor - line_start);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// 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(line.data(), line.size(), &wide_line, 0)) {
|
2014-06-17 03:25:33 +08:00
|
|
|
env_universal_t::parse_message_internal(wide_line, &result, &storage);
|
2014-06-10 07:41:10 +08:00
|
|
|
}
|
|
|
|
line.clear();
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Skip over the newline (or skip past the end).
|
2014-06-10 07:41:10 +08:00
|
|
|
line_start = cursor + 1;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// We make no effort to handle an unterminated last line.
|
2014-06-17 03:25:33 +08:00
|
|
|
return result;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Parse message msg/
|
|
|
|
void env_universal_t::parse_message_internal(const wcstring &msgstr, var_table_t *vars,
|
|
|
|
wcstring *storage) {
|
2014-06-16 08:30:50 +08:00
|
|
|
const wchar_t *msg = msgstr.c_str();
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-04-04 11:23:43 +08:00
|
|
|
// debug(3, L"parse_message( %ls );", msg);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (msg[0] == L'#') return;
|
|
|
|
|
2014-06-17 03:25:33 +08:00
|
|
|
bool is_set_export = match(msg, SET_EXPORT_STR);
|
2016-04-29 09:55:41 +08:00
|
|
|
bool is_set = !is_set_export && match(msg, SET_STR);
|
|
|
|
if (is_set || is_set_export) {
|
2014-06-16 08:30:50 +08:00
|
|
|
const wchar_t *name, *tmp;
|
2014-06-17 03:25:33 +08:00
|
|
|
const bool exportv = is_set_export;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
name = msg + (exportv ? wcslen(SET_EXPORT_STR) : wcslen(SET_STR));
|
|
|
|
while (name[0] == L'\t' || name[0] == L' ') name++;
|
|
|
|
|
2014-04-27 02:41:34 +08:00
|
|
|
tmp = wcschr(name, L':');
|
2016-04-29 09:55:41 +08:00
|
|
|
if (tmp) {
|
|
|
|
// Use 'storage' to hold our key to avoid allocations.
|
2014-06-17 03:25:33 +08:00
|
|
|
storage->assign(name, tmp - name);
|
|
|
|
const wcstring &key = *storage;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-04-27 02:41:34 +08:00
|
|
|
wcstring val;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (unescape_string(tmp + 1, &val, 0)) {
|
2014-06-17 03:25:33 +08:00
|
|
|
var_entry_t &entry = (*vars)[key];
|
|
|
|
entry.exportv = exportv;
|
2017-01-27 08:14:50 +08:00
|
|
|
entry.val = std::move(val); // acquire the value
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2014-04-27 02:41:34 +08:00
|
|
|
debug(1, PARSE_ERR, msg);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2014-04-27 02:41:34 +08:00
|
|
|
debug(1, PARSE_ERR, msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Maximum length of hostname. Longer hostnames are truncated.
|
2014-04-26 08:44:49 +08:00
|
|
|
#define HOSTNAME_LEN 32
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Length of a MAC address.
|
2014-04-26 08:44:49 +08:00
|
|
|
#define MAC_ADDRESS_MAX_LEN 6
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Thanks to Jan Brittenson, http://lists.apple.com/archives/xcode-users/2009/May/msg00062.html
|
2014-04-26 08:44:49 +08:00
|
|
|
#ifdef SIOCGIFHWADDR
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Linux
|
2014-04-26 08:44:49 +08:00
|
|
|
#include <net/if.h>
|
2017-01-27 12:00:43 +08:00
|
|
|
#include <sys/socket.h>
|
2016-04-29 09:55:41 +08:00
|
|
|
static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN],
|
|
|
|
const char *interface = "eth0") {
|
2014-04-26 08:44:49 +08:00
|
|
|
bool result = false;
|
|
|
|
const int dummy = socket(AF_INET, SOCK_STREAM, 0);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (dummy >= 0) {
|
2014-04-26 08:44:49 +08:00
|
|
|
struct ifreq r;
|
|
|
|
strncpy((char *)r.ifr_name, interface, sizeof r.ifr_name - 1);
|
|
|
|
r.ifr_name[sizeof r.ifr_name - 1] = 0;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (ioctl(dummy, SIOCGIFHWADDR, &r) >= 0) {
|
2014-04-26 08:44:49 +08:00
|
|
|
memcpy(macaddr, r.ifr_hwaddr.sa_data, MAC_ADDRESS_MAX_LEN);
|
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
close(dummy);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(HAVE_GETIFADDRS)
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// OS X and BSD
|
2014-04-26 08:44:49 +08:00
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <net/if_dl.h>
|
2017-01-27 12:00:43 +08:00
|
|
|
#include <sys/socket.h>
|
2016-04-29 09:55:41 +08:00
|
|
|
static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN],
|
|
|
|
const char *interface = "en0") {
|
2014-04-26 08:44:49 +08:00
|
|
|
// BSD, Mac OS X
|
|
|
|
struct ifaddrs *ifap;
|
|
|
|
bool ok = false;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-10-30 12:46:11 +08:00
|
|
|
if (getifaddrs(&ifap) != 0) {
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const ifaddrs *p = ifap; p; p = p->ifa_next) {
|
|
|
|
bool is_af_link = p->ifa_addr && p->ifa_addr->sa_family == AF_LINK;
|
|
|
|
if (is_af_link && p->ifa_name && p->ifa_name[0] &&
|
|
|
|
!strcmp((const char *)p->ifa_name, interface)) {
|
|
|
|
const sockaddr_dl &sdl = *reinterpret_cast<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;
|
2014-04-26 08:44:49 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-30 12:46:11 +08:00
|
|
|
freeifaddrs(ifap);
|
2014-04-26 08:44:49 +08:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Unsupported
|
|
|
|
static bool get_mac_address(unsigned char macaddr[MAC_ADDRESS_MAX_LEN]) { return false; }
|
2014-04-26 08:44:49 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Function to get an identifier based on the hostname.
|
|
|
|
static bool get_hostname_identifier(wcstring *result) {
|
2014-04-26 08:44:49 +08:00
|
|
|
bool success = false;
|
|
|
|
char hostname[HOSTNAME_LEN + 1] = {};
|
2016-04-29 09:55:41 +08:00
|
|
|
if (gethostname(hostname, HOSTNAME_LEN) == 0) {
|
2014-11-10 16:06:25 +08:00
|
|
|
result->assign(str2wcstring(hostname));
|
2014-04-26 08:44:49 +08:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// 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() {
|
2014-11-10 16:06:25 +08:00
|
|
|
wcstring result;
|
2014-04-26 08:44:49 +08:00
|
|
|
unsigned char mac_addr[MAC_ADDRESS_MAX_LEN] = {};
|
2016-04-29 09:55:41 +08:00
|
|
|
if (get_mac_address(mac_addr)) {
|
2014-04-26 08:44:49 +08:00
|
|
|
result.reserve(2 * MAC_ADDRESS_MAX_LEN);
|
2016-04-29 09:55:41 +08:00
|
|
|
for (size_t i = 0; i < MAC_ADDRESS_MAX_LEN; i++) {
|
2014-11-10 16:06:25 +08:00
|
|
|
append_format(result, L"%02x", mac_addr[i]);
|
2014-04-26 08:44:49 +08:00
|
|
|
}
|
2016-11-02 11:42:02 +08:00
|
|
|
} else if (!get_hostname_identifier(&result)) {
|
|
|
|
result.assign(L"nohost"); // fallback to a dummy value
|
2014-04-26 08:44:49 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
class universal_notifier_shmem_poller_t : public universal_notifier_t {
|
2016-12-26 12:53:59 +08:00
|
|
|
#ifdef __CYGWIN__
|
2016-04-29 09:55:41 +08:00
|
|
|
// This is what our shared memory looks like. Everything here is stored in network byte order
|
|
|
|
// (big-endian).
|
|
|
|
struct universal_notifier_shmem_t {
|
2014-04-30 05:14:50 +08:00
|
|
|
uint32_t magic;
|
|
|
|
uint32_t version;
|
|
|
|
uint32_t universal_variable_seed;
|
|
|
|
};
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-04-30 05:14:50 +08:00
|
|
|
#define SHMEM_MAGIC_NUMBER 0xF154
|
|
|
|
#define SHMEM_VERSION_CURRENT 1000
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
private:
|
2014-04-30 08:03:00 +08:00
|
|
|
long long last_change_time;
|
2014-04-30 05:14:50 +08:00
|
|
|
uint32_t last_seed;
|
|
|
|
volatile universal_notifier_shmem_t *region;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
void open_shmem() {
|
2014-04-30 05:14:50 +08:00
|
|
|
assert(region == NULL);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Use a path based on our uid to avoid collisions.
|
2014-04-30 05:14:50 +08:00
|
|
|
char path[NAME_MAX];
|
2016-04-29 09:55:41 +08:00
|
|
|
snprintf(path, sizeof path, "/%ls_shmem_%d", program_name ? program_name : L"fish",
|
|
|
|
getuid());
|
|
|
|
|
2014-04-30 05:14:50 +08:00
|
|
|
bool errored = false;
|
|
|
|
int fd = shm_open(path, O_RDWR | O_CREAT, 0600);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (fd < 0) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to open shared memory with path '%s': %s"), path, error);
|
2014-04-30 05:14:50 +08:00
|
|
|
errored = true;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Get the size.
|
2016-06-12 18:39:36 +08:00
|
|
|
off_t size = 0;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (!errored) {
|
2014-04-30 05:14:50 +08:00
|
|
|
struct stat buf = {};
|
2016-04-29 09:55:41 +08:00
|
|
|
if (fstat(fd, &buf) < 0) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to fstat shared memory object with path '%s': %s"), path,
|
|
|
|
error);
|
2014-04-30 05:14:50 +08:00
|
|
|
errored = true;
|
|
|
|
}
|
|
|
|
size = buf.st_size;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Set the size, if it's too small.
|
2016-10-23 02:21:13 +08:00
|
|
|
bool set_size = !errored && size < (off_t)sizeof(universal_notifier_shmem_t);
|
|
|
|
if (set_size && ftruncate(fd, sizeof(universal_notifier_shmem_t)) < 0) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to truncate shared memory object with path '%s': %s"), path, error);
|
2016-10-23 02:21:13 +08:00
|
|
|
errored = true;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Memory map the region.
|
|
|
|
if (!errored) {
|
|
|
|
void *addr = mmap(NULL, sizeof(universal_notifier_shmem_t), PROT_READ | PROT_WRITE,
|
2016-08-30 21:43:25 +08:00
|
|
|
MAP_SHARED, fd, 0);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (addr == MAP_FAILED) {
|
2016-11-27 08:35:48 +08:00
|
|
|
const char *error = strerror(errno);
|
|
|
|
debug(0, _(L"Unable to memory map shared memory object with path '%s': %s"), path,
|
|
|
|
error);
|
2016-02-20 07:45:12 +08:00
|
|
|
this->region = NULL;
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
|
|
|
this->region = static_cast<universal_notifier_shmem_t *>(addr);
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Close the fd, even if the mapping succeeded.
|
|
|
|
if (fd >= 0) {
|
2014-04-30 05:14:50 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Read the current seed.
|
2014-04-30 05:14:50 +08:00
|
|
|
this->poll();
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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) {
|
2014-04-30 05:14:50 +08:00
|
|
|
/* Read off the seed */
|
2016-11-02 12:19:34 +08:00
|
|
|
uint32_t seed = ntohl(region->universal_variable_seed); //!OCLINT(constant cond op)
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Increment it. Don't let it wrap to zero.
|
|
|
|
do {
|
2014-04-30 05:14:50 +08:00
|
|
|
seed++;
|
2016-04-29 09:55:41 +08:00
|
|
|
} while (seed == 0);
|
2014-04-30 05:14:50 +08:00
|
|
|
last_seed = seed;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Write out our data.
|
2016-11-02 12:19:34 +08:00
|
|
|
region->magic = htonl(SHMEM_MAGIC_NUMBER); //!OCLINT(constant cond op)
|
|
|
|
region->version = htonl(SHMEM_VERSION_CURRENT); //!OCLINT(constant cond op)
|
|
|
|
region->universal_variable_seed = htonl(seed); //!OCLINT(constant cond op)
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
universal_notifier_shmem_poller_t() : last_change_time(0), last_seed(0), region(NULL) {
|
2014-04-30 05:14:50 +08:00
|
|
|
open_shmem();
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
~universal_notifier_shmem_poller_t() {
|
|
|
|
if (region != NULL) {
|
2014-04-30 05:14:50 +08:00
|
|
|
// Behold: C++ in all its glory!
|
|
|
|
void *address = const_cast<void *>(static_cast<volatile void *>(region));
|
2016-04-29 09:55:41 +08:00
|
|
|
if (munmap(address, sizeof(universal_notifier_shmem_t)) < 0) {
|
2014-04-30 05:14:50 +08:00
|
|
|
wperror(L"munmap");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
bool poll() {
|
2014-04-30 05:14:50 +08:00
|
|
|
bool result = false;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (region != NULL) {
|
2016-11-02 12:19:34 +08:00
|
|
|
uint32_t seed = ntohl(region->universal_variable_seed); //!OCLINT(constant cond op)
|
2016-04-29 09:55:41 +08:00
|
|
|
if (seed != last_seed) {
|
2014-04-30 05:14:50 +08:00
|
|
|
result = true;
|
|
|
|
last_seed = seed;
|
2014-04-30 08:03:00 +08:00
|
|
|
last_change_time = get_time();
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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.
|
2016-10-10 05:36:08 +08:00
|
|
|
long long usec_per_sec = 1000000;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (get_time() - last_change_time < 5LL * usec_per_sec) {
|
|
|
|
return usec_per_sec / 10; // 10 times a second
|
2014-04-30 08:03:00 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
return usec_per_sec / 3; // 3 times a second
|
2014-04-30 08:03:00 +08:00
|
|
|
}
|
2016-12-26 12:53:59 +08:00
|
|
|
#else // this class isn't valid on this system
|
|
|
|
public:
|
|
|
|
universal_notifier_shmem_poller_t() {
|
|
|
|
DIE("universal_notifier_shmem_poller_t cannot be used on this system");
|
|
|
|
}
|
2016-11-27 05:08:14 +08:00
|
|
|
#endif
|
2016-12-26 12:53:59 +08:00
|
|
|
};
|
2014-05-01 06:50:03 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// A notifyd-based notifier. Very straightforward.
|
|
|
|
class universal_notifier_notifyd_t : public universal_notifier_t {
|
2016-12-26 12:53:59 +08:00
|
|
|
#if FISH_NOTIFYD_AVAILABLE
|
2014-05-01 06:50:03 +08:00
|
|
|
int notify_fd;
|
|
|
|
int token;
|
|
|
|
std::string name;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
void setup_notifyd() {
|
|
|
|
// Per notify(3), the user.uid.%d style is only accessible to processes with that uid.
|
2014-05-01 06:50:03 +08:00
|
|
|
char local_name[256];
|
2016-04-29 09:55:41 +08:00
|
|
|
snprintf(local_name, sizeof local_name, "user.uid.%d.%ls.uvars", getuid(),
|
|
|
|
program_name ? program_name : L"fish");
|
2014-05-01 06:50:03 +08:00
|
|
|
name.assign(local_name);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
uint32_t status =
|
|
|
|
notify_register_file_descriptor(name.c_str(), &this->notify_fd, 0, &this->token);
|
|
|
|
if (status != NOTIFY_STATUS_OK) {
|
2017-01-03 13:11:53 +08:00
|
|
|
debug(1, "notify_register_file_descriptor() failed with status %u.", status);
|
|
|
|
debug(1, "Universal variable notifications may not be received.");
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
if (this->notify_fd >= 0) {
|
|
|
|
// Mark us for non-blocking reads, and CLO_EXEC.
|
2014-05-01 06:50:03 +08:00
|
|
|
int flags = fcntl(this->notify_fd, F_GETFL, 0);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (flags >= 0 && !(flags & O_NONBLOCK)) {
|
2015-01-04 17:21:23 +08:00
|
|
|
fcntl(this->notify_fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2015-01-04 17:21:23 +08:00
|
|
|
set_cloexec(this->notify_fd);
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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.
|
2015-01-04 17:21:23 +08:00
|
|
|
set_cloexec(this->notify_fd + 1);
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
universal_notifier_notifyd_t() : notify_fd(-1), token(-1 /* NOTIFY_TOKEN_INVALID */) {
|
2014-05-01 06:50:03 +08:00
|
|
|
setup_notifyd();
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
~universal_notifier_notifyd_t() {
|
|
|
|
if (token != -1 /* NOTIFY_TOKEN_INVALID */) {
|
2014-05-01 06:50:03 +08:00
|
|
|
notify_cancel(token);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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.
|
2014-05-01 06:50:03 +08:00
|
|
|
assert(fd == notify_fd);
|
2014-05-02 07:44:37 +08:00
|
|
|
bool read_something = false;
|
2014-05-01 06:50:03 +08:00
|
|
|
unsigned char buff[64];
|
|
|
|
ssize_t amt_read;
|
2016-04-29 09:55:41 +08:00
|
|
|
do {
|
2014-05-01 06:50:03 +08:00
|
|
|
amt_read = read(notify_fd, buff, sizeof buff);
|
2014-05-02 07:44:37 +08:00
|
|
|
read_something = (read_something || amt_read > 0);
|
2014-05-01 06:50:03 +08:00
|
|
|
} while (amt_read == sizeof buff);
|
2014-05-02 07:44:37 +08:00
|
|
|
return read_something;
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
void post_notification() {
|
2014-05-01 06:50:03 +08:00
|
|
|
uint32_t status = notify_post(name.c_str());
|
2016-04-29 09:55:41 +08:00
|
|
|
if (status != NOTIFY_STATUS_OK) {
|
2016-12-26 12:53:59 +08:00
|
|
|
debug(1, "notify_post() failed with status %u. Uvar notifications may not be sent.",
|
|
|
|
status);
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2016-12-26 12:53:59 +08:00
|
|
|
#else // this class isn't valid on this system
|
|
|
|
public:
|
|
|
|
universal_notifier_notifyd_t() {
|
|
|
|
DIE("universal_notifier_notifyd_t cannot be used on this system");
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-30 05:14:50 +08:00
|
|
|
};
|
|
|
|
|
2017-01-03 13:11:53 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(__CYGWIN__)
|
|
|
|
#define NAMED_PIPE_FLASH_DURATION_USEC (1e5)
|
|
|
|
#define SUSTAINED_READABILITY_CLEANUP_DURATION_USEC (5 * 1e6)
|
|
|
|
#endif
|
2014-05-06 14:33:05 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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 {
|
2016-12-26 12:53:59 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(__CYGWIN__)
|
2014-05-05 06:06:40 +08:00
|
|
|
int pipe_fd;
|
2014-05-06 14:33:05 +08:00
|
|
|
long long readback_time_usec;
|
|
|
|
size_t readback_amount;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-05-07 05:10:55 +08:00
|
|
|
bool polling_due_to_readable_fd;
|
2014-05-06 14:33:05 +08:00
|
|
|
long long drain_if_still_readable_time_usec;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-12-21 14:04:00 +08:00
|
|
|
void make_pipe(const wchar_t *test_path);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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!
|
2014-05-06 14:33:05 +08:00
|
|
|
size_t read_amt = 64 * 1024;
|
|
|
|
void *buff = malloc(read_amt);
|
2017-07-26 12:34:22 +08:00
|
|
|
(void)read(this->pipe_fd, buff, read_amt);
|
2014-05-06 14:33:05 +08:00
|
|
|
free(buff);
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
public:
|
2016-12-26 12:53:59 +08:00
|
|
|
universal_notifier_named_pipe_t(const wchar_t *test_path)
|
2016-04-29 09:55:41 +08:00
|
|
|
: pipe_fd(-1),
|
|
|
|
readback_time_usec(0),
|
|
|
|
readback_amount(0),
|
|
|
|
polling_due_to_readable_fd(false),
|
|
|
|
drain_if_still_readable_time_usec(0) {
|
2014-05-05 06:06:40 +08:00
|
|
|
make_pipe(test_path);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
~universal_notifier_named_pipe_t() {
|
|
|
|
if (pipe_fd >= 0) {
|
2014-05-06 14:33:05 +08:00
|
|
|
close(pipe_fd);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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.
|
2014-05-07 05:10:55 +08:00
|
|
|
return -1;
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2016-05-05 06:19:47 +08:00
|
|
|
// We are not in polling mode. Return the fd so it can be watched.
|
|
|
|
return pipe_fd;
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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!)
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(fd);
|
2014-05-07 05:10:55 +08:00
|
|
|
bool should_sync = false;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (readback_time_usec == 0) {
|
2014-05-07 05:10:55 +08:00
|
|
|
polling_due_to_readable_fd = true;
|
2016-04-29 09:55:41 +08:00
|
|
|
drain_if_still_readable_time_usec =
|
|
|
|
get_time() + SUSTAINED_READABILITY_CLEANUP_DURATION_USEC;
|
2014-05-07 05:10:55 +08:00
|
|
|
should_sync = true;
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2014-05-07 05:10:55 +08:00
|
|
|
return should_sync;
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
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.
|
2016-11-02 12:19:34 +08:00
|
|
|
int pid_nbo = htonl(getpid()); //!OCLINT(constant cond op)
|
2014-05-06 14:33:05 +08:00
|
|
|
ssize_t amt_written = write(this->pipe_fd, &pid_nbo, sizeof pid_nbo);
|
2016-10-23 12:14:01 +08:00
|
|
|
if (amt_written < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
|
2016-10-23 02:21:13 +08:00
|
|
|
// Very unsual: the pipe is full!
|
|
|
|
drain_excessive_data();
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Now schedule a read for some time in the future.
|
2014-05-07 05:10:55 +08:00
|
|
|
this->readback_time_usec = get_time() + NAMED_PIPE_FLASH_DURATION_USEC;
|
|
|
|
this->readback_amount += sizeof pid_nbo;
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
unsigned long usec_delay_between_polls() const {
|
2014-05-07 05:10:55 +08:00
|
|
|
unsigned long readback_delay = ULONG_MAX;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (this->readback_time_usec > 0) {
|
2014-05-06 14:33:05 +08:00
|
|
|
// How long until the readback?
|
|
|
|
long long now = get_time();
|
2016-04-29 09:55:41 +08:00
|
|
|
if (now >= this->readback_time_usec) {
|
2014-05-06 14:33:05 +08:00
|
|
|
// Oops, it already passed! Return something tiny.
|
2014-05-07 05:10:55 +08:00
|
|
|
readback_delay = 1000;
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2014-05-07 05:10:55 +08:00
|
|
|
readback_delay = (unsigned long)(this->readback_time_usec - now);
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2014-05-07 05:10:55 +08:00
|
|
|
unsigned long polling_delay = ULONG_MAX;
|
2016-04-29 09:55:41 +08:00
|
|
|
if (polling_due_to_readable_fd) {
|
|
|
|
// We're in polling mode. Don't return a value less than our polling interval.
|
2014-05-07 05:10:55 +08:00
|
|
|
polling_delay = NAMED_PIPE_FLASH_DURATION_USEC;
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// 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.
|
2014-05-07 05:10:55 +08:00
|
|
|
unsigned long result = mini(readback_delay, polling_delay);
|
2016-06-12 18:39:36 +08:00
|
|
|
if (result == ULONG_MAX) {
|
2014-05-07 05:10:55 +08:00
|
|
|
result = 0;
|
|
|
|
}
|
2014-05-06 14:33:05 +08:00
|
|
|
return result;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
bool poll() {
|
|
|
|
// Check if we are past the readback time.
|
|
|
|
if (this->readback_time_usec > 0 && get_time() >= this->readback_time_usec) {
|
2014-05-06 14:33:05 +08:00
|
|
|
// Read back what we wrote. We do nothing with the value.
|
2016-04-29 09:55:41 +08:00
|
|
|
while (this->readback_amount > 0) {
|
2014-05-06 14:33:05 +08:00
|
|
|
char buff[64];
|
|
|
|
size_t amt_to_read = mini(this->readback_amount, sizeof buff);
|
2017-07-26 12:34:22 +08:00
|
|
|
(void)read(this->pipe_fd, buff, amt_to_read);
|
2014-05-06 14:33:05 +08:00
|
|
|
this->readback_amount -= amt_to_read;
|
|
|
|
}
|
|
|
|
assert(this->readback_amount == 0);
|
|
|
|
this->readback_time_usec = 0;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Check to see if we are doing readability polling.
|
2016-10-30 12:46:11 +08:00
|
|
|
if (!polling_due_to_readable_fd || pipe_fd < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2016-10-30 12:46:11 +08:00
|
|
|
// We are polling, so we are definitely going to sync.
|
|
|
|
// 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();
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-30 12:46:11 +08:00
|
|
|
return true;
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2016-12-26 12:53:59 +08:00
|
|
|
#else // this class isn't valid on this system
|
|
|
|
public:
|
|
|
|
universal_notifier_named_pipe_t(const wchar_t *test_path) {
|
2017-01-03 13:11:53 +08:00
|
|
|
static_cast<void>(test_path);
|
2016-12-26 12:53:59 +08:00
|
|
|
DIE("universal_notifier_named_pipe_t cannot be used on this system");
|
2014-05-08 05:22:05 +08:00
|
|
|
}
|
2016-12-26 12:53:59 +08:00
|
|
|
#endif
|
|
|
|
};
|
2014-05-08 05:22:05 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
universal_notifier_t::notifier_strategy_t universal_notifier_t::resolve_default_strategy() {
|
2014-05-01 06:50:03 +08:00
|
|
|
#if FISH_NOTIFYD_AVAILABLE
|
|
|
|
return strategy_notifyd;
|
2015-08-24 18:45:57 +08:00
|
|
|
#elif defined(__CYGWIN__)
|
|
|
|
return strategy_shmem_polling;
|
2014-05-01 06:50:03 +08:00
|
|
|
#else
|
2014-05-05 06:06:40 +08:00
|
|
|
return strategy_named_pipe;
|
2014-05-01 06:50:03 +08:00
|
|
|
#endif
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
universal_notifier_t &universal_notifier_t::default_notifier() {
|
2017-01-22 08:56:45 +08:00
|
|
|
static std::unique_ptr<universal_notifier_t> result =
|
2016-12-26 12:53:59 +08:00
|
|
|
new_notifier_for_strategy(universal_notifier_t::resolve_default_strategy());
|
2014-04-30 05:14:50 +08:00
|
|
|
return *result;
|
|
|
|
}
|
|
|
|
|
2017-01-22 08:56:45 +08:00
|
|
|
std::unique_ptr<universal_notifier_t> universal_notifier_t::new_notifier_for_strategy(
|
2016-04-29 09:55:41 +08:00
|
|
|
universal_notifier_t::notifier_strategy_t strat, const wchar_t *test_path) {
|
|
|
|
switch (strat) {
|
|
|
|
case strategy_notifyd: {
|
2017-01-22 08:56:45 +08:00
|
|
|
return make_unique<universal_notifier_notifyd_t>();
|
2016-04-29 09:55:41 +08:00
|
|
|
}
|
2016-12-26 12:53:59 +08:00
|
|
|
case strategy_shmem_polling: {
|
2017-01-22 08:56:45 +08:00
|
|
|
return make_unique<universal_notifier_shmem_poller_t>();
|
2016-12-26 12:53:59 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
case strategy_named_pipe: {
|
2017-01-22 08:56:45 +08:00
|
|
|
return make_unique<universal_notifier_named_pipe_t>(test_path);
|
2016-04-29 09:55:41 +08:00
|
|
|
}
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
2017-05-12 05:27:40 +08:00
|
|
|
DIE("should never reach this statement");
|
|
|
|
return NULL;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Default implementations.
|
|
|
|
universal_notifier_t::universal_notifier_t() {}
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
universal_notifier_t::~universal_notifier_t() {}
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
int universal_notifier_t::notification_fd() { return -1; }
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
void universal_notifier_t::post_notification() {}
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
bool universal_notifier_t::poll() { return false; }
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
unsigned long universal_notifier_t::usec_delay_between_polls() const { return 0; }
|
2014-04-30 08:03:00 +08:00
|
|
|
|
2016-10-10 05:38:26 +08:00
|
|
|
bool universal_notifier_t::notification_fd_became_readable(int fd) {
|
|
|
|
UNUSED(fd);
|
|
|
|
return false;
|
|
|
|
}
|
2016-12-21 14:04:00 +08:00
|
|
|
|
2016-12-26 12:53:59 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(__CYGWIN__)
|
2016-12-21 14:04:00 +08:00
|
|
|
void universal_notifier_named_pipe_t::make_pipe(const wchar_t *test_path) {
|
|
|
|
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 mkfifo_status = mkfifo(narrow_path.c_str(), 0600);
|
|
|
|
if (mkfifo_status == -1 && errno != EEXIST) {
|
|
|
|
const char *error = strerror(errno);
|
|
|
|
const wchar_t *errmsg = _(L"Unable to make a pipe for universal variables using '%ls': %s");
|
|
|
|
debug(0, errmsg, vars_path.c_str(), error);
|
|
|
|
pipe_fd = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = wopen_cloexec(vars_path, O_RDWR | O_NONBLOCK, 0600);
|
|
|
|
if (fd < 0) {
|
|
|
|
const char *error = strerror(errno);
|
|
|
|
const wchar_t *errmsg = _(L"Unable to open a pipe for universal variables using '%ls': %s");
|
|
|
|
debug(0, errmsg, vars_path.c_str(), error);
|
|
|
|
pipe_fd = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_fd = fd;
|
|
|
|
}
|
2016-12-26 12:53:59 +08:00
|
|
|
#endif
|