]>
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>
46 void pa_make_nonblock_fd(int fd
) {
49 if ((v
= fcntl(fd
, F_GETFL
)) >= 0)
50 if (!(v
& O_NONBLOCK
))
51 fcntl(fd
, F_SETFL
, v
|O_NONBLOCK
);
54 int pa_make_secure_dir(const char* dir
) {
57 if (mkdir(dir
, 0700) < 0)
61 if (lstat(dir
, &st
) < 0)
64 if (!S_ISDIR(st
.st_mode
) || (st
.st_uid
!= getuid()) || ((st
.st_mode
& 0777) != 0700))
74 ssize_t
pa_loop_read(int fd
, void*data
, size_t size
) {
76 assert(fd
>= 0 && data
&& size
);
81 if ((r
= read(fd
, data
, size
)) < 0)
88 data
= (uint8_t*) data
+ r
;
95 ssize_t
pa_loop_write(int fd
, const void*data
, size_t size
) {
97 assert(fd
>= 0 && data
&& size
);
102 if ((r
= write(fd
, data
, size
)) < 0)
109 data
= (uint8_t*) data
+ r
;
116 void pa_check_for_sigpipe(void) {
121 if (pthread_sigmask(SIG_SETMASK
, NULL
, &set
) < 0) {
123 if (sigprocmask(SIG_SETMASK
, NULL
, &set
) < 0) {
124 fprintf(stderr
, __FILE__
": sigprocmask() failed: %s\n", strerror(errno
));
131 if (sigismember(&set
, SIGPIPE
))
134 if (sigaction(SIGPIPE
, NULL
, &sa
) < 0) {
135 fprintf(stderr
, __FILE__
": sigaction() failed: %s\n", strerror(errno
));
139 if (sa
.sa_handler
!= SIG_DFL
)
142 fprintf(stderr
, "polypaudio: WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
145 /* The following is based on an example from the GNU libc documentation */
146 char *pa_sprintf_malloc(const char *format
, ...) {
156 c
= pa_xrealloc(c
, size
);
158 va_start(ap
, format
);
159 r
= vsnprintf(c
, size
, format
, ap
);
162 if (r
> -1 && r
< size
)
165 if (r
> -1) /* glibc 2.1 */
172 char *pa_get_user_name(char *s
, size_t l
) {
173 struct passwd pw
, *r
;
177 if (!(p
= getenv("USER")))
178 if (!(p
= getenv("LOGNAME")))
179 if (!(p
= getenv("USERNAME"))) {
181 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
182 snprintf(s
, l
, "%lu", (unsigned long) getuid());
189 snprintf(s
, l
, "%s", p
);
193 char *pa_get_host_name(char *s
, size_t l
) {
199 uint32_t pa_age(struct timeval
*tv
) {
207 gettimeofday(&now
, NULL
);
209 r
= (now
.tv_sec
-tv
->tv_sec
) * 1000000;
211 if (now
.tv_usec
>= tv
->tv_usec
)
212 r
+= now
.tv_usec
- tv
->tv_usec
;
214 r
-= tv
->tv_usec
- now
.tv_usec
;
219 #define NICE_LEVEL (-15)
221 void pa_raise_priority(void) {
222 if (setpriority(PRIO_PROCESS
, 0, NICE_LEVEL
) < 0)
223 fprintf(stderr
, __FILE__
": setpriority() failed: %s\n", strerror(errno
));
225 fprintf(stderr
, __FILE__
": Successfully gained nice level %i.\n", NICE_LEVEL
);
227 #ifdef _POSIX_PRIORITY_SCHEDULING
229 struct sched_param sp
;
230 sched_getparam(0, &sp
);
231 sp
.sched_priority
= 1;
232 if (sched_setscheduler(0, SCHED_FIFO
, &sp
) < 0)
233 fprintf(stderr
, __FILE__
": sched_setscheduler() failed: %s\n", strerror(errno
));
235 fprintf(stderr
, __FILE__
": Successfully gained SCHED_FIFO scheduling.\n");
240 void pa_reset_priority(void) {
241 #ifdef _POSIX_PRIORITY_SCHEDULING
243 struct sched_param sp
;
244 sched_getparam(0, &sp
);
245 sp
.sched_priority
= 0;
246 sched_setscheduler(0, SCHED_OTHER
, &sp
);
250 setpriority(PRIO_PROCESS
, 0, 0);