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