Skip to content

Commit

Permalink
tools/xenstore: drop creation of read-only socket in xenstored
Browse files Browse the repository at this point in the history
With xs_daemon_open_readonly() now no longer using the read-only socket
the creation of that socket can be dropped.

Signed-off-by: Juergen Gross <jgross@suse.com>
Acked-by: Wei Liu <wl@xen.org>
  • Loading branch information
jgross1 authored and liuw committed Oct 8, 2020
1 parent 90c9f9f commit 1d246c7
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 56 deletions.
55 changes: 11 additions & 44 deletions tools/xenstore/xenstored_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ static unsigned int current_array_size;
static unsigned int nr_fds;

static int sock = -1;
static int ro_sock = -1;

static bool verbose = false;
LIST_HEAD(connections);
Expand Down Expand Up @@ -311,8 +310,7 @@ static int set_fd(int fd, short events)
return -1;
}

static void initialize_fds(int *p_sock_pollfd_idx, int *p_ro_sock_pollfd_idx,
int *ptimeout)
static void initialize_fds(int *p_sock_pollfd_idx, int *ptimeout)
{
struct connection *conn;
struct wrl_timestampt now;
Expand All @@ -325,8 +323,6 @@ static void initialize_fds(int *p_sock_pollfd_idx, int *p_ro_sock_pollfd_idx,

if (sock != -1)
*p_sock_pollfd_idx = set_fd(sock, POLLIN|POLLPRI);
if (ro_sock != -1)
*p_ro_sock_pollfd_idx = set_fd(ro_sock, POLLIN|POLLPRI);
if (reopen_log_pipe[0] != -1)
reopen_log_pipe0_pollfd_idx =
set_fd(reopen_log_pipe[0], POLLIN|POLLPRI);
Expand Down Expand Up @@ -472,9 +468,6 @@ static enum xs_perm_type perm_for_conn(struct connection *conn,
unsigned int i;
enum xs_perm_type mask = XS_PERM_READ|XS_PERM_WRITE|XS_PERM_OWNER;

if (!conn->can_write)
mask &= ~XS_PERM_WRITE;

/* Owners and tools get it all... */
if (!domain_is_unprivileged(conn) || perms[0].id == conn->id
|| (conn->target && perms[0].id == conn->target->id))
Expand Down Expand Up @@ -1422,7 +1415,6 @@ struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
new->pollfd_idx = -1;
new->write = write;
new->read = read;
new->can_write = true;
new->transaction_started = 0;
INIT_LIST_HEAD(&new->out_list);
INIT_LIST_HEAD(&new->watches);
Expand All @@ -1435,7 +1427,7 @@ struct connection *new_connection(connwritefn_t *write, connreadfn_t *read)
}

#ifdef NO_SOCKETS
static void accept_connection(int sock, bool canwrite)
static void accept_connection(int sock)
{
}
#else
Expand Down Expand Up @@ -1477,7 +1469,7 @@ static int readfd(struct connection *conn, void *data, unsigned int len)
return rc;
}

static void accept_connection(int sock, bool canwrite)
static void accept_connection(int sock)
{
int fd;
struct connection *conn;
Expand All @@ -1487,10 +1479,9 @@ static void accept_connection(int sock, bool canwrite)
return;

conn = new_connection(writefd, readfd);
if (conn) {
if (conn)
conn->fd = fd;
conn->can_write = canwrite;
} else
else
close(fd);
}
#endif
Expand Down Expand Up @@ -1794,28 +1785,21 @@ static void destroy_fds(void)
{
if (sock >= 0)
close(sock);
if (ro_sock >= 0)
close(ro_sock);
}

static void init_sockets(void)
{
struct sockaddr_un addr;
const char *soc_str = xs_daemon_socket();
const char *soc_str_ro = xs_daemon_socket_ro();

/* Create sockets for them to listen to. */
atexit(destroy_fds);
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
barf_perror("Could not create socket");
ro_sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (ro_sock < 0)
barf_perror("Could not create socket");

/* FIXME: Be more sophisticated, don't mug running daemon. */
unlink(soc_str);
unlink(soc_str_ro);

addr.sun_family = AF_UNIX;

Expand All @@ -1825,17 +1809,10 @@ static void init_sockets(void)
if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) != 0)
barf_perror("Could not bind socket to %s", soc_str);

if(strlen(soc_str_ro) >= sizeof(addr.sun_path))
barf_perror("socket string '%s' too long", soc_str_ro);
strcpy(addr.sun_path, soc_str_ro);
if (bind(ro_sock, (struct sockaddr *)&addr, sizeof(addr)) != 0)
barf_perror("Could not bind socket to %s", soc_str_ro);

if (chmod(soc_str, 0600) != 0
|| chmod(soc_str_ro, 0660) != 0)
if (chmod(soc_str, 0600) != 0)
barf_perror("Could not chmod sockets");

if (listen(sock, 1) != 0 || listen(ro_sock, 1) != 0)
if (listen(sock, 1) != 0)
barf_perror("Could not listen on sockets");
}
#endif
Expand Down Expand Up @@ -1893,7 +1870,7 @@ int priv_domid = 0;
int main(int argc, char *argv[])
{
int opt;
int sock_pollfd_idx = -1, ro_sock_pollfd_idx = -1;
int sock_pollfd_idx = -1;
bool dofork = true;
bool outputpid = false;
bool no_domain_init = false;
Expand Down Expand Up @@ -2010,7 +1987,7 @@ int main(int argc, char *argv[])
tracefile = talloc_strdup(NULL, tracefile);

/* Get ready to listen to the tools. */
initialize_fds(&sock_pollfd_idx, &ro_sock_pollfd_idx, &timeout);
initialize_fds(&sock_pollfd_idx, &timeout);

/* Tell the kernel we're up and running. */
xenbus_notify_running();
Expand Down Expand Up @@ -2051,21 +2028,11 @@ int main(int argc, char *argv[])
barf_perror("sock poll failed");
break;
} else if (fds[sock_pollfd_idx].revents & POLLIN) {
accept_connection(sock, true);
accept_connection(sock);
sock_pollfd_idx = -1;
}
}

if (ro_sock_pollfd_idx != -1) {
if (fds[ro_sock_pollfd_idx].revents & ~POLLIN) {
barf_perror("ro sock poll failed");
break;
} else if (fds[ro_sock_pollfd_idx].revents & POLLIN) {
accept_connection(ro_sock, false);
ro_sock_pollfd_idx = -1;
}
}

if (xce_pollfd_idx != -1) {
if (fds[xce_pollfd_idx].revents & ~POLLIN) {
barf_perror("xce_handle poll failed");
Expand Down Expand Up @@ -2128,7 +2095,7 @@ int main(int argc, char *argv[])
}
}

initialize_fds(&sock_pollfd_idx, &ro_sock_pollfd_idx, &timeout);
initialize_fds(&sock_pollfd_idx, &timeout);
}
}

Expand Down
3 changes: 0 additions & 3 deletions tools/xenstore/xenstored_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ struct connection
/* Who am I? 0 for socket connections. */
unsigned int id;

/* Is this a read-only connection? */
bool can_write;

/* Buffered incoming data. */
struct buffered_data *in;

Expand Down
4 changes: 2 additions & 2 deletions tools/xenstore/xenstored_domain.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ int do_introduce(struct connection *conn, struct buffered_data *in)
if (get_strings(in, vec, ARRAY_SIZE(vec)) < ARRAY_SIZE(vec))
return EINVAL;

if (domain_is_unprivileged(conn) || !conn->can_write)
if (domain_is_unprivileged(conn))
return EACCES;

domid = atoi(vec[0]);
Expand Down Expand Up @@ -438,7 +438,7 @@ int do_set_target(struct connection *conn, struct buffered_data *in)
if (get_strings(in, vec, ARRAY_SIZE(vec)) < ARRAY_SIZE(vec))
return EINVAL;

if (domain_is_unprivileged(conn) || !conn->can_write)
if (domain_is_unprivileged(conn))
return EACCES;

domid = atoi(vec[0]);
Expand Down
8 changes: 1 addition & 7 deletions tools/xenstore/xs_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,7 @@ const char *xs_daemon_socket(void)

const char *xs_daemon_socket_ro(void)
{
static char buf[PATH_MAX];
const char *s = xs_daemon_path();
if (s == NULL)
return NULL;
if (snprintf(buf, sizeof(buf), "%s_ro", s) >= PATH_MAX)
return NULL;
return buf;
return xs_daemon_path();
}

const char *xs_domain_dev(void)
Expand Down

0 comments on commit 1d246c7

Please sign in to comment.