-const char* pa_get_library_version(void) {
- return PACKAGE_VERSION;
-}
-
-static int is_running(void) {
- struct stat st;
-
- if (DEFAULT_SERVER[0] != '/')
- return 1;
-
- if (stat(DEFAULT_SERVER, &st) < 0)
- return 0;
-
- return 1;
-}
-
-int pa_context_connect_spawn(struct pa_context *c, void (*atfork)(void), void (*prefork)(void), void (*postfork)(void)) {
- pid_t pid;
- int status, r;
- int fds[2] = { -1, -1} ;
- struct pa_iochannel *io;
-
- if (getenv(ENV_DEFAULT_SERVER) || is_running())
- return pa_context_connect(c, NULL);
-
- if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
- pa_log(__FILE__": socketpair() failed: %s\n", strerror(errno));
- pa_context_fail(c, PA_ERROR_INTERNAL);
- goto fail;
- }
-
- if (prefork)
- prefork();
-
- if ((pid = fork()) < 0) {
- pa_log(__FILE__": fork() failed: %s\n", strerror(errno));
- pa_context_fail(c, PA_ERROR_INTERNAL);
-
- if (postfork)
- postfork();
-
- goto fail;
- } else if (!pid) {
- char t[64];
- char *p;
- /* Child */
-
- close(fds[0]);
-
- if (atfork)
- atfork();
-
- if (!(p = getenv(ENV_DEFAULT_BINARY)))
- p = POLYPAUDIO_BINARY;
-
- snprintf(t, sizeof(t), "-Lmodule-native-protocol-fd fd=%i", fds[1]);
- execl(p, p, "-r", "-D", "-lsyslog", "-X 5", t, NULL);
-
- exit(1);
- }
-
- /* Parent */
-
- r = waitpid(pid, &status, 0);
-
- if (postfork)
- postfork();
-
- if (r < 0) {
- pa_log(__FILE__": waitpid() failed: %s\n", strerror(errno));
- pa_context_fail(c, PA_ERROR_INTERNAL);
- goto fail;
- } else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
- pa_context_fail(c, PA_ERROR_CONNECTIONREFUSED);
- goto fail;
- }
-
- close(fds[1]);
-
- io = pa_iochannel_new(c->mainloop, fds[0], fds[0]);
- setup_context(c, io);
- return 0;
-
-fail:
- if (fds[0] != -1)
- close(fds[0]);
- if (fds[1] != -1)
- close(fds[1]);
-
- return -1;
-}
-