codec: spudec: force osd start time for forced spu overlays
[vlc.git] / src / network / rootbind.c
blobd1a8ce1e07ee2c81355d3e4356e0a57e4b2b2100
1 /*****************************************************************************
2 * rootbind.c: bind to reserved ports through the root wrapper
3 *****************************************************************************
4 * Copyright © 2005-2008 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 #define _XPG4_2 /* ancillary data on Solaris */
27 #if !defined (_WIN32) && !defined (__OS2__)
28 # define ENABLE_ROOTWRAP 1
29 #endif
31 #include <stddef.h>
32 struct sockaddr;
33 int rootwrap_bind (int, int, int, const struct sockaddr *, size_t);
35 #include <errno.h>
37 #ifdef ENABLE_ROOTWRAP
39 #include <string.h>
40 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <sys/socket.h>
45 #include <sys/uio.h>
46 #include <sys/un.h>
47 #include <netinet/in.h>
48 #include <pthread.h>
50 /* Required yet non-standard cmsg functions */
51 #ifndef CMSG_ALIGN
52 # define CMSG_ALIGN(len) (((len) + sizeof(intptr_t)-1) & ~(sizeof(intptr_t)-1))
53 #endif
54 #ifndef CMSG_SPACE
55 # define CMSG_SPACE(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + CMSG_ALIGN(len))
56 #endif
57 #ifndef CMSG_LEN
58 # define CMSG_LEN(len) (CMSG_ALIGN(sizeof(struct cmsghdr)) + (len))
59 #endif
60 #if !defined(MSG_NOSIGNAL)
61 /* If the other end of the pipe hangs up and MSG_NOSIGNAL is missing, the
62 * process will get a (likely fatal) SIGPIPE signal. Then again, the other end
63 * can screw us up in various ways already (e.g. not answer to deadlock). */
64 # define MSG_NOSIGNAL 0
65 #endif
67 #if defined(__OS2__) && !defined(ALIGN)
68 /* CMSG_NXTHDR requires this */
69 # define ALIGN(p) _ALIGN(p)
70 #endif
72 /**
73 * Receive a file descriptor from another process
75 static int recv_fd (int p)
77 struct msghdr hdr;
78 struct iovec iov;
79 struct cmsghdr *cmsg;
80 int val, fd;
81 char buf[CMSG_SPACE (sizeof (fd))];
83 hdr.msg_name = NULL;
84 hdr.msg_namelen = 0;
85 hdr.msg_iov = &iov;
86 hdr.msg_iovlen = 1;
87 hdr.msg_control = buf;
88 hdr.msg_controllen = sizeof (buf);
90 iov.iov_base = &val;
91 iov.iov_len = sizeof (val);
93 if (recvmsg (p, &hdr, 0) != sizeof (val))
94 return -1;
96 for (cmsg = CMSG_FIRSTHDR (&hdr); cmsg != NULL;
97 cmsg = CMSG_NXTHDR (&hdr, cmsg))
99 if ((cmsg->cmsg_level == SOL_SOCKET)
100 && (cmsg->cmsg_type == SCM_RIGHTS)
101 && (cmsg->cmsg_len >= CMSG_LEN (sizeof (fd))))
103 memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd));
104 return fd;
108 errno = val;
109 return -1;
113 * Tries to obtain a bound TCP socket from the root process
115 int rootwrap_bind (int family, int socktype, int protocol,
116 const struct sockaddr *addr, size_t alen)
118 /* can't use libvlc */
119 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
120 struct sockaddr_storage ss;
121 int fd, sock = -1;
123 const char *sockenv = getenv ("VLC_ROOTWRAP_SOCK");
124 if (sockenv != NULL)
125 sock = atoi (sockenv);
126 if (sock == -1)
128 errno = EACCES;
129 return -1;
132 switch (family)
134 case AF_INET:
135 if (alen < sizeof (struct sockaddr_in))
137 errno = EINVAL;
138 return -1;
140 break;
142 #ifdef AF_INET6
143 case AF_INET6:
144 if (alen < sizeof (struct sockaddr_in6))
146 errno = EINVAL;
147 return -1;
149 break;
150 #endif
152 default:
153 errno = EAFNOSUPPORT;
154 return -1;
157 if (family != addr->sa_family)
159 errno = EAFNOSUPPORT;
160 return -1;
163 /* Only TCP is implemented at the moment */
164 if ((socktype != SOCK_STREAM)
165 || (protocol && (protocol != IPPROTO_TCP)))
167 errno = EACCES;
168 return -1;
171 memset (&ss, 0, sizeof (ss));
172 memcpy (&ss, addr, (alen > sizeof (ss)) ? sizeof (ss) : alen);
174 pthread_mutex_lock (&mutex);
175 if (send (sock, &ss, sizeof (ss), MSG_NOSIGNAL) != sizeof (ss))
177 pthread_mutex_unlock (&mutex);
178 return -1;
181 fd = recv_fd (sock);
182 pthread_mutex_unlock (&mutex);
183 return fd;
186 #else
187 int rootwrap_bind (int family, int socktype, int protocol,
188 const struct sockaddr *addr, size_t alen)
190 (void)family;
191 (void)socktype;
192 (void)protocol;
193 (void)addr;
194 (void)alen;
195 errno = EACCES;
196 return -1;
199 #endif /* ENABLE_ROOTWRAP */