[Spice-devel] [RFC spice-vdagent 08/18] udscs-server: split initialization
Jakub Janků
jjanku at redhat.com
Tue Aug 14 18:53:42 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
udscs_server_listen_to_address() creates a new socket and
binds it to the given address.
Remove udscs_create_server() and udscs_create_server_for_fd().
TODO: maybe squash with the previous commit
if this is the way to go?
---
src/udscs.c | 46 ++++++++++-------------------------------
src/udscs.h | 25 +++++++++-------------
src/vdagentd/vdagentd.c | 25 +++++++++-------------
3 files changed, 31 insertions(+), 65 deletions(-)
diff --git a/src/udscs.c b/src/udscs.c
index 45565b6..4a657c9 100644
--- a/src/udscs.c
+++ b/src/udscs.c
@@ -216,7 +216,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,
@@ -242,55 +242,34 @@ 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,
- const char * const type_to_string[], int no_types, int debug)
+void udscs_server_listen_to_socket(struct udscs_server **server, gint fd)
{
- struct udscs_server *server;
GSocket *socket;
GError *err = NULL;
- server = udscs_server_new(connect_callback, read_callback, disconnect_callback,
- type_to_string, no_types, debug);
- if (server == NULL)
- return NULL;
-
socket = g_socket_new_from_fd(fd, &err);
if (err)
goto error;
- g_socket_listener_add_socket(G_SOCKET_LISTENER(server->service),
+ g_socket_listener_add_socket(G_SOCKET_LISTENER((*server)->service),
socket, NULL, &err);
g_object_unref(socket);
- if (err)
- goto error;
+ if (!err)
+ return;
- return server;
error:
syslog(LOG_ERR, "%s: %s", __func__, err->message);
g_error_free(err);
- udscs_destroy_server(server);
- return NULL;
+ g_clear_pointer(server, udscs_destroy_server);
}
-struct udscs_server *udscs_create_server(const char *socketname,
- udscs_connect_callback connect_callback,
- udscs_read_callback read_callback,
- udscs_disconnect_callback disconnect_callback,
- const char * const type_to_string[], int no_types, int debug)
+void udscs_server_listen_to_address(struct udscs_server **server,
+ const gchar *address)
{
- struct udscs_server *server;
GSocketAddress *socket_addr;
GError *err = NULL;
- server = udscs_server_new(connect_callback, read_callback, disconnect_callback,
- type_to_string, no_types, debug);
- if (server == NULL)
- return NULL;
-
- socket_addr = g_unix_socket_address_new(socketname);
- g_socket_listener_add_address(G_SOCKET_LISTENER(server->service),
+ 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,
@@ -299,11 +278,8 @@ struct udscs_server *udscs_create_server(const char *socketname,
if (err) {
syslog(LOG_ERR, "%s: %s", __func__, err->message);
g_error_free(err);
- udscs_destroy_server(server);
- return NULL;
+ g_clear_pointer(server, udscs_destroy_server);
}
-
- return server;
}
void udscs_destroy_server(struct udscs_server *server)
diff --git a/src/udscs.h b/src/udscs.h
index dba3fb9..d02d5b4 100644
--- a/src/udscs.h
+++ b/src/udscs.h
@@ -102,20 +102,7 @@ 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,
- const char * const type_to_string[], int no_types, 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.
@@ -124,12 +111,20 @@ struct udscs_server *udscs_create_server_for_fd(int fd,
* and type_to_string must point to a string array of size no_types for
* converting the message ids to their 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,
const char * const type_to_string[], int no_types, 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);
+
+/* 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);
+
/* 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 027406f..fd54723 100644
--- a/src/vdagentd/vdagentd.c
+++ b/src/vdagentd/vdagentd.c
@@ -1105,6 +1105,12 @@ 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,
+ vdagentd_messages, VDAGENTD_NO_MESSAGES, debug);
+ if (server == NULL) {
+ syslog(LOG_CRIT, "Fatal could not allocate memory for udscs server");
+ return 1;
+ }
#ifdef WITH_SYSTEMD_SOCKET_ACTIVATION
int n_fds;
/* try to retrieve pre-configured sockets from systemd */
@@ -1113,29 +1119,18 @@ 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,
- vdagentd_messages,
- VDAGENTD_NO_MESSAGES, debug);
+ udscs_server_listen_to_socket(&server, SD_LISTEN_FDS_START);
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,
- vdagentd_messages, VDAGENTD_NO_MESSAGES,
- debug);
+ udscs_server_listen_to_address(&server, vdagentd_socket);
}
if (!server) {
- if (errno == ENOMEM) {
- syslog(LOG_CRIT, "Fatal could not allocate memory for udscs server");
- } else {
- syslog(LOG_CRIT, "Fatal could not create the server socket %s",
- vdagentd_socket);
- }
+ syslog(LOG_CRIT, "Fatal could not create the server socket %s",
+ vdagentd_socket);
return 1;
}
--
2.17.1
More information about the Spice-devel
mailing list