]>
code.delx.au - pulseaudio/blob - polyp/util.c
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>
41 #include <sys/resource.h>
44 #include <samplerate.h>
50 void pa_make_nonblock_fd(int fd
) {
53 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
54 if (!(v
& O_NONBLOCK
))
55 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
58 int pa_make_secure_dir(const char* dir
) {
61 if (mkdir(dir
, 0700) < 0)
65 if (lstat(dir
, &st
) < 0)
68 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
78 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
80 assert(fd
>= 0 && data
&& size
);
85 if ((r
= read(fd
, data
, size
)) < 0)
92 data
= (uint8_t*) data
+ r
;
99 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
101 assert(fd
>= 0 && data
&& size
);
106 if ((r
= write(fd
, data
, size
)) < 0)
113 data
= (uint8_t*) data
+ r
;
120 void pa_check_signal_is_blocked(int sig
) {
125 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
127 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
128 pa_log(__FILE__
": sigprocmask() failed: %s\n", strerror(errno
));
135 if (sigismember(&set
, sig
))
138 if (sigaction(sig
, NULL
, &sa
) < 0) {
139 pa_log(__FILE__
": sigaction() failed: %s\n", strerror(errno
));
143 if (sa
.sa_handler
!= SIG_DFL
)
146 pa_log(__FILE__
": WARNING: %s is not trapped. This might cause malfunction!\n", pa_strsignal(sig
));
149 /* The following is based on an example from the GNU libc documentation */
150 char *pa_sprintf_malloc(const char *format
, ...) {
160 c
= pa_xrealloc(c
, size
);
162 va_start(ap
, format
);
163 r
= vsnprintf(c
, size
, format
, ap
);
166 if (r
> -1 && r
< size
)
169 if (r
> -1) /* glibc 2.1 */
176 char *pa_vsprintf_malloc(const char *format
, va_list ap
) {
186 c
= pa_xrealloc(c
, size
);
187 r
= vsnprintf(c
, size
, format
, ap
);
189 if (r
> -1 && r
< size
)
192 if (r
> -1) /* glibc 2.1 */
200 char *pa_get_user_name(char *s
, size_t l
) {
201 struct passwd pw
, *r
;
205 if (!(p
= getenv("USER")))
206 if (!(p
= getenv("LOGNAME")))
207 if (!(p
= getenv("USERNAME"))) {
209 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
210 snprintf(s
, l
, "%lu", (unsigned long) getuid());
217 snprintf(s
, l
, "%s", p
);
221 char *pa_get_host_name(char *s
, size_t l
) {
227 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
231 if (pa_timeval_cmp(a
, b
) < 0) {
232 const struct timeval
*c
;
238 r
= ((pa_usec_t
) a
->tv_sec
- b
->tv_sec
)* 1000000;
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
);
248 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
251 if (a
->tv_sec
< b
->tv_sec
)
254 if (a
->tv_sec
> b
->tv_sec
)
257 if (a
->tv_usec
< b
->tv_usec
)
260 if (a
->tv_usec
> b
->tv_usec
)
266 pa_usec_t
pa_age(const struct timeval
*tv
) {
269 gettimeofday(&now
, NULL
);
270 return pa_timeval_diff(&now
, tv
);
273 void pa_timeval_add(struct timeval
*tv
, pa_usec_t v
) {
278 tv
->tv_sec
+= (unsigned long) secs
;
283 while (tv
->tv_usec
>= 1000000) {
285 tv
->tv_usec
-= 1000000;
289 #define NICE_LEVEL (-15)
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
));
295 pa_log(__FILE__
": Successfully gained nice level %i.\n", NICE_LEVEL
);
297 #ifdef _POSIX_PRIORITY_SCHEDULING
299 struct sched_param sp
;
301 if (sched_getparam(0, &sp
) < 0) {
302 pa_log(__FILE__
": sched_getparam() failed: %s\n", strerror(errno
));
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
));
312 pa_log(__FILE__
": Successfully enabled SCHED_FIFO scheduling.\n");
317 void pa_reset_priority(void) {
318 #ifdef _POSIX_PRIORITY_SCHEDULING
320 struct sched_param sp
;
321 sched_getparam(0, &sp
);
322 sp
.sched_priority
= 0;
323 sched_setscheduler(0, SCHED_OTHER
, &sp
);
327 setpriority(PRIO_PROCESS
, 0, 0);
330 int pa_fd_set_cloexec(int fd
, int b
) {
334 if ((v
= fcntl(fd
, F_GETFD
, 0)) < 0)
337 v
= (v
& ~FD_CLOEXEC
) | (b
? FD_CLOEXEC
: 0);
339 if (fcntl(fd
, F_SETFD
, v
) < 0)
345 char *pa_get_binary_name(char *s
, size_t l
) {
350 /* This works on Linux only */
352 snprintf(path
, sizeof(path
), "/proc/%u/exe", (unsigned) getpid());
353 if ((i
= readlink(path
, s
, l
-1)) < 0)
360 char *pa_path_get_filename(const char *p
) {
363 if ((fn
= strrchr(p
, '/')))
369 int pa_parse_boolean(const char *v
) {
371 if (!strcmp(v
, "1") || v
[0] == 'y' || v
[0] == 'Y' || v
[0] == 't' || v
[0] == 'T' || !strcasecmp(v
, "on"))
373 else if (!strcmp(v
, "0") || v
[0] == 'n' || v
[0] == 'N' || v
[0] == 'f' || v
[0] == 'F' || !strcasecmp(v
, "off"))
379 char *pa_split(const char *c
, const char *delimiter
, const char**state
) {
380 const char *current
= *state
? *state
: c
;
386 l
= strcspn(current
, delimiter
);
392 return pa_xstrndup(current
, l
);
395 #define WHITESPACE " \t\n"
397 char *pa_split_spaces(const char *c
, const char **state
) {
398 const char *current
= *state
? *state
: c
;
404 current
+= strspn(current
, WHITESPACE
);
405 l
= strcspn(current
, WHITESPACE
);
409 return pa_xstrndup(current
, l
);
412 const char *pa_strsignal(int 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";
425 int pa_parse_resample_method(const char *string
) {
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"))