]> code.delx.au - pulseaudio/blob - polyp/util.c
implement proper logging
[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 #include <pwd.h>
37 #include <signal.h>
38 #include <pthread.h>
39 #include <sys/time.h>
40 #include <sched.h>
41 #include <sys/resource.h>
42
43 #include "util.h"
44 #include "xmalloc.h"
45 #include "log.h"
46
47 void pa_make_nonblock_fd(int fd) {
48 int v;
49
50 if ((v = fcntl(fd, F_GETFL)) >= 0)
51 if (!(v & O_NONBLOCK))
52 fcntl(fd, F_SETFL, v|O_NONBLOCK);
53 }
54
55 int pa_make_secure_dir(const char* dir) {
56 struct stat st;
57
58 if (mkdir(dir, 0700) < 0)
59 if (errno != EEXIST)
60 return -1;
61
62 if (lstat(dir, &st) < 0)
63 goto fail;
64
65 if (!S_ISDIR(st.st_mode) || (st.st_uid != getuid()) || ((st.st_mode & 0777) != 0700))
66 goto fail;
67
68 return 0;
69
70 fail:
71 rmdir(dir);
72 return -1;
73 }
74
75 ssize_t pa_loop_read(int fd, void*data, size_t size) {
76 ssize_t ret = 0;
77 assert(fd >= 0 && data && size);
78
79 while (size > 0) {
80 ssize_t r;
81
82 if ((r = read(fd, data, size)) < 0)
83 return r;
84
85 if (r == 0)
86 break;
87
88 ret += r;
89 data = (uint8_t*) data + r;
90 size -= r;
91 }
92
93 return ret;
94 }
95
96 ssize_t pa_loop_write(int fd, const void*data, size_t size) {
97 ssize_t ret = 0;
98 assert(fd >= 0 && data && size);
99
100 while (size > 0) {
101 ssize_t r;
102
103 if ((r = write(fd, data, size)) < 0)
104 return r;
105
106 if (r == 0)
107 break;
108
109 ret += r;
110 data = (uint8_t*) data + r;
111 size -= r;
112 }
113
114 return ret;
115 }
116
117 void pa_check_for_sigpipe(void) {
118 struct sigaction sa;
119 sigset_t set;
120
121 #ifdef HAVE_PTHREAD
122 if (pthread_sigmask(SIG_SETMASK, NULL, &set) < 0) {
123 #endif
124 if (sigprocmask(SIG_SETMASK, NULL, &set) < 0) {
125 pa_log(__FILE__": sigprocmask() failed: %s\n", strerror(errno));
126 return;
127 }
128 #ifdef HAVE_PTHREAD
129 }
130 #endif
131
132 if (sigismember(&set, SIGPIPE))
133 return;
134
135 if (sigaction(SIGPIPE, NULL, &sa) < 0) {
136 pa_log(__FILE__": sigaction() failed: %s\n", strerror(errno));
137 return;
138 }
139
140 if (sa.sa_handler != SIG_DFL)
141 return;
142
143 pa_log(__FILE__": WARNING: SIGPIPE is not trapped. This might cause malfunction!\n");
144 }
145
146 /* The following is based on an example from the GNU libc documentation */
147 char *pa_sprintf_malloc(const char *format, ...) {
148 int size = 100;
149 char *c = NULL;
150
151 assert(format);
152
153 for(;;) {
154 int r;
155 va_list ap;
156
157 c = pa_xrealloc(c, size);
158
159 va_start(ap, format);
160 r = vsnprintf(c, size, format, ap);
161 va_end(ap);
162
163 if (r > -1 && r < size)
164 return c;
165
166 if (r > -1) /* glibc 2.1 */
167 size = r+1;
168 else /* glibc 2.0 */
169 size *= 2;
170 }
171 }
172
173 char *pa_vsprintf_malloc(const char *format, va_list ap) {
174 int size = 100;
175 char *c = NULL;
176
177 assert(format);
178
179 for(;;) {
180 int r;
181 va_list ap;
182
183 c = pa_xrealloc(c, size);
184 r = vsnprintf(c, size, format, ap);
185
186 if (r > -1 && r < size)
187 return c;
188
189 if (r > -1) /* glibc 2.1 */
190 size = r+1;
191 else /* glibc 2.0 */
192 size *= 2;
193 }
194 }
195
196
197 char *pa_get_user_name(char *s, size_t l) {
198 struct passwd pw, *r;
199 char buf[1024];
200 char *p;
201
202 if (!(p = getenv("USER")))
203 if (!(p = getenv("LOGNAME")))
204 if (!(p = getenv("USERNAME"))) {
205
206 if (getpwuid_r(getuid(), &pw, buf, sizeof(buf), &r) != 0 || !r) {
207 snprintf(s, l, "%lu", (unsigned long) getuid());
208 return s;
209 }
210
211 p = r->pw_name;
212 }
213
214 snprintf(s, l, "%s", p);
215 return s;
216 }
217
218 char *pa_get_host_name(char *s, size_t l) {
219 gethostname(s, l);
220 s[l-1] = 0;
221 return s;
222 }
223
224 uint32_t pa_age(struct timeval *tv) {
225 struct timeval now;
226 uint32_t r;
227 assert(tv);
228
229 if (tv->tv_sec == 0)
230 return 0;
231
232 gettimeofday(&now, NULL);
233
234 r = (now.tv_sec-tv->tv_sec) * 1000000;
235
236 if (now.tv_usec >= tv->tv_usec)
237 r += now.tv_usec - tv->tv_usec;
238 else
239 r -= tv->tv_usec - now.tv_usec;
240
241 return r;
242 }
243
244 #define NICE_LEVEL (-15)
245
246 void pa_raise_priority(void) {
247 if (setpriority(PRIO_PROCESS, 0, NICE_LEVEL) < 0)
248 pa_log(__FILE__": setpriority() failed: %s\n", strerror(errno));
249 else
250 pa_log(__FILE__": Successfully gained nice level %i.\n", NICE_LEVEL);
251
252 #ifdef _POSIX_PRIORITY_SCHEDULING
253 {
254 struct sched_param sp;
255
256 if (sched_getparam(0, &sp) < 0) {
257 pa_log(__FILE__": sched_getparam() failed: %s\n", strerror(errno));
258 return;
259 }
260
261 sp.sched_priority = 1;
262 if (sched_setscheduler(0, SCHED_FIFO, &sp) < 0) {
263 pa_log(__FILE__": sched_setscheduler() failed: %s\n", strerror(errno));
264 return;
265 }
266
267 pa_log(__FILE__": Successfully enabled SCHED_FIFO scheduling.\n");
268 }
269 #endif
270 }
271
272 void pa_reset_priority(void) {
273 #ifdef _POSIX_PRIORITY_SCHEDULING
274 {
275 struct sched_param sp;
276 sched_getparam(0, &sp);
277 sp.sched_priority = 0;
278 sched_setscheduler(0, SCHED_OTHER, &sp);
279 }
280 #endif
281
282 setpriority(PRIO_PROCESS, 0, 0);
283 }
284
285 int pa_fd_set_cloexec(int fd, int b) {
286 int v;
287 assert(fd >= 0);
288
289 if ((v = fcntl(fd, F_GETFD, 0)) < 0)
290 return -1;
291
292 v = (v & ~FD_CLOEXEC) | (b ? FD_CLOEXEC : 0);
293
294 if (fcntl(fd, F_SETFD, v) < 0)
295 return -1;
296
297 return 0;
298 }