Remove large parts of fishd interaction, including

env_universal_server
This commit is contained in:
ridiculousfish 2014-06-06 10:34:42 -07:00
parent be3e64e5ea
commit ecf56606dc
5 changed files with 14 additions and 458 deletions

View file

@ -2129,7 +2129,7 @@ static pid_t initial_foreground_process_group = -1;
bool is_forked_child(void)
{
/* Just bail if nobody's called setup_fork_guards - e.g. fishd */
/* Just bail if nobody's called setup_fork_guards, e.g. some of our tools */
if (! initial_pid) return false;
bool is_child_of_fork = (getpid() != initial_pid);

53
env.cpp
View file

@ -57,12 +57,6 @@
#include "complete.h"
#include "fish_version.h"
/** Command used to start fishd */
#define FISHD_CMD L"fishd ^ /dev/null"
// Version for easier debugging
//#define FISHD_CMD L"fishd"
/** Value denoting a null string */
#define ENV_NULL L"\x1d"
@ -219,43 +213,6 @@ env_node_t *env_node_t::next_scope_to_search(void)
return this->new_scope ? global_env : this->next;
}
/**
When fishd isn't started, this function is provided to
env_universal as a callback, it tries to start up fishd. It's
implementation is a bit of a hack, since it evaluates a bit of
shellscript, and it might be used at times when that might not be
the best idea.
*/
static void start_fishd()
{
struct passwd *pw = getpwuid(getuid());
debug(3, L"Spawning new copy of fishd");
if (!pw)
{
debug(0, _(L"Could not get user information"));
return;
}
wcstring cmd = format_string(FISHD_CMD, pw->pw_name);
/* Prefer the fishd in __fish_bin_dir, if exists */
const env_var_t bin_dir = env_get_string(L"__fish_bin_dir");
if (! bin_dir.missing_or_empty())
{
wcstring path = bin_dir + L"/fishd";
if (waccess(path, X_OK) == 0)
{
/* The path command just looks like 'fishd', so insert the bin path to make it absolute */
cmd.insert(0, bin_dir + L"/");
}
}
parser_t &parser = parser_t::principal_parser();
parser.eval(cmd, io_chain_t(), TOP);
}
/**
Return the current umask value.
*/
@ -604,15 +561,7 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
env_set(L"version", version.c_str(), ENV_GLOBAL);
env_set(L"FISH_VERSION", version.c_str(), ENV_GLOBAL);
const env_var_t fishd_dir_wstr = env_get_string(L"FISHD_SOCKET_DIR");
const env_var_t user_dir_wstr = env_get_string(L"USER");
wchar_t * fishd_dir = fishd_dir_wstr.missing()?NULL:const_cast<wchar_t*>(fishd_dir_wstr.c_str());
wchar_t * user_dir = user_dir_wstr.missing()?NULL:const_cast<wchar_t*>(user_dir_wstr.c_str());
env_universal_init(fishd_dir , user_dir ,
&start_fishd,
&universal_callback);
env_universal_init(universal_callback);
/*
Set up SHLVL variable

View file

@ -7,7 +7,6 @@
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <pwd.h>
#include <errno.h>
@ -40,31 +39,11 @@
#include "env_universal.h"
#include "env.h"
/**
Maximum number of times to try to get a new fishd socket
*/
#define RECONNECT_COUNT 32
connection_t env_universal_server(-1);
/**
Set to true after initialization has been performed
*/
static bool s_env_univeral_inited = false;
/**
The number of attempts to start fishd
*/
static int get_socket_count = 0;
#define DEFAULT_RETRY_COUNT 15
#define DEFAULT_RETRY_DELAY 0.2
static wchar_t * path;
static wchar_t *user;
static void (*start_fishd)();
static void (*external_callback)(fish_message_type_t type, const wchar_t *name, const wchar_t *val);
/**
@ -74,133 +53,6 @@ static int barrier_reply = 0;
void env_universal_barrier();
static int is_dead()
{
return env_universal_server.fd < 0;
}
static int try_get_socket_once(void)
{
int s;
wchar_t *wdir;
wchar_t *wuname;
char *dir = 0;
wdir = path;
wuname = user;
uid_t seuid;
gid_t segid;
if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
{
wperror(L"socket");
return -1;
}
if (wdir)
dir = wcs2str(wdir);
else
dir = strdup("/tmp");
std::string uname;
if (wuname)
{
uname = wcs2string(wuname);
}
else
{
struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_name)
{
uname = pw->pw_name;
}
}
std::string name;
name.reserve(strlen(dir) + uname.size() + strlen(SOCK_FILENAME) + 2);
name.append(dir);
name.append("/");
name.append(SOCK_FILENAME);
name.append(uname);
free(dir);
debug(3, L"Connect to socket %s at fd %d", name.c_str(), s);
struct sockaddr_un local = {};
local.sun_family = AF_UNIX;
strncpy(local.sun_path, name.c_str(), (sizeof local.sun_path) - 1);
if (connect(s, (struct sockaddr *)&local, sizeof local) == -1)
{
close(s);
/* If it fails on first try, it's probably no serious error, but fishd hasn't been launched yet.
This happens (at least) on the first concurrent session. */
if (get_socket_count > 1)
wperror(L"connect");
return -1;
}
if ((getpeereid(s, &seuid, &segid) != 0) || seuid != geteuid())
{
debug(1, L"Wrong credentials for socket %s at fd %d", name.c_str(), s);
close(s);
return -1;
}
if ((make_fd_nonblocking(s) != 0) || (fcntl(s, F_SETFD, FD_CLOEXEC) != 0))
{
wperror(L"fcntl");
close(s);
return -1;
}
debug(3, L"Connected to fd %d", s);
return s;
}
/**
Get a socket for reading from the server
*/
static int get_socket(void)
{
get_socket_count++;
int s = try_get_socket_once();
if (s < 0)
{
if (start_fishd)
{
debug(2, L"Could not connect to socket %d, starting fishd", s);
start_fishd();
for (size_t i=0; s < 0 && i < DEFAULT_RETRY_COUNT; i++)
{
if (i > 0)
{
// Wait before next try
usleep((useconds_t)(DEFAULT_RETRY_DELAY * 1E6));
}
s = try_get_socket_once();
}
}
}
if (s < 0)
{
debug(1, L"Could not connect to universal variable server, already tried manual restart (or no command supplied). You will not be able to share variable values between fish sessions. Is fish properly installed?");
return -1;
}
return s;
}
/**
Callback function used whenever a new fishd message is recieved
*/
@ -217,151 +69,19 @@ static void callback(fish_message_type_t type, const wchar_t *name, const wchar_
}
}
/**
Make sure the connection is healthy. If not, close it, and try to
establish a new connection.
*/
static void check_connection(void)
void env_universal_init(void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val))
{
if (! s_env_univeral_inited)
return;
if (env_universal_server.killme)
{
debug(3, L"Lost connection to universal variable server.");
if (close(env_universal_server.fd))
{
wperror(L"close");
}
env_universal_server.fd = -1;
env_universal_server.killme=0;
env_universal_server.input.clear();
env_universal_read_all();
}
}
/**
Remove all universal variables.
*/
static void env_universal_remove_all()
{
size_t i;
wcstring_list_t lst;
env_universal_common_get_names(lst, true, true);
for (i=0; i<lst.size(); i++)
{
const wcstring &key = lst.at(i);
env_universal_common_remove(key);
}
}
/**
Try to establish a new connection to fishd. If successfull, end
with call to env_universal_barrier(), to make sure everything is in
sync.
*/
static void reconnect()
{
assert(synchronizes_via_fishd());
if (get_socket_count >= RECONNECT_COUNT)
return;
debug(3, L"Get new fishd connection");
s_env_univeral_inited = false;
env_universal_server.buffer_consumed = 0;
env_universal_server.read_buffer.clear();
env_universal_server.fd = get_socket();
external_callback = cb;
env_universal_common_init(&callback);
s_env_univeral_inited = true;
if (env_universal_server.fd >= 0)
{
env_universal_remove_all();
env_universal_barrier();
}
}
void env_universal_init(wchar_t * p,
wchar_t *u,
void (*sf)(),
void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val))
{
if (! synchronizes_via_fishd())
{
external_callback = cb;
env_universal_common_init(&callback);
s_env_univeral_inited = true;
}
else
{
path=p;
user=u;
start_fishd=sf;
external_callback = cb;
env_universal_server.fd = get_socket();
env_universal_common_init(&callback);
env_universal_read_all();
s_env_univeral_inited = true;
if (env_universal_server.fd >= 0)
{
env_universal_barrier();
}
}
}
void env_universal_destroy()
{
/*
Go into blocking mode and send all data before exiting
*/
if (env_universal_server.fd >= 0)
{
if (fcntl(env_universal_server.fd, F_SETFL, 0) != 0)
{
wperror(L"fcntl");
}
try_send_all(&env_universal_server);
}
connection_destroy(&env_universal_server);
env_universal_server.fd =-1;
s_env_univeral_inited = false;
}
/**
Read all available messages from the server.
*/
int env_universal_read_all()
{
if (! s_env_univeral_inited)
return 0;
if (env_universal_server.fd == -1)
{
reconnect();
if (env_universal_server.fd == -1)
return 0;
}
if (env_universal_server.fd != -1)
{
read_message(&env_universal_server);
check_connection();
return 1;
}
else
{
debug(2, L"No connection to universal variable server");
return 0;
}
}
env_var_t env_universal_get(const wcstring &name)
{
if (!s_env_univeral_inited)
@ -382,69 +102,7 @@ void env_universal_barrier()
{
ASSERT_IS_MAIN_THREAD();
UNIVERSAL_LOG("BARRIER");
message_t *msg;
fd_set fds;
if (! synchronizes_via_fishd())
{
env_universal_common_sync();
return;
}
if (!s_env_univeral_inited || is_dead())
return;
barrier_reply = 0;
/*
Create barrier request
*/
msg= create_message(BARRIER, 0, 0);
msg->count=1;
env_universal_server.unsent.push(msg);
/*
Wait until barrier request has been sent
*/
debug(3, L"Create barrier");
while (1)
{
try_send_all(&env_universal_server);
check_connection();
if (env_universal_server.unsent.empty())
break;
if (env_universal_server.fd == -1)
{
reconnect();
debug(2, L"barrier interrupted, exiting");
return;
}
FD_ZERO(&fds);
FD_SET(env_universal_server.fd, &fds);
select(env_universal_server.fd+1, 0, &fds, 0, 0);
}
/*
Wait for barrier reply
*/
debug(3, L"Sent barrier request");
while (!barrier_reply)
{
if (env_universal_server.fd == -1)
{
reconnect();
debug(2, L"barrier interrupted, exiting (2)");
return;
}
FD_ZERO(&fds);
FD_SET(env_universal_server.fd, &fds);
select(env_universal_server.fd+1, &fds, 0, 0, 0);
env_universal_read_all();
}
debug(3, L"End barrier");
env_universal_common_sync();
}
@ -455,27 +113,8 @@ void env_universal_set(const wcstring &name, const wcstring &value, bool exportv
debug(3, L"env_universal_set( \"%ls\", \"%ls\" )", name.c_str(), value.c_str());
if (! synchronizes_via_fishd() || is_dead())
{
env_universal_common_set(name.c_str(), value.c_str(), exportv);
env_universal_barrier();
}
else
{
message_t *msg = create_message(exportv?SET_EXPORT:SET,
name.c_str(),
value.c_str());
if (!msg)
{
debug(1, L"Could not create universal variable message");
return;
}
msg->count=1;
env_universal_server.unsent.push(msg);
env_universal_barrier();
}
env_universal_common_set(name.c_str(), value.c_str(), exportv);
env_universal_barrier();
}
int env_universal_remove(const wchar_t *name)
@ -487,24 +126,10 @@ int env_universal_remove(const wchar_t *name)
CHECK(name, 1);
wcstring name_str = name;
const wcstring name_str = name;
// TODO: shouldn't have to call get() here, should just have remove return success
res = env_universal_common_get(name_str).missing();
debug(3,
L"env_universal_remove( \"%ls\" )",
name);
if (! synchronizes_via_fishd() || is_dead())
{
env_universal_common_remove(name_str);
}
else
{
message_t *msg = create_message(ERASE, name, 0);
msg->count=1;
env_universal_server.unsent.push(msg);
env_universal_barrier();
}
env_universal_common_remove(name_str);
return res;
}

View file

@ -18,10 +18,7 @@ extern connection_t env_universal_server;
/**
Initialize the envuni library
*/
void env_universal_init(wchar_t * p,
wchar_t *u,
void (*sf)(),
void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val));
void env_universal_init(void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val));
/**
Free memory used by envuni
*/

View file

@ -102,11 +102,6 @@ static wint_t readb()
FD_ZERO(&fdset);
FD_SET(0, &fdset);
if (env_universal_server.fd > 0)
{
FD_SET(env_universal_server.fd, &fdset);
fd_max = maxi(fd_max, env_universal_server.fd);
}
if (ioport > 0)
{
FD_SET(ioport, &fdset);
@ -174,17 +169,7 @@ static wint_t readb()
/* Assume we loop unless we see a character in stdin */
do_loop = true;
if (env_universal_server.fd > 0 && FD_ISSET(env_universal_server.fd, &fdset))
{
debug(3, L"Wake up on universal variable event");
env_universal_read_all();
if (has_lookahead())
{
return lookahead_pop();
}
}
/* Check to see if we want a barrier */
/* Check to see if we want a universal variable barrier */
bool barrier_from_poll = notifier.poll();
bool barrier_from_readability = false;
if (notifier_fd > 0 && FD_ISSET(notifier_fd, &fdset))