add description field for sinks/sources
[profile/ivi/pulseaudio.git] / src / util.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <signal.h>
4 #include <errno.h>
5 #include <assert.h>
6 #include <string.h>
7 #include <stdio.h>
8 #include <sys/un.h>
9 #include <netinet/in.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <netinet/tcp.h>
14 #include <netinet/ip.h>
15
16 #include "util.h"
17
18 void pa_make_nonblock_fd(int fd) {
19     int v;
20
21     if ((v = fcntl(fd, F_GETFL)) >= 0)
22         if (!(v & O_NONBLOCK))
23             fcntl(fd, F_SETFL, v|O_NONBLOCK);
24 }
25
26 void pa_peer_to_string(char *c, size_t l, int fd) {
27     struct stat st;
28
29     assert(c && l && fd >= 0);
30     
31     if (fstat(fd, &st) < 0) {
32         snprintf(c, l, "Invalid client fd");
33         return;
34     }
35
36     if (S_ISSOCK(st.st_mode)) {
37         union {
38             struct sockaddr sa;
39             struct sockaddr_in in;
40             struct sockaddr_un un;
41         } sa;
42         socklen_t sa_len = sizeof(sa);
43         
44         if (getpeername(fd, &sa.sa, &sa_len) >= 0) {
45
46             if (sa.sa.sa_family == AF_INET) {
47                 uint32_t ip = ntohl(sa.in.sin_addr.s_addr);
48                 
49                 snprintf(c, l, "TCP/IP client from %i.%i.%i.%i:%u",
50                          ip >> 24,
51                          (ip >> 16) & 0xFF,
52                          (ip >> 8) & 0xFF,
53                          ip & 0xFF,
54                          ntohs(sa.in.sin_port));
55                 return;
56             } else if (sa.sa.sa_family == AF_LOCAL) {
57                 snprintf(c, l, "UNIX socket client");
58                 return;
59             }
60
61         }
62         snprintf(c, l, "Unknown network client");
63         return;
64     } else if (S_ISCHR(st.st_mode) && (fd == 0 || fd == 1)) {
65         snprintf(c, l, "STDIN/STDOUT client");
66         return;
67     }
68
69     snprintf(c, l, "Unknown client");
70 }
71
72 int pa_make_secure_dir(const char* dir) {
73     struct stat st;
74
75     if (mkdir(dir, 0700) < 0) 
76         if (errno != EEXIST)
77             return -1;
78     
79     if (lstat(dir, &st) < 0) 
80         goto fail;
81     
82     if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
83         goto fail;
84     
85     return 0;
86     
87 fail:
88     rmdir(dir);
89     return -1;
90 }
91
92 int pa_socket_low_delay(int fd) {
93     int priority;
94     assert(fd >= 0);
95
96     priority = 7;
97     if (setsockopt(fd, SOL_SOCKET, SO_PRIORITY, &priority, sizeof(priority)) < 0)
98         return -1;
99
100     return 0;
101 }
102
103 int pa_socket_tcp_low_delay(int fd) {
104     int ret, tos;
105
106     assert(fd >= 0);
107
108     ret = pa_socket_low_delay(fd);
109     
110 /*     on = 1; */
111 /*     if (setsockopt(fd, SOL_TCP, TCP_NODELAY, &on, sizeof(on)) < 0) */
112 /*         ret = -1; */
113
114     tos = IPTOS_LOWDELAY;
115     if (setsockopt(fd, SOL_IP, IP_TOS, &tos, sizeof(tos)) < 0)
116         ret = -1;
117
118     return ret;
119
120 }
121
122 int pa_socket_set_rcvbuf(int fd, size_t l) {
123     assert(fd >= 0);
124
125     if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &l, sizeof(l)) < 0)
126         return -1;
127
128     return 0;
129 }
130
131 int pa_socket_set_sndbuf(int fd, size_t l) {
132     assert(fd >= 0);
133
134     if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &l, sizeof(l)) < 0)
135         return -1;
136
137     return 0;
138 }
139
140 ssize_t pa_loop_read(int fd, void*data, size_t size) {
141     ssize_t ret = 0;
142     assert(fd >= 0 && data && size);
143
144     while (size > 0) {
145         ssize_t r;
146
147         if ((r = read(fd, data, size)) < 0)
148             return r;
149
150         if (r == 0)
151             break;
152         
153         ret += r;
154         data += r;
155         size -= r;
156     }
157
158     return ret;
159 }
160
161 ssize_t pa_loop_write(int fd, const void*data, size_t size) {
162     ssize_t ret = 0;
163     assert(fd >= 0 && data && size);
164
165     while (size > 0) {
166         ssize_t r;
167
168         if ((r = write(fd, data, size)) < 0)
169             return r;
170
171         if (r == 0)
172             break;
173         
174         ret += r;
175         data += r;
176         size -= r;
177     }
178
179     return ret;
180 }
181
182 int pa_unix_socket_is_stale(const char *fn) {
183     struct sockaddr_un sa;
184     int fd = -1, ret = -1;
185
186     if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
187         fprintf(stderr, "socket(): %s\n", strerror(errno));
188         goto finish;
189     }
190
191     sa.sun_family = AF_LOCAL;
192     strncpy(sa.sun_path, fn, sizeof(sa.sun_path)-1);
193     sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
194
195     if (connect(fd, (struct sockaddr*) &sa, sizeof(sa)) < 0) {
196         if (errno == ECONNREFUSED)
197             ret = 1;
198     } else
199         ret = 0;
200
201 finish:
202     if (fd >= 0)
203         close(fd);
204
205     return ret;
206 }
207
208 int pa_unix_socket_remove_stale(const char *fn) {
209     int r;
210     
211     if ((r = pa_unix_socket_is_stale(fn)) < 0)
212         return errno != ENOENT ? -1 : 0;
213
214     if (!r)
215         return 0;
216         
217     /* Yes, here is a race condition. But who cares? */
218     if (unlink(fn) < 0)
219         return -1;
220
221     return 0;
222 }
223
224 void pa_check_for_sigpipe(void) {
225     struct sigaction sa;
226
227     if (sigaction(SIGPIPE, NULL, &sa) < 0) {
228         fprintf(stderr, __FILE__": sigaction() failed: %s\n", strerror(errno));
229         return;
230     }
231         
232     if (sa.sa_handler == SIG_DFL)
233         fprintf(stderr, "polypaudio: WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
234 }
235
236 /* The following is based on an example from the GNU libc documentation */
237 char *pa_sprintf_malloc(const char *format, ...) {
238     int  size = 100;
239     char *c = NULL;
240     
241     assert(format);
242     
243     for(;;) {
244         int r;
245         va_list ap;
246
247         c = realloc(c, size);
248         assert(c);
249
250         va_start(ap, format);
251         r = vsnprintf(c, size, format, ap);
252         va_end(ap);
253         
254         if (r > -1 && r < size)
255             return c;
256
257         if (r > -1)    /* glibc 2.1 */
258             size = r+1; 
259         else           /* glibc 2.0 */
260             size *= 2;
261     }
262 }