]> code.delx.au - pulseaudio/blob - polyp/util.c
improve sync clock change
[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 Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License 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 <sys/stat.h>
37 #include <pwd.h>
38 #include <signal.h>
39 #include <pthread.h>
40 #include <sys/time.h>
41 #include <sched.h>
42 #include <sys/resource.h>
43 #include <limits.h>
44 #include <unistd.h>
45 #include <grp.h>
46 #include <netdb.h>
47
48 #include <samplerate.h>
49
50 #include "util.h"
51 #include "xmalloc.h"
52 #include "log.h"
53
54 #define PA_RUNTIME_PATH_PREFIX "/tmp/polypaudio-"
55
56 /** Make a file descriptor nonblock. Doesn't do any error checking */
57 void pa_make_nonblock_fd(int fd) {
58 int v;
59 assert(fd >= 0);
60
61 if ((v = fcntl(fd, F_GETFL)) >= 0)
62 if (!(v & O_NONBLOCK))
63 fcntl(fd, F_SETFL, v|O_NONBLOCK);
64 }
65
66 /** Creates a directory securely */
67 int pa_make_secure_dir(const char* dir) {
68 struct stat st;
69 assert(dir);
70
71 if (mkdir(dir, 0700) < 0)
72 if (errno != EEXIST)
73 return -1;
74
75 if (lstat(dir, &st) < 0)
76 goto fail;
77
78 if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
79 goto fail;
80
81 return 0;
82
83 fail:
84 rmdir(dir);
85 return -1;
86 }
87
88 /* Creates a the parent directory of the specified path securely */
89 int pa_make_secure_parent_dir(const char *fn) {
90 int ret = -1;
91 char *slash, *dir = pa_xstrdup(fn);
92
93 if (!(slash = strrchr(dir, '/')))
94 goto finish;
95 *slash = 0;
96
97 if (pa_make_secure_dir(dir) < 0)
98 goto finish;
99
100 ret = 0;
101
102 finish:
103 pa_xfree(dir);
104 return ret;
105 }
106
107
108 /** Calls read() in a loop. Makes sure that as much as 'size' bytes,
109 * unless EOF is reached or an error occured */
110 ssize_t pa_loop_read(int fd, void*data, size_t size) {
111 ssize_t ret = 0;
112 assert(fd >= 0 && data && size);
113
114 while (size > 0) {
115 ssize_t r;
116
117 if ((r = read(fd, data, size)) < 0)
118 return r;
119
120 if (r == 0)
121 break;
122
123 ret += r;
124 data = (uint8_t*) data + r;
125 size -= r;
126 }
127
128 return ret;
129 }
130
131 /** Similar to pa_loop_read(), but wraps write() */
132 ssize_t pa_loop_write(int fd, const void*data, size_t size) {
133 ssize_t ret = 0;
134 assert(fd >= 0 && data && size);
135
136 while (size > 0) {
137 ssize_t r;
138
139 if ((r = write(fd, data, size)) < 0)
140 return r;
141
142 if (r == 0)
143 break;
144
145 ret += r;
146 data = (uint8_t*) data + r;
147 size -= r;
148 }
149
150 return ret;
151 }
152
153 /* Print a warning messages in case that the given signal is not
154 * blocked or trapped */
155 void pa_check_signal_is_blocked(int sig) {
156 struct sigaction sa;
157 sigset_t set;
158
159 /* If POSIX threads are supported use thread-aware
160 * pthread_sigmask() function, to check if the signal is
161 * blocked. Otherwise fall back to sigprocmask() */
162
163 #ifdef HAVE_PTHREAD
164 if (pthread_sigmask(SIG_SETMASK, NULL, &set) < 0) {
165 #endif
166 if (sigprocmask(SIG_SETMASK, NULL, &set) < 0) {
167 pa_log(__FILE__": sigprocmask() failed: %s\n", strerror(errno));
168 return;
169 }
170 #ifdef HAVE_PTHREAD
171 }
172 #endif
173
174 if (sigismember(&set, sig))
175 return;
176
177 /* Check whether the signal is trapped */
178
179 if (sigaction(sig, NULL, &sa) < 0) {
180 pa_log(__FILE__": sigaction() failed: %s\n", strerror(errno));
181 return;
182 }
183
184 if (sa.sa_handler != SIG_DFL)
185 return;
186
187 pa_log(__FILE__": WARNING: %s is not trapped. This might cause malfunction!\n", pa_strsignal(sig));
188 }
189
190 /* The following function is based on an example from the GNU libc
191 * documentation. This function is similar to GNU's asprintf(). */
192 char *pa_sprintf_malloc(const char *format, ...) {
193 int size = 100;
194 char *c = NULL;
195
196 assert(format);
197
198 for(;;) {
199 int r;
200 va_list ap;
201
202 c = pa_xrealloc(c, size);
203
204 va_start(ap, format);
205 r = vsnprintf(c, size, format, ap);
206 va_end(ap);
207
208 if (r > -1 && r < size)
209 return c;
210
211 if (r > -1) /* glibc 2.1 */
212 size = r+1;
213 else /* glibc 2.0 */
214 size *= 2;
215 }
216 }
217
218 /* Same as the previous function, but use a va_list instead of an
219 * ellipsis */
220 char *pa_vsprintf_malloc(const char *format, va_list ap) {
221 int size = 100;
222 char *c = NULL;
223
224 assert(format);
225
226 for(;;) {
227 int r;
228 c = pa_xrealloc(c, size);
229 r = vsnprintf(c, size, format, ap);
230
231 if (r > -1 && r < size)
232 return c;
233
234 if (r > -1) /* glibc 2.1 */
235 size = r+1;
236 else /* glibc 2.0 */
237 size *= 2;
238 }
239 }
240
241 /* Return the current username in the specified string buffer. */
242 char *pa_get_user_name(char *s, size_t l) {
243 struct passwd pw, *r;
244 char buf[1024];
245 char *p;
246 assert(s && l > 0);
247
248 if (!(p = getenv("USER")) && !(p = getenv("LOGNAME")) && !(p = getenv("USERNAME"))) {
249
250 #ifdef HAVE_GETPWUID_R
251 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
252 #else
253 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
254 * that do not support getpwuid_r. */
255 if ((r = getpwuid(getuid())) == NULL) {
256 #endif
257 snprintf(s, l, "%lu", (unsigned long) getuid());
258 return s;
259 }
260
261 p = r->pw_name;
262 }
263
264 return pa_strlcpy(s, p, l);
265 }
266
267 /* Return the current hostname in the specified buffer. */
268 char *pa_get_host_name(char *s, size_t l) {
269 assert(s && l > 0);
270 if (gethostname(s, l) < 0) {
271 pa_log(__FILE__": gethostname(): %s\n", strerror(errno));
272 return NULL;
273 }
274 s[l-1] = 0;
275 return s;
276 }
277
278 /* Return the home directory of the current user */
279 char *pa_get_home_dir(char *s, size_t l) {
280 char *e;
281 char buf[1024];
282 struct passwd pw, *r;
283 assert(s && l);
284
285 if ((e = getenv("HOME")))
286 return pa_strlcpy(s, e, l);
287
288 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
289 pa_log(__FILE__": getpwuid_r() failed\n");
290 return NULL;
291 }
292
293 return pa_strlcpy(s, r->pw_dir, l);
294 }
295
296 /* Similar to OpenBSD's strlcpy() function */
297 char *pa_strlcpy(char *b, const char *s, size_t l) {
298 assert(b && s && l > 0);
299
300 strncpy(b, s, l);
301 b[l-1] = 0;
302 return b;
303 }
304
305 /* Calculate the difference between the two specfified timeval
306 * timestamsps. */
307 pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
308 pa_usec_t r;
309 assert(a && b);
310
311 /* Check which whan is the earlier time and swap the two arguments if reuqired. */
312 if (pa_timeval_cmp(a, b) < 0) {
313 const struct timeval *c;
314 c = a;
315 a = b;
316 b = c;
317 }
318
319 /* Calculate the second difference*/
320 r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000;
321
322 /* Calculate the microsecond difference */
323 if (a->tv_usec > b->tv_usec)
324 r += ((pa_usec_t) a->tv_usec - b->tv_usec);
325 else if (a->tv_usec < b->tv_usec)
326 r -= ((pa_usec_t) b->tv_usec - a->tv_usec);
327
328 return r;
329 }
330
331 /* Compare the two timeval structs and return 0 when equal, negative when a < b, positive otherwse */
332 int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
333 assert(a && b);
334
335 if (a->tv_sec < b->tv_sec)
336 return -1;
337
338 if (a->tv_sec > b->tv_sec)
339 return 1;
340
341 if (a->tv_usec < b->tv_usec)
342 return -1;
343
344 if (a->tv_usec > b->tv_usec)
345 return 1;
346
347 return 0;
348 }
349
350 /* Return the time difference between now and the specified timestamp */
351 pa_usec_t pa_timeval_age(const struct timeval *tv) {
352 struct timeval now;
353 assert(tv);
354 gettimeofday(&now, NULL);
355 return pa_timeval_diff(&now, tv);
356 }
357
358 /* Add the specified time inmicroseconds to the specified timeval structure */
359 void pa_timeval_add(struct timeval *tv, pa_usec_t v) {
360 unsigned long secs;
361 assert(tv);
362
363 secs = (v/1000000);
364 tv->tv_sec += (unsigned long) secs;
365 v -= secs*1000000;
366
367 tv->tv_usec += v;
368
369 /* Normalize */
370 while (tv->tv_usec >= 1000000) {
371 tv->tv_sec++;
372 tv->tv_usec -= 1000000;
373 }
374 }
375
376 #define NICE_LEVEL (-15)
377
378 /* Raise the priority of the current process as much as possible and
379 sensible: set the nice level to -15 and enable realtime scheduling if
380 supported.*/
381 void pa_raise_priority(void) {
382
383 if (setpriority(PRIO_PROCESS, 0, NICE_LEVEL) < 0)
384 pa_log_warn(__FILE__": setpriority() failed: %s\n", strerror(errno));
385 else
386 pa_log_info(__FILE__": Successfully gained nice level %i.\n", NICE_LEVEL);
387
388 #ifdef _POSIX_PRIORITY_SCHEDULING
389 {
390 struct sched_param sp;
391
392 if (sched_getparam(0, &sp) < 0) {
393 pa_log(__FILE__": sched_getparam() failed: %s\n", strerror(errno));
394 return;
395 }
396
397 sp.sched_priority = 1;
398 if (sched_setscheduler(0, SCHED_FIFO, &sp) < 0) {
399 pa_log_warn(__FILE__": sched_setscheduler() failed: %s\n", strerror(errno));
400 return;
401 }
402
403 pa_log_info(__FILE__": Successfully enabled SCHED_FIFO scheduling.\n");
404 }
405 #endif
406 }
407
408 /* Reset the priority to normal, inverting the changes made by pa_raise_priority() */
409 void pa_reset_priority(void) {
410 #ifdef _POSIX_PRIORITY_SCHEDULING
411 {
412 struct sched_param sp;
413 sched_getparam(0, &sp);
414 sp.sched_priority = 0;
415 sched_setscheduler(0, SCHED_OTHER, &sp);
416 }
417 #endif
418
419 setpriority(PRIO_PROCESS, 0, 0);
420 }
421
422 /* Set the FD_CLOEXEC flag for a fd */
423 int pa_fd_set_cloexec(int fd, int b) {
424 int v;
425 assert(fd >= 0);
426
427 if ((v = fcntl(fd, F_GETFD, 0)) < 0)
428 return -1;
429
430 v = (v & ~FD_CLOEXEC) | (b ? FD_CLOEXEC : 0);
431
432 if (fcntl(fd, F_SETFD, v) < 0)
433 return -1;
434
435 return 0;
436 }
437
438 /* Return the binary file name of the current process. Works on Linux
439 * only. This shoul be used for eyecandy only, don't rely on return
440 * non-NULL! */
441 char *pa_get_binary_name(char *s, size_t l) {
442 char path[PATH_MAX];
443 int i;
444 assert(s && l);
445
446 /* This works on Linux only */
447
448 snprintf(path, sizeof(path), "/proc/%u/exe", (unsigned) getpid());
449 if ((i = readlink(path, s, l-1)) < 0)
450 return NULL;
451
452 s[i] = 0;
453 return s;
454 }
455
456 /* Return a pointer to the filename inside a path (which is the last
457 * component). */
458 char *pa_path_get_filename(const char *p) {
459 char *fn;
460
461 if ((fn = strrchr(p, '/')))
462 return fn+1;
463
464 return (char*) p;
465 }
466
467 /* Try to parse a boolean string value.*/
468 int pa_parse_boolean(const char *v) {
469
470 if (!strcmp(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
471 return 1;
472 else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
473 return 0;
474
475 return -1;
476 }
477
478 /* Split the specified string wherever one of the strings in delimiter
479 * occurs. Each time it is called returns a newly allocated string
480 * with pa_xmalloc(). The variable state points to, should be
481 * initiallized to NULL before the first call. */
482 char *pa_split(const char *c, const char *delimiter, const char**state) {
483 const char *current = *state ? *state : c;
484 size_t l;
485
486 if (!*current)
487 return NULL;
488
489 l = strcspn(current, delimiter);
490 *state = current+l;
491
492 if (**state)
493 (*state)++;
494
495 return pa_xstrndup(current, l);
496 }
497
498 /* What is interpreted as whitespace? */
499 #define WHITESPACE " \t\n"
500
501 /* Split a string into words. Otherwise similar to pa_split(). */
502 char *pa_split_spaces(const char *c, const char **state) {
503 const char *current = *state ? *state : c;
504 size_t l;
505
506 if (!*current || *c == 0)
507 return NULL;
508
509 current += strspn(current, WHITESPACE);
510 l = strcspn(current, WHITESPACE);
511
512 *state = current+l;
513
514 return pa_xstrndup(current, l);
515 }
516
517 /* Return the name of an UNIX signal. Similar to GNU's strsignal() */
518 const char *pa_strsignal(int sig) {
519 switch(sig) {
520 case SIGINT: return "SIGINT";
521 case SIGTERM: return "SIGTERM";
522 case SIGUSR1: return "SIGUSR1";
523 case SIGUSR2: return "SIGUSR2";
524 case SIGXCPU: return "SIGXCPU";
525 case SIGPIPE: return "SIGPIPE";
526 case SIGCHLD: return "SIGCHLD";
527 case SIGHUP: return "SIGHUP";
528 default: return "UNKNOWN SIGNAL";
529 }
530 }
531
532
533 /* Check whether the specified GID and the group name match */
534 static int is_group(gid_t gid, const char *name) {
535 struct group group, *result = NULL;
536 long n;
537 void *data;
538 int r = -1;
539
540 #ifdef HAVE_GETGRGID_R
541 #ifdef _SC_GETGR_R_SIZE_MAX
542 n = sysconf(_SC_GETGR_R_SIZE_MAX);
543 #else
544 n = -1;
545 #endif
546 if (n < 0) n = 512;
547 data = pa_xmalloc(n);
548
549 if (getgrgid_r(gid, &group, data, n, &result) < 0 || !result) {
550 pa_log(__FILE__ ": getgrgid_r(%u) failed: %s\n", gid, strerror(errno));
551 goto finish;
552 }
553
554
555 r = strcmp(name, result->gr_name) == 0;
556
557 finish:
558 pa_xfree(data);
559 #else
560 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X) that do not
561 * support getgrgid_r. */
562 if ((result = getgrgid(gid)) == NULL) {
563 pa_log(__FILE__ ": getgrgid(%u) failed: %s\n", gid, strerror(errno));
564 goto finish;
565 }
566
567 r = strcmp(name, result->gr_name) == 0;
568
569 finish:
570 #endif
571
572 return r;
573 }
574
575 /* Check the current user is member of the specified group */
576 int pa_uid_in_group(const char *name, gid_t *gid) {
577 gid_t *gids, tgid;
578 long n = sysconf(_SC_NGROUPS_MAX);
579 int r = -1, i;
580
581 assert(n > 0);
582
583 gids = pa_xmalloc(sizeof(gid_t)*n);
584
585 if ((n = getgroups(n, gids)) < 0) {
586 pa_log(__FILE__": getgroups() failed: %s\n", strerror(errno));
587 goto finish;
588 }
589
590 for (i = 0; i < n; i++) {
591 if (is_group(gids[i], name) > 0) {
592 *gid = gids[i];
593 r = 1;
594 goto finish;
595 }
596 }
597
598 if (is_group(tgid = getgid(), name) > 0) {
599 *gid = tgid;
600 r = 1;
601 goto finish;
602 }
603
604 r = 0;
605
606 finish:
607
608 pa_xfree(gids);
609 return r;
610 }
611
612 /* Lock or unlock a file entirely. (advisory) */
613 int pa_lock_fd(int fd, int b) {
614 struct flock flock;
615
616 /* Try a R/W lock first */
617
618 flock.l_type = b ? F_WRLCK : F_UNLCK;
619 flock.l_whence = SEEK_SET;
620 flock.l_start = 0;
621 flock.l_len = 0;
622
623 if (fcntl(fd, F_SETLKW, &flock) >= 0)
624 return 0;
625
626 /* Perhaps the file descriptor qas opened for read only, than try again with a read lock. */
627 if (b && errno == EBADF) {
628 flock.l_type = F_RDLCK;
629 if (fcntl(fd, F_SETLKW, &flock) >= 0)
630 return 0;
631 }
632
633 pa_log(__FILE__": %slock failed: %s\n", !b ? "un" : "", strerror(errno));
634 return -1;
635 }
636
637 /* Remove trailing newlines from a string */
638 char* pa_strip_nl(char *s) {
639 assert(s);
640
641 s[strcspn(s, "\r\n")] = 0;
642 return s;
643 }
644
645 /* Create a temporary lock file and lock it. */
646 int pa_lock_lockfile(const char *fn) {
647 int fd = -1;
648 assert(fn);
649
650 for (;;) {
651 struct stat st;
652
653 if ((fd = open(fn, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
654 pa_log(__FILE__": failed to create lock file '%s': %s\n", fn, strerror(errno));
655 goto fail;
656 }
657
658 if (pa_lock_fd(fd, 1) < 0) {
659 pa_log(__FILE__": failed to lock file '%s'.\n", fn);
660 goto fail;
661 }
662
663 if (fstat(fd, &st) < 0) {
664 pa_log(__FILE__": failed to fstat() file '%s'.\n", fn);
665 goto fail;
666 }
667
668 /* Check wheter the file has been removed meanwhile. When yes, restart this loop, otherwise, we're done */
669 if (st.st_nlink >= 1)
670 break;
671
672 if (pa_lock_fd(fd, 0) < 0) {
673 pa_log(__FILE__": failed to unlock file '%s'.\n", fn);
674 goto fail;
675 }
676
677 if (close(fd) < 0) {
678 pa_log(__FILE__": failed to close file '%s'.\n", fn);
679 goto fail;
680 }
681
682 fd = -1;
683 }
684
685 return fd;
686
687 fail:
688
689 if (fd >= 0)
690 close(fd);
691
692 return -1;
693 }
694
695 /* Unlock a temporary lcok file */
696 int pa_unlock_lockfile(const char *fn, int fd) {
697 int r = 0;
698 assert(fn && fd >= 0);
699
700 if (unlink(fn) < 0) {
701 pa_log_warn(__FILE__": WARNING: unable to remove lock file '%s': %s\n", fn, strerror(errno));
702 r = -1;
703 }
704
705 if (pa_lock_fd(fd, 0) < 0) {
706 pa_log_warn(__FILE__": WARNING: failed to unlock file '%s'.\n", fn);
707 r = -1;
708 }
709
710 if (close(fd) < 0) {
711 pa_log_warn(__FILE__": WARNING: failed to close lock file '%s': %s\n", fn, strerror(errno));
712 r = -1;
713 }
714
715 return r;
716 }
717
718 /* Try to open a configuration file. If "env" is specified, open the
719 * value of the specified environment variable. Otherwise look for a
720 * file "local" in the home directory or a file "global" in global
721 * file system. If "result" is non-NULL, a pointer to a newly
722 * allocated buffer containing the used configuration file is
723 * stored there.*/
724 FILE *pa_open_config_file(const char *global, const char *local, const char *env, char **result) {
725 const char *e;
726 char h[PATH_MAX];
727
728 if (env && (e = getenv(env))) {
729 if (result)
730 *result = pa_xstrdup(e);
731 return fopen(e, "r");
732 }
733
734 if (local && pa_get_home_dir(h, sizeof(h))) {
735 FILE *f;
736 char *l;
737
738 l = pa_sprintf_malloc("%s/%s", h, local);
739 f = fopen(l, "r");
740
741 if (f || errno != ENOENT) {
742 if (result)
743 *result = l;
744 else
745 pa_xfree(l);
746 return f;
747 }
748
749 pa_xfree(l);
750 }
751
752 if (!global) {
753 if (result)
754 *result = NULL;
755 errno = ENOENT;
756 return NULL;
757 }
758
759 if (result)
760 *result = pa_xstrdup(global);
761
762 return fopen(global, "r");
763 }
764
765 /* Format the specified data as a hexademical string */
766 char *pa_hexstr(const uint8_t* d, size_t dlength, char *s, size_t slength) {
767 size_t i = 0, j = 0;
768 const char hex[] = "0123456789abcdef";
769 assert(d && s && slength > 0);
770
771 while (i < dlength && j+3 <= slength) {
772 s[j++] = hex[*d >> 4];
773 s[j++] = hex[*d & 0xF];
774
775 d++;
776 i++;
777 }
778
779 s[j < slength ? j : slength] = 0;
780 return s;
781 }
782
783 /* Convert a hexadecimal digit to a number or -1 if invalid */
784 static int hexc(char c) {
785 if (c >= '0' && c <= '9')
786 return c - '0';
787
788 if (c >= 'A' && c <= 'F')
789 return c - 'A' + 10;
790
791 if (c >= 'a' && c <= 'f')
792 return c - 'a' + 10;
793
794 return -1;
795 }
796
797 /* Parse a hexadecimal string as created by pa_hexstr() to a BLOB */
798 size_t pa_parsehex(const char *p, uint8_t *d, size_t dlength) {
799 size_t j = 0;
800 assert(p && d);
801
802 while (j < dlength && *p) {
803 int b;
804
805 if ((b = hexc(*(p++))) < 0)
806 return (size_t) -1;
807
808 d[j] = (uint8_t) (b << 4);
809
810 if (!*p)
811 return (size_t) -1;
812
813 if ((b = hexc(*(p++))) < 0)
814 return (size_t) -1;
815
816 d[j] |= (uint8_t) b;
817 j++;
818 }
819
820 return j;
821 }
822
823 /* Return the fully qualified domain name in *s */
824 char *pa_get_fqdn(char *s, size_t l) {
825 char hn[256];
826 struct addrinfo *a, hints;
827
828 if (!pa_get_host_name(hn, sizeof(hn)))
829 return NULL;
830
831 memset(&hints, 0, sizeof(hints));
832 hints.ai_family = AF_UNSPEC;
833 hints.ai_flags = AI_CANONNAME;
834
835 if (getaddrinfo(hn, NULL, &hints, &a) < 0 || !a || !a->ai_canonname || !*a->ai_canonname)
836 return pa_strlcpy(s, hn, l);
837
838 pa_strlcpy(s, a->ai_canonname, l);
839 freeaddrinfo(a);
840 return s;
841 }
842
843 /* Returns nonzero when *s starts with *pfx */
844 int pa_startswith(const char *s, const char *pfx) {
845 size_t l;
846 assert(s && pfx);
847 l = strlen(pfx);
848
849 return strlen(s) >= l && strncmp(s, pfx, l) == 0;
850 }
851
852 /* if fn is null return the polypaudio run time path in s (/tmp/polypaudio)
853 * if fn is non-null and starts with / return fn in s
854 * otherwise append fn to the run time path and return it in s */
855 char *pa_runtime_path(const char *fn, char *s, size_t l) {
856 char u[256];
857
858 if (fn && *fn == '/')
859 return pa_strlcpy(s, fn, l);
860
861 snprintf(s, l, PA_RUNTIME_PATH_PREFIX"%s%s%s", pa_get_user_name(u, sizeof(u)), fn ? "/" : "", fn ? fn : "");
862 return s;
863 }
864
865 /* Wait t milliseconds */
866 int pa_msleep(unsigned long t) {
867 struct timespec ts;
868
869 ts.tv_sec = t/1000;
870 ts.tv_nsec = (t % 1000) * 1000000;
871
872 return nanosleep(&ts, NULL);
873 }
874
875 /* Convert the string s to a signed integer in *ret_i */
876 int pa_atoi(const char *s, int32_t *ret_i) {
877 char *x = NULL;
878 long l;
879 assert(s && ret_i);
880
881 l = strtol(s, &x, 0);
882
883 if (!x || *x)
884 return -1;
885
886 *ret_i = (int32_t) l;
887
888 return 0;
889 }
890
891 /* Convert the string s to an unsigned integer in *ret_u */
892 int pa_atou(const char *s, uint32_t *ret_u) {
893 char *x = NULL;
894 unsigned long l;
895 assert(s && ret_u);
896
897 l = strtoul(s, &x, 0);
898
899 if (!x || *x)
900 return -1;
901
902 *ret_u = (uint32_t) l;
903
904 return 0;
905 }