]>
code.delx.au - pulseaudio/blob - src/pulse/util.c
48ccf2951beef152bacb3b507f03613e20e1bfe0
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>
48 #ifdef HAVE_SYS_PRCTL_H
49 #include <sys/prctl.h>
54 #include <sys/sysctl.h>
57 #include <pulse/xmalloc.h>
58 #include <pulse/timeval.h>
60 #include <pulsecore/socket.h>
61 #include <pulsecore/core-error.h>
62 #include <pulsecore/log.h>
63 #include <pulsecore/core-util.h>
64 #include <pulsecore/macro.h>
65 #include <pulsecore/usergroup.h>
69 char *pa_get_user_name(char *s
, size_t l
) {
85 p
= getuid() == 0 ? "root" : NULL
;
87 if (!p
) p
= getenv("USER");
88 if (!p
) p
= getenv("LOGNAME");
89 if (!p
) p
= getenv("USERNAME");
92 name
= pa_strlcpy(s
, p
, l
);
96 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
97 pa_snprintf(s
, l
, "%lu", (unsigned long) getuid());
101 name
= pa_strlcpy(s
, r
->pw_name
, l
);
104 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
105 DWORD size
= sizeof(buf
);
107 if (!GetUserName(buf
, &size
)) {
112 name
= pa_strlcpy(s
, buf
, l
);
114 #else /* HAVE_PWD_H */
117 #endif /* HAVE_PWD_H */
123 char *pa_get_host_name(char *s
, size_t l
) {
128 if (gethostname(s
, l
) < 0)
135 char *pa_get_home_dir(char *s
, size_t l
) {
145 if ((e
= getenv("HOME")))
146 return pa_strlcpy(s
, e
, l
);
148 if ((e
= getenv("USERPROFILE")))
149 return pa_strlcpy(s
, e
, l
);
153 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
160 dir
= pa_strlcpy(s
, r
->pw_dir
, l
);
165 #else /* HAVE_PWD_H */
172 char *pa_get_binary_name(char *s
, size_t l
) {
177 #if defined(OS_IS_WIN32)
181 if (GetModuleFileName(NULL
, path
, PATH_MAX
))
182 return pa_strlcpy(s
, pa_path_get_filename(path
), l
);
189 /* This works on Linux only */
191 if ((rp
= pa_readlink("/proc/self/exe"))) {
192 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
203 if ((rp
= pa_readlink("/proc/curproc/file"))) {
204 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
211 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
214 #ifndef TASK_COMM_LEN
215 /* Actually defined in linux/sched.h */
216 #define TASK_COMM_LEN 16
219 char tcomm
[TASK_COMM_LEN
+1];
220 memset(tcomm
, 0, sizeof(tcomm
));
222 /* This works on Linux only */
223 if (prctl(PR_GET_NAME
, (unsigned long) tcomm
, 0, 0, 0) == 0)
224 return pa_strlcpy(s
, tcomm
, l
);
231 int mib
[] = { CTL_KERN
, KERN_PROCARGS
, getpid(), 0 };
232 size_t len
, nmib
= (sizeof(mib
) / sizeof(mib
[0])) - 1;
235 sysctl(mib
, nmib
, NULL
, &len
, NULL
, 0);
236 buf
= (char *) pa_xmalloc(len
);
238 if (sysctl(mib
, nmib
, buf
, &len
, NULL
, 0) == 0) {
239 pa_strlcpy(s
, basename(buf
), l
);
248 #endif /* OS_IS_DARWIN */
254 char *pa_path_get_filename(const char *p
) {
260 if ((fn
= strrchr(p
, PA_PATH_SEP_CHAR
)))
266 char *pa_get_fqdn(char *s
, size_t l
) {
268 #ifdef HAVE_GETADDRINFO
269 struct addrinfo
*a
, hints
;
275 if (!pa_get_host_name(hn
, sizeof(hn
)))
278 #ifdef HAVE_GETADDRINFO
279 memset(&hints
, 0, sizeof(hints
));
280 hints
.ai_family
= AF_UNSPEC
;
281 hints
.ai_flags
= AI_CANONNAME
;
283 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
284 return pa_strlcpy(s
, hn
, l
);
286 pa_strlcpy(s
, a
->ai_canonname
, l
);
290 return pa_strlcpy(s
, hn
, l
);
294 int pa_msleep(unsigned long t
) {
298 #elif defined(HAVE_NANOSLEEP)
301 ts
.tv_sec
= (time_t) (t
/ PA_MSEC_PER_SEC
);
302 ts
.tv_nsec
= (long) ((t
% PA_MSEC_PER_SEC
) * PA_NSEC_PER_MSEC
);
304 return nanosleep(&ts
, NULL
);
306 #error "Platform lacks a sleep function."