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>
|
2022-08-21 14:14:48 +08:00
|
|
|
#include <unistd.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.
|
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
|
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
|
2022-08-21 14:14:48 +08:00
|
|
|
#if !defined(__APPLE__) && !defined(__CYGWIN__)
|
|
|
|
#include <pwd.h>
|
|
|
|
#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
|
2017-02-13 12:24:22 +08:00
|
|
|
|
2022-08-21 14:14:48 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
2022-08-22 05:51:33 +08:00
|
|
|
#include <cstring>
|
2019-10-14 06:50:48 +08:00
|
|
|
#include <cwchar>
|
2022-08-21 14:14:48 +08:00
|
|
|
#include <functional>
|
2016-12-16 13:50:27 +08:00
|
|
|
#include <string>
|
2022-08-21 14:14:48 +08:00
|
|
|
#include <unordered_map>
|
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
|
2023-01-15 06:56:24 +08:00
|
|
|
#include "fd_readable_set.rs.h"
|
2019-05-28 06:56:53 +08:00
|
|
|
#include "flog.h"
|
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
|
2018-03-10 04:52:29 +08:00
|
|
|
#include "wcstringutil.h"
|
2019-05-05 18:09:25 +08:00
|
|
|
#include "wutil.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2020-04-05 10:15:08 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#define FISH_NOTIFYD_AVAILABLE
|
2014-05-01 06:50:03 +08:00
|
|
|
#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
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Error message.
|
2022-09-21 02:58:37 +08:00
|
|
|
#define PARSE_ERR L"Unable to parse universal variable message: '%ls'"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Small note about not editing ~/.fishd manually. Inserted at the top of all .fishd files.
|
2022-09-21 02:58:37 +08:00
|
|
|
#define SAVE_MSG "# This file contains fish universal variable definitions.\n"
|
2014-04-28 04:34:51 +08:00
|
|
|
|
2018-10-21 05:38:49 +08:00
|
|
|
/// Version for fish 3.0
|
|
|
|
#define UVARS_VERSION_3_0 "3.0"
|
|
|
|
|
2018-10-21 09:12:01 +08:00
|
|
|
// Maximum file size we'll read.
|
2022-09-21 02:58:37 +08:00
|
|
|
static constexpr size_t k_max_read_size = 16 * 1024 * 1024;
|
2018-10-21 09:12:01 +08:00
|
|
|
|
2018-10-21 09:19:30 +08:00
|
|
|
// Fields used in fish 2.x uvars.
|
|
|
|
namespace fish2x_uvars {
|
|
|
|
namespace {
|
|
|
|
constexpr const char *SET = "SET";
|
|
|
|
constexpr const char *SET_EXPORT = "SET_EXPORT";
|
|
|
|
} // namespace
|
|
|
|
} // namespace fish2x_uvars
|
|
|
|
|
2018-10-21 08:20:49 +08:00
|
|
|
// Fields used in fish 3.0 uvars
|
|
|
|
namespace fish3_uvars {
|
|
|
|
namespace {
|
2018-10-21 09:19:30 +08:00
|
|
|
constexpr const char *SETUVAR = "SETUVAR";
|
|
|
|
constexpr const char *EXPORT = "--export";
|
|
|
|
constexpr const char *PATH = "--path";
|
2018-10-21 08:20:49 +08:00
|
|
|
} // namespace
|
|
|
|
} // namespace fish3_uvars
|
|
|
|
|
2018-10-21 04:16:14 +08:00
|
|
|
/// The different types of messages found in the fishd file.
|
|
|
|
enum class uvar_message_type_t { set, set_export };
|
|
|
|
|
2018-04-02 08:43:12 +08:00
|
|
|
static maybe_t<wcstring> default_vars_path_directory() {
|
2016-11-27 08:35:48 +08:00
|
|
|
wcstring path;
|
2018-04-02 08:43:12 +08:00
|
|
|
if (!path_get_config(path)) return none();
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2021-05-10 08:40:02 +08:00
|
|
|
/// \return the default variable path, or an empty string on failure.
|
|
|
|
static wcstring default_vars_path() {
|
2018-04-02 08:43:12 +08:00
|
|
|
if (auto path = default_vars_path_directory()) {
|
2018-10-22 02:12:11 +08:00
|
|
|
path->append(L"/fish_variables");
|
2021-05-10 08:40:02 +08:00
|
|
|
return path.acquire();
|
2018-04-02 08:43:12 +08:00
|
|
|
}
|
2021-05-10 08:40:02 +08:00
|
|
|
return wcstring{};
|
2014-11-10 08:55:02 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Test if the message msg contains the command cmd.
|
2018-10-21 08:20:49 +08:00
|
|
|
/// On success, updates the cursor to just past the command.
|
2018-10-21 09:19:30 +08:00
|
|
|
static bool match(const wchar_t **inout_cursor, const char *cmd) {
|
2018-10-21 08:20:49 +08:00
|
|
|
const wchar_t *cursor = *inout_cursor;
|
2019-03-13 06:07:07 +08:00
|
|
|
size_t len = std::strlen(cmd);
|
2018-10-21 09:19:30 +08:00
|
|
|
if (!std::equal(cmd, cmd + len, cursor)) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-21 08:20:49 +08:00
|
|
|
if (cursor[len] && cursor[len] != L' ' && cursor[len] != L'\t') return false;
|
|
|
|
*inout_cursor = cursor + len;
|
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
|
|
|
|
2019-03-13 05:06:01 +08:00
|
|
|
return iswalnum(c) || std::wcschr(L"/_", c);
|
2012-10-09 05:47:25 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Escape specified string.
|
2018-10-21 01:22:53 +08:00
|
|
|
static wcstring full_escape(const wcstring &in) {
|
2012-11-19 08:30:30 +08:00
|
|
|
wcstring out;
|
2018-10-21 01:22:53 +08:00
|
|
|
for (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);
|
2019-11-19 09:08:16 +08:00
|
|
|
} else if (c <= static_cast<wchar_t>(ASCII_MAX)) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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.
|
2018-10-21 09:21:05 +08:00
|
|
|
static bool append_file_entry(env_var_t::env_var_flags_t flags, const wcstring &key_in,
|
2016-04-29 09:55:41 +08:00
|
|
|
const wcstring &val_in, std::string *result, std::string *storage) {
|
2018-10-21 09:21:05 +08:00
|
|
|
namespace f3 = fish3_uvars;
|
2019-11-19 10:34:50 +08:00
|
|
|
assert(storage != nullptr);
|
|
|
|
assert(result != nullptr);
|
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
|
|
|
|
2018-10-21 09:21:05 +08:00
|
|
|
// Append SETVAR header.
|
|
|
|
result->append(f3::SETUVAR);
|
2014-06-10 03:57:44 +08:00
|
|
|
result->push_back(' ');
|
|
|
|
|
2018-10-21 09:21:05 +08:00
|
|
|
// Append flags.
|
|
|
|
if (flags & env_var_t::flag_export) {
|
|
|
|
result->append(f3::EXPORT);
|
|
|
|
result->push_back(' ');
|
|
|
|
}
|
|
|
|
if (flags & env_var_t::flag_pathvar) {
|
|
|
|
result->append(f3::PATH);
|
|
|
|
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)) {
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, L"Illegal variable name: '%ls'", key_in.c_str());
|
2014-06-10 03:57:44 +08:00
|
|
|
success = false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
if (success && !append_utf8(key_in, result, storage)) {
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, L"Could not convert %ls to narrow character string", key_in.c_str());
|
2014-06-10 03:57:44 +08:00
|
|
|
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.
|
2018-10-21 01:22:53 +08:00
|
|
|
if (success && !append_utf8(full_escape(val_in), result, storage)) {
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, L"Could not convert %ls to narrow character string", val_in.c_str());
|
2014-06-10 03:57:44 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-03-25 14:32:55 +08:00
|
|
|
/// Encoding of a null string.
|
2022-09-21 02:58:37 +08:00
|
|
|
static const wchar_t *const ENV_NULL = L"\x1d";
|
2018-03-25 14:32:55 +08:00
|
|
|
|
2018-09-10 09:32:15 +08:00
|
|
|
/// Character used to separate arrays in universal variables file.
|
|
|
|
/// This is 30, the ASCII record separator.
|
2022-09-21 02:58:37 +08:00
|
|
|
static const wchar_t UVAR_ARRAY_SEP = 0x1e;
|
2018-09-10 09:32:15 +08:00
|
|
|
|
2018-03-25 14:32:55 +08:00
|
|
|
/// Decode a serialized universal variable value into a list.
|
|
|
|
static wcstring_list_t decode_serialized(const wcstring &val) {
|
|
|
|
if (val == ENV_NULL) return {};
|
2018-09-10 09:32:15 +08:00
|
|
|
return split_string(val, UVAR_ARRAY_SEP);
|
2018-03-25 14:32:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Decode a a list into a serialized universal variable value.
|
|
|
|
static wcstring encode_serialized(const wcstring_list_t &vals) {
|
|
|
|
if (vals.empty()) return ENV_NULL;
|
2018-09-10 09:32:15 +08:00
|
|
|
return join_strings(vals, UVAR_ARRAY_SEP);
|
2018-03-25 14:32:55 +08:00
|
|
|
}
|
|
|
|
|
2017-08-28 15:25:41 +08:00
|
|
|
maybe_t<env_var_t> env_universal_t::get(const wcstring &name) const {
|
2020-04-03 07:04:04 +08:00
|
|
|
auto where = vars.find(name);
|
2017-08-11 12:11:47 +08:00
|
|
|
if (where != vars.end()) return where->second;
|
2017-08-28 15:25:41 +08:00
|
|
|
return none();
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
|
2018-10-22 15:49:09 +08:00
|
|
|
maybe_t<env_var_t::env_var_flags_t> env_universal_t::get_flags(const wcstring &name) const {
|
2020-04-03 07:04:04 +08:00
|
|
|
auto where = vars.find(name);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (where != vars.end()) {
|
2018-10-22 15:49:09 +08:00
|
|
|
return where->second.get_flags();
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
2018-10-22 15:49:09 +08:00
|
|
|
return none();
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 05:13:16 +08:00
|
|
|
void env_universal_t::set(const wcstring &key, const env_var_t &var) {
|
2019-12-11 18:08:19 +08:00
|
|
|
bool new_entry = vars.count(key) == 0;
|
2017-08-10 02:11:58 +08:00
|
|
|
env_var_t &entry = vars[key];
|
2019-12-11 18:08:19 +08:00
|
|
|
if (new_entry || entry != var) {
|
2018-10-22 15:49:09 +08:00
|
|
|
entry = var;
|
2019-06-10 04:48:07 +08:00
|
|
|
this->modified.insert(key);
|
|
|
|
if (entry.exports()) export_generation += 1;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2014-04-26 07:09:26 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 05:13:16 +08:00
|
|
|
bool env_universal_t::remove(const wcstring &key) {
|
2019-06-10 04:48:07 +08:00
|
|
|
auto iter = this->vars.find(key);
|
|
|
|
if (iter != this->vars.end()) {
|
|
|
|
if (iter->second.exports()) export_generation += 1;
|
|
|
|
this->vars.erase(iter);
|
2014-04-28 04:34:51 +08:00
|
|
|
this->modified.insert(key);
|
2019-06-10 04:48:07 +08:00
|
|
|
return true;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2019-06-10 04:48:07 +08:00
|
|
|
return false;
|
2014-04-26 07:09:26 +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;
|
2019-07-22 03:44:07 +08:00
|
|
|
for (const auto &kv : vars) {
|
|
|
|
const wcstring &key = kv.first;
|
|
|
|
const env_var_t &var = kv.second;
|
2018-01-31 04:36:50 +08:00
|
|
|
if ((var.exports() && show_exported) || (!var.exports() && 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
|
2019-06-10 04:48:07 +08:00
|
|
|
// new vars. Update our exports generation.
|
|
|
|
void env_universal_t::generate_callbacks_and_update_exports(const var_table_t &new_vars,
|
|
|
|
callback_data_list_t &callbacks) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Construct callbacks for erased values.
|
2019-06-10 04:48:07 +08:00
|
|
|
for (const auto &kv : this->vars) {
|
|
|
|
const wcstring &key = kv.first;
|
2016-04-29 09:55:41 +08:00
|
|
|
// Skip modified values.
|
2019-06-10 04:48:07 +08:00
|
|
|
if (this->modified.count(key)) {
|
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.
|
2019-06-10 04:48:07 +08:00
|
|
|
if (new_vars.count(key) == 0) {
|
2018-10-21 04:16:14 +08:00
|
|
|
callbacks.push_back(callback_data_t(key, none()));
|
2019-06-10 04:48:07 +08:00
|
|
|
if (kv.second.exports()) export_generation += 1;
|
2014-06-17 03:25:33 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Construct callbacks for newly inserted or changed values.
|
2019-06-10 04:48:07 +08:00
|
|
|
for (const auto &kv : new_vars) {
|
|
|
|
const wcstring &key = kv.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.
|
2019-06-10 04:48:07 +08:00
|
|
|
const env_var_t &new_entry = kv.second;
|
2014-06-17 03:25:33 +08:00
|
|
|
var_table_t::const_iterator existing = this->vars.find(key);
|
2019-06-10 04:48:07 +08:00
|
|
|
|
|
|
|
bool old_exports = (existing != this->vars.end() && existing->second.exports());
|
|
|
|
bool export_changed = (old_exports != new_entry.exports());
|
2020-02-17 01:51:19 +08:00
|
|
|
bool value_changed = existing != this->vars.end() && existing->second != new_entry;
|
|
|
|
if (export_changed || value_changed) {
|
2019-06-10 04:48:07 +08:00
|
|
|
export_generation += 1;
|
|
|
|
}
|
2020-02-17 01:51:19 +08:00
|
|
|
if (existing == this->vars.end() || export_changed || value_changed) {
|
|
|
|
// Value is set for the first time, or has changed.
|
2021-11-15 09:39:52 +08:00
|
|
|
callbacks.push_back(callback_data_t(key, new_entry));
|
2014-06-17 03:25:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 08:40:02 +08:00
|
|
|
void env_universal_t::acquire_variables(var_table_t &&vars_to_acquire) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Copy modified values from existing vars to vars_to_acquire.
|
2017-08-20 04:29:52 +08:00
|
|
|
for (const auto &key : this->modified) {
|
2020-04-03 07:04:04 +08:00
|
|
|
auto 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. */
|
2017-08-10 02:11:58 +08:00
|
|
|
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.
|
2017-08-10 02:11:58 +08:00
|
|
|
env_var_t &src = src_iter->second;
|
|
|
|
env_var_t &dst = vars_to_acquire[key];
|
2017-08-06 06:08:39 +08:00
|
|
|
dst = src;
|
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-08-10 02:11:58 +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(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) {
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, L"universal log sync elided based on fstat()");
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
|
|
|
// Read a variables table from the file.
|
2018-10-21 15:31:30 +08:00
|
|
|
var_table_t new_vars;
|
|
|
|
uvar_format_t format = this->read_message_internal(fd, &new_vars);
|
|
|
|
|
|
|
|
// Hacky: if the read format is in the future, avoid overwriting the file: never try to
|
|
|
|
// save.
|
|
|
|
if (format == uvar_format_t::future) {
|
|
|
|
ok_to_save = false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2019-06-10 04:48:07 +08:00
|
|
|
// Announce changes and update our exports generation.
|
|
|
|
this->generate_callbacks_and_update_exports(new_vars, callbacks);
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Acquire the new variables.
|
2021-05-10 08:40:02 +08:00
|
|
|
this->acquire_variables(std::move(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
|
|
|
|
2021-05-10 09:04:39 +08:00
|
|
|
bool env_universal_t::load_from_path(const wcstring &path, callback_data_list_t &callbacks) {
|
2023-04-01 19:50:30 +08:00
|
|
|
return load_from_path(wcs2zstring(path), callbacks);
|
2021-10-14 04:51:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool env_universal_t::load_from_path(const std::string &path, callback_data_list_t &callbacks) {
|
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) {
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, 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;
|
2021-10-14 04:51:18 +08:00
|
|
|
autoclose_fd_t fd{open_cloexec(path, O_RDONLY)};
|
2020-01-30 05:55:10 +08:00
|
|
|
if (fd.valid()) {
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, L"universal log reading from file");
|
2020-01-30 05:55:10 +08:00
|
|
|
this->load_from_fd(fd.fd(), callbacks);
|
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
|
|
|
|
2018-10-21 06:55:46 +08:00
|
|
|
/// Serialize the contents to a string.
|
|
|
|
std::string env_universal_t::serialize_with_vars(const var_table_t &vars) {
|
2014-06-10 03:57:44 +08:00
|
|
|
std::string storage;
|
2018-10-21 09:21:05 +08:00
|
|
|
std::string contents;
|
|
|
|
contents.append(SAVE_MSG);
|
|
|
|
contents.append("# VERSION: " UVARS_VERSION_3_0 "\n");
|
|
|
|
|
2020-06-25 13:46:32 +08:00
|
|
|
// Preserve legacy behavior by sorting the values first
|
2020-09-28 08:36:23 +08:00
|
|
|
using env_pair_t =
|
|
|
|
std::pair<std::reference_wrapper<const wcstring>, std::reference_wrapper<const env_var_t>>;
|
2020-06-25 13:46:32 +08:00
|
|
|
std::vector<env_pair_t> cloned(vars.begin(), vars.end());
|
|
|
|
std::sort(cloned.begin(), cloned.end(), [](const env_pair_t &p1, const env_pair_t &p2) {
|
|
|
|
return p1.first.get() < p2.first.get();
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const auto &kv : cloned) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Append the entry. Note that append_file_entry may fail, but that only affects one
|
|
|
|
// variable; soldier on.
|
2018-10-21 06:55:46 +08:00
|
|
|
const wcstring &key = kv.first;
|
|
|
|
const env_var_t &var = kv.second;
|
2019-05-05 18:09:25 +08:00
|
|
|
append_file_entry(var.get_flags(), key, encode_serialized(var.as_list()), &contents,
|
|
|
|
&storage);
|
2018-10-21 06:55:46 +08:00
|
|
|
}
|
|
|
|
return contents;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-10-21 06:55:46 +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) {
|
|
|
|
assert(fd >= 0);
|
|
|
|
bool success = true;
|
|
|
|
std::string contents = serialize_with_vars(vars);
|
|
|
|
if (write_loop(fd, contents.data(), contents.size()) < 0) {
|
2019-03-13 06:07:07 +08:00
|
|
|
const char *error = std::strerror(errno);
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, _(L"Unable to write to universal variables file '%ls': %s"), path.c_str(),
|
2019-06-04 11:30:48 +08:00
|
|
|
error);
|
2018-10-21 06:55:46 +08:00
|
|
|
success = false;
|
2014-06-10 03:57:44 +08:00
|
|
|
}
|
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) {
|
2019-03-13 06:07:07 +08:00
|
|
|
const char *error = std::strerror(errno);
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, _(L"Unable to rename file from '%ls' to '%ls': %s"), src.c_str(), dst.c_str(),
|
2019-06-04 11:30:48 +08:00
|
|
|
error);
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
return ret == 0;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2022-01-08 02:39:37 +08:00
|
|
|
void env_universal_t::initialize_at_path(callback_data_list_t &callbacks, wcstring path) {
|
2021-05-11 03:26:30 +08:00
|
|
|
if (path.empty()) return;
|
|
|
|
assert(!initialized() && "Already initialized");
|
|
|
|
vars_path_ = std::move(path);
|
2023-04-01 19:50:30 +08:00
|
|
|
narrow_vars_path_ = wcs2zstring(vars_path_);
|
2018-04-02 08:43:12 +08:00
|
|
|
|
2021-10-14 04:51:18 +08:00
|
|
|
if (load_from_path(narrow_vars_path_, callbacks)) {
|
2021-05-10 08:40:02 +08:00
|
|
|
// Successfully loaded from our normal path.
|
|
|
|
return;
|
|
|
|
}
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 03:26:30 +08:00
|
|
|
void env_universal_t::initialize(callback_data_list_t &callbacks) {
|
2021-05-11 06:10:52 +08:00
|
|
|
// Set do_flock to false immediately if the default variable path is on a remote filesystem.
|
|
|
|
// See #7968.
|
2021-12-19 12:39:00 +08:00
|
|
|
if (path_get_config_remoteness() == dir_remoteness_t::remote) do_flock = false;
|
2022-01-08 02:39:37 +08:00
|
|
|
this->initialize_at_path(callbacks, default_vars_path());
|
2021-05-11 03:26:30 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 06:01:55 +08:00
|
|
|
autoclose_fd_t env_universal_t::open_temporary_file(const wcstring &directory, wcstring *out_path) {
|
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
|
|
|
|
2021-01-10 05:14:54 +08:00
|
|
|
int saved_errno = 0;
|
2014-04-28 04:34:51 +08:00
|
|
|
const wcstring tmp_name_template = directory + L"/fishd.tmp.XXXXXX";
|
2020-01-30 06:01:55 +08:00
|
|
|
autoclose_fd_t result;
|
2020-07-30 07:37:23 +08:00
|
|
|
std::string narrow_str;
|
2020-01-30 06:01:55 +08:00
|
|
|
for (size_t attempt = 0; attempt < 10 && !result.valid(); attempt++) {
|
2023-04-01 19:50:30 +08:00
|
|
|
narrow_str = wcs2zstring(tmp_name_template);
|
2020-07-30 07:37:23 +08:00
|
|
|
result.reset(fish_mkstemp_cloexec(&narrow_str[0]));
|
2016-07-31 10:32:05 +08:00
|
|
|
saved_errno = errno;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2019-05-14 03:15:58 +08:00
|
|
|
*out_path = str2wcstring(narrow_str);
|
|
|
|
|
2020-01-30 06:01:55 +08:00
|
|
|
if (!result.valid()) {
|
2019-03-13 06:07:07 +08:00
|
|
|
const char *error = std::strerror(saved_errno);
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, _(L"Unable to open temporary file '%ls': %s"), out_path->c_str(), error);
|
2016-07-31 10:32:05 +08:00
|
|
|
}
|
2020-01-30 06:01:55 +08:00
|
|
|
return result;
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
|
2021-05-11 05:58:14 +08:00
|
|
|
/// Try locking the file.
|
|
|
|
/// \return true on success, false on error.
|
|
|
|
static bool flock_uvar_file(int fd) {
|
|
|
|
double start_time = timef();
|
|
|
|
while (flock(fd, LOCK_EX) == -1) {
|
|
|
|
if (errno != EINTR) return false; // do nothing per issue #2149
|
|
|
|
}
|
2016-12-16 13:50:27 +08:00
|
|
|
double duration = timef() - start_time;
|
|
|
|
if (duration > 0.25) {
|
2020-01-30 09:06:11 +08:00
|
|
|
FLOGF(warning, _(L"Locking the universal var file took too long (%.3f seconds)."),
|
|
|
|
duration);
|
2016-12-16 13:50:27 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-05-10 09:04:39 +08:00
|
|
|
bool env_universal_t::open_and_acquire_lock(const wcstring &path, autoclose_fd_t *out_fd) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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.
|
2021-05-11 05:58:14 +08:00
|
|
|
bool locked_by_open = false;
|
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
|
2021-05-11 05:58:14 +08:00
|
|
|
if (do_flock) {
|
2016-12-16 13:50:27 +08:00
|
|
|
flags |= O_EXLOCK;
|
2021-05-11 05:58:14 +08:00
|
|
|
locked_by_open = true;
|
2016-12-16 13:50:27 +08:00
|
|
|
}
|
2014-04-28 04:34:51 +08:00
|
|
|
#endif
|
2016-12-16 13:50:27 +08:00
|
|
|
|
2020-01-30 05:55:10 +08:00
|
|
|
autoclose_fd_t fd{};
|
|
|
|
while (!fd.valid()) {
|
2021-05-10 09:04:39 +08:00
|
|
|
fd = autoclose_fd_t{wopen_cloexec(path, flags, 0644)};
|
2021-05-11 05:58:14 +08:00
|
|
|
|
2020-01-30 05:55:10 +08:00
|
|
|
if (!fd.valid()) {
|
2021-05-11 05:58:14 +08:00
|
|
|
int err = errno;
|
|
|
|
if (err == EINTR) continue; // signaled; try again
|
|
|
|
|
2014-04-28 04:34:51 +08:00
|
|
|
#ifdef O_EXLOCK
|
2021-05-11 05:58:14 +08:00
|
|
|
if ((flags & O_EXLOCK) && (err == ENOTSUP || err == EOPNOTSUPP)) {
|
|
|
|
// Filesystem probably does not support locking. Give up on locking.
|
|
|
|
// 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;
|
2021-05-11 05:58:14 +08:00
|
|
|
do_flock = false;
|
|
|
|
locked_by_open = false;
|
2014-04-28 04:34:51 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-14 00:47:40 +08:00
|
|
|
FLOGF(error, _(L"Unable to open universal variable file '%s': %s"), path.c_str(),
|
2021-05-11 05:58:14 +08:00
|
|
|
std::strerror(err));
|
2016-12-16 13:50:27 +08:00
|
|
|
break;
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-01-30 05:55:10 +08:00
|
|
|
assert(fd.valid() && "Should have a valid fd here");
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2021-05-11 05:58:14 +08:00
|
|
|
// Lock if we want to lock and open() didn't do it for us.
|
|
|
|
// If flock fails, give up on locking forever.
|
|
|
|
if (do_flock && !locked_by_open) {
|
|
|
|
if (!flock_uvar_file(fd.fd())) do_flock = false;
|
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.
|
2020-01-30 05:55:10 +08:00
|
|
|
if (file_id_for_fd(fd.fd()) != file_id_for_path(path)) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Oops, it changed! Try again.
|
2020-01-30 05:55:10 +08:00
|
|
|
fd.close();
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-01-30 05:55:10 +08:00
|
|
|
*out_fd = std::move(fd);
|
|
|
|
return out_fd->valid();
|
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) {
|
2021-05-11 03:26:30 +08:00
|
|
|
if (!initialized()) return false;
|
2021-05-10 08:40:02 +08:00
|
|
|
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, L"universal log sync");
|
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.
|
|
|
|
// If we have no changes, just load.
|
|
|
|
if (modified.empty()) {
|
2021-10-14 04:51:18 +08:00
|
|
|
this->load_from_path(narrow_vars_path_, callbacks);
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, L"universal log no modifications");
|
2014-04-30 08:03:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2021-05-10 09:55:35 +08:00
|
|
|
const wcstring directory = wdirname(vars_path_);
|
2020-01-30 05:55:10 +08:00
|
|
|
autoclose_fd_t vars_fd{};
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, L"universal log performing full sync");
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Open the file.
|
2021-05-10 09:55:35 +08:00
|
|
|
if (!this->open_and_acquire_lock(vars_path_, &vars_fd)) {
|
2021-05-10 08:40:02 +08:00
|
|
|
FLOGF(uvar_file, L"universal log open_and_acquire_lock() failed");
|
|
|
|
return false;
|
2014-05-15 10:49:06 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Read from it.
|
2021-05-10 08:40:02 +08:00
|
|
|
assert(vars_fd.valid());
|
|
|
|
this->load_from_fd(vars_fd.fd(), callbacks);
|
2014-05-15 10:49:06 +08:00
|
|
|
|
2021-05-10 08:40:02 +08:00
|
|
|
if (ok_to_save) {
|
2021-05-10 09:55:35 +08:00
|
|
|
return this->save(directory, vars_path_);
|
2021-05-10 08:40:02 +08:00
|
|
|
} else {
|
|
|
|
return true;
|
2018-10-21 15:31:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write our file contents.
|
|
|
|
// \return true on success, false on failure.
|
|
|
|
bool env_universal_t::save(const wcstring &directory, const wcstring &vars_path) {
|
|
|
|
assert(ok_to_save && "It's not OK to save");
|
|
|
|
|
|
|
|
wcstring private_file_path;
|
|
|
|
|
|
|
|
// Open adjacent temporary file.
|
2020-01-30 06:01:55 +08:00
|
|
|
autoclose_fd_t private_fd = this->open_temporary_file(directory, &private_file_path);
|
|
|
|
bool success = private_fd.valid();
|
|
|
|
|
2020-01-19 20:43:33 +08:00
|
|
|
if (!success) FLOGF(uvar_file, L"universal log open_temporary_file() failed");
|
2018-10-21 15:31:30 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Write to it.
|
|
|
|
if (success) {
|
2020-01-30 06:01:55 +08:00
|
|
|
assert(private_fd.valid());
|
|
|
|
success = this->write_to_fd(private_fd.fd(), private_file_path);
|
2020-01-19 20:43:33 +08:00
|
|
|
if (!success) FLOGF(uvar_file, 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) {
|
2021-02-18 08:44:07 +08:00
|
|
|
wcstring real_path;
|
|
|
|
if (auto maybe_real_path = wrealpath(vars_path)) {
|
|
|
|
real_path = *maybe_real_path;
|
|
|
|
} else {
|
|
|
|
real_path = vars_path;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Ensure we maintain ownership and permissions (#2176).
|
2015-09-27 06:24:55 +08:00
|
|
|
struct stat sbuf;
|
2021-02-18 08:44:07 +08:00
|
|
|
if (wstat(real_path, &sbuf) >= 0) {
|
2020-01-30 06:01:55 +08:00
|
|
|
if (fchown(private_fd.fd(), sbuf.st_uid, sbuf.st_gid) == -1)
|
2020-01-19 20:43:33 +08:00
|
|
|
FLOGF(uvar_file, L"universal log fchown() failed");
|
2020-01-30 06:01:55 +08:00
|
|
|
if (fchmod(private_fd.fd(), sbuf.st_mode) == -1)
|
|
|
|
FLOGF(uvar_file, L"universal log fchmod() failed");
|
2015-09-27 06:24:55 +08:00
|
|
|
}
|
2016-03-13 03:21:52 +08:00
|
|
|
|
2018-10-21 15:31:30 +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!)
|
|
|
|
//
|
2021-10-03 07:47:17 +08:00
|
|
|
// The current time within the Linux kernel is cached, and generally only updated on a timer
|
|
|
|
// interrupt. So if the timer interrupt is running at 10 milliseconds, the cached time will
|
|
|
|
// only be updated once every 10 milliseconds.
|
|
|
|
//
|
2018-10-21 15:31:30 +08:00
|
|
|
// It's probably worth finding a simpler solution to this. The tests ran into this, but it's
|
|
|
|
// unlikely to affect users.
|
2021-10-03 07:47:17 +08:00
|
|
|
#if defined(UVAR_FILE_SET_MTIME_HACK)
|
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
|
|
|
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])) {
|
2020-01-30 06:01:55 +08:00
|
|
|
futimens(private_fd.fd(), times);
|
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
|
|
|
}
|
|
|
|
#endif
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
// Apply new file.
|
2021-02-18 08:44:07 +08:00
|
|
|
success = this->move_new_vars_file_into_place(private_file_path, real_path);
|
2020-01-19 20:43:33 +08:00
|
|
|
if (!success) FLOGF(uvar_file, L"universal log move_new_vars_file_into_place() failed");
|
2016-04-29 09:55:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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 (!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();
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:31:30 +08:00
|
|
|
uvar_format_t env_universal_t::read_message_internal(int fd, var_table_t *vars) {
|
2018-10-21 09:12:01 +08:00
|
|
|
// Read everything from the fd. Put a sane limit on it.
|
|
|
|
std::string contents;
|
|
|
|
while (contents.size() < k_max_read_size) {
|
|
|
|
char buffer[4096];
|
2014-06-10 07:41:10 +08:00
|
|
|
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
|
|
|
}
|
2018-10-21 09:12:01 +08:00
|
|
|
contents.append(buffer, amt);
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-10-21 09:12:01 +08:00
|
|
|
// Handle overlong files.
|
|
|
|
if (contents.size() >= k_max_read_size) {
|
|
|
|
contents.resize(k_max_read_size);
|
|
|
|
// Back up to a newline.
|
|
|
|
size_t newline = contents.rfind('\n');
|
|
|
|
contents.resize(newline == wcstring::npos ? 0 : newline);
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-10-21 15:31:30 +08:00
|
|
|
return populate_variables(contents, vars);
|
2014-04-28 04:34:51 +08:00
|
|
|
}
|
|
|
|
|
2018-10-21 05:38:49 +08:00
|
|
|
/// \return the format corresponding to file contents \p s.
|
|
|
|
uvar_format_t env_universal_t::format_for_contents(const std::string &s) {
|
|
|
|
// Walk over leading comments, looking for one like '# version'
|
|
|
|
line_iterator_t<std::string> iter{s};
|
|
|
|
while (iter.next()) {
|
|
|
|
const std::string &line = iter.line();
|
|
|
|
if (line.empty()) continue;
|
|
|
|
if (line.front() != L'#') {
|
|
|
|
// Exhausted leading comments.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Note scanf %s is max characters to write; add 1 for null terminator.
|
|
|
|
char versionbuf[64 + 1];
|
|
|
|
if (sscanf(line.c_str(), "# VERSION: %64s", versionbuf) != 1) continue;
|
|
|
|
|
|
|
|
// Try reading the version.
|
2019-03-13 06:07:07 +08:00
|
|
|
if (!std::strcmp(versionbuf, UVARS_VERSION_3_0)) {
|
2018-10-21 05:38:49 +08:00
|
|
|
return uvar_format_t::fish_3_0;
|
|
|
|
} else {
|
|
|
|
// Unknown future version.
|
|
|
|
return uvar_format_t::future;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No version found, assume 2.x
|
|
|
|
return uvar_format_t::fish_2_x;
|
|
|
|
}
|
|
|
|
|
2018-10-21 15:31:30 +08:00
|
|
|
uvar_format_t env_universal_t::populate_variables(const std::string &s, var_table_t *out_vars) {
|
2018-10-21 05:38:49 +08:00
|
|
|
// Decide on the format.
|
|
|
|
const uvar_format_t format = format_for_contents(s);
|
|
|
|
|
|
|
|
line_iterator_t<std::string> iter{s};
|
|
|
|
wcstring wide_line;
|
|
|
|
wcstring storage;
|
|
|
|
while (iter.next()) {
|
|
|
|
const std::string &line = iter.line();
|
|
|
|
// Skip empties and constants.
|
|
|
|
if (line.empty() || line.front() == L'#') continue;
|
|
|
|
|
|
|
|
// Convert to UTF8.
|
|
|
|
wide_line.clear();
|
|
|
|
if (!utf8_to_wchar(line.data(), line.size(), &wide_line, 0)) continue;
|
|
|
|
|
|
|
|
switch (format) {
|
|
|
|
case uvar_format_t::fish_2_x:
|
|
|
|
env_universal_t::parse_message_2x_internal(wide_line, out_vars, &storage);
|
|
|
|
break;
|
|
|
|
case uvar_format_t::fish_3_0:
|
|
|
|
// For future formats, just try with the most recent one.
|
|
|
|
case uvar_format_t::future:
|
|
|
|
env_universal_t::parse_message_30_internal(wide_line, out_vars, &storage);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-10-21 15:31:30 +08:00
|
|
|
return format;
|
2018-10-21 05:38:49 +08:00
|
|
|
}
|
|
|
|
|
2018-10-21 08:20:49 +08:00
|
|
|
static const wchar_t *skip_spaces(const wchar_t *str) {
|
|
|
|
while (*str == L' ' || *str == L'\t') str++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool env_universal_t::populate_1_variable(const wchar_t *input, env_var_t::env_var_flags_t flags,
|
|
|
|
var_table_t *vars, wcstring *storage) {
|
|
|
|
const wchar_t *str = skip_spaces(input);
|
2019-03-13 05:06:01 +08:00
|
|
|
const wchar_t *colon = std::wcschr(str, L':');
|
2018-10-21 08:20:49 +08:00
|
|
|
if (!colon) return false;
|
|
|
|
|
|
|
|
// Parse out the value into storage, and decode it into a variable.
|
|
|
|
storage->clear();
|
2023-03-26 23:23:05 +08:00
|
|
|
auto unescaped = unescape_string(colon + 1, 0);
|
|
|
|
if (!unescaped) {
|
2018-10-21 08:20:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-03-26 23:23:05 +08:00
|
|
|
*storage = *unescaped;
|
2018-10-21 08:20:49 +08:00
|
|
|
env_var_t var{decode_serialized(*storage), flags};
|
|
|
|
|
|
|
|
// Parse out the key and write into the map.
|
|
|
|
storage->assign(str, colon - str);
|
|
|
|
const wcstring &key = *storage;
|
|
|
|
(*vars)[key] = std::move(var);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-21 05:38:49 +08:00
|
|
|
/// Parse message msg per fish 3.0 format.
|
|
|
|
void env_universal_t::parse_message_30_internal(const wcstring &msgstr, var_table_t *vars,
|
|
|
|
wcstring *storage) {
|
2018-10-21 08:20:49 +08:00
|
|
|
namespace f3 = fish3_uvars;
|
|
|
|
const wchar_t *const msg = msgstr.c_str();
|
|
|
|
if (msg[0] == L'#') return;
|
|
|
|
|
|
|
|
const wchar_t *cursor = msg;
|
|
|
|
if (!match(&cursor, f3::SETUVAR)) {
|
2020-01-19 20:38:47 +08:00
|
|
|
FLOGF(warning, PARSE_ERR, msg);
|
2018-10-21 08:20:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Parse out flags.
|
|
|
|
env_var_t::env_var_flags_t flags = 0;
|
|
|
|
for (;;) {
|
|
|
|
cursor = skip_spaces(cursor);
|
|
|
|
if (*cursor != L'-') break;
|
|
|
|
if (match(&cursor, f3::EXPORT)) {
|
|
|
|
flags |= env_var_t::flag_export;
|
|
|
|
} else if (match(&cursor, f3::PATH)) {
|
|
|
|
flags |= env_var_t::flag_pathvar;
|
|
|
|
} else {
|
|
|
|
// Skip this unknown flag, for future proofing.
|
|
|
|
while (*cursor && *cursor != L' ' && *cursor != L'\t') cursor++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the variable with these flags.
|
|
|
|
if (!populate_1_variable(cursor, flags, vars, storage)) {
|
2020-01-19 20:38:47 +08:00
|
|
|
FLOGF(warning, PARSE_ERR, msg);
|
2018-10-21 08:20:49 +08:00
|
|
|
}
|
2018-10-21 05:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Parse message msg per fish 2.x format.
|
|
|
|
void env_universal_t::parse_message_2x_internal(const wcstring &msgstr, var_table_t *vars,
|
2019-05-05 18:09:25 +08:00
|
|
|
wcstring *storage) {
|
2018-10-21 09:19:30 +08:00
|
|
|
namespace f2x = fish2x_uvars;
|
2018-10-21 08:20:49 +08:00
|
|
|
const wchar_t *const msg = msgstr.c_str();
|
|
|
|
const wchar_t *cursor = msg;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-10-21 08:20:49 +08:00
|
|
|
if (cursor[0] == L'#') return;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-10-21 08:20:49 +08:00
|
|
|
env_var_t::env_var_flags_t flags = 0;
|
2018-10-21 09:19:30 +08:00
|
|
|
if (match(&cursor, f2x::SET_EXPORT)) {
|
2018-10-21 08:20:49 +08:00
|
|
|
flags |= env_var_t::flag_export;
|
2018-10-21 09:19:30 +08:00
|
|
|
} else if (match(&cursor, f2x::SET)) {
|
2018-10-21 08:20:49 +08:00
|
|
|
flags |= 0;
|
2016-04-29 09:55:41 +08:00
|
|
|
} else {
|
2020-01-19 20:38:47 +08:00
|
|
|
FLOGF(warning, PARSE_ERR, msg);
|
2018-10-21 08:20:49 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!populate_1_variable(cursor, flags, vars, storage)) {
|
2020-01-19 20:38:47 +08:00
|
|
|
FLOGF(warning, PARSE_ERR, msg);
|
2014-04-27 02:41:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Maximum length of hostname. Longer hostnames are truncated.
|
2022-09-21 02:58:37 +08:00
|
|
|
#define HOSTNAME_LEN 255
|
2014-04-26 08:44:49 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
/// Function to get an identifier based on the hostname.
|
2018-03-10 05:02:32 +08:00
|
|
|
bool get_hostname_identifier(wcstring &result) {
|
2019-05-05 18:09:25 +08:00
|
|
|
// The behavior of gethostname if the buffer size is insufficient differs by implementation and
|
|
|
|
// libc version Work around this by using a "guaranteed" sufficient buffer size then truncating
|
|
|
|
// the result.
|
2014-04-26 08:44:49 +08:00
|
|
|
bool success = false;
|
2018-03-10 04:52:29 +08:00
|
|
|
char hostname[256] = {};
|
|
|
|
if (gethostname(hostname, sizeof(hostname)) == 0) {
|
2018-03-10 05:02:32 +08:00
|
|
|
result.assign(str2wcstring(hostname));
|
|
|
|
result.assign(truncate(result, HOSTNAME_LEN));
|
2021-09-18 02:17:15 +08:00
|
|
|
// Don't return an empty hostname, we may attempt to open a directory instead.
|
|
|
|
success = !result.empty();
|
2014-04-26 08:44:49 +08:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-09-29 03:50:27 +08:00
|
|
|
namespace {
|
2020-09-30 08:11:37 +08:00
|
|
|
class universal_notifier_shmem_poller_t final : 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
|
|
|
|
2022-09-21 02:58:37 +08:00
|
|
|
#define SHMEM_MAGIC_NUMBER 0xF154
|
|
|
|
#define SHMEM_VERSION_CURRENT 1000
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
private:
|
2020-09-30 08:11:37 +08:00
|
|
|
long long last_change_time{0};
|
|
|
|
uint32_t last_seed{0};
|
|
|
|
volatile universal_notifier_shmem_t *region{nullptr};
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
void open_shmem() {
|
2019-11-19 10:34:50 +08:00
|
|
|
assert(region == nullptr);
|
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];
|
2018-11-28 22:08:24 +08:00
|
|
|
snprintf(path, sizeof path, "/%ls_shmem_%d", program_name ? program_name : L"fish",
|
2016-04-29 09:55:41 +08:00
|
|
|
getuid());
|
|
|
|
|
2020-01-30 06:01:55 +08:00
|
|
|
autoclose_fd_t fd{shm_open(path, O_RDWR | O_CREAT, 0600)};
|
|
|
|
if (!fd.valid()) {
|
2019-03-13 06:07:07 +08:00
|
|
|
const char *error = std::strerror(errno);
|
2019-05-30 17:54:09 +08:00
|
|
|
FLOGF(error, _(L"Unable to open shared memory with path '%s': %s"), path, error);
|
2020-09-30 08:11:37 +08:00
|
|
|
return;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Get the size.
|
2016-06-12 18:39:36 +08:00
|
|
|
off_t size = 0;
|
2020-09-30 08:11:37 +08:00
|
|
|
struct stat buf = {};
|
|
|
|
if (fstat(fd.fd(), &buf) < 0) {
|
2019-03-13 06:07:07 +08:00
|
|
|
const char *error = std::strerror(errno);
|
2020-09-30 08:11:37 +08:00
|
|
|
FLOGF(error, _(L"Unable to fstat shared memory object with path '%s': %s"), path,
|
2019-06-04 11:30:48 +08:00
|
|
|
error);
|
2020-09-30 08:11:37 +08:00
|
|
|
return;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
2020-09-30 08:11:37 +08:00
|
|
|
size = buf.st_size;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
// Set the size, if it's too small.
|
|
|
|
if (size < (off_t)sizeof(universal_notifier_shmem_t)) {
|
|
|
|
if (ftruncate(fd.fd(), sizeof(universal_notifier_shmem_t)) < 0) {
|
2019-03-13 06:07:07 +08:00
|
|
|
const char *error = std::strerror(errno);
|
2020-09-30 08:11:37 +08:00
|
|
|
FLOGF(error, _(L"Unable to truncate shared memory object with path '%s': %s"), path,
|
|
|
|
error);
|
|
|
|
return;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
// Memory map the region.
|
|
|
|
void *addr = mmap(nullptr, sizeof(universal_notifier_shmem_t), PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED, fd.fd(), 0);
|
|
|
|
if (addr == MAP_FAILED) {
|
|
|
|
const char *error = std::strerror(errno);
|
|
|
|
FLOGF(error, _(L"Unable to memory map shared memory object with path '%s': %s"), path,
|
|
|
|
error);
|
|
|
|
this->region = nullptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this->region = static_cast<universal_notifier_shmem_t *>(addr);
|
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.
|
2020-09-30 08:11:37 +08:00
|
|
|
void post_notification() override {
|
2019-11-19 10:34:50 +08:00
|
|
|
if (region != nullptr) {
|
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);
|
|
|
|
|
|
|
|
// 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)
|
2021-05-03 06:33:54 +08:00
|
|
|
|
|
|
|
FLOGF(uvar_notifier, "posting notification: seed %u -> %u", last_seed, seed);
|
|
|
|
last_seed = seed;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
universal_notifier_shmem_poller_t() { open_shmem(); }
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
~universal_notifier_shmem_poller_t() {
|
2019-11-19 10:34:50 +08:00
|
|
|
if (region != nullptr) {
|
2014-04-30 05:14:50 +08:00
|
|
|
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
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
bool poll() override {
|
2014-04-30 05:14:50 +08:00
|
|
|
bool result = false;
|
2019-11-19 10:34:50 +08:00
|
|
|
if (region != nullptr) {
|
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;
|
2021-05-03 06:33:54 +08:00
|
|
|
FLOGF(uvar_notifier, "polled true: shmem seed change %u -> %u", last_seed, seed);
|
2014-04-30 05:14:50 +08:00
|
|
|
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
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
unsigned long usec_delay_between_polls() const override {
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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:
|
2020-04-05 10:07:08 +08:00
|
|
|
[[noreturn]] universal_notifier_shmem_poller_t() {
|
2016-12-26 12:53:59 +08:00
|
|
|
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.
|
2020-09-30 08:11:37 +08:00
|
|
|
class universal_notifier_notifyd_t final : public universal_notifier_t {
|
2020-04-05 10:15:08 +08:00
|
|
|
#ifdef FISH_NOTIFYD_AVAILABLE
|
2020-09-30 08:11:37 +08:00
|
|
|
// Note that we should not use autoclose_fd_t, as notify_cancel() takes responsibility for
|
|
|
|
// closing it.
|
|
|
|
int notify_fd{-1};
|
|
|
|
int token{-1}; // NOTIFY_TOKEN_INVALID
|
|
|
|
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];
|
2018-11-28 22:08:24 +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);
|
2020-09-30 08:11:37 +08:00
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
if (status != NOTIFY_STATUS_OK) {
|
2020-01-19 20:38:47 +08:00
|
|
|
FLOGF(warning, "notify_register_file_descriptor() failed with status %u.", status);
|
|
|
|
FLOGF(warning, "Universal variable notifications may not be received.");
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
2020-09-30 08:11:37 +08:00
|
|
|
if (notify_fd >= 0) {
|
2016-04-29 09:55:41 +08:00
|
|
|
// Mark us for non-blocking reads, and CLO_EXEC.
|
2020-09-30 08:11:37 +08:00
|
|
|
int flags = fcntl(notify_fd, F_GETFL, 0);
|
2016-04-29 09:55:41 +08:00
|
|
|
if (flags >= 0 && !(flags & O_NONBLOCK)) {
|
2020-09-30 08:11:37 +08:00
|
|
|
fcntl(notify_fd, F_SETFL, flags | O_NONBLOCK);
|
2015-01-04 17:21:23 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
(void)set_cloexec(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.
|
2020-09-30 08:11:37 +08:00
|
|
|
(void)set_cloexec(notify_fd + 1);
|
2014-05-01 06:50:03 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
public:
|
2020-09-30 08:11:37 +08:00
|
|
|
universal_notifier_notifyd_t() { setup_notifyd(); }
|
2016-04-29 09:55:41 +08:00
|
|
|
|
|
|
|
~universal_notifier_notifyd_t() {
|
|
|
|
if (token != -1 /* NOTIFY_TOKEN_INVALID */) {
|
2020-09-30 08:11:37 +08:00
|
|
|
// Note this closes notify_fd.
|
2014-05-01 06:50:03 +08:00
|
|
|
notify_cancel(token);
|
|
|
|
}
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
int notification_fd() const override { return notify_fd; }
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
bool notification_fd_became_readable(int fd) override {
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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);
|
2021-05-03 06:33:54 +08:00
|
|
|
FLOGF(uvar_notifier, "notify fd %s readable", read_something ? "was" : "was not");
|
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
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
void post_notification() override {
|
2021-05-03 06:33:54 +08:00
|
|
|
FLOG(uvar_notifier, "posting 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) {
|
2020-01-30 09:06:11 +08:00
|
|
|
FLOGF(warning,
|
|
|
|
"notify_post() failed with status %u. Uvar notifications may not be sent.",
|
2016-12-26 12:53:59 +08:00
|
|
|
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:
|
2020-04-05 10:07:08 +08:00
|
|
|
[[noreturn]] universal_notifier_notifyd_t() {
|
2016-12-26 12:53:59 +08:00
|
|
|
DIE("universal_notifier_notifyd_t cannot be used on this system");
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-30 05:14:50 +08:00
|
|
|
};
|
|
|
|
|
2020-09-30 08:11:37 +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() {
|
|
|
|
wcstring result = env_get_runtime_path();
|
|
|
|
if (!result.empty()) {
|
|
|
|
result.append(L"/fish_universal_variables");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a fifo (named pipe) at \p test_path if non-null, or a default runtime path if null.
|
|
|
|
/// Open the fifo for both reading and writing, in non-blocking mode.
|
|
|
|
/// \return the fifo, or an invalid fd on failure.
|
|
|
|
static autoclose_fd_t make_fifo(const wchar_t *test_path, const wchar_t *suffix) {
|
|
|
|
wcstring vars_path = test_path ? wcstring(test_path) : default_named_pipe_path();
|
|
|
|
vars_path.append(suffix);
|
2023-04-01 19:50:30 +08:00
|
|
|
const std::string narrow_path = wcs2zstring(vars_path);
|
2020-09-30 08:11:37 +08:00
|
|
|
|
|
|
|
int mkfifo_status = mkfifo(narrow_path.c_str(), 0600);
|
|
|
|
if (mkfifo_status == -1 && errno != EEXIST) {
|
|
|
|
const char *error = std::strerror(errno);
|
|
|
|
const wchar_t *errmsg = _(L"Unable to make a pipe for universal variables using '%ls': %s");
|
|
|
|
FLOGF(error, errmsg, vars_path.c_str(), error);
|
|
|
|
return autoclose_fd_t{};
|
|
|
|
}
|
|
|
|
|
|
|
|
autoclose_fd_t res{wopen_cloexec(vars_path, O_RDWR | O_NONBLOCK, 0600)};
|
|
|
|
if (!res.valid()) {
|
|
|
|
const char *error = std::strerror(errno);
|
|
|
|
const wchar_t *errmsg = _(L"Unable to open a pipe for universal variables using '%ls': %s");
|
|
|
|
FLOGF(error, errmsg, vars_path.c_str(), error);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
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
|
2021-05-04 09:12:34 +08:00
|
|
|
// mode until the pipe is no longer readable, where we poll based on the modification date of the
|
|
|
|
// pipe. 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).
|
2020-09-30 08:11:37 +08:00
|
|
|
class universal_notifier_named_pipe_t final : public universal_notifier_t {
|
|
|
|
#if !defined(__CYGWIN__)
|
2021-05-04 09:12:34 +08:00
|
|
|
// We operate a state machine.
|
|
|
|
enum state_t{
|
|
|
|
// The pipe is not yet readable. There is nothing to do in poll.
|
|
|
|
// If the pipe becomes readable we will enter the polling state.
|
|
|
|
waiting_for_readable,
|
|
|
|
|
|
|
|
// The pipe is readable. In poll, check if the pipe is still readable,
|
|
|
|
// and whether its timestamp has changed.
|
|
|
|
polling_during_readable,
|
|
|
|
|
|
|
|
// We have written to the pipe (so we expect it to be readable).
|
|
|
|
// We may read back from it in poll().
|
|
|
|
waiting_to_drain,
|
|
|
|
};
|
|
|
|
|
|
|
|
// The state we are currently in.
|
|
|
|
state_t state{waiting_for_readable};
|
|
|
|
|
|
|
|
// When we entered that state, in microseconds since epoch.
|
|
|
|
long long state_start_usec{-1};
|
|
|
|
|
|
|
|
// The pipe itself; this is opened read/write.
|
2020-09-30 08:11:37 +08:00
|
|
|
autoclose_fd_t pipe_fd;
|
2021-05-04 09:12:34 +08:00
|
|
|
|
|
|
|
// The pipe's file ID containing the last modified timestamp.
|
|
|
|
file_id_t pipe_timestamps{};
|
|
|
|
|
|
|
|
// If we are in waiting_to_drain state, how much we have written and therefore are responsible
|
|
|
|
// for draining.
|
|
|
|
size_t drain_amount{0};
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
// We "flash" the pipe to make it briefly readable, for this many usec.
|
2021-09-17 06:25:22 +08:00
|
|
|
static constexpr long long k_flash_duration_usec = 1e4;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
// If the pipe remains readable for this many usec, we drain it.
|
2021-09-17 06:25:22 +08:00
|
|
|
static constexpr long long k_readable_too_long_duration_usec = 1e6;
|
2020-09-30 08:11:37 +08:00
|
|
|
|
2021-05-04 09:12:34 +08:00
|
|
|
/// \return the name of a state.
|
|
|
|
static const char *state_name(state_t s) {
|
|
|
|
switch (s) {
|
|
|
|
case waiting_for_readable:
|
|
|
|
return "waiting";
|
|
|
|
case polling_during_readable:
|
|
|
|
return "polling";
|
|
|
|
case waiting_to_drain:
|
|
|
|
return "draining";
|
|
|
|
}
|
|
|
|
DIE("Unreachable");
|
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2021-05-04 09:12:34 +08:00
|
|
|
// Switch to a state (may or may not be new).
|
|
|
|
void set_state(state_t new_state) {
|
|
|
|
FLOGF(uvar_notifier, "changing from %s to %s", state_name(state), state_name(new_state));
|
|
|
|
state = new_state;
|
|
|
|
state_start_usec = get_time();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called when the pipe has been readable for too long.
|
|
|
|
void drain_excess() const {
|
2016-04-29 09:55:41 +08:00
|
|
|
// 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!
|
2021-05-03 06:33:54 +08:00
|
|
|
FLOG(uvar_notifier, "pipe was full, draining it");
|
2020-09-30 08:11:37 +08:00
|
|
|
char buff[512];
|
|
|
|
ignore_result(read(pipe_fd.fd(), buff, sizeof buff));
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2021-05-04 09:12:34 +08:00
|
|
|
// Called when we want to read back data we have written, to mark the pipe as non-readable.
|
|
|
|
void drain_written() {
|
|
|
|
while (this->drain_amount > 0) {
|
|
|
|
char buff[64];
|
|
|
|
size_t amt = std::min(this->drain_amount, sizeof buff);
|
|
|
|
ignore_result(read(this->pipe_fd.fd(), buff, amt));
|
|
|
|
this->drain_amount -= amt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-04 02:51:00 +08:00
|
|
|
/// Check if the pipe's file ID (aka struct stat) is different from what we have stored.
|
2021-05-04 09:12:34 +08:00
|
|
|
/// If it has changed, it indicates that someone has modified the pipe; update our stored id.
|
2021-05-04 02:51:00 +08:00
|
|
|
/// \return true if changed, false if not.
|
2021-05-04 09:12:34 +08:00
|
|
|
bool update_pipe_timestamps() {
|
2021-05-04 02:51:00 +08:00
|
|
|
if (!pipe_fd.valid()) return false;
|
2021-05-04 09:12:34 +08:00
|
|
|
file_id_t timestamps = file_id_for_fd(pipe_fd.fd());
|
|
|
|
if (timestamps == this->pipe_timestamps) {
|
2021-05-04 02:51:00 +08:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-04 09:12:34 +08:00
|
|
|
this->pipe_timestamps = timestamps;
|
2021-05-04 02:51:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
public:
|
2020-02-21 14:54:30 +08:00
|
|
|
explicit universal_notifier_named_pipe_t(const wchar_t *test_path)
|
2020-09-30 08:11:37 +08:00
|
|
|
: pipe_fd(make_fifo(test_path, L".notifier")) {}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
~universal_notifier_named_pipe_t() override = default;
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-08-10 07:46:11 +08:00
|
|
|
int notification_fd() const override {
|
2021-05-04 09:12:34 +08:00
|
|
|
if (!pipe_fd.valid()) return -1;
|
|
|
|
// If we are waiting for the pipe to be readable, return it for select.
|
|
|
|
// Otherwise we expect it to be readable already; return invalid.
|
|
|
|
switch (state) {
|
|
|
|
case waiting_for_readable:
|
|
|
|
return pipe_fd.fd();
|
|
|
|
case polling_during_readable:
|
|
|
|
case waiting_to_drain:
|
|
|
|
return -1;
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2021-05-04 09:12:34 +08:00
|
|
|
DIE("unreachable");
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
bool notification_fd_became_readable(int fd) override {
|
2021-05-04 09:12:34 +08:00
|
|
|
assert(fd == pipe_fd.fd() && "Wrong fd");
|
2016-10-10 05:38:26 +08:00
|
|
|
UNUSED(fd);
|
2021-05-04 09:12:34 +08:00
|
|
|
switch (state) {
|
|
|
|
case waiting_for_readable:
|
|
|
|
// We are now readable.
|
|
|
|
// Grab the timestamp and return true indicating that we received a notification.
|
|
|
|
set_state(polling_during_readable);
|
|
|
|
update_pipe_timestamps();
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case polling_during_readable:
|
|
|
|
case waiting_to_drain:
|
|
|
|
// We did not return an fd to wait on, so should not be called.
|
|
|
|
DIE("should not be called in this state");
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2021-05-04 09:12:34 +08:00
|
|
|
DIE("unreachable");
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
void post_notification() override {
|
2020-09-30 08:11:37 +08:00
|
|
|
if (!pipe_fd.valid()) return;
|
|
|
|
// 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.
|
2021-05-04 09:12:34 +08:00
|
|
|
FLOGF(uvar_notifier, "writing to pipe (written %lu)", (unsigned long)drain_amount);
|
2020-09-30 08:11:37 +08:00
|
|
|
char c[1] = {'\0'};
|
|
|
|
ssize_t amt_written = write(pipe_fd.fd(), c, sizeof c);
|
|
|
|
if (amt_written < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) {
|
2022-10-29 11:07:36 +08:00
|
|
|
// Very unusual: the pipe is full! Try to read some and repeat once.
|
2021-05-04 09:12:34 +08:00
|
|
|
drain_excess();
|
|
|
|
amt_written = write(pipe_fd.fd(), c, sizeof c);
|
|
|
|
if (amt_written < 0) {
|
|
|
|
FLOG(uvar_notifier, "pipe could not be drained, skipping notification");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FLOG(uvar_notifier, "pipe drained");
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2021-05-04 09:12:34 +08:00
|
|
|
assert(amt_written >= 0 && "Amount should not be negative");
|
|
|
|
this->drain_amount += amt_written;
|
|
|
|
// We unconditionally set our state to waiting to drain.
|
|
|
|
set_state(waiting_to_drain);
|
|
|
|
update_pipe_timestamps();
|
2014-05-05 06:06:40 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
unsigned long usec_delay_between_polls() const override {
|
2021-05-04 09:12:34 +08:00
|
|
|
if (!pipe_fd.valid()) return 0;
|
|
|
|
switch (state) {
|
|
|
|
case waiting_for_readable:
|
|
|
|
// No polling necessary until it becomes readable.
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case polling_during_readable:
|
|
|
|
case waiting_to_drain:
|
|
|
|
return k_flash_duration_usec;
|
2014-05-07 05:10:55 +08:00
|
|
|
}
|
2021-05-04 09:12:34 +08:00
|
|
|
DIE("Unreachable");
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2018-02-19 10:50:35 +08:00
|
|
|
bool poll() override {
|
2020-09-30 08:11:37 +08:00
|
|
|
if (!pipe_fd.valid()) return false;
|
2021-05-04 09:12:34 +08:00
|
|
|
switch (state) {
|
|
|
|
case waiting_for_readable:
|
|
|
|
// Nothing to do until the fd is readable.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case polling_during_readable: {
|
|
|
|
// If we're no longer readable, go back to wait mode.
|
|
|
|
// Conversely, if we have been readable too long, perhaps some fish died while its
|
|
|
|
// written data was still on the pipe; drain some.
|
2023-01-15 06:56:24 +08:00
|
|
|
if (!poll_fd_readable(pipe_fd.fd())) {
|
2021-05-04 09:12:34 +08:00
|
|
|
set_state(waiting_for_readable);
|
|
|
|
} else if (get_time() >= state_start_usec + k_readable_too_long_duration_usec) {
|
|
|
|
drain_excess();
|
|
|
|
}
|
|
|
|
// Sync if the pipe's timestamp is different, meaning someone modified the pipe
|
|
|
|
// since we last saw it.
|
|
|
|
if (update_pipe_timestamps()) {
|
|
|
|
FLOG(uvar_notifier, "pipe changed, will sync uvars");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2016-04-29 09:55:41 +08:00
|
|
|
|
2021-05-04 09:12:34 +08:00
|
|
|
case waiting_to_drain: {
|
|
|
|
// We wrote data to the pipe. Maybe read it back.
|
|
|
|
// If we are still readable, then there is still data on the pipe; maybe another
|
|
|
|
// change occurred with ours.
|
|
|
|
if (get_time() >= state_start_usec + k_flash_duration_usec) {
|
|
|
|
drain_written();
|
2023-01-15 06:56:24 +08:00
|
|
|
if (!poll_fd_readable(pipe_fd.fd())) {
|
2021-05-04 09:12:34 +08:00
|
|
|
set_state(waiting_for_readable);
|
|
|
|
} else {
|
|
|
|
set_state(polling_during_readable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return update_pipe_timestamps();
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
|
|
|
}
|
2021-05-04 09:12:34 +08:00
|
|
|
DIE("Unreachable");
|
2014-05-06 14:33:05 +08:00
|
|
|
}
|
2021-05-04 09:12:34 +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
|
|
|
|
};
|
2021-09-29 03:50:27 +08:00
|
|
|
} // namespace
|
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() {
|
2020-04-05 10:15:08 +08:00
|
|
|
#ifdef FISH_NOTIFYD_AVAILABLE
|
2014-05-01 06:50:03 +08:00
|
|
|
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");
|
2019-11-19 10:34:50 +08:00
|
|
|
return nullptr;
|
2014-04-30 05:14:50 +08:00
|
|
|
}
|
|
|
|
|
2016-04-29 09:55:41 +08:00
|
|
|
// Default implementations.
|
2018-02-19 10:44:58 +08:00
|
|
|
universal_notifier_t::universal_notifier_t() = default;
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2018-02-19 10:44:58 +08:00
|
|
|
universal_notifier_t::~universal_notifier_t() = default;
|
2014-04-30 05:14:50 +08:00
|
|
|
|
2018-08-10 07:46:11 +08:00
|
|
|
int universal_notifier_t::notification_fd() const { return -1; }
|
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
|
|
|
|
2020-09-30 08:11:37 +08:00
|
|
|
void universal_notifier_t::post_notification() {}
|
|
|
|
|
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;
|
|
|
|
}
|