X-Git-Url: https://code.delx.au/pulseaudio/blobdiff_plain/7938442f77fafbc6c14d5072d14b773b289dc809..8534149fbe87c63a5af85f5610c0f62b45500d90:/src/pulsecore/core-util.c diff --git a/src/pulsecore/core-util.c b/src/pulsecore/core-util.c index 2b0a60a8..4e7d0d71 100644 --- a/src/pulsecore/core-util.c +++ b/src/pulsecore/core-util.c @@ -117,6 +117,7 @@ #include #include #include +#include #include "core-util.h" @@ -125,6 +126,9 @@ #define MSG_NOSIGNAL 0 #endif +#define NEWLINE "\r\n" +#define WHITESPACE "\n\r \t" + static pa_strlist *recorded_env = NULL; #ifdef OS_IS_WIN32 @@ -195,7 +199,7 @@ void pa_make_fd_cloexec(int fd) { /** Creates a directory securely */ int pa_make_secure_dir(const char* dir, mode_t m, uid_t uid, gid_t gid) { struct stat st; - int r, saved_errno; + int r, saved_errno, fd; pa_assert(dir); @@ -213,16 +217,45 @@ int pa_make_secure_dir(const char* dir, mode_t m, uid_t uid, gid_t gid) { if (r < 0 && errno != EEXIST) return -1; -#ifdef HAVE_CHOWN +#ifdef HAVE_FSTAT + if ((fd = open(dir, +#ifdef O_CLOEXEC + O_CLOEXEC| +#endif +#ifdef O_NOCTTY + O_NOCTTY| +#endif +#ifdef O_NOFOLLOW + O_NOFOLLOW| +#endif + O_RDONLY)) < 0) + goto fail; + + if (fstat(fd, &st) < 0) { + pa_assert_se(pa_close(fd) >= 0); + goto fail; + } + + if (!S_ISDIR(st.st_mode)) { + pa_assert_se(pa_close(fd) >= 0); + errno = EEXIST; + goto fail; + } + +#ifdef HAVE_FCHOWN if (uid == (uid_t)-1) uid = getuid(); if (gid == (gid_t)-1) gid = getgid(); - (void) chown(dir, uid, gid); + (void) fchown(fd, uid, gid); +#endif + +#ifdef HAVE_FCHMOD + (void) fchmod(fd, m); #endif -#ifdef HAVE_CHMOD - chmod(dir, m); + pa_assert_se(pa_close(fd) >= 0); + #endif #ifdef HAVE_LSTAT @@ -579,8 +612,8 @@ char *pa_strlcpy(char *b, const char *s, size_t l) { static int set_scheduler(int rtprio) { struct sched_param sp; - int r; #ifdef HAVE_DBUS + int r; DBusError error; DBusConnection *bus; @@ -627,7 +660,7 @@ static int set_scheduler(int rtprio) { errno = -r; #else - errno = r; + errno = 0; #endif return -1; @@ -647,7 +680,7 @@ int pa_make_realtime(int rtprio) { } for (p = rtprio-1; p >= 1; p--) - if (set_scheduler(p)) { + if (set_scheduler(p) >= 0) { pa_log_info("Successfully enabled SCHED_RR scheduling for thread, with priority %i, which is lower than the requested %i.", p, rtprio); return 0; } @@ -717,7 +750,7 @@ int pa_raise_priority(int nice_level) { } for (n = nice_level+1; n < 0; n++) - if (set_nice(n) > 0) { + if (set_nice(n) >= 0) { pa_log_info("Successfully acquired nice level %i, which is lower than the requested %i.", n, nice_level); return 0; } @@ -829,9 +862,6 @@ char *pa_split(const char *c, const char *delimiter, const char**state) { return pa_xstrndup(current, l); } -/* What is interpreted as whitespace? */ -#define WHITESPACE " \t\n" - /* Split a string into words. Otherwise similar to pa_split(). */ char *pa_split_spaces(const char *c, const char **state) { const char *current = *state ? *state : c; @@ -1188,7 +1218,27 @@ int pa_lock_fd(int fd, int b) { char* pa_strip_nl(char *s) { pa_assert(s); - s[strcspn(s, "\r\n")] = 0; + s[strcspn(s, NEWLINE)] = 0; + return s; +} + +char *pa_strip(char *s) { + char *e, *l = NULL; + + /* Drops trailing whitespace. Modifies the string in + * place. Returns pointer to first non-space character */ + + s += strspn(s, WHITESPACE); + + for (e = s; *e; e++) + if (!strchr(WHITESPACE, *e)) + l = e; + + if (l) + *(l+1) = 0; + else + *s = 0; + return s; } @@ -2944,6 +2994,7 @@ int pa_pipe_cloexec(int pipefd[2]) { if (errno != EINVAL && errno != ENOSYS) return r; + #endif if ((r = pipe(pipefd)) < 0) @@ -2965,6 +3016,7 @@ int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen) { if (errno != EINVAL && errno != ENOSYS) return fd; + #endif if ((fd = accept(sockfd, addr, addrlen)) < 0) @@ -3015,3 +3067,93 @@ void pa_nullify_stdfds(void) { #endif } + +char *pa_read_line_from_file(const char *fn) { + FILE *f; + char ln[256] = "", *r; + + if (!(f = pa_fopen_cloexec(fn, "r"))) + return NULL; + + r = fgets(ln, sizeof(ln)-1, f); + fclose(f); + + if (!r) { + errno = EIO; + return NULL; + } + + pa_strip_nl(ln); + return pa_xstrdup(ln); +} + +pa_bool_t pa_running_in_vm(void) { + +#if defined(__i386__) || defined(__x86_64__) + + /* Both CPUID and DMI are x86 specific interfaces... */ + + uint32_t eax = 0x40000000; + union { + uint32_t sig32[3]; + char text[13]; + } sig; + +#ifdef __linux__ + const char *const dmi_vendors[] = { + "/sys/class/dmi/id/sys_vendor", + "/sys/class/dmi/id/board_vendor", + "/sys/class/dmi/id/bios_vendor" + }; + + unsigned i; + + for (i = 0; i < PA_ELEMENTSOF(dmi_vendors); i++) { + char *s; + + if ((s = pa_read_line_from_file(dmi_vendors[i]))) { + + if (pa_startswith(s, "QEMU") || + /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */ + pa_startswith(s, "VMware") || + pa_startswith(s, "VMW") || + pa_startswith(s, "Microsoft Corporation") || + pa_startswith(s, "innotek GmbH") || + pa_startswith(s, "Xen")) { + + pa_xfree(s); + return TRUE; + } + + pa_xfree(s); + } + } + +#endif + + /* http://lwn.net/Articles/301888/ */ + pa_zero(sig); + + __asm__ __volatile__ ( + /* ebx/rbx is being used for PIC! */ + " push %%"PA_REG_b" \n\t" + " cpuid \n\t" + " mov %%ebx, %1 \n\t" + " pop %%"PA_REG_b" \n\t" + + : "=a" (eax), "=r" (sig.sig32[0]), "=c" (sig.sig32[1]), "=d" (sig.sig32[2]) + : "0" (eax) + ); + + if (pa_streq(sig.text, "XenVMMXenVMM") || + pa_streq(sig.text, "KVMKVMKVM") || + /* http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458 */ + pa_streq(sig.text, "VMwareVMware") || + /* http://msdn.microsoft.com/en-us/library/bb969719.aspx */ + pa_streq(sig.text, "Microsoft Hv")) + return TRUE; + +#endif + + return FALSE; +}