1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Authors: Ryan Lortie <desrt@desrt.ca>
16 #include "gunixconnection.h"
17 #include "gunixcredentialsmessage.h"
21 * SECTION: gunixconnection
22 * @title: GUnixConnection
23 * @short_description: A UNIX domain GSocketConnection
24 * @include: gio/gunixconnection.h
25 * @see_also: #GSocketConnection.
27 * This is the subclass of #GSocketConnection that is created
28 * for UNIX domain sockets.
30 * It contains functions to do some of the UNIX socket specific
31 * functionality like passing file descriptors.
33 * Note that <filename><gio/gunixconnection.h></filename> belongs to
34 * the UNIX-specific GIO interfaces, thus you have to use the
35 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
40 #include <gio/gsocketcontrolmessage.h>
41 #include <gio/gunixfdmessage.h>
42 #include <gio/gsocket.h>
46 /* for getsockopt() and setsockopt() */
47 #include <sys/types.h> /* See NOTES */
48 #include <sys/socket.h>
55 G_DEFINE_TYPE_WITH_CODE (GUnixConnection, g_unix_connection,
56 G_TYPE_SOCKET_CONNECTION,
57 g_socket_connection_factory_register_type (g_define_type_id,
60 G_SOCKET_PROTOCOL_DEFAULT);
64 * g_unix_connection_send_fd:
65 * @connection: a #GUnixConnection
66 * @fd: a file descriptor
67 * @cancellable: optional #GCancellable object, %NULL to ignore.
68 * @error: #GError for error reporting, or %NULL to ignore.
70 * Passes a file descriptor to the recieving side of the
71 * connection. The recieving end has to call g_unix_connection_receive_fd()
72 * to accept the file descriptor.
74 * As well as sending the fd this also writes a single byte to the
75 * stream, as this is required for fd passing to work on some
78 * Returns: a %TRUE on success, %NULL on error.
83 g_unix_connection_send_fd (GUnixConnection *connection,
85 GCancellable *cancellable,
88 GSocketControlMessage *scm;
91 g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
92 g_return_val_if_fail (fd >= 0, FALSE);
94 scm = g_unix_fd_message_new ();
96 if (!g_unix_fd_message_append_fd (G_UNIX_FD_MESSAGE (scm), fd, error))
102 g_object_get (connection, "socket", &socket, NULL);
103 if (g_socket_send_message (socket, NULL, NULL, 0, &scm, 1, 0, cancellable, error) != 1)
104 /* XXX could it 'fail' with zero? */
106 g_object_unref (socket);
107 g_object_unref (scm);
112 g_object_unref (socket);
113 g_object_unref (scm);
119 * g_unix_connection_receive_fd:
120 * @connection: a #GUnixConnection
121 * @cancellable: optional #GCancellable object, %NULL to ignore
122 * @error: #GError for error reporting, or %NULL to ignore
124 * Receives a file descriptor from the sending end of the connection.
125 * The sending end has to call g_unix_connection_send_fd() for this
128 * As well as reading the fd this also reads a single byte from the
129 * stream, as this is required for fd passing to work on some
132 * Returns: a file descriptor on success, -1 on error.
137 g_unix_connection_receive_fd (GUnixConnection *connection,
138 GCancellable *cancellable,
141 GSocketControlMessage **scms;
142 gint *fds, nfd, fd, nscm;
143 GUnixFDMessage *fdmsg;
146 g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), -1);
148 g_object_get (connection, "socket", &socket, NULL);
149 if (g_socket_receive_message (socket, NULL, NULL, 0,
150 &scms, &nscm, NULL, cancellable, error) != 1)
151 /* XXX it _could_ 'fail' with zero. */
153 g_object_unref (socket);
158 g_object_unref (socket);
164 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
165 _("Expecting 1 control message, got %d"), nscm);
167 for (i = 0; i < nscm; i++)
168 g_object_unref (scms[i]);
175 if (!G_IS_UNIX_FD_MESSAGE (scms[0]))
177 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
178 _("Unexpected type of ancillary data"));
179 g_object_unref (scms[0]);
185 fdmsg = G_UNIX_FD_MESSAGE (scms[0]);
188 fds = g_unix_fd_message_steal_fds (fdmsg, &nfd);
189 g_object_unref (fdmsg);
195 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
196 _("Expecting one fd, but got %d\n"), nfd);
198 for (i = 0; i < nfd; i++)
211 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
212 _("Received invalid fd"));
220 g_unix_connection_init (GUnixConnection *connection)
225 g_unix_connection_class_init (GUnixConnectionClass *class)
229 /* TODO: Other stuff we might want to add are:
230 void g_unix_connection_send_fd_async (GUnixConnection *connection,
234 GAsyncReadyCallback callback,
236 gboolean g_unix_connection_send_fd_finish (GUnixConnection *connection,
239 gboolean g_unix_connection_send_fds (GUnixConnection *connection,
243 void g_unix_connection_send_fds_async (GUnixConnection *connection,
247 GAsyncReadyCallback callback,
249 gboolean g_unix_connection_send_fds_finish (GUnixConnection *connection,
252 void g_unix_connection_receive_fd_async (GUnixConnection *connection,
254 GAsyncReadyCallback callback,
256 gint g_unix_connection_receive_fd_finish (GUnixConnection *connection,
260 gboolean g_unix_connection_send_credentials (GUnixConnection *connection,
262 void g_unix_connection_send_credentials_async (GUnixConnection *connection,
264 GAsyncReadyCallback callback,
266 gboolean g_unix_connection_send_credentials_finish (GUnixConnection *connection,
269 gboolean g_unix_connection_send_fake_credentials (GUnixConnection *connection,
274 void g_unix_connection_send_fake_credentials_async (GUnixConnection *connection,
279 GAsyncReadyCallback callback,
281 gboolean g_unix_connection_send_fake_credentials_finish (GUnixConnection *connection,
284 gboolean g_unix_connection_receive_credentials (GUnixConnection *connection,
289 void g_unix_connection_receive_credentials_async (GUnixConnection *connection,
291 GAsyncReadyCallback callback,
293 gboolean g_unix_connection_receive_credentials_finish (GUnixConnection *connection,
299 gboolean g_unix_connection_create_pair (GUnixConnection **one,
300 GUnixConnection **two,
306 * g_unix_connection_send_credentials:
307 * @connection: A #GUnixConnection.
308 * @cancellable: A #GCancellable or %NULL.
309 * @error: Return location for error or %NULL.
311 * Passes the credentials of the current user the receiving side
312 * of the connection. The recieving end has to call
313 * g_unix_connection_receive_credentials() (or similar) to accept the
316 * As well as sending the credentials this also writes a single NUL
317 * byte to the stream, as this is required for credentials passing to
318 * work on some implementations.
320 * Note that this function only works on Linux, currently.
322 * Returns: %TRUE on success, %FALSE if @error is set.
327 g_unix_connection_send_credentials (GUnixConnection *connection,
328 GCancellable *cancellable,
331 GCredentials *credentials;
332 GSocketControlMessage *scm;
335 GOutputVector vector;
336 guchar nul_byte[1] = {'\0'};
338 g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
339 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
343 credentials = g_credentials_new ();
345 vector.buffer = &nul_byte;
347 scm = g_unix_credentials_message_new_with_credentials (credentials);
348 g_object_get (connection, "socket", &socket, NULL);
349 if (g_socket_send_message (socket,
359 g_prefix_error (error, _("Error sending credentials: "));
366 g_object_unref (socket);
367 g_object_unref (scm);
368 g_object_unref (credentials);
373 * g_unix_connection_receive_credentials:
374 * @connection: A #GUnixConnection.
375 * @cancellable: A #GCancellable or %NULL.
376 * @error: Return location for error or %NULL.
378 * Receives credentials from the sending end of the connection. The
379 * sending end has to call g_unix_connection_send_credentials() (or
380 * similar) for this to work.
382 * As well as reading the credentials this also reads (and discards) a
383 * single byte from the stream, as this is required for credentials
384 * passing to work on some implementations.
386 * Returns: Received credentials on success (free with
387 * g_object_unref()), %NULL if @error is set.
392 g_unix_connection_receive_credentials (GUnixConnection *connection,
393 GCancellable *cancellable,
397 GSocketControlMessage **scms;
401 volatile GType credentials_message_gtype;
402 gssize num_bytes_read;
404 gboolean turn_off_so_passcreds;
407 g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), NULL);
408 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
413 g_object_get (connection, "socket", &socket, NULL);
415 /* On Linux, we need to turn on SO_PASSCRED if it isn't enabled
416 * already. We also need to turn it off when we're done. See
417 * #617483 for more discussion.
424 turn_off_so_passcreds = FALSE;
426 opt_len = sizeof (gint);
427 if (getsockopt (g_socket_get_fd (socket),
435 g_io_error_from_errno (errno),
436 _("Error checking if SO_PASSCRED is enabled for socket: %s"),
440 if (opt_len != sizeof (gint))
445 _("Unexpected option length while checking if SO_PASSCRED is enabled for socket. "
446 "Expected %d bytes, got %d"),
447 (gint) sizeof (gint), (gint) opt_len);
453 if (setsockopt (g_socket_get_fd (socket),
457 sizeof opt_val) != 0)
461 g_io_error_from_errno (errno),
462 _("Error enabling SO_PASSCRED: %s"),
466 turn_off_so_passcreds = TRUE;
471 /* ensure the type of GUnixCredentialsMessage has been registered with the type system */
472 credentials_message_gtype = G_TYPE_UNIX_CREDENTIALS_MESSAGE;
473 num_bytes_read = g_socket_receive_message (socket,
474 NULL, /* GSocketAddress **address */
482 if (num_bytes_read != 1)
484 /* Handle situation where g_socket_receive_message() returns
485 * 0 bytes and not setting @error
487 if (num_bytes_read == 0 && error != NULL && *error == NULL)
489 g_set_error_literal (error,
492 _("Expecting to read a single byte for receiving credentials but read zero bytes"));
502 _("Expecting 1 control message, got %d"),
507 if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
509 g_set_error_literal (error,
512 _("Unexpected type of ancillary data"));
516 ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
522 if (turn_off_so_passcreds)
526 if (setsockopt (g_socket_get_fd (socket),
530 sizeof opt_val) != 0)
534 g_io_error_from_errno (errno),
535 _("Error while disabling SO_PASSCRED: %s"),
544 for (n = 0; n < nscm; n++)
545 g_object_unref (scms[n]);
548 g_object_unref (socket);
552 #define __G_UNIX_CONNECTION_C__
553 #include "gioaliasdef.c"