pipewire/pipewire/modules/module-protocol-native.c

473 lines
12 KiB
C
Raw Normal View History

2017-05-23 19:15:33 +02:00
/* PipeWire
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/file.h>
#include "config.h"
2017-05-23 19:15:33 +02:00
#include "pipewire/client/pipewire.h"
#include "pipewire/client/log.h"
#include "pipewire/client/interfaces.h"
2017-05-23 19:15:33 +02:00
#include "pipewire/server/core.h"
#include "pipewire/server/protocol-native.h"
#include "pipewire/server/node.h"
#include "pipewire/server/module.h"
#include "pipewire/server/client-node.h"
#include "pipewire/server/client.h"
#include "pipewire/server/resource.h"
#include "pipewire/server/link.h"
#include "pipewire/server/node-factory.h"
#include "pipewire/server/data-loop.h"
#include "pipewire/server/main-loop.h"
#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX 108
#endif
#define LOCK_SUFFIX ".lock"
#define LOCK_SUFFIXLEN 5
2017-05-23 19:15:33 +02:00
typedef bool (*demarshal_func_t) (void *object, void *data, size_t size);
2017-05-23 19:15:33 +02:00
struct socket {
int fd;
int fd_lock;
struct sockaddr_un addr;
char lock_addr[UNIX_PATH_MAX + LOCK_SUFFIXLEN];
2017-05-23 19:15:33 +02:00
struct pw_loop *loop;
SpaSource *source;
char *core_name;
SpaList link;
2017-05-23 19:15:33 +02:00
};
2017-05-23 19:15:33 +02:00
struct impl {
struct pw_core *core;
SpaList link;
2017-05-23 19:15:33 +02:00
struct pw_properties *properties;
SpaList socket_list;
SpaList client_list;
2017-05-23 19:15:33 +02:00
struct pw_listener before_iterate;
};
2017-05-23 19:15:33 +02:00
struct native_client {
struct impl *impl;
SpaList link;
struct pw_client *client;
int fd;
SpaSource *source;
struct pw_connection *connection;
struct pw_listener resource_added;
};
2016-12-02 16:06:16 +01:00
static void
2017-05-23 19:15:33 +02:00
client_destroy (struct native_client *this)
2016-12-02 16:06:16 +01:00
{
2017-05-23 19:15:33 +02:00
pw_loop_destroy_source (this->impl->core->main_loop->loop,
this->source);
pw_client_destroy (this->client);
2016-12-02 16:06:16 +01:00
spa_list_remove (&this->link);
2017-05-23 19:15:33 +02:00
pw_connection_destroy (this->connection);
close (this->fd);
free (this);
2016-12-02 16:06:16 +01:00
}
static void
2017-05-23 19:15:33 +02:00
on_resource_added (struct pw_listener *listener,
struct pw_client *client,
struct pw_resource *resource)
{
2017-05-23 19:15:33 +02:00
pw_protocol_native_server_setup (resource);
}
static void
2017-05-23 19:15:33 +02:00
on_before_iterate (struct pw_listener *listener,
struct pw_loop *loop)
{
2017-05-23 19:15:33 +02:00
struct impl *this = SPA_CONTAINER_OF (listener, struct impl, before_iterate);
struct native_client *client, *tmp;
spa_list_for_each_safe (client, tmp, &this->client_list, link)
2017-05-23 19:15:33 +02:00
pw_connection_flush (client->connection);
}
static void
connection_data (SpaLoopUtils *utils,
SpaSource *source,
int fd,
SpaIO mask,
void *data)
{
2017-05-23 19:15:33 +02:00
struct native_client *client = data;
struct pw_connection *conn = client->connection;
uint8_t opcode;
uint32_t id;
uint32_t size;
2017-05-23 19:15:33 +02:00
struct pw_client *c = client->client;
void *message;
if (mask & (SPA_IO_ERR | SPA_IO_HUP)) {
2017-05-23 19:15:33 +02:00
pw_log_error ("protocol-native %p: got connection error", client->impl);
2016-12-02 13:38:43 +01:00
client_destroy (client);
return;
}
if (mask & SPA_IO_IN) {
2017-05-23 19:15:33 +02:00
while (pw_connection_get_next (conn, &opcode, &id, &message, &size)) {
struct pw_resource *resource;
const demarshal_func_t *demarshal;
2017-05-23 19:15:33 +02:00
pw_log_trace ("protocol-native %p: got message %d from %u", client->impl, opcode, id);
2017-05-23 19:15:33 +02:00
resource = pw_map_lookup (&c->objects, id);
if (resource == NULL) {
2017-05-23 19:15:33 +02:00
pw_log_error ("protocol-native %p: unknown resource %u", client->impl, id);
continue;
}
if (opcode >= resource->iface->n_methods) {
2017-05-23 19:15:33 +02:00
pw_log_error ("protocol-native %p: invalid method %u", client->impl, opcode);
2017-03-23 16:08:50 +01:00
client_destroy (client);
break;
}
demarshal = resource->iface->methods;
2017-03-23 16:08:50 +01:00
if (!demarshal[opcode] || !demarshal[opcode] (resource, message, size)) {
2017-05-23 19:15:33 +02:00
pw_log_error ("protocol-native %p: invalid message received", client->impl);
2017-03-23 16:08:50 +01:00
client_destroy (client);
break;
}
}
}
}
2017-05-23 19:15:33 +02:00
static struct native_client *
client_new (struct impl *impl,
int fd)
{
2017-05-23 19:15:33 +02:00
struct native_client *this;
struct pw_client *client;
socklen_t len;
struct ucred ucred, *ucredp;
2017-05-23 19:15:33 +02:00
this = calloc (1, sizeof (struct native_client));
if (this == NULL)
goto no_native_client;
2016-12-02 13:38:43 +01:00
this->impl = impl;
this->fd = fd;
2017-05-23 19:15:33 +02:00
this->source = pw_loop_add_io (impl->core->main_loop->loop,
this->fd,
SPA_IO_ERR | SPA_IO_HUP,
false,
connection_data,
this);
if (this->source == NULL)
goto no_source;
2017-05-23 19:15:33 +02:00
this->connection = pw_connection_new (fd);
if (this->connection == NULL)
goto no_connection;
len = sizeof (ucred);
if (getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
2017-05-23 19:15:33 +02:00
pw_log_error ("no peercred: %m");
ucredp = NULL;
} else {
ucredp = &ucred;
}
2017-05-23 19:15:33 +02:00
client = pw_client_new (impl->core, ucredp, NULL);
if (client == NULL)
goto no_client;
client->protocol_private = this->connection;
this->client = client;
spa_list_insert (impl->client_list.prev, &this->link);
2017-05-23 19:15:33 +02:00
pw_signal_add (&client->resource_added,
&this->resource_added,
on_resource_added);
2017-05-23 19:15:33 +02:00
pw_global_bind (impl->core->global,
client,
0,
0);
return this;
no_client:
2017-05-23 19:15:33 +02:00
pw_connection_destroy (this->connection);
no_connection:
2017-05-23 19:15:33 +02:00
pw_loop_destroy_source (impl->core->main_loop->loop,
this->source);
no_source:
free (this);
no_native_client:
return NULL;
}
2017-05-23 19:15:33 +02:00
static struct socket *
create_socket (void)
{
2017-05-23 19:15:33 +02:00
struct socket *s;
2017-05-23 19:15:33 +02:00
if ((s = calloc(1, sizeof (struct socket))) == NULL)
return NULL;
s->fd = -1;
s->fd_lock = -1;
return s;
}
static void
2017-05-23 19:15:33 +02:00
destroy_socket (struct socket *s)
{
if (s->source)
2017-05-23 19:15:33 +02:00
pw_loop_destroy_source (s->loop, s->source);
if (s->addr.sun_path[0])
unlink (s->addr.sun_path);
if (s->fd >= 0)
close (s->fd);
if (s->lock_addr[0])
unlink (s->lock_addr);
if (s->fd_lock >= 0)
close (s->fd_lock);
free (s);
}
static bool
2017-05-23 19:15:33 +02:00
init_socket_name (struct socket *s, const char *name)
{
int name_size;
const char *runtime_dir;
if ((runtime_dir = getenv ("XDG_RUNTIME_DIR")) == NULL) {
2017-05-23 19:15:33 +02:00
pw_log_error ("XDG_RUNTIME_DIR not set in the environment");
return false;
}
s->addr.sun_family = AF_LOCAL;
name_size = snprintf (s->addr.sun_path, sizeof (s->addr.sun_path),
"%s/%s", runtime_dir, name) + 1;
s->core_name = (s->addr.sun_path + name_size - 1) - strlen (name);
if (name_size > (int)sizeof (s->addr.sun_path)) {
2017-05-23 19:15:33 +02:00
pw_log_error ("socket path \"%s/%s\" plus null terminator exceeds 108 bytes",
runtime_dir, name);
*s->addr.sun_path = 0;
return false;
}
return true;
}
static bool
2017-05-23 19:15:33 +02:00
lock_socket (struct socket *s)
{
struct stat socket_stat;
snprintf (s->lock_addr, sizeof (s->lock_addr),
"%s%s", s->addr.sun_path, LOCK_SUFFIX);
s->fd_lock = open (s->lock_addr, O_CREAT | O_CLOEXEC,
(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP));
if (s->fd_lock < 0) {
2017-05-23 19:15:33 +02:00
pw_log_error ("unable to open lockfile %s check permissions", s->lock_addr);
goto err;
}
if (flock (s->fd_lock, LOCK_EX | LOCK_NB) < 0) {
2017-05-23 19:15:33 +02:00
pw_log_error ("unable to lock lockfile %s, maybe another daemon is running", s->lock_addr);
goto err_fd;
}
if (stat (s->addr.sun_path, &socket_stat) < 0 ) {
if (errno != ENOENT) {
2017-05-23 19:15:33 +02:00
pw_log_error ("did not manage to stat file %s\n", s->addr.sun_path);
goto err_fd;
}
} else if (socket_stat.st_mode & S_IWUSR ||
socket_stat.st_mode & S_IWGRP) {
unlink (s->addr.sun_path);
}
return true;
err_fd:
close (s->fd_lock);
s->fd_lock = -1;
err:
*s->lock_addr = 0;
*s->addr.sun_path = 0;
return false;
}
static void
socket_data (SpaLoopUtils *utils,
SpaSource *source,
int fd,
SpaIO mask,
void *data)
{
2017-05-23 19:15:33 +02:00
struct impl *impl = data;
struct native_client *client;
struct sockaddr_un name;
socklen_t length;
int client_fd;
length = sizeof (name);
client_fd = accept4 (fd, (struct sockaddr *) &name, &length, SOCK_CLOEXEC);
if (client_fd < 0) {
2017-05-23 19:15:33 +02:00
pw_log_error ("failed to accept: %m");
return;
}
client = client_new (impl, client_fd);
if (client == NULL) {
2017-05-23 19:15:33 +02:00
pw_log_error ("failed to create client");
close (client_fd);
return;
}
2017-05-23 19:15:33 +02:00
pw_loop_update_io (impl->core->main_loop->loop,
client->source,
SPA_IO_IN | SPA_IO_ERR | SPA_IO_HUP);
}
static bool
2017-05-23 19:15:33 +02:00
add_socket (struct impl *impl, struct socket *s)
{
socklen_t size;
if ((s->fd = socket (PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0)) < 0)
return false;
size = offsetof (struct sockaddr_un, sun_path) + strlen (s->addr.sun_path);
if (bind (s->fd, (struct sockaddr *) &s->addr, size) < 0) {
2017-05-23 19:15:33 +02:00
pw_log_error ("bind() failed with error: %m");
return false;
}
if (listen (s->fd, 128) < 0) {
2017-05-23 19:15:33 +02:00
pw_log_error ("listen() failed with error: %m");
return false;
}
s->loop = impl->core->main_loop->loop;
2017-05-23 19:15:33 +02:00
s->source = pw_loop_add_io (s->loop,
s->fd,
SPA_IO_IN,
false,
socket_data,
impl);
if (s->source == NULL)
return false;
spa_list_insert (impl->socket_list.prev, &s->link);
return true;
}
2017-05-23 19:15:33 +02:00
static struct impl *
pw_protocol_native_new (struct pw_core *core,
struct pw_properties *properties)
{
2017-05-23 19:15:33 +02:00
struct impl *impl;
struct socket *s;
const char *name;
2017-05-23 19:15:33 +02:00
impl = calloc (1, sizeof (struct impl));
pw_log_debug ("protocol-native %p: new", impl);
impl->core = core;
impl->properties = properties;
name = NULL;
if (impl->properties)
2017-05-23 19:15:33 +02:00
name = pw_properties_get (impl->properties, "pipewire.core.name");
if (name == NULL)
2017-05-23 19:15:33 +02:00
name = getenv ("PIPEWIRE_CORE");
if (name == NULL)
2017-05-23 19:15:33 +02:00
name = "pipewire-0";
s = create_socket ();
spa_list_init (&impl->socket_list);
spa_list_init (&impl->client_list);
if (!init_socket_name (s, name))
goto error;
if (!lock_socket (s))
goto error;
if (!add_socket (impl, s))
goto error;
2017-05-23 19:15:33 +02:00
pw_signal_add (&impl->core->main_loop->loop->before_iterate,
&impl->before_iterate,
on_before_iterate);
return impl;
error:
destroy_socket (s);
free (impl);
return NULL;
}
#if 0
static void
2017-05-23 19:15:33 +02:00
pw_protocol_native_destroy (struct impl *impl)
{
2017-05-23 19:15:33 +02:00
struct impl *object, *tmp;
2017-05-23 19:15:33 +02:00
pw_log_debug ("protocol-native %p: destroy", impl);
2017-05-23 19:15:33 +02:00
pw_signal_remove (&impl->before_iterate);
2017-05-23 19:15:33 +02:00
pw_global_destroy (impl->global);
spa_list_for_each_safe (object, tmp, &impl->object_list, link)
object_destroy (object);
free (impl);
}
#endif
bool
2017-05-23 19:15:33 +02:00
pipewire__module_init (struct pw_module * module, const char * args)
{
2017-05-23 19:15:33 +02:00
pw_protocol_native_new (module->core, NULL);
return true;
}