+
+int pa_open_cloexec(const char *fn, int flags, mode_t mode) {
+ int fd;
+
+#ifdef O_NOCTTY
+ flags |= O_NOCTTY;
+#endif
+
+#ifdef O_CLOEXEC
+ if ((fd = open(fn, flags|O_CLOEXEC, mode)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL)
+ return fd;
+#endif
+
+ if ((fd = open(fn, flags, mode)) < 0)
+ return fd;
+
+finish:
+ /* Some implementations might simply ignore O_CLOEXEC if it is not
+ * understood, make sure FD_CLOEXEC is enabled anyway */
+
+ pa_make_fd_cloexec(fd);
+ return fd;
+}
+
+int pa_socket_cloexec(int domain, int type, int protocol) {
+ int fd;
+
+#ifdef SOCK_CLOEXEC
+ if ((fd = socket(domain, type | SOCK_CLOEXEC, protocol)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL)
+ return fd;
+#endif
+
+ if ((fd = socket(domain, type, protocol)) < 0)
+ return fd;
+
+finish:
+ /* Some implementations might simply ignore SOCK_CLOEXEC if it is
+ * not understood, make sure FD_CLOEXEC is enabled anyway */
+
+ pa_make_fd_cloexec(fd);
+ return fd;
+}
+
+int pa_pipe_cloexec(int pipefd[2]) {
+ int r;
+
+#ifdef HAVE_PIPE2
+ if ((r = pipe2(pipefd, O_CLOEXEC)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL && errno != ENOSYS)
+ return r;
+
+#endif
+
+ if ((r = pipe(pipefd)) < 0)
+ return r;
+
+finish:
+ pa_make_fd_cloexec(pipefd[0]);
+ pa_make_fd_cloexec(pipefd[1]);
+
+ return 0;
+}
+
+int pa_accept_cloexec(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
+ int fd;
+
+#ifdef HAVE_ACCEPT4
+ if ((fd = accept4(sockfd, addr, addrlen, SOCK_CLOEXEC)) >= 0)
+ goto finish;
+
+ if (errno != EINVAL && errno != ENOSYS)
+ return fd;
+
+#endif
+
+ if ((fd = accept(sockfd, addr, addrlen)) < 0)
+ return fd;
+
+finish:
+ pa_make_fd_cloexec(fd);
+ return fd;
+}
+
+FILE* pa_fopen_cloexec(const char *path, const char *mode) {
+ FILE *f;
+ char *m;
+
+ m = pa_sprintf_malloc("%se", mode);
+
+ errno = 0;
+ if ((f = fopen(path, m))) {
+ pa_xfree(m);
+ goto finish;
+ }
+
+ pa_xfree(m);
+
+ if (errno != EINVAL)
+ return NULL;
+
+ if (!(f = fopen(path, mode)))
+ return NULL;
+
+finish:
+ pa_make_fd_cloexec(fileno(f));
+ return f;
+}
+
+void pa_nullify_stdfds(void) {
+
+#ifndef OS_IS_WIN32
+ pa_close(STDIN_FILENO);
+ pa_close(STDOUT_FILENO);
+ pa_close(STDERR_FILENO);
+
+ pa_assert_se(open("/dev/null", O_RDONLY) == STDIN_FILENO);
+ pa_assert_se(open("/dev/null", O_WRONLY) == STDOUT_FILENO);
+ pa_assert_se(open("/dev/null", O_WRONLY) == STDERR_FILENO);
+#else
+ FreeConsole();
+#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;
+}