summaryrefslogtreecommitdiff
path: root/src/lib/ecore_con/efl_net_server_unix.c
blob: 37d2c7302d0323c737dc046f9993df06aa06a9a5 (plain) (blame)
1
2
3
4
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#define EFL_NET_SERVER_UNIX_PROTECTED 1
#define EFL_NET_SERVER_FD_PROTECTED 1
#define EFL_NET_SERVER_PROTECTED 1
#define EFL_LOOP_FD_PROTECTED 1

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "Ecore.h"
#include "Ecore_Con.h"
#include "ecore_con_private.h"

#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#ifdef HAVE_SYS_UN_H
#include <sys/un.h>
#endif

// BSD workaround - unable to reproduce.... but we seem to
// get a blocking bind here if 2 processes fight over the same
// socket where one of them loses out by sitting here and
// blockign forever - as i can't reproduce in the freebsd vm
// i have, so i'm limited in what to do so this is a
// workaround to try mitigate this
#if defined (__FreeBSD__)
# define BIND_HANG_WORKAROUND 1
#else
// only need on freebsd
//# define BIND_HANG_WORKAROUND 1
#endif

#ifdef BIND_HANG_WORKAROUND
# include <sys/file.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif

/* no include EVIL as it's not supposed to be compiled on Windows */

#define MY_CLASS EFL_NET_SERVER_UNIX_CLASS

typedef struct _Efl_Net_Server_Unix_Data
{
   unsigned int leading_directories_create_mode;
#ifdef BIND_HANG_WORKAROUND
   int lock_fd;
   Eina_Bool have_lock_fd : 1;
#endif
   Eina_Bool leading_directories_create : 1;
   Eina_Bool unlink_before_bind : 1;
} Efl_Net_Server_Unix_Data;

#ifdef BIND_HANG_WORKAROUND
static int
_efl_net_server_unix_bind_hang_lock_workaround(const char *address, Eina_Bool lock, int lockfile_fd)
{
   size_t addrlen;
   char *lockfile;
   int ret;

   addrlen = strlen(address);
   lockfile = malloc(addrlen + 1 + 5);
   if (!lockfile) return -1;

   strcpy(lockfile, address);
   strncpy(lockfile + addrlen, ".lock", 6);
   if (lock)
     {
#ifdef HAVE_OPEN_CLOEXEC
        lockfile_fd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC,
                           S_IRUSR | S_IWUSR);
        if (lockfile_fd < 0) return -1;
#else
        lockfile_fd = open(lockfile, O_RDWR | O_CREAT | O_TRUNC,
                           S_IRUSR | S_IWUSR);
        if (lockfile_fd < 0) return -1;
        eina_file_close_on_exec(lockfile_fd, EINA_TRUE);
#endif
     }

   errno = 0;
   if (lock)
     {
        ret = flock(lockfile_fd, LOCK_EX | LOCK_NB);
        if ((ret != 0) && (errno == EWOULDBLOCK))
          {
             close(lockfile_fd);
             lockfile_fd = -1;
          }
     }
   else
     {
        flock(lockfile_fd, LOCK_UN | LOCK_NB);
        unlink(lockfile);
        close(lockfile_fd);
        lockfile_fd = -1;
     }
   free(lockfile);
   return lockfile_fd;
}
#endif

EOLIAN static void
_efl_net_server_unix_efl_object_destructor(Eo *o, Efl_Net_Server_Unix_Data *pd EINA_UNUSED)
{
   SOCKET fd = efl_loop_fd_get(o);

   if (fd != INVALID_SOCKET)
     {
        const char *address = efl_net_server_address_get(o);
        if ((address) && (strncmp(address, "abstract:", strlen("abstract:")) != 0))
          {
             unlink(address);
#ifdef BIND_HANG_WORKAROUND
             if ((pd->have_lock_fd) && (pd->lock_fd >= 0))
               {
                  _efl_net_server_unix_bind_hang_lock_workaround
                    (address, EINA_FALSE, pd->lock_fd);
                  pd->lock_fd = -1;
                  pd->have_lock_fd = EINA_FALSE;
               }
#endif
          }
     }

   efl_destructor(efl_super(o, MY_CLASS));
}

static Eina_Error
_efl_net_server_unix_bind(Eo *o, Efl_Net_Server_Unix_Data *pd)
{
   const char *address = efl_net_server_address_get(o);
   struct sockaddr_un addr = { .sun_family = AF_UNIX };
   socklen_t addrlen;
   SOCKET fd = INVALID_SOCKET;
   Eina_Error err = 0;
   int r;

   efl_net_server_fd_family_set(o, AF_UNIX);

   if ((pd->leading_directories_create) &&
       (strncmp(address, "abstract:", strlen("abstract:")) != 0))
     _ecore_con_local_mkpath(address, pd->leading_directories_create_mode);

   do
     {
        fd = efl_net_socket4(AF_UNIX, SOCK_STREAM, 0,
                             efl_net_server_fd_close_on_exec_get(o));
        if (fd == INVALID_SOCKET)
          {
             err = efl_net_socket_error_get();
             ERR("socket(AF_UNIX, SOCK_STREAM, 0): %s", eina_error_msg_get(err));
             goto error;
          }

        if (strncmp(address, "abstract:", strlen("abstract:")) == 0)
          {
             const char *path = address + strlen("abstract:");
             if (strlen(path) + 2 > sizeof(addr.sun_path))
               {
                  ERR("abstract path is too long: %s", path);
                  err = EFL_NET_ERROR_COULDNT_RESOLVE_HOST;
               }
             addr.sun_path[0] = '\0';
             memcpy(addr.sun_path + 1, path, strlen(path) + 1);
             addrlen = strlen(path) + 2 + offsetof(struct sockaddr_un, sun_path);
          }
        else
          {
             const char *path = address;
             if (strlen(path) + 1 > sizeof(addr.sun_path))
               {
                  ERR("path is too long: %s", path);
                  err = EFL_NET_ERROR_COULDNT_RESOLVE_HOST;
               }
             memcpy(addr.sun_path, path, strlen(path) + 1);
             addrlen = strlen(path) + 1 + offsetof(struct sockaddr_un, sun_path);
          }

        if ((pd->unlink_before_bind) && (addr.sun_path[0] != '\0'))
          {
             DBG("unlinking AF_UNIX path '%s'", addr.sun_path);
             unlink(addr.sun_path);
          }

#ifdef BIND_HANG_WORKAROUND
        if (addr.sun_path[0] != '\0')
          {
             pd->lock_fd = _efl_net_server_unix_bind_hang_lock_workaround
               (addr.sun_path, EINA_TRUE, -1);
             if (pd->lock_fd < 0)
               {
                  err = EADDRINUSE;
                  goto error;
               }
             pd->have_lock_fd = EINA_TRUE;
             unlink(addr.sun_path);
          }
#endif
        r = bind(fd, (struct sockaddr *)&addr, addrlen);
        if (r != 0)
          {
             err = efl_net_socket_error_get();
             if ((err == EADDRINUSE) && (pd->unlink_before_bind) && (addr.sun_path[0] != '\0'))
               {
                  closesocket(fd);
                  fd = INVALID_SOCKET;
                  err = 0;
                  continue;
               }
             if ((err == EADDRINUSE) && (addr.sun_path[0] != '\0') &&
                 (connect(fd, (struct sockaddr *)&addr, addrlen) != 0))
               {
                  DBG("bind(" SOCKET_FMT ", %s): failed with EADDRINUSE but connect also failed, so unlink socket file and try again", fd, address);
                  closesocket(fd);
                  unlink(addr.sun_path);
                  fd = INVALID_SOCKET;
                  err = 0;
                  continue;
               }
             DBG("bind(" SOCKET_FMT ", %s): %s", fd, address, eina_error_msg_get(err));
             goto error;
          }
        break;
     }
   while (pd->unlink_before_bind);

   if (fd == INVALID_SOCKET) goto error;
   efl_loop_fd_set(o, fd);

   r = listen(fd, 0);
   if (r != 0)
     {
        err = efl_net_socket_error_get();
        DBG("listen(" SOCKET_FMT "): %s", fd, eina_error_msg_get(err));
        goto error;
     }

   addrlen = sizeof(addr);
   if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) != 0)
     {
        err = efl_net_socket_error_get();
        ERR("getsockname(" SOCKET_FMT "): %s", fd, eina_error_msg_get(err));
        goto error;
     }
   else
     {
        char str[sizeof(addr) + sizeof("abstract:")];
        if (!efl_net_unix_fmt(str, sizeof(str), fd, &addr, addrlen))
          ERR("could not format unix address");
        else
          {
             efl_net_server_address_set(o, str);
             address = efl_net_server_address_get(o);
          }
     }

   DBG("fd=" SOCKET_FMT " serving at %s", fd, address);
   efl_net_server_serving_set(o, EINA_TRUE);

 error:
   if (err)
     {
        efl_event_callback_call(o, EFL_NET_SERVER_EVENT_SERVER_ERROR, &err);
        if (fd != INVALID_SOCKET) closesocket(fd);
        efl_loop_fd_set(o, SOCKET_TO_LOOP_FD(INVALID_SOCKET));
     }
   return err;
}

EOLIAN static Eina_Error
_efl_net_server_unix_efl_net_server_fd_socket_activate(Eo *o, Efl_Net_Server_Unix_Data *pd EINA_UNUSED, const char *address)
{
   EINA_SAFETY_ON_TRUE_RETURN_VAL((SOCKET)efl_loop_fd_get(o) != INVALID_SOCKET, EALREADY);
   EINA_SAFETY_ON_NULL_RETURN_VAL(address, EINVAL);

#ifndef HAVE_SYSTEMD
   return efl_net_server_fd_socket_activate(efl_super(o, MY_CLASS), address);
#else
   {
      char buf[INET6_ADDRSTRLEN + sizeof("[]:65536")];
      Eina_Bool listening;
      Eina_Error err;
      struct sockaddr_storage addr;
      socklen_t addrlen;
      SOCKET fd;

      err = efl_net_ip_socket_activate_check(address, AF_UNIX, SOCK_STREAM, &listening);
      if (err) return err;

      err = efl_net_server_fd_socket_activate(efl_super(o, MY_CLASS), address);
      if (err) return err;

      fd = efl_loop_fd_get(o);

      if (!listening)
        {
           if (listen(fd, 0) != 0)
             {
                err = efl_net_socket_error_get();
                DBG("listen(" SOCKET_FMT "): %s", fd, eina_error_msg_get(err));
                goto error;
             }
        }

      addrlen = sizeof(addr);
      if (getsockname(fd, (struct sockaddr *)&addr, &addrlen) != 0)
        {
           err = efl_net_socket_error_get();
           ERR("getsockname(" SOCKET_FMT "): %s", fd, eina_error_msg_get(err));
           goto error;
        }
      else if (efl_net_ip_port_fmt(buf, sizeof(buf), (struct sockaddr *)&addr))
        efl_net_server_address_set(o, buf);

      DBG("fd=" SOCKET_FMT " serving at %s", fd, address);
      efl_net_server_serving_set(o, EINA_TRUE);
      return 0;

   error:
      efl_net_server_fd_family_set(o, AF_UNSPEC);
      efl_loop_fd_set(o, SOCKET_TO_LOOP_FD(INVALID_SOCKET));
      closesocket(fd);
      return err;
   }
#endif
}

EOLIAN static Eina_Error
_efl_net_server_unix_efl_net_server_serve(Eo *o, Efl_Net_Server_Unix_Data *pd, const char *address)
{
   EINA_SAFETY_ON_NULL_RETURN_VAL(address, EINVAL);
   EINA_SAFETY_ON_TRUE_RETURN_VAL(address[0] == '\0', EINVAL);

   efl_net_server_address_set(o, address);

   return _efl_net_server_unix_bind(o, pd);
}

static void
_efl_net_server_unix_efl_net_server_fd_client_add(Eo *o, Efl_Net_Server_Unix_Data *pd EINA_UNUSED, int client_fd)
{
   Eo *client = efl_add(EFL_NET_SOCKET_UNIX_CLASS, o,
                        efl_io_closer_close_on_exec_set(efl_added, efl_net_server_fd_close_on_exec_get(o)),
                        efl_io_closer_close_on_invalidate_set(efl_added, EINA_TRUE),
                        efl_loop_fd_set(efl_added, client_fd));
   if (!client)
     {
        ERR("could not create client object fd=" SOCKET_FMT, (SOCKET)client_fd);
        closesocket(client_fd);
        return;
     }

   efl_net_server_client_announce(o, client);
}

static void
_efl_net_server_unix_efl_net_server_fd_client_reject(Eo *o, Efl_Net_Server_Unix_Data *pd EINA_UNUSED, int client_fd)
{
   struct sockaddr_un addr;
   socklen_t addrlen;
   char str[sizeof(addr) + sizeof("abstract:")] = "";

   addrlen = sizeof(addr);
   if (getpeername(client_fd, (struct sockaddr *)&addr, &addrlen) != 0)
     ERR("getpeername(%d): %s", client_fd, eina_error_msg_get(efl_net_socket_error_get()));
   else
     {
        if (!efl_net_unix_fmt(str, sizeof(str), client_fd, &addr, addrlen))
          ERR("could not format rejected client unix address fd=" SOCKET_FMT, (SOCKET)client_fd);
     }

   closesocket(client_fd);
   efl_event_callback_call(o, EFL_NET_SERVER_EVENT_CLIENT_REJECTED, str);
}

static void
_efl_net_server_unix_unlink_before_bind_set(Eo *o EINA_UNUSED, Efl_Net_Server_Unix_Data *pd, Eina_Bool unlink_before_bind)
{
   pd->unlink_before_bind = unlink_before_bind;
}

static Eina_Bool
_efl_net_server_unix_unlink_before_bind_get(const Eo *o EINA_UNUSED, Efl_Net_Server_Unix_Data *pd)
{
   return pd->unlink_before_bind;
}


static void
_efl_net_server_unix_leading_directories_create_set(Eo *o EINA_UNUSED, Efl_Net_Server_Unix_Data *pd, Eina_Bool do_it, unsigned int mode)
{
   pd->leading_directories_create = do_it;
   pd->leading_directories_create_mode = mode;
}

static void
_efl_net_server_unix_leading_directories_create_get(const Eo *o EINA_UNUSED, Efl_Net_Server_Unix_Data *pd, Eina_Bool *do_it, unsigned int *mode)
{
   if (do_it) *do_it = pd->leading_directories_create;
   if (mode) *mode = pd->leading_directories_create_mode;
}

#include "efl_net_server_unix.eo.c"