Use <pulsecore/socket.h> instead of <sys/socket.h>
[pulseaudio-mirror.git] / src / pulse / util.c
blob3206e94c6ff4da3e6511c1c84f1c21f9737af384
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
27 #include <errno.h>
28 #include <limits.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <sys/types.h>
36 #ifdef HAVE_PWD_H
37 #include <pwd.h>
38 #endif
40 #ifdef HAVE_NETDB_H
41 #include <netdb.h>
42 #endif
44 #ifdef HAVE_WINDOWS_H
45 #include <windows.h>
46 #endif
48 #ifdef HAVE_SYS_PRCTL_H
49 #include <sys/prctl.h>
50 #endif
52 #include <pulse/xmalloc.h>
53 #include <pulse/timeval.h>
55 #include <pulsecore/socket.h>
56 #include <pulsecore/core-error.h>
57 #include <pulsecore/log.h>
58 #include <pulsecore/core-util.h>
59 #include <pulsecore/macro.h>
60 #include <pulsecore/usergroup.h>
62 #include "util.h"
64 char *pa_get_user_name(char *s, size_t l) {
65 const char *p;
66 char *name = NULL;
67 #ifdef OS_IS_WIN32
68 char buf[1024];
69 #endif
71 #ifdef HAVE_PWD_H
72 struct passwd *r;
73 #endif
75 pa_assert(s);
76 pa_assert(l > 0);
78 if ((p = (getuid() == 0 ? "root" : NULL)) ||
79 (p = getenv("USER")) ||
80 (p = getenv("LOGNAME")) ||
81 (p = getenv("USERNAME")))
83 name = pa_strlcpy(s, p, l);
84 } else {
85 #ifdef HAVE_PWD_H
87 if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
88 pa_snprintf(s, l, "%lu", (unsigned long) getuid());
89 return s;
92 name = pa_strlcpy(s, r->pw_name, l);
93 pa_getpwuid_free(r);
95 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
96 DWORD size = sizeof(buf);
98 if (!GetUserName(buf, &size)) {
99 errno = ENOENT;
100 return NULL;
103 name = pa_strlcpy(s, buf, l);
105 #else /* HAVE_PWD_H */
107 return NULL;
108 #endif /* HAVE_PWD_H */
111 return name;
114 char *pa_get_host_name(char *s, size_t l) {
116 pa_assert(s);
117 pa_assert(l > 0);
119 if (gethostname(s, l) < 0)
120 return NULL;
122 s[l-1] = 0;
123 return s;
126 char *pa_get_home_dir(char *s, size_t l) {
127 char *e, *dir;
129 #ifdef HAVE_PWD_H
130 struct passwd *r;
131 #endif
133 pa_assert(s);
134 pa_assert(l > 0);
136 if ((e = getenv("HOME")))
137 return pa_strlcpy(s, e, l);
139 if ((e = getenv("USERPROFILE")))
140 return pa_strlcpy(s, e, l);
142 #ifdef HAVE_PWD_H
143 errno = 0;
144 if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
145 if (!errno)
146 errno = ENOENT;
148 return NULL;
151 dir = pa_strlcpy(s, r->pw_dir, l);
153 pa_getpwuid_free(r);
155 return dir;
156 #else /* HAVE_PWD_H */
158 errno = ENOENT;
159 return NULL;
160 #endif
163 char *pa_get_binary_name(char *s, size_t l) {
165 pa_assert(s);
166 pa_assert(l > 0);
168 #if defined(OS_IS_WIN32)
170 char path[PATH_MAX];
172 if (GetModuleFileName(NULL, path, PATH_MAX))
173 return pa_strlcpy(s, pa_path_get_filename(path), l);
175 #endif
177 #ifdef __linux__
179 char *rp;
180 /* This works on Linux only */
182 if ((rp = pa_readlink("/proc/self/exe"))) {
183 pa_strlcpy(s, pa_path_get_filename(rp), l);
184 pa_xfree(rp);
185 return s;
188 #endif
190 #ifdef __FreeBSD__
192 char *rp;
194 if ((rp = pa_readlink("/proc/curproc/file"))) {
195 pa_strlcpy(s, pa_path_get_filename(rp), l);
196 pa_xfree(rp);
197 return s;
200 #endif
202 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
205 #ifndef TASK_COMM_LEN
206 /* Actually defined in linux/sched.h */
207 #define TASK_COMM_LEN 16
208 #endif
210 char tcomm[TASK_COMM_LEN+1];
211 memset(tcomm, 0, sizeof(tcomm));
213 /* This works on Linux only */
214 if (prctl(PR_GET_NAME, (unsigned long) tcomm, 0, 0, 0) == 0)
215 return pa_strlcpy(s, tcomm, l);
218 #endif
220 errno = ENOENT;
221 return NULL;
224 char *pa_path_get_filename(const char *p) {
225 char *fn;
227 if (!p)
228 return NULL;
230 if ((fn = strrchr(p, PA_PATH_SEP_CHAR)))
231 return fn+1;
233 return (char*) p;
236 char *pa_get_fqdn(char *s, size_t l) {
237 char hn[256];
238 #ifdef HAVE_GETADDRINFO
239 struct addrinfo *a, hints;
240 #endif
242 pa_assert(s);
243 pa_assert(l > 0);
245 if (!pa_get_host_name(hn, sizeof(hn)))
246 return NULL;
248 #ifdef HAVE_GETADDRINFO
249 memset(&hints, 0, sizeof(hints));
250 hints.ai_family = AF_UNSPEC;
251 hints.ai_flags = AI_CANONNAME;
253 if (getaddrinfo(hn, NULL, &hints, &a) < 0 || !a || !a->ai_canonname || !*a->ai_canonname)
254 return pa_strlcpy(s, hn, l);
256 pa_strlcpy(s, a->ai_canonname, l);
257 freeaddrinfo(a);
258 return s;
259 #else
260 return pa_strlcpy(s, hn, l);
261 #endif
264 int pa_msleep(unsigned long t) {
265 #ifdef OS_IS_WIN32
266 Sleep(t);
267 return 0;
268 #elif defined(HAVE_NANOSLEEP)
269 struct timespec ts;
271 ts.tv_sec = (time_t) (t / PA_MSEC_PER_SEC);
272 ts.tv_nsec = (long) ((t % PA_MSEC_PER_SEC) * PA_NSEC_PER_MSEC);
274 return nanosleep(&ts, NULL);
275 #else
276 #error "Platform lacks a sleep function."
277 #endif