4 This file is part of polypaudio.
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.
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.
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
35 #include <sys/types.h>
42 #include <sys/resource.h>
47 #include <samplerate.h>
53 void pa_make_nonblock_fd(int fd) {
56 if ((v = fcntl(fd, F_GETFL)) >= 0)
57 if (!(v & O_NONBLOCK))
58 fcntl(fd, F_SETFL, v|O_NONBLOCK);
61 int pa_make_secure_dir(const char* dir) {
64 if (mkdir(dir, 0700) < 0)
68 if (lstat(dir, &st) < 0)
71 if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
81 ssize_t pa_loop_read(int fd, void*data, size_t size) {
83 assert(fd >= 0 && data && size);
88 if ((r = read(fd, data, size)) < 0)
95 data = (uint8_t*) data + r;
102 ssize_t pa_loop_write(int fd, const void*data, size_t size) {
104 assert(fd >= 0 && data && size);
109 if ((r = write(fd, data, size)) < 0)
116 data = (uint8_t*) data + r;
123 void pa_check_signal_is_blocked(int sig) {
128 if (pthread_sigmask(SIG_SETMASK, NULL, &set) < 0) {
130 if (sigprocmask(SIG_SETMASK, NULL, &set) < 0) {
131 pa_log(__FILE__": sigprocmask() failed: %s\n", strerror(errno));
138 if (sigismember(&set, sig))
141 if (sigaction(sig, NULL, &sa) < 0) {
142 pa_log(__FILE__": sigaction() failed: %s\n", strerror(errno));
146 if (sa.sa_handler != SIG_DFL)
149 pa_log(__FILE__": WARNING: %s is not trapped. This might cause malfunction!\n", pa_strsignal(sig));
152 /* The following is based on an example from the GNU libc documentation */
153 char *pa_sprintf_malloc(const char *format, ...) {
163 c = pa_xrealloc(c, size);
165 va_start(ap, format);
166 r = vsnprintf(c, size, format, ap);
169 if (r > -1 && r < size)
172 if (r > -1) /* glibc 2.1 */
179 char *pa_vsprintf_malloc(const char *format, va_list ap) {
189 c = pa_xrealloc(c, size);
190 r = vsnprintf(c, size, format, ap);
192 if (r > -1 && r < size)
195 if (r > -1) /* glibc 2.1 */
203 char *pa_get_user_name(char *s, size_t l) {
204 struct passwd pw, *r;
208 if (!(p = getenv("USER")))
209 if (!(p = getenv("LOGNAME")))
210 if (!(p = getenv("USERNAME"))) {
212 #ifdef HAVE_GETPWUID_R
213 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
215 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
216 * that do not support getpwuid_r. */
217 if ((r = getpwuid(getuid())) == NULL) {
219 snprintf(s, l, "%lu", (unsigned long) getuid());
226 snprintf(s, l, "%s", p);
230 char *pa_get_host_name(char *s, size_t l) {
236 pa_usec_t pa_timeval_diff(const struct timeval *a, const struct timeval *b) {
240 if (pa_timeval_cmp(a, b) < 0) {
241 const struct timeval *c;
247 r = ((pa_usec_t) a->tv_sec - b->tv_sec)* 1000000;
249 if (a->tv_usec > b->tv_usec)
250 r += ((pa_usec_t) a->tv_usec - b->tv_usec);
251 else if (a->tv_usec < b->tv_usec)
252 r -= ((pa_usec_t) b->tv_usec - a->tv_usec);
257 int pa_timeval_cmp(const struct timeval *a, const struct timeval *b) {
260 if (a->tv_sec < b->tv_sec)
263 if (a->tv_sec > b->tv_sec)
266 if (a->tv_usec < b->tv_usec)
269 if (a->tv_usec > b->tv_usec)
275 pa_usec_t pa_timeval_age(const struct timeval *tv) {
278 gettimeofday(&now, NULL);
279 return pa_timeval_diff(&now, tv);
282 void pa_timeval_add(struct timeval *tv, pa_usec_t v) {
287 tv->tv_sec += (unsigned long) secs;
292 while (tv->tv_usec >= 1000000) {
294 tv->tv_usec -= 1000000;
298 #define NICE_LEVEL (-15)
300 void pa_raise_priority(void) {
301 if (setpriority(PRIO_PROCESS, 0, NICE_LEVEL) < 0)
302 pa_log(__FILE__": setpriority() failed: %s\n", strerror(errno));
304 pa_log(__FILE__": Successfully gained nice level %i.\n", NICE_LEVEL);
306 #ifdef _POSIX_PRIORITY_SCHEDULING
308 struct sched_param sp;
310 if (sched_getparam(0, &sp) < 0) {
311 pa_log(__FILE__": sched_getparam() failed: %s\n", strerror(errno));
315 sp.sched_priority = 1;
316 if (sched_setscheduler(0, SCHED_FIFO, &sp) < 0) {
317 pa_log(__FILE__": sched_setscheduler() failed: %s\n", strerror(errno));
321 pa_log(__FILE__": Successfully enabled SCHED_FIFO scheduling.\n");
326 void pa_reset_priority(void) {
327 #ifdef _POSIX_PRIORITY_SCHEDULING
329 struct sched_param sp;
330 sched_getparam(0, &sp);
331 sp.sched_priority = 0;
332 sched_setscheduler(0, SCHED_OTHER, &sp);
336 setpriority(PRIO_PROCESS, 0, 0);
339 int pa_fd_set_cloexec(int fd, int b) {
343 if ((v = fcntl(fd, F_GETFD, 0)) < 0)
346 v = (v & ~FD_CLOEXEC) | (b ? FD_CLOEXEC : 0);
348 if (fcntl(fd, F_SETFD, v) < 0)
354 char *pa_get_binary_name(char *s, size_t l) {
359 /* This works on Linux only */
361 snprintf(path, sizeof(path), "/proc/%u/exe", (unsigned) getpid());
362 if ((i = readlink(path, s, l-1)) < 0)
369 char *pa_path_get_filename(const char *p) {
372 if ((fn = strrchr(p, '/')))
378 int pa_parse_boolean(const char *v) {
380 if (!strcmp(v, "1") || v[0] == 'y' || v[0] == 'Y' || v[0] == 't' || v[0] == 'T' || !strcasecmp(v, "on"))
382 else if (!strcmp(v, "0") || v[0] == 'n' || v[0] == 'N' || v[0] == 'f' || v[0] == 'F' || !strcasecmp(v, "off"))
388 char *pa_split(const char *c, const char *delimiter, const char**state) {
389 const char *current = *state ? *state : c;
395 l = strcspn(current, delimiter);
401 return pa_xstrndup(current, l);
404 #define WHITESPACE " \t\n"
406 char *pa_split_spaces(const char *c, const char **state) {
407 const char *current = *state ? *state : c;
413 current += strspn(current, WHITESPACE);
414 l = strcspn(current, WHITESPACE);
418 return pa_xstrndup(current, l);
421 const char *pa_strsignal(int sig) {
423 case SIGINT: return "SIGINT";
424 case SIGTERM: return "SIGTERM";
425 case SIGUSR1: return "SIGUSR1";
426 case SIGUSR2: return "SIGUSR2";
427 case SIGXCPU: return "SIGXCPU";
428 case SIGPIPE: return "SIGPIPE";
429 case SIGCHLD: return "SIGCHLD";
430 case SIGHUP: return "SIGHUP";
431 default: return "UNKNOWN SIGNAL";
435 int pa_parse_resample_method(const char *string) {
438 if (!strcmp(string, "sinc-best-quality"))
439 return SRC_SINC_BEST_QUALITY;
440 else if (!strcmp(string, "sinc-medium-quality"))
441 return SRC_SINC_MEDIUM_QUALITY;
442 else if (!strcmp(string, "sinc-fastest"))
443 return SRC_SINC_FASTEST;
444 else if (!strcmp(string, "zero-order-hold"))
445 return SRC_ZERO_ORDER_HOLD;
446 else if (!strcmp(string, "linear"))
452 static int is_group(gid_t gid, const char *name) {
453 struct group group, *result = NULL;
458 #ifdef HAVE_GETGRGID_R
459 #ifdef _SC_GETGR_R_SIZE_MAX
460 n = sysconf(_SC_GETGR_R_SIZE_MAX);
465 data = pa_xmalloc(n);
467 if (getgrgid_r(gid, &group, data, n, &result) < 0 || !result) {
468 pa_log(__FILE__ ": getgrgid_r(%u) failed: %s\n", gid, strerror(errno));
473 r = strcmp(name, result->gr_name) == 0;
478 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X) that do not
479 * support getgrgid_r. */
480 if ((result = getgrgid(gid)) == NULL) {
481 pa_log(__FILE__ ": getgrgid(%u) failed: %s\n", gid, strerror(errno));
485 r = strcmp(name, result->gr_name) == 0;
493 int pa_uid_in_group(const char *name, gid_t *gid) {
495 long n = sysconf(_SC_NGROUPS_MAX);
500 gids = pa_xmalloc(sizeof(gid_t)*n);
502 if ((n = getgroups(n, gids)) < 0) {
503 pa_log(__FILE__": getgroups() failed: %s\n", strerror(errno));
507 for (i = 0; i < n; i++) {
508 if (is_group(gids[i], name) > 0) {
515 if (is_group(tgid = getgid(), name) > 0) {
529 int pa_lock_fd(int fd, int b) {
533 flock.l_type = b ? F_WRLCK : F_UNLCK;
534 flock.l_whence = SEEK_SET;
538 if (fcntl(fd, F_SETLKW, &flock) < 0) {
539 pa_log(__FILE__": %slock failed: %s\n", !b ? "un" : "", strerror(errno));
546 char* pa_strip_nl(char *s) {
549 s[strcspn(s, "\r\n")] = 0;
553 int pa_lock_lockfile(const char *fn) {
557 if ((fd = open(fn, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
558 pa_log(__FILE__": failed to create lock file '%s'\n", fn);
562 if (pa_lock_fd(fd, 1) < 0)
576 int pa_unlock_lockfile(int fd) {
580 if (pa_lock_fd(fd, 0) < 0) {
581 pa_log(__FILE__": WARNING: failed to unlock file.\n");
586 pa_log(__FILE__": WARNING: failed to close lock file.\n");