2006-05-17 20:09:57 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2006-05-17 20:09:57 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2006-05-17 20:09:57 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
Lesser General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2006-06-19 21:53:48 +00:00
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
2006-05-17 20:09:57 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-05-18 06:45:43 +00:00
|
|
|
#include <errno.h>
|
2006-05-18 06:53:54 +00:00
|
|
|
#include <limits.h>
|
2006-05-17 20:09:57 +00:00
|
|
|
#include <stdio.h>
|
2006-05-18 06:45:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2006-05-17 20:09:57 +00:00
|
|
|
#include <time.h>
|
2006-05-18 06:45:43 +00:00
|
|
|
#include <unistd.h>
|
2006-05-17 20:09:57 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_PWD_H
|
|
|
|
|
#include <pwd.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-05-18 06:45:43 +00:00
|
|
|
#ifdef HAVE_NETDB_H
|
|
|
|
|
#include <netdb.h>
|
2006-05-17 20:09:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
#ifdef HAVE_SYS_PRCTL_H
|
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-04-15 19:35:25 +02:00
|
|
|
#ifdef OS_IS_DARWIN
|
|
|
|
|
#include <libgen.h>
|
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-29 15:32:22 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2009-02-19 04:01:12 +01:00
|
|
|
#include <pulse/timeval.h>
|
|
|
|
|
|
2011-01-04 17:03:13 +01:00
|
|
|
#include <pulsecore/socket.h>
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/core-util.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2009-08-21 16:02:57 -06:00
|
|
|
#include <pulsecore/usergroup.h>
|
2006-05-17 20:09:57 +00:00
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
2014-10-11 01:34:23 -03:00
|
|
|
#if defined(HAVE_DLADDR) && defined(PA_GCC_WEAKREF)
|
|
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
#endif
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
|
|
static int _main() PA_GCC_WEAKREF(main);
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
char *pa_get_user_name(char *s, size_t l) {
|
2008-03-31 22:06:15 +00:00
|
|
|
const char *p;
|
2009-08-21 16:02:57 -06:00
|
|
|
char *name = NULL;
|
|
|
|
|
#ifdef OS_IS_WIN32
|
2006-05-17 20:09:57 +00:00
|
|
|
char buf[1024];
|
2009-08-21 16:02:57 -06:00
|
|
|
#endif
|
2006-05-17 20:09:57 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_PWD_H
|
2009-08-21 16:02:57 -06:00
|
|
|
struct passwd *r;
|
2006-05-17 20:09:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(l > 0);
|
2006-05-17 20:09:57 +00:00
|
|
|
|
2011-01-06 00:51:33 +01:00
|
|
|
p = NULL;
|
|
|
|
|
#ifdef HAVE_GETUID
|
|
|
|
|
p = getuid() == 0 ? "root" : NULL;
|
|
|
|
|
#endif
|
|
|
|
|
if (!p) p = getenv("USER");
|
|
|
|
|
if (!p) p = getenv("LOGNAME");
|
|
|
|
|
if (!p) p = getenv("USERNAME");
|
|
|
|
|
|
|
|
|
|
if (p) {
|
2009-08-21 16:02:57 -06:00
|
|
|
name = pa_strlcpy(s, p, l);
|
|
|
|
|
} else {
|
2006-05-17 20:09:57 +00:00
|
|
|
#ifdef HAVE_PWD_H
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-08-21 16:02:57 -06:00
|
|
|
if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_snprintf(s, l, "%lu", (unsigned long) getuid());
|
2006-05-17 20:09:57 +00:00
|
|
|
return s;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-08-21 16:02:57 -06:00
|
|
|
name = pa_strlcpy(s, r->pw_name, l);
|
|
|
|
|
pa_getpwuid_free(r);
|
2006-05-17 20:09:57 +00:00
|
|
|
|
|
|
|
|
#elif defined(OS_IS_WIN32) /* HAVE_PWD_H */
|
|
|
|
|
DWORD size = sizeof(buf);
|
|
|
|
|
|
2008-08-09 03:44:46 +02:00
|
|
|
if (!GetUserName(buf, &size)) {
|
|
|
|
|
errno = ENOENT;
|
2006-05-17 20:09:57 +00:00
|
|
|
return NULL;
|
2008-08-09 03:44:46 +02:00
|
|
|
}
|
2006-05-17 20:09:57 +00:00
|
|
|
|
2009-08-21 16:02:57 -06:00
|
|
|
name = pa_strlcpy(s, buf, l);
|
2006-05-17 20:09:57 +00:00
|
|
|
|
|
|
|
|
#else /* HAVE_PWD_H */
|
2008-08-09 03:44:46 +02:00
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
return NULL;
|
|
|
|
|
#endif /* HAVE_PWD_H */
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-21 16:02:57 -06:00
|
|
|
return name;
|
2006-05-17 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *pa_get_host_name(char *s, size_t l) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(l > 0);
|
|
|
|
|
|
2008-08-07 02:24:19 +02:00
|
|
|
if (gethostname(s, l) < 0)
|
2006-05-17 20:09:57 +00:00
|
|
|
return NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
s[l-1] = 0;
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *pa_get_home_dir(char *s, size_t l) {
|
2011-06-23 22:21:03 +02:00
|
|
|
char *e;
|
|
|
|
|
char *dir;
|
2014-03-19 12:16:08 +02:00
|
|
|
#ifdef HAVE_PWD_H
|
2009-08-21 16:02:57 -06:00
|
|
|
struct passwd *r;
|
2006-05-17 20:09:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(l > 0);
|
2006-05-17 20:09:57 +00:00
|
|
|
|
2014-03-19 12:16:08 +02:00
|
|
|
if ((e = getenv("HOME"))) {
|
|
|
|
|
dir = pa_strlcpy(s, e, l);
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2006-05-17 20:09:57 +00:00
|
|
|
|
2014-03-19 12:16:08 +02:00
|
|
|
if ((e = getenv("USERPROFILE"))) {
|
|
|
|
|
dir = pa_strlcpy(s, e, l);
|
|
|
|
|
goto finish;
|
|
|
|
|
}
|
2006-05-17 20:09:57 +00:00
|
|
|
|
|
|
|
|
#ifdef HAVE_PWD_H
|
2008-08-09 03:44:46 +02:00
|
|
|
errno = 0;
|
2009-08-21 16:02:57 -06:00
|
|
|
if ((r = pa_getpwuid_malloc(getuid())) == NULL) {
|
2008-08-09 03:44:46 +02:00
|
|
|
if (!errno)
|
|
|
|
|
errno = ENOENT;
|
|
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-21 16:02:57 -06:00
|
|
|
dir = pa_strlcpy(s, r->pw_dir, l);
|
|
|
|
|
|
|
|
|
|
pa_getpwuid_free(r);
|
2014-03-19 12:16:08 +02:00
|
|
|
#endif /* HAVE_PWD_H */
|
2009-08-21 16:02:57 -06:00
|
|
|
|
2014-03-19 12:16:08 +02:00
|
|
|
finish:
|
|
|
|
|
if (!dir) {
|
|
|
|
|
errno = ENOENT;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2008-08-09 03:44:46 +02:00
|
|
|
|
2014-03-19 12:16:08 +02:00
|
|
|
if (!pa_is_path_absolute(dir)) {
|
|
|
|
|
pa_log("Failed to get the home directory, not an absolute path: %s", dir);
|
|
|
|
|
errno = ENOENT;
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dir;
|
2006-05-17 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *pa_get_binary_name(char *s, size_t l) {
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(l > 0);
|
2006-05-17 20:09:57 +00:00
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
#if defined(OS_IS_WIN32)
|
|
|
|
|
{
|
|
|
|
|
char path[PATH_MAX];
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
if (GetModuleFileName(NULL, path, PATH_MAX))
|
|
|
|
|
return pa_strlcpy(s, pa_path_get_filename(path), l);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2014-08-04 14:45:23 +02:00
|
|
|
#if defined(__linux__) || defined(__FreeBSD_kernel__)
|
2006-07-14 00:17:31 +00:00
|
|
|
{
|
2007-10-29 15:32:22 +00:00
|
|
|
char *rp;
|
2014-08-04 14:45:23 +02:00
|
|
|
/* This works on Linux and Debian/kFreeBSD */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-29 15:32:22 +00:00
|
|
|
if ((rp = pa_readlink("/proc/self/exe"))) {
|
|
|
|
|
pa_strlcpy(s, pa_path_get_filename(rp), l);
|
|
|
|
|
pa_xfree(rp);
|
|
|
|
|
return s;
|
2006-07-14 00:17:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
2009-11-21 01:13:35 +01:00
|
|
|
#endif
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-11-21 01:13:35 +01:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
|
{
|
|
|
|
|
char *rp;
|
|
|
|
|
|
2011-01-04 17:03:13 +01:00
|
|
|
if ((rp = pa_readlink("/proc/curproc/file"))) {
|
|
|
|
|
pa_strlcpy(s, pa_path_get_filename(rp), l);
|
|
|
|
|
pa_xfree(rp);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
2009-11-21 01:13:35 +01:00
|
|
|
}
|
2006-07-14 00:17:31 +00:00
|
|
|
#endif
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2014-10-11 01:34:23 -03:00
|
|
|
#if defined(HAVE_DLADDR) && defined(PA_GCC_WEAKREF)
|
|
|
|
|
{
|
|
|
|
|
Dl_info info;
|
|
|
|
|
if(_main) {
|
|
|
|
|
int err = dladdr(&_main, &info);
|
|
|
|
|
if (err != 0) {
|
|
|
|
|
char *p = pa_realpath(info.dli_fname);
|
|
|
|
|
if (p)
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
#if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
|
|
|
|
|
{
|
2006-05-17 20:09:57 +00:00
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
#ifndef TASK_COMM_LEN
|
|
|
|
|
/* Actually defined in linux/sched.h */
|
|
|
|
|
#define TASK_COMM_LEN 16
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
char tcomm[TASK_COMM_LEN+1];
|
|
|
|
|
memset(tcomm, 0, sizeof(tcomm));
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
/* This works on Linux only */
|
|
|
|
|
if (prctl(PR_GET_NAME, (unsigned long) tcomm, 0, 0, 0) == 0)
|
|
|
|
|
return pa_strlcpy(s, tcomm, l);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-07-14 00:17:31 +00:00
|
|
|
}
|
2006-05-17 20:09:57 +00:00
|
|
|
#endif
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2011-04-15 19:35:25 +02:00
|
|
|
#ifdef OS_IS_DARWIN
|
|
|
|
|
{
|
|
|
|
|
int mib[] = { CTL_KERN, KERN_PROCARGS, getpid(), 0 };
|
|
|
|
|
size_t len, nmib = (sizeof(mib) / sizeof(mib[0])) - 1;
|
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
|
|
sysctl(mib, nmib, NULL, &len, NULL, 0);
|
|
|
|
|
buf = (char *) pa_xmalloc(len);
|
|
|
|
|
|
|
|
|
|
if (sysctl(mib, nmib, buf, &len, NULL, 0) == 0) {
|
|
|
|
|
pa_strlcpy(s, basename(buf), l);
|
|
|
|
|
pa_xfree(buf);
|
|
|
|
|
return s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_xfree(buf);
|
|
|
|
|
|
|
|
|
|
/* fall thru */
|
|
|
|
|
}
|
|
|
|
|
#endif /* OS_IS_DARWIN */
|
|
|
|
|
|
2008-08-09 03:44:46 +02:00
|
|
|
errno = ENOENT;
|
2006-07-14 00:17:31 +00:00
|
|
|
return NULL;
|
2006-05-17 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
char *pa_path_get_filename(const char *p) {
|
2006-05-17 20:09:57 +00:00
|
|
|
char *fn;
|
|
|
|
|
|
2009-04-19 19:02:16 +02:00
|
|
|
if (!p)
|
|
|
|
|
return NULL;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
if ((fn = strrchr(p, PA_PATH_SEP_CHAR)))
|
2006-05-17 20:09:57 +00:00
|
|
|
return fn+1;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return (char*) p;
|
2006-05-17 20:09:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *pa_get_fqdn(char *s, size_t l) {
|
|
|
|
|
char hn[256];
|
2007-01-04 13:43:45 +00:00
|
|
|
#ifdef HAVE_GETADDRINFO
|
2006-05-17 20:09:57 +00:00
|
|
|
struct addrinfo *a, hints;
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(s);
|
|
|
|
|
pa_assert(l > 0);
|
|
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
if (!pa_get_host_name(hn, sizeof(hn)))
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_GETADDRINFO
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
|
hints.ai_flags = AI_CANONNAME;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-05-17 20:09:57 +00:00
|
|
|
if (getaddrinfo(hn, NULL, &hints, &a) < 0 || !a || !a->ai_canonname || !*a->ai_canonname)
|
|
|
|
|
return pa_strlcpy(s, hn, l);
|
|
|
|
|
|
|
|
|
|
pa_strlcpy(s, a->ai_canonname, l);
|
|
|
|
|
freeaddrinfo(a);
|
|
|
|
|
return s;
|
|
|
|
|
#else
|
|
|
|
|
return pa_strlcpy(s, hn, l);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pa_msleep(unsigned long t) {
|
|
|
|
|
#ifdef OS_IS_WIN32
|
|
|
|
|
Sleep(t);
|
|
|
|
|
return 0;
|
|
|
|
|
#elif defined(HAVE_NANOSLEEP)
|
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
2009-02-19 04:01:12 +01:00
|
|
|
ts.tv_sec = (time_t) (t / PA_MSEC_PER_SEC);
|
|
|
|
|
ts.tv_nsec = (long) ((t % PA_MSEC_PER_SEC) * PA_NSEC_PER_MSEC);
|
2006-05-17 20:09:57 +00:00
|
|
|
|
|
|
|
|
return nanosleep(&ts, NULL);
|
|
|
|
|
#else
|
|
|
|
|
#error "Platform lacks a sleep function."
|
|
|
|
|
#endif
|
|
|
|
|
}
|