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