]>
code.delx.au - pulseaudio/blob - src/pulse/util.c
6656bc3ff36fc679e5b74ef665fb67588886502f
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 return pa_strlcpy(s
, e
, l
);
146 if ((e
= getenv("USERPROFILE")))
147 return pa_strlcpy(s
, e
, l
);
151 if ((r
= pa_getpwuid_malloc(getuid())) == NULL
) {
158 dir
= pa_strlcpy(s
, r
->pw_dir
, l
);
163 #else /* HAVE_PWD_H */
170 char *pa_get_binary_name(char *s
, size_t l
) {
175 #if defined(OS_IS_WIN32)
179 if (GetModuleFileName(NULL
, path
, PATH_MAX
))
180 return pa_strlcpy(s
, pa_path_get_filename(path
), l
);
187 /* This works on Linux only */
189 if ((rp
= pa_readlink("/proc/self/exe"))) {
190 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
201 if ((rp
= pa_readlink("/proc/curproc/file"))) {
202 pa_strlcpy(s
, pa_path_get_filename(rp
), l
);
209 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
212 #ifndef TASK_COMM_LEN
213 /* Actually defined in linux/sched.h */
214 #define TASK_COMM_LEN 16
217 char tcomm
[TASK_COMM_LEN
+1];
218 memset(tcomm
, 0, sizeof(tcomm
));
220 /* This works on Linux only */
221 if (prctl(PR_GET_NAME
, (unsigned long) tcomm
, 0, 0, 0) == 0)
222 return pa_strlcpy(s
, tcomm
, l
);
229 int mib
[] = { CTL_KERN
, KERN_PROCARGS
, getpid(), 0 };
230 size_t len
, nmib
= (sizeof(mib
) / sizeof(mib
[0])) - 1;
233 sysctl(mib
, nmib
, NULL
, &len
, NULL
, 0);
234 buf
= (char *) pa_xmalloc(len
);
236 if (sysctl(mib
, nmib
, buf
, &len
, NULL
, 0) == 0) {
237 pa_strlcpy(s
, basename(buf
), l
);
246 #endif /* OS_IS_DARWIN */
252 char *pa_path_get_filename(const char *p
) {
258 if ((fn
= strrchr(p
, PA_PATH_SEP_CHAR
)))
264 char *pa_get_fqdn(char *s
, size_t l
) {
266 #ifdef HAVE_GETADDRINFO
267 struct addrinfo
*a
, hints
;
273 if (!pa_get_host_name(hn
, sizeof(hn
)))
276 #ifdef HAVE_GETADDRINFO
277 memset(&hints
, 0, sizeof(hints
));
278 hints
.ai_family
= AF_UNSPEC
;
279 hints
.ai_flags
= AI_CANONNAME
;
281 if (getaddrinfo(hn
, NULL
, &hints
, &a
) < 0 || !a
|| !a
->ai_canonname
|| !*a
->ai_canonname
)
282 return pa_strlcpy(s
, hn
, l
);
284 pa_strlcpy(s
, a
->ai_canonname
, l
);
288 return pa_strlcpy(s
, hn
, l
);
292 int pa_msleep(unsigned long t
) {
296 #elif defined(HAVE_NANOSLEEP)
299 ts
.tv_sec
= (time_t) (t
/ PA_MSEC_PER_SEC
);
300 ts
.tv_nsec
= (long) ((t
% PA_MSEC_PER_SEC
) * PA_NSEC_PER_MSEC
);
302 return nanosleep(&ts
, NULL
);
304 #error "Platform lacks a sleep function."