]>
code.delx.au - pulseaudio/blob - src/pulse/util.c
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-util.h>
62 #include <pulsecore/macro.h>
63 #include <pulsecore/usergroup.h>
67 char *pa_get_user_name(char *s
, size_t l
) {
83 p
= getuid() == 0 ? "root" : NULL
;
85 if (!p
) p
= getenv("USER");
86 if (!p
) p
= getenv("LOGNAME");
87 if (!p
) p
= getenv("USERNAME");
90 name
= pa_strlcpy(s
, p
, l
);
94 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
95 pa_snprintf(s
, l
, "%lu", (unsigned long) getuid());
99 name
= pa_strlcpy(s
, r
->pw_name
, l
);
102 #elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
103 DWORD size
= sizeof(buf
);
105 if (!GetUserName(buf
, &size
)) {
110 name
= pa_strlcpy(s
, buf
, l
);
112 #else /* HAVE_PWD_H */
115 #endif /* HAVE_PWD_H */
121 char *pa_get_host_name(char *s
, size_t l
) {
126 if (gethostname(s
, l
) < 0)
133 char *pa_get_home_dir(char *s
, size_t l
) {
143 if ((e
= getenv("HOME"))) {
144 dir
= pa_strlcpy(s
, e
, l
);
148 if ((e
= getenv("USERPROFILE"))) {
149 dir
= pa_strlcpy(s
, e
, l
);
155 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
162 dir
= pa_strlcpy(s
, r
->pw_dir
, l
);
165 #endif /* HAVE_PWD_H */
173 if (!pa_is_path_absolute(dir
)) {
174 pa_log("Failed to get the home directory, not an absolute path: %s", dir
);
182 char *pa_get_binary_name(char *s
, size_t l
) {
187 #if defined(OS_IS_WIN32)
191 if (GetModuleFileName(NULL
, path
, PATH_MAX
))
192 return pa_strlcpy(s
, pa_path_get_filename(path
), l
);
199 /* This works on Linux only */
201 if ((rp
= pa_readlink("/proc/self/exe"))) {
202 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
213 if ((rp
= pa_readlink("/proc/curproc/file"))) {
214 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
221 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
224 #ifndef TASK_COMM_LEN
225 /* Actually defined in linux/sched.h */
226 #define TASK_COMM_LEN 16
229 char tcomm
[TASK_COMM_LEN
+1];
230 memset(tcomm
, 0, sizeof(tcomm
));
232 /* This works on Linux only */
233 if (prctl(PR_GET_NAME
, (unsigned long) tcomm
, 0, 0, 0) == 0)
234 return pa_strlcpy(s
, tcomm
, l
);
241 int mib
[] = { CTL_KERN
, KERN_PROCARGS
, getpid(), 0 };
242 size_t len
, nmib
= (sizeof(mib
) / sizeof(mib
[0])) - 1;
245 sysctl(mib
, nmib
, NULL
, &len
, NULL
, 0);
246 buf
= (char *) pa_xmalloc(len
);
248 if (sysctl(mib
, nmib
, buf
, &len
, NULL
, 0) == 0) {
249 pa_strlcpy(s
, basename(buf
), l
);
258 #endif /* OS_IS_DARWIN */
264 char *pa_path_get_filename(const char *p
) {
270 if ((fn
= strrchr(p
, PA_PATH_SEP_CHAR
)))
276 char *pa_get_fqdn(char *s
, size_t l
) {
278 #ifdef HAVE_GETADDRINFO
279 struct addrinfo
*a
, hints
;
285 if (!pa_get_host_name(hn
, sizeof(hn
)))
288 #ifdef HAVE_GETADDRINFO
289 memset(&hints
, 0, sizeof(hints
));
290 hints
.ai_family
= AF_UNSPEC
;
291 hints
.ai_flags
= AI_CANONNAME
;
293 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
294 return pa_strlcpy(s
, hn
, l
);
296 pa_strlcpy(s
, a
->ai_canonname
, l
);
300 return pa_strlcpy(s
, hn
, l
);
304 int pa_msleep(unsigned long t
) {
308 #elif defined(HAVE_NANOSLEEP)
311 ts
.tv_sec
= (time_t) (t
/ PA_MSEC_PER_SEC
);
312 ts
.tv_nsec
= (long) ((t
% PA_MSEC_PER_SEC
) * PA_NSEC_PER_MSEC
);
314 return nanosleep(&ts
, NULL
);
316 #error "Platform lacks a sleep function."