1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
#include <stdarg.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include "util.h"
void pa_make_nonblock_fd(int fd) {
int v;
if ((v = fcntl(fd, F_GETFL)) >= 0)
if (!(v & O_NONBLOCK))
fcntl(fd, F_SETFL, v|O_NONBLOCK);
}
int pa_make_secure_dir(const char* dir) {
struct stat st;
if (mkdir(dir, 0700) < 0)
if (errno != EEXIST)
return -1;
if (lstat(dir, &st) < 0)
goto fail;
if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
goto fail;
return 0;
fail:
rmdir(dir);
return -1;
}
ssize_t pa_loop_read(int fd, void*data, size_t size) {
ssize_t ret = 0;
assert(fd >= 0 && data && size);
while (size > 0) {
ssize_t r;
if ((r = read(fd, data, size)) < 0)
return r;
if (r == 0)
break;
ret += r;
data += r;
size -= r;
}
return ret;
}
ssize_t pa_loop_write(int fd, const void*data, size_t size) {
ssize_t ret = 0;
assert(fd >= 0 && data && size);
while (size > 0) {
ssize_t r;
if ((r = write(fd, data, size)) < 0)
return r;
if (r == 0)
break;
ret += r;
data += r;
size -= r;
}
return ret;
}
void pa_check_for_sigpipe(void) {
struct sigaction sa;
if (sigaction(SIGPIPE, NULL, &sa) < 0) {
fprintf(stderr, __FILE__": sigaction() failed: %s\n", strerror(errno));
return;
}
if (sa.sa_handler == SIG_DFL)
fprintf(stderr, "polypaudio: WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
}
/* The following is based on an example from the GNU libc documentation */
char *pa_sprintf_malloc(const char *format, ...) {
int size = 100;
char *c = NULL;
assert(format);
for(;;) {
int r;
va_list ap;
c = realloc(c, size);
assert(c);
va_start(ap, format);
r = vsnprintf(c, size, format, ap);
va_end(ap);
if (r > -1 && r < size)
return c;
if (r > -1) /* glibc 2.1 */
size = r+1;
else /* glibc 2.0 */
size *= 2;
}
}
|