]> code.delx.au - pulseaudio/blob - polyp/util.c
update module descriptions
[pulseaudio] / polyp / 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 <fcntl.h>
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <pwd.h>
37 #include <signal.h>
38 #include <pthread.h>
39 #include <sys/time.h>
40 #include <sched.h>
41 #include <sys/resource.h>
42 #include <limits.h>
43
44 #include <samplerate.h>
45
46 #include "util.h"
47 #include "xmalloc.h"
48 #include "log.h"
49
50 void pa_make_nonblock_fd(int fd) {
51 int v;
52
53 if ((v = fcntl(fd, F_GETFL)) >= 0)
54 if (!(v & O_NONBLOCK))
55 fcntl(fd, F_SETFL, v|O_NONBLOCK);
56 }
57
58 int pa_make_secure_dir(const char* dir) {
59 struct stat st;
60
61 if (mkdir(dir, 0700) < 0)
62 if (errno != EEXIST)
63 return -1;
64
65 if (lstat(dir, &st) < 0)
66 goto fail;
67
68 if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
69 goto fail;
70
71 return 0;
72
73 fail:
74 rmdir(dir);
75 return -1;
76 }
77
78 ssize_t pa_loop_read(int fd, void*data, size_t size) {
79 ssize_t ret = 0;
80 assert(fd >= 0 && data && size);
81
82 while (size > 0) {
83 ssize_t r;
84
85 if ((r = read(fd, data, size)) < 0)
86 return r;
87
88 if (r == 0)
89 break;
90
91 ret += r;
92 data = (uint8_t*) data + r;
93 size -= r;
94 }
95
96 return ret;
97 }
98
99 ssize_t pa_loop_write(int fd, const void*data, size_t size) {
100 ssize_t ret = 0;
101 assert(fd >= 0 && data && size);
102
103 while (size > 0) {
104 ssize_t r;
105
106 if ((r = write(fd, data, size)) < 0)
107 return r;
108
109 if (r == 0)
110 break;
111
112 ret += r;
113 data = (uint8_t*) data + r;
114 size -= r;
115 }
116
117 return ret;
118 }
119
120 void pa_check_signal_is_blocked(int sig) {
121 struct sigaction sa;
122 sigset_t set;
123
124 #ifdef HAVE_PTHREAD
125 if (pthread_sigmask(SIG_SETMASK, NULL, &set) < 0) {
126 #endif
127 if (sigprocmask(SIG_SETMASK, NULL, &set) < 0) {
128 pa_log(__FILE__": sigprocmask() failed: %s\n", strerror(errno));
129 return;
130 }
131 #ifdef HAVE_PTHREAD
132 }
133 #endif
134
135 if (sigismember(&set, sig))
136 return;
137
138 if (sigaction(sig, NULL, &sa) < 0) {
139 pa_log(__FILE__": sigaction() failed: %s\n", strerror(errno));
140 return;
141 }
142
143 if (sa.sa_handler != SIG_DFL)
144 return;
145
146 pa_log(__FILE__": WARNING: %s is not trapped. This might cause malfunction!\n", pa_strsignal(sig));
147 }
148
149 /* The following is based on an example from the GNU libc documentation */
150 char *pa_sprintf_malloc(const char *format, ...) {
151 int size = 100;
152 char *c = NULL;
153
154 assert(format);
155
156 for(;;) {
157 int r;
158 va_list ap;
159
160 c = pa_xrealloc(c, size);
161
162 va_start(ap, format);
163 r = vsnprintf(c, size, format, ap);
164 va_end(ap);
165
166 if (r > -1 && r < size)
167 return c;
168
169 if (r > -1) /* glibc 2.1 */
170 size = r+1;
171 else /* glibc 2.0 */
172 size *= 2;
173 }
174 }
175
176 char *pa_vsprintf_malloc(const char *format, va_list ap) {
177 int size = 100;
178 char *c = NULL;
179
180 assert(format);
181
182 for(;;) {
183 int r;
184 va_list ap;
185
186 c = pa_xrealloc(c, size);
187 r = vsnprintf(c, size, format, ap);
188
189 if (r > -1 && r < size)
190 return c;
191
192 if (r > -1) /* glibc 2.1 */
193 size = r+1;
194 else /* glibc 2.0 */
195 size *= 2;
196 }
197 }
198
199
200 char *pa_get_user_name(char *s, size_t l) {
201 struct passwd pw, *r;
202 char buf[1024];
203 char *p;
204
205 if (!(p = getenv("USER")))
206 if (!(p = getenv("LOGNAME")))
207 if (!(p = getenv("USERNAME"))) {
208
209 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
210 snprintf(s, l, "%lu", (unsigned long) getuid());
211 return s;
212 }
213
214 p = r->pw_name;
215 }
216
217 snprintf(s, l, "%s", p);
218 return s;
219 }
220
221 char *pa_get_host_name(char *s, size_t l) {
222 gethostname(s, l);
223 s[l-1] = 0;
224 return s;
225 }
226
227 pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
228 pa_usec_t r;
229 assert(a && b);
230
231 if (pa_timeval_cmp(a, b) < 0) {
232 const struct timeval *c;
233 c = a;
234 a = b;
235 b = c;
236 }
237
238 r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000;
239
240 if (a->tv_usec > b->tv_usec)
241 r += ((pa_usec_t) a->tv_usec - b->tv_usec);
242 else if (a->tv_usec < b->tv_usec)
243 r -= ((pa_usec_t) b->tv_usec - a->tv_usec);
244
245 return r;
246 }
247
248 int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
249 assert(a && b);
250
251 if (a->tv_sec < b->tv_sec)
252 return -1;
253
254 if (a->tv_sec > b->tv_sec)
255 return 1;
256
257 if (a->tv_usec < b->tv_usec)
258 return -1;
259
260 if (a->tv_usec > b->tv_usec)
261 return 1;
262
263 return 0;
264 }
265
266 pa_usec_t pa_age(const struct timeval *tv) {
267 struct timeval now;
268 assert(tv);
269 gettimeofday(&now, NULL);
270 return pa_timeval_diff(&now, tv);
271 }
272
273 void pa_timeval_add(struct timeval *tv, pa_usec_t v) {
274 unsigned long secs;
275 assert(tv);
276
277 secs = (v/1000000);
278 tv->tv_sec += (unsigned long) secs;
279 v -= secs*1000000;
280
281 tv->tv_usec += v;
282
283 while (tv->tv_usec >= 1000000) {
284 tv->tv_sec++;
285 tv->tv_usec -= 1000000;
286 }
287 }
288
289 #define NICE_LEVEL (-15)
290
291 void pa_raise_priority(void) {
292 if (setpriority(PRIO_PROCESS, 0, NICE_LEVEL) < 0)
293 pa_log(__FILE__": setpriority() failed: %s\n", strerror(errno));
294 else
295 pa_log(__FILE__": Successfully gained nice level %i.\n", NICE_LEVEL);
296
297 #ifdef _POSIX_PRIORITY_SCHEDULING
298 {
299 struct sched_param sp;
300
301 if (sched_getparam(0, &sp) < 0) {
302 pa_log(__FILE__": sched_getparam() failed: %s\n", strerror(errno));
303 return;
304 }
305
306 sp.sched_priority = 1;
307 if (sched_setscheduler(0, SCHED_FIFO, &sp) < 0) {
308 pa_log(__FILE__": sched_setscheduler() failed: %s\n", strerror(errno));
309 return;
310 }
311
312 pa_log(__FILE__": Successfully enabled SCHED_FIFO scheduling.\n");
313 }
314 #endif
315 }
316
317 void pa_reset_priority(void) {
318 #ifdef _POSIX_PRIORITY_SCHEDULING
319 {
320 struct sched_param sp;
321 sched_getparam(0, &sp);
322 sp.sched_priority = 0;
323 sched_setscheduler(0, SCHED_OTHER, &sp);
324 }
325 #endif
326
327 setpriority(PRIO_PROCESS, 0, 0);
328 }
329
330 int pa_fd_set_cloexec(int fd, int b) {
331 int v;
332 assert(fd >= 0);
333
334 if ((v = fcntl(fd, F_GETFD, 0)) < 0)
335 return -1;
336
337 v = (v & ~FD_CLOEXEC) | (b ? FD_CLOEXEC : 0);
338
339 if (fcntl(fd, F_SETFD, v) < 0)
340 return -1;
341
342 return 0;
343 }
344
345 char *pa_get_binary_name(char *s, size_t l) {
346 char path[PATH_MAX];
347 int i;
348 assert(s && l);
349
350 /* This works on Linux only */
351
352 snprintf(path, sizeof(path), "/proc/%u/exe", (unsigned) getpid());
353 if ((i = readlink(path, s, l-1)) < 0)
354 return NULL;
355
356 s[i] = 0;
357 return s;
358 }
359
360 char *pa_path_get_filename(const char *p) {
361 char *fn;
362
363 if ((fn = strrchr(p, '/')))
364 return fn+1;
365
366 return (char*) p;
367 }
368
369 int pa_parse_boolean(const char *v) {
370
371 if (!strcmp(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
372 return 1;
373 else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
374 return 0;
375
376 return -1;
377 }
378
379 char *pa_split(const char *c, const char *delimiter, const char**state) {
380 const char *current = *state ? *state : c;
381 size_t l;
382
383 if (!*current)
384 return NULL;
385
386 l = strcspn(current, delimiter);
387 *state = current+l;
388
389 if (**state)
390 (*state)++;
391
392 return pa_xstrndup(current, l);
393 }
394
395 #define WHITESPACE " \t\n"
396
397 char *pa_split_spaces(const char *c, const char **state) {
398 const char *current = *state ? *state : c;
399 size_t l;
400
401 if (!*current)
402 return NULL;
403
404 current += strspn(current, WHITESPACE);
405 l = strcspn(current, WHITESPACE);
406
407 *state = current+l;
408
409 return pa_xstrndup(current, l);
410 }
411
412 const char *pa_strsignal(int sig) {
413 switch(sig) {
414 case SIGINT: return "SIGINT";
415 case SIGTERM: return "SIGTERM";
416 case SIGUSR1: return "SIGUSR1";
417 case SIGUSR2: return "SIGUSR2";
418 case SIGXCPU: return "SIGXCPU";
419 case SIGPIPE: return "SIGPIPE";
420 case SIGCHLD: return "SIGCHLD";
421 default: return "UNKNOWN SIGNAL";
422 }
423 }
424
425 int pa_parse_resample_method(const char *string) {
426 assert(string);
427
428 if (!strcmp(string, "sinc-best-quality"))
429 return SRC_SINC_BEST_QUALITY;
430 else if (!strcmp(string, "sinc-medium-quality"))
431 return SRC_SINC_MEDIUM_QUALITY;
432 else if (!strcmp(string, "sinc-fastest"))
433 return SRC_SINC_FASTEST;
434 else if (!strcmp(string, "zero-order-hold"))
435 return SRC_ZERO_ORDER_HOLD;
436 else if (!strcmp(string, "linear"))
437 return SRC_LINEAR;
438 else
439 return -1;
440 }