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