]>
code.delx.au - pulseaudio/blob - src/pulse/util.c
d561329cb70e9d25ccd628ddf41485df5e4268bd
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
7 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
37 #include <sys/types.h>
43 #ifdef HAVE_SYS_SOCKET_H
44 #include <sys/socket.h>
55 #ifdef HAVE_SYS_PRCTL_H
56 #include <sys/prctl.h>
59 #include "../pulsecore/winsock.h"
61 #include <pulsecore/core-error.h>
62 #include <pulsecore/log.h>
63 #include <pulsecore/core-util.h>
73 char *pa_get_user_name(char *s
, size_t l
) {
83 if (!(p
= getenv("USER")) && !(p
= getenv("LOGNAME")) && !(p
= getenv("USERNAME"))) {
86 #ifdef HAVE_GETPWUID_R
87 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
89 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
90 * that do not support getpwuid_r. */
91 if ((r
= getpwuid(getuid())) == NULL
) {
93 snprintf(s
, l
, "%lu", (unsigned long) getuid());
99 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
100 DWORD size
= sizeof(buf
);
102 if (!GetUserName(buf
, &size
))
107 #else /* HAVE_PWD_H */
109 #endif /* HAVE_PWD_H */
112 return pa_strlcpy(s
, p
, l
);
115 char *pa_get_host_name(char *s
, size_t l
) {
117 if (gethostname(s
, l
) < 0) {
118 pa_log("gethostname(): %s", pa_cstrerror(errno
));
125 char *pa_get_home_dir(char *s
, size_t l
) {
130 struct passwd pw
, *r
;
135 if ((e
= getenv("HOME")))
136 return pa_strlcpy(s
, e
, l
);
138 if ((e
= getenv("USERPROFILE")))
139 return pa_strlcpy(s
, e
, l
);
142 #ifdef HAVE_GETPWUID_R
143 if (getpwuid_r(getuid(), &pw
, buf
, sizeof(buf
), &r
) != 0 || !r
) {
144 pa_log("getpwuid_r() failed");
146 /* XXX Not thread-safe, but needed on OSes (e.g. FreeBSD 4.X)
147 * that do not support getpwuid_r. */
148 if ((r
= getpwuid(getuid())) == NULL
) {
149 pa_log("getpwuid_r() failed");
154 return pa_strlcpy(s
, r
->pw_dir
, l
);
155 #else /* HAVE_PWD_H */
160 char *pa_get_binary_name(char *s
, size_t l
) {
165 #if defined(OS_IS_WIN32)
169 if (GetModuleFileName(NULL
, path
, PATH_MAX
))
170 return pa_strlcpy(s
, pa_path_get_filename(path
), l
);
178 /* This works on Linux only */
180 if ((i
= readlink("/proc/self/exe", path
, sizeof(path
)-1)) >= 0) {
182 return pa_strlcpy(s
, pa_path_get_filename(path
), l
);
188 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
191 #ifndef TASK_COMM_LEN
192 /* Actually defined in linux/sched.h */
193 #define TASK_COMM_LEN 16
196 char tcomm
[TASK_COMM_LEN
+1];
197 memset(tcomm
, 0, sizeof(tcomm
));
199 /* This works on Linux only */
200 if (prctl(PR_GET_NAME
, (unsigned long) tcomm
, 0, 0, 0) == 0)
201 return pa_strlcpy(s
, tcomm
, l
);
209 const char *pa_path_get_filename(const char *p
) {
212 if ((fn
= strrchr(p
, PATH_SEP
)))
215 return (const char*) p
;
218 char *pa_get_fqdn(char *s
, size_t l
) {
220 #ifdef HAVE_GETADDRINFO
221 struct addrinfo
*a
, hints
;
224 if (!pa_get_host_name(hn
, sizeof(hn
)))
227 #ifdef HAVE_GETADDRINFO
228 memset(&hints
, 0, sizeof(hints
));
229 hints
.ai_family
= AF_UNSPEC
;
230 hints
.ai_flags
= AI_CANONNAME
;
232 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
233 return pa_strlcpy(s
, hn
, l
);
235 pa_strlcpy(s
, a
->ai_canonname
, l
);
239 return pa_strlcpy(s
, hn
, l
);
243 int pa_msleep(unsigned long t
) {
247 #elif defined(HAVE_NANOSLEEP)
251 ts
.tv_nsec
= (t
% 1000) * 1000000;
253 return nanosleep(&ts
, NULL
);
255 #error "Platform lacks a sleep function."