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