[Spice-devel] [PATCH vdagent-linux 4/4] udscs-server: split initialization
Jakub Janků
jjanku at redhat.com
Sun Sep 30 18:05:23 UTC 2018
Divide creation of udscs_server into 2 steps:
1) udscs_server_new()
- allocates new udscs_server struct and inits it
2) udscs_server_listen_to_fd(), udscs_server_listen_to_address()
- starts accepting new connections
Remove udscs_create_server() and udscs_create_server_for_fd().
This makes the code a bit shorter and nicer.
Signed-off-by: Jakub Janků <jjanku at redhat.com>
---
src/udscs.c | 56 +++++++++--------------------------------
src/udscs.h | 27 +++++++++-----------
src/vdagentd/vdagentd.c | 24 +++++++-----------
3 files changed, 33 insertions(+), 74 deletions(-)
diff --git a/src/udscs.c b/src/udscs.c
index 3bf0089..130e22e 100644
--- a/src/udscs.c
+++ b/src/udscs.c
@@ -207,7 +207,7 @@ static gboolean udscs_server_accept_cb(GSocketService *service,
GObject *source_object,
gpointer user_data);
-static struct udscs_server *udscs_server_new(
+struct udscs_server *udscs_server_new(
udscs_connect_callback connect_callback,
udscs_read_callback read_callback,
udscs_disconnect_callback disconnect_callback,
@@ -228,64 +228,32 @@ static struct udscs_server *udscs_server_new(
return server;
}
-struct udscs_server *udscs_create_server_for_fd(int fd,
- udscs_connect_callback connect_callback,
- udscs_read_callback read_callback,
- udscs_disconnect_callback disconnect_callback,
- int debug)
+void udscs_server_listen_to_socket(struct udscs_server *server,
+ gint fd, GError **err)
{
- struct udscs_server *server;
GSocket *socket;
- GError *err = NULL;
-
- server = udscs_server_new(connect_callback, read_callback,
- disconnect_callback, debug);
- socket = g_socket_new_from_fd(fd, &err);
- if (err)
- goto error;
+ socket = g_socket_new_from_fd(fd, err);
+ if (socket == NULL)
+ return;
g_socket_listener_add_socket(G_SOCKET_LISTENER(server->service),
- socket, NULL, &err);
+ socket, NULL, err);
g_object_unref(socket);
- if (err)
- goto error;
-
- return server;
-error:
- syslog(LOG_ERR, "%s: %s", __func__, err->message);
- g_error_free(err);
- udscs_destroy_server(server);
- return NULL;
}
-struct udscs_server *udscs_create_server(const char *socketname,
- udscs_connect_callback connect_callback,
- udscs_read_callback read_callback,
- udscs_disconnect_callback disconnect_callback,
- int debug)
+void udscs_server_listen_to_address(struct udscs_server *server,
+ const gchar *address,
+ GError **err)
{
- struct udscs_server *server;
GSocketAddress *socket_addr;
- GError *err = NULL;
-
- server = udscs_server_new(connect_callback, read_callback,
- disconnect_callback, debug);
- socket_addr = g_unix_socket_address_new(socketname);
+ socket_addr = g_unix_socket_address_new(address);
g_socket_listener_add_address(G_SOCKET_LISTENER(server->service),
socket_addr,
G_SOCKET_TYPE_STREAM,
G_SOCKET_PROTOCOL_DEFAULT,
- NULL, NULL, &err);
+ NULL, NULL, err);
g_object_unref(socket_addr);
- if (err) {
- syslog(LOG_ERR, "%s: %s", __func__, err->message);
- g_error_free(err);
- udscs_destroy_server(server);
- return NULL;
- }
-
- return server;
}
void udscs_destroy_server(struct udscs_server *server)
diff --git a/src/udscs.h b/src/udscs.h
index 1c7fa2b..c9a45f5 100644
--- a/src/udscs.h
+++ b/src/udscs.h
@@ -98,31 +98,28 @@ struct udscs_server;
*/
typedef void (*udscs_connect_callback)(struct udscs_connection *conn);
-/* Create a server for the given file descriptor. This allows us to use
- * pre-configured sockets for use with systemd socket activation, etc.
- *
- * See udscs_create_server() for more information
- */
-struct udscs_server *udscs_create_server_for_fd(int fd,
- udscs_connect_callback connect_callback,
- udscs_read_callback read_callback,
- udscs_disconnect_callback disconnect_callback,
- int debug);
-
-/* Create the unix domain socket specified by socketname and
- * start listening on it.
- * Only sockets bound to a pathname are supported.
+/* Initialize a new udscs_server struct.
*
* If debug is true then the events on this socket and related individual
* connections will be traced.
* This includes the incoming and outgoing message names.
*/
-struct udscs_server *udscs_create_server(const char *socketname,
+struct udscs_server *udscs_server_new(
udscs_connect_callback connect_callback,
udscs_read_callback read_callback,
udscs_disconnect_callback disconnect_callback,
int debug);
+/* Start listening on a pre-configured socket specified by the given @fd.
+ * This can be used with systemd socket activation, etc. */
+void udscs_server_listen_to_socket(struct udscs_server *server, gint fd,
+ GError **err);
+
+/* Create a new socket, bind it to @address and start listening on it. */
+void udscs_server_listen_to_address(struct udscs_server *server,
+ const gchar *address,
+ GError **err);
+
/* Close all the server's connections and releases the corresponding
* resources.
* Does nothing if server is NULL.
diff --git a/src/vdagentd/vdagentd.c b/src/vdagentd/vdagentd.c
index f52f039..22b61b3 100644
--- a/src/vdagentd/vdagentd.c
+++ b/src/vdagentd/vdagentd.c
@@ -1100,6 +1100,8 @@ int main(int argc, char *argv[])
openlog("spice-vdagentd", do_daemonize ? 0 : LOG_PERROR, LOG_USER);
/* Setup communication with vdagent process(es) */
+ server = udscs_server_new(agent_connect, agent_read_complete,
+ agent_disconnect, debug);
#ifdef WITH_SYSTEMD_SOCKET_ACTIVATION
int n_fds;
/* try to retrieve pre-configured sockets from systemd */
@@ -1108,28 +1110,20 @@ int main(int argc, char *argv[])
syslog(LOG_CRIT, "Received too many sockets from systemd (%i)", n_fds);
return 1;
} else if (n_fds == 1) {
- server = udscs_create_server_for_fd(SD_LISTEN_FDS_START, agent_connect,
- agent_read_complete,
- agent_disconnect,
- debug);
+ udscs_server_listen_to_socket(server, SD_LISTEN_FDS_START, &err);
own_socket = FALSE;
} else
/* systemd socket activation not enabled, create our own */
#endif /* WITH_SYSTEMD_SOCKET_ACTIVATION */
{
- server = udscs_create_server(vdagentd_socket, agent_connect,
- agent_read_complete, agent_disconnect,
- debug);
+ udscs_server_listen_to_address(server, vdagentd_socket, &err);
}
- if (!server) {
- if (errno == EADDRINUSE) {
- syslog(LOG_CRIT, "Fatal the server socket %s exists already. Delete it?",
- vdagentd_socket);
- } else {
- syslog(LOG_CRIT, "Fatal could not create the server socket %s",
- vdagentd_socket);
- }
+ if (err) {
+ syslog(LOG_CRIT, "Fatal could not create the server socket %s: %s",
+ vdagentd_socket, err->message);
+ g_error_free(err);
+ udscs_destroy_server(server);
return 1;
}
--
2.17.1
More information about the Spice-devel
mailing list