]> code.delx.au - pulseaudio/blob - polyp/socket-util.c
add support for SCHED_FIFO
[pulseaudio] / polyp / socket-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <assert.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <sys/un.h>
34 #include <netinet/in.h>
35 #include <fcntl.h>
36 #include <unistd.h>
37 #include <sys/types.h>
38 #include <netinet/tcp.h>
39 #include <netinet/ip.h>
40
41 #include "socket-util.h"
42 #include "util.h"
43 #include "xmalloc.h"
44
45 void pa_socket_peer_to_string(int fd, char *c, size_t l) {
46 struct stat st;
47
48 assert(c && l && fd >= 0);
49
50 if (fstat(fd, &st) < 0) {
51 snprintf(c, l, "Invalid client fd");
52 return;
53 }
54
55 if (S_ISSOCK(st.st_mode)) {
56 union {
57 struct sockaddr sa;
58 struct sockaddr_in in;
59 struct sockaddr_un un;
60 } sa;
61 socklen_t sa_len = sizeof(sa);
62
63 if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
64
65 if (sa.sa.sa_family == AF_INET) {
66 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
67
68 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
69 ip >> 24,
70 (ip >> 16) & 0xFF,
71 (ip >> 8) & 0xFF,
72 ip & 0xFF,
73 ntohs(sa.in.sin_port));
74 return;
75 } else if (sa.sa.sa_family == AF_LOCAL) {
76 snprintf(c, l, "UNIX socket client");
77 return;
78 }
79
80 }
81 snprintf(c, l, "Unknown network client");
82 return;
83 } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
84 snprintf(c, l, "STDIN/STDOUT client");
85 return;
86 }
87
88 snprintf(c, l, "Unknown client");
89 }
90
91 int pa_socket_low_delay(int fd) {
92 int priority;
93 assert(fd >= 0);
94
95 priority = 7;
96 if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
97 return -1;
98
99 return 0;
100 }
101
102 int pa_socket_tcp_low_delay(int fd) {
103 int ret, tos;
104
105 assert(fd >= 0);
106
107 ret = pa_socket_low_delay(fd);
108
109 /* on = 1; */
110 /* if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
111 /* ret = -1; */
112
113 tos = IPTOS_LOWDELAY;
114 if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
115 ret = -1;
116
117 return ret;
118
119 }
120
121 int pa_socket_set_rcvbuf(int fd, size_t l) {
122 assert(fd >= 0);
123
124 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0) {
125 fprintf(stderr, "SO_RCVBUF: %s\n", strerror(errno));
126 return -1;
127 }
128
129 return 0;
130 }
131
132 int pa_socket_set_sndbuf(int fd, size_t l) {
133 assert(fd >= 0);
134
135 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0) {
136 fprintf(stderr, "SO_SNDBUF: %s\n", strerror(errno));
137 return -1;
138 }
139
140 return 0;
141 }
142
143 int pa_unix_socket_is_stale(const char *fn) {
144 struct sockaddr_un sa;
145 int fd = -1, ret = -1;
146
147 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
148 fprintf(stderr, "socket(): %s\n", strerror(errno));
149 goto finish;
150 }
151
152 sa.sun_family = AF_LOCAL;
153 strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
154 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
155
156 if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
157 if (errno == ECONNREFUSED)
158 ret = 1;
159 } else
160 ret = 0;
161
162 finish:
163 if (fd >= 0)
164 close(fd);
165
166 return ret;
167 }
168
169 int pa_unix_socket_remove_stale(const char *fn) {
170 int r;
171
172 if ((r = pa_unix_socket_is_stale(fn)) < 0)
173 return errno != ENOENT ? -1 : 0;
174
175 if (!r)
176 return 0;
177
178 /* Yes, here is a race condition. But who cares? */
179 if (unlink(fn) < 0)
180 return -1;
181
182 return 0;
183 }
184
185 int pa_unix_socket_make_secure_dir(const char *fn) {
186 int ret = -1;
187 char *slash, *dir = pa_xstrdup(fn);
188
189 if (!(slash = strrchr(dir, '/')))
190 goto finish;
191 *slash = 0;
192
193 if (pa_make_secure_dir(dir) < 0)
194 goto finish;
195
196 ret = 0;
197
198 finish:
199 pa_xfree(dir);
200 return ret;
201 }
202
203 int pa_unix_socket_remove_secure_dir(const char *fn) {
204 int ret = -1;
205 char *slash, *dir = pa_xstrdup(fn);
206
207 if (!(slash = strrchr(dir, '/')))
208 goto finish;
209 *slash = 0;
210
211 if (rmdir(dir) < 0)
212 goto finish;
213
214 ret = 0;
215
216 finish:
217 pa_xfree(dir);
218 return ret;
219 }