]>
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>
47 void pa_make_nonblock_fd(int fd
) {
50 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
51 if (!(v
& O_NONBLOCK
))
52 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
55 int pa_make_secure_dir(const char* dir
) {
58 if (mkdir(dir
, 0700) < 0)
62 if (lstat(dir
, &st
) < 0)
65 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
75 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
77 assert(fd
>= 0 && data
&& size
);
82 if ((r
= read(fd
, data
, size
)) < 0)
89 data
= (uint8_t*) data
+ r
;
96 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
98 assert(fd
>= 0 && data
&& size
);
103 if ((r
= write(fd
, data
, size
)) < 0)
110 data
= (uint8_t*) data
+ r
;
117 void pa_check_for_sigpipe(void) {
122 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
124 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
125 pa_log(__FILE__
": sigprocmask() failed: %s\n", strerror(errno
));
132 if (sigismember(&set
, SIGPIPE
))
135 if (sigaction(SIGPIPE
, NULL
, &sa
) < 0) {
136 pa_log(__FILE__
": sigaction() failed: %s\n", strerror(errno
));
140 if (sa
.sa_handler
!= SIG_DFL
)
143 pa_log(__FILE__
": WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
146 /* The following is based on an example from the GNU libc documentation */
147 char *pa_sprintf_malloc(const char *format
, ...) {
157 c
= pa_xrealloc(c
, size
);
159 va_start(ap
, format
);
160 r
= vsnprintf(c
, size
, format
, ap
);
163 if (r
> -1 && r
< size
)
166 if (r
> -1) /* glibc 2.1 */
173 char *pa_vsprintf_malloc(const char *format
, va_list ap
) {
183 c
= pa_xrealloc(c
, size
);
184 r
= vsnprintf(c
, size
, format
, ap
);
186 if (r
> -1 && r
< size
)
189 if (r
> -1) /* glibc 2.1 */
197 char *pa_get_user_name(char *s
, size_t l
) {
198 struct passwd pw
, *r
;
202 if (!(p
= getenv("USER")))
203 if (!(p
= getenv("LOGNAME")))
204 if (!(p
= getenv("USERNAME"))) {
206 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
207 snprintf(s
, l
, "%lu", (unsigned long) getuid());
214 snprintf(s
, l
, "%s", p
);
218 char *pa_get_host_name(char *s
, size_t l
) {
224 pa_usec_t
pa_timeval_diff(const struct timeval
*a
, const struct timeval
*b
) {
228 if (pa_timeval_cmp(a
, b
) < 0) {
229 const struct timeval
*c
;
235 r
= (a
->tv_sec
- b
->tv_sec
)* 1000000;
237 if (a
->tv_usec
> b
->tv_usec
)
238 r
+= (a
->tv_usec
- b
->tv_usec
);
239 else if (a
->tv_usec
< b
->tv_usec
)
240 r
-= (b
->tv_usec
- a
->tv_usec
);
245 int pa_timeval_cmp(const struct timeval
*a
, const struct timeval
*b
) {
248 if (a
->tv_sec
< b
->tv_sec
)
251 if (a
->tv_sec
> b
->tv_sec
)
254 if (a
->tv_usec
< b
->tv_usec
)
257 if (a
->tv_usec
> b
->tv_usec
)
263 pa_usec_t
pa_age(const struct timeval
*tv
) {
266 gettimeofday(&now
, NULL
);
267 return pa_timeval_diff(&now
, tv
);
270 #define NICE_LEVEL (-15)
272 void pa_raise_priority(void) {
273 if (setpriority(PRIO_PROCESS
, 0, NICE_LEVEL
) < 0)
274 pa_log(__FILE__
": setpriority() failed: %s\n", strerror(errno
));
276 pa_log(__FILE__
": Successfully gained nice level %i.\n", NICE_LEVEL
);
278 #ifdef _POSIX_PRIORITY_SCHEDULING
280 struct sched_param sp
;
282 if (sched_getparam(0, &sp
) < 0) {
283 pa_log(__FILE__
": sched_getparam() failed: %s\n", strerror(errno
));
287 sp
.sched_priority
= 1;
288 if (sched_setscheduler(0, SCHED_FIFO
, &sp
) < 0) {
289 pa_log(__FILE__
": sched_setscheduler() failed: %s\n", strerror(errno
));
293 pa_log(__FILE__
": Successfully enabled SCHED_FIFO scheduling.\n");
298 void pa_reset_priority(void) {
299 #ifdef _POSIX_PRIORITY_SCHEDULING
301 struct sched_param sp
;
302 sched_getparam(0, &sp
);
303 sp
.sched_priority
= 0;
304 sched_setscheduler(0, SCHED_OTHER
, &sp
);
308 setpriority(PRIO_PROCESS
, 0, 0);
311 int pa_fd_set_cloexec(int fd
, int b
) {
315 if ((v
= fcntl(fd
, F_GETFD
, 0)) < 0)
318 v
= (v
& ~FD_CLOEXEC
) | (b
? FD_CLOEXEC
: 0);
320 if (fcntl(fd
, F_SETFD
, v
) < 0)