inhibit/dbus: fix flawed logic
[vlc.git] / src / linux / getaddrinfo.c
blobaaba24df88772bd2fd544f5be9c29e044f20f810
1 /*****************************************************************************
2 * linux/getaddrinfo.c: interruptible DNS resolution for Linux
3 *****************************************************************************
4 * Copyright (C) 2016 RĂ©mi Denis-Courmont
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
25 #include <assert.h>
26 #include <stdio.h>
27 #include <netdb.h>
28 #include <signal.h>
30 #include <vlc_common.h>
31 #include <vlc_interrupt.h>
32 #include <vlc_network.h>
34 static void vlc_getaddrinfo_notify(union sigval val)
36 vlc_sem_post(val.sival_ptr);
39 int vlc_getaddrinfo_i11e(const char *name, unsigned port,
40 const struct addrinfo *hints,
41 struct addrinfo **res)
43 struct gaicb req =
45 .ar_name = name,
46 .ar_service = NULL,
47 .ar_request = hints,
49 char portbuf[6];
50 vlc_sem_t done;
52 if (port != 0)
54 if ((size_t)snprintf(portbuf, sizeof (portbuf), "%u",
55 port) >= sizeof (portbuf))
56 return EAI_NONAME;
58 req.ar_service = portbuf;
61 struct sigevent sev =
63 .sigev_notify = SIGEV_THREAD,
64 .sigev_value = { .sival_ptr = &done, },
65 .sigev_notify_function = vlc_getaddrinfo_notify,
68 vlc_sem_init(&done, 0);
70 int val = getaddrinfo_a(GAI_NOWAIT, &(struct gaicb *){ &req }, 1, &sev);
71 if (val)
73 vlc_sem_destroy(&done);
74 return val;
77 vlc_sem_wait_i11e(&done);
79 if (gai_cancel(&req) == EAI_CANCELED)
80 vlc_sem_wait(&done);
82 while (gai_suspend(&(const struct gaicb *){ &req }, 1, NULL) == EAI_INTR);
84 val = gai_error(&req);
85 assert(val != EAI_INPROGRESS);
87 if (val == 0)
88 *res = req.ar_result;
90 return val;