unix-fd.c 6.31 KB
Newer Older
1
#include <gio/gio.h>
Dan Winship's avatar
Dan Winship committed
2
#include <gio/gnetworking.h>
3
#include <gio/gunixfdmessage.h>
4
#include <gio/gunixsocketaddress.h>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include <string.h>
#include <unistd.h>

/* ensures that no FDs are left open at the end */
static void
check_fd_list (const gint *fd_list)
{
  gint i;

  for (i = 0; i < 40; i++)
    {
      int my_fd;

      my_fd = dup (0);
      g_assert_cmpint (fd_list[i], ==, my_fd);
    }

  for (i = 0; i < 40; i++)
    close (fd_list[i]);
}

static void
create_fd_list (gint *fd_list)
{
  gint i;

  for (i = 0; i < 40; i++)
    {
      fd_list[i] = dup (0);
      g_assert_cmpint (fd_list[i], >, 0);
    }

  for (i = 0; i < 40; i++)
    close (fd_list[i]);
}

static void
test_fds (void)
{
44
  GError *err = NULL;
45 46 47 48
  GUnixFDMessage *message;
  GUnixFDMessage **mv;
  GUnixFDList *list, *l2;
  GSocket *sockets[2];
49
  GSocketAddress *addr;
50 51 52 53 54 55 56 57 58 59
  const gint *peek;
  char buffer[1024];
  gint fd_list[40];
  GOutputVector ov;
  GInputVector iv;
  gint *stolen;
  gint sv[3];
  gint flags;
  gint nm;
  gint s;
60 61 62 63
  gchar *path;
  GByteArray *array;
  gboolean abstract;
  GUnixSocketAddressType type;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

  create_fd_list (fd_list);

  s = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
  g_assert_cmpint (s, ==, 0);
  sv[2] = -1;

  list = g_unix_fd_list_new_from_array (sv, -1);
  message = G_UNIX_FD_MESSAGE (g_unix_fd_message_new_with_fd_list (list));
  g_object_unref (list);

  g_assert (g_unix_fd_message_get_fd_list (message) == list);
  g_object_get (message, "fd-list", &l2, NULL);
  g_assert (l2 == list);
  g_assert_cmpint (g_unix_fd_list_get_length (list), ==, 2);

  peek = g_unix_fd_list_peek_fds (list, &s);
  g_assert_cmpint (s, ==, 2);
  stolen = g_unix_fd_message_steal_fds (message, &s);
  g_assert_cmpint (s, ==, 2);
  g_assert (stolen == peek);

  g_assert_cmpint (stolen[0], ==, sv[0]);
  g_assert_cmpint (stolen[1], ==, sv[1]);
  g_assert_cmpint (stolen[2], ==, sv[2]);
  g_free (stolen);

91 92
  g_unix_fd_message_append_fd (message, sv[0], &err);
  g_assert_no_error (err);
93 94
  s = close (sv[0]);
  g_assert_cmpint (s, ==, 0);
95 96
  g_unix_fd_message_append_fd (message, sv[1], &err);
  g_assert_no_error (err);
97 98 99
  s = close (sv[1]);
  g_assert_cmpint (s, ==, 0);

100 101
  s = close (g_unix_fd_list_get (list, 0, &err));
  g_assert_no_error (err);
102
  g_assert_cmpint (s, ==, 0);
103 104
  s = close (g_unix_fd_list_get (list, 1, &err));
  g_assert_no_error (err);
105
  g_assert_cmpint (s, ==, 0);
106 107
  s = close (g_unix_fd_list_get (list, 0, &err));
  g_assert_no_error (err);
108
  g_assert_cmpint (s, ==, 0);
109 110
  s = close (g_unix_fd_list_get (list, 1, &err));
  g_assert_no_error (err);
111
  g_assert_cmpint (s, ==, 0);
112 113
  s = close (g_unix_fd_list_get (list, 0, &err));
  g_assert_no_error (err);
114
  g_assert_cmpint (s, ==, 0);
115 116
  s = close (g_unix_fd_list_get (list, 1, &err));
  g_assert_no_error (err);
117 118 119 120 121 122 123 124 125 126
  g_assert_cmpint (s, ==, 0);

  g_object_unref (message);
  g_object_unref (list);

  message = G_UNIX_FD_MESSAGE (g_unix_fd_message_new ());
  list = g_unix_fd_message_get_fd_list (message);
  s = pipe (sv);
  g_assert_cmpint (s, ==, 0);

127 128
  s = g_unix_fd_list_append (list, sv[0], &err);
  g_assert_no_error (err);
129
  g_assert_cmpint (s, >=, 0);
130 131
  s = g_unix_fd_list_append (list, sv[1], &err);
  g_assert_no_error (err);
132 133 134 135 136 137
  g_assert_cmpint (s, >=, 0);

  s = close (sv[0]);
  g_assert_cmpint (s, ==, 0);
  s = close (sv[1]);
  g_assert_cmpint (s, ==, 0);
138 139
  s = close (g_unix_fd_list_get (list, 0, &err));
  g_assert_no_error (err);
140
  g_assert_cmpint (s, ==, 0);
141 142
  s = close (g_unix_fd_list_get (list, 1, &err));
  g_assert_no_error (err);
143 144 145 146 147
  g_assert_cmpint (s, ==, 0);

  s = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
  g_assert_cmpint (s, ==, 0);

148 149
  sockets[0] = g_socket_new_from_fd (sv[0], &err);
  g_assert_no_error (err);
150
  g_assert (G_IS_SOCKET (sockets[0]));
151 152
  sockets[1] = g_socket_new_from_fd (sv[1], &err);
  g_assert_no_error (err);
153 154
  g_assert (G_IS_SOCKET (sockets[1]));

155 156 157 158 159
  addr = g_socket_get_local_address (sockets[0], &err);
  g_assert_no_error (err);
  g_assert (G_IS_UNIX_SOCKET_ADDRESS (addr));
  g_assert_cmpint (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (addr)), ==, G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
  g_assert_cmpint (g_unix_socket_address_get_path_len (G_UNIX_SOCKET_ADDRESS (addr)), ==, 0);
160

161 162 163 164
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  g_assert (!g_unix_socket_address_get_is_abstract (G_UNIX_SOCKET_ADDRESS (addr)));
G_GNUC_END_IGNORE_DEPRECATIONS

165 166 167 168 169 170 171 172 173 174 175 176 177
  g_object_get (addr,
                "path", &path,
                "path-as-array", &array,
                "abstract", &abstract,
                "address-type", &type,
                NULL);
  g_assert_cmpstr (path, ==, "");
  g_assert_cmpint (array->len, ==, 0);
  g_assert (!abstract);
  g_assert (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
  g_free (path);
  g_byte_array_free (array, TRUE);

178 179
  g_object_unref (addr);

180 181 182 183 184
  buffer[0] = 0xff;
  ov.buffer = buffer;
  ov.size = 1;
  s = g_socket_send_message (sockets[0], NULL, &ov, 1,
                             (GSocketControlMessage **) &message,
185 186
                             1, 0, NULL, &err);
  g_assert_no_error (err);
187 188 189 190 191 192 193 194 195 196
  g_assert_cmpint (s, ==, 1);
  g_object_unref (message);

  message = NULL;

  flags = 0;
  iv.buffer = buffer;
  iv.size = 1;
  s = g_socket_receive_message (sockets[1], NULL, &iv, 1,
                                (GSocketControlMessage ***) &mv,
197 198
                                &nm, &flags, NULL, &err);
  g_assert_no_error (err);
199 200 201 202 203 204 205 206 207 208 209 210 211 212
  g_assert_cmpint (s, ==, 1);
  g_object_unref (sockets[0]);
  g_object_unref (sockets[1]);

  g_assert_cmpint (nm, ==, 1);
  message = mv[0];
  g_free (mv);

  g_assert (G_IS_UNIX_FD_MESSAGE (message));
  list = g_object_ref (g_unix_fd_message_get_fd_list (message));
  g_object_unref (message);

  peek = g_unix_fd_list_peek_fds (list, &s);
  g_assert_cmpint (s, ==, 2);
213 214
  sv[0] = g_unix_fd_list_get (list, 1, &err);
  g_assert_no_error (err);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

  strcpy (buffer, "failure to say failure to say 'i love gnome-panel!'.");
  s = write (sv[0], buffer, strlen (buffer) + 1);
  g_assert_cmpint (s, ==, strlen (buffer) + 1);

  close (sv[0]);
  memset (buffer, 0xff, sizeof buffer);

  s = read (peek[0], buffer, sizeof buffer);
  g_assert_cmpint (s, ==, 53);
  g_assert_cmpstr (buffer, ==,
                   "failure to say failure to say 'i love gnome-panel!'.");

  g_object_unref (list);

  check_fd_list (fd_list);
}

int
main (int argc, char **argv)
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/unix-streams/file-descriptors", test_fds);

  return g_test_run();

}