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
34 #include <sys/types.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
52 #ifdef HAVE_SYS_PRCTL_H
53 #include <sys/prctl.h>
56 #include <pulse/xmalloc.h>
57 #include <pulse/timeval.h>
59 #include <pulsecore/winsock.h>
60 #include <pulsecore/core-error.h>
61 #include <pulsecore/log.h>
62 #include <pulsecore/core-util.h>
63 #include <pulsecore/macro.h>
64 #include <pulsecore/usergroup.h>
68 char *pa_get_user_name(char *s
, size_t l
) {
82 if ((p
= (getuid() == 0 ? "root" : NULL
)) ||
83 (p
= getenv("USER")) ||
84 (p
= getenv("LOGNAME")) ||
85 (p
= getenv("USERNAME")))
87 name
= pa_strlcpy(s
, p
, l
);
91 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
92 pa_snprintf(s
, l
, "%lu", (unsigned long) getuid());
96 name
= pa_strlcpy(s
, r
->pw_name
, l
);
99 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
100 DWORD size
= sizeof(buf
);
102 if (!GetUserName(buf
, &size
)) {
107 name
= pa_strlcpy(s
, buf
, l
);
109 #else /* HAVE_PWD_H */
112 #endif /* HAVE_PWD_H */
118 char *pa_get_host_name(char *s
, size_t l
) {
123 if (gethostname(s
, l
) < 0)
130 char *pa_get_home_dir(char *s
, size_t l
) {
140 if ((e
= getenv("HOME")))
141 return pa_strlcpy(s
, e
, l
);
143 if ((e
= getenv("USERPROFILE")))
144 return pa_strlcpy(s
, e
, l
);
148 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
155 dir
= pa_strlcpy(s
, r
->pw_dir
, l
);
160 #else /* HAVE_PWD_H */
167 char *pa_get_binary_name(char *s
, size_t l
) {
172 #if defined(OS_IS_WIN32)
176 if (GetModuleFileName(NULL
, path
, PATH_MAX
))
177 return pa_strlcpy(s
, pa_path_get_filename(path
), l
);
184 /* This works on Linux only */
186 if ((rp
= pa_readlink("/proc/self/exe"))) {
187 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
195 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
198 #ifndef TASK_COMM_LEN
199 /* Actually defined in linux/sched.h */
200 #define TASK_COMM_LEN 16
203 char tcomm
[TASK_COMM_LEN
+1];
204 memset(tcomm
, 0, sizeof(tcomm
));
206 /* This works on Linux only */
207 if (prctl(PR_GET_NAME
, (unsigned long) tcomm
, 0, 0, 0) == 0)
208 return pa_strlcpy(s
, tcomm
, l
);
217 char *pa_path_get_filename(const char *p
) {
223 if ((fn
= strrchr(p
, PA_PATH_SEP_CHAR
)))
229 char *pa_get_fqdn(char *s
, size_t l
) {
231 #ifdef HAVE_GETADDRINFO
232 struct addrinfo
*a
, hints
;
238 if (!pa_get_host_name(hn
, sizeof(hn
)))
241 #ifdef HAVE_GETADDRINFO
242 memset(&hints
, 0, sizeof(hints
));
243 hints
.ai_family
= AF_UNSPEC
;
244 hints
.ai_flags
= AI_CANONNAME
;
246 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
247 return pa_strlcpy(s
, hn
, l
);
249 pa_strlcpy(s
, a
->ai_canonname
, l
);
253 return pa_strlcpy(s
, hn
, l
);
257 int pa_msleep(unsigned long t
) {
261 #elif defined(HAVE_NANOSLEEP)
264 ts
.tv_sec
= (time_t) (t
/ PA_MSEC_PER_SEC
);
265 ts
.tv_nsec
= (long) ((t
% PA_MSEC_PER_SEC
) * PA_NSEC_PER_MSEC
);
267 return nanosleep(&ts
, NULL
);
269 #error "Platform lacks a sleep function."