2016-03-17 08:27:30 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify 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.
|
|
|
|
*
|
|
|
|
* mpv 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 Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2020-04-08 23:05:51 +00:00
|
|
|
#include <limits.h>
|
2016-03-17 08:27:30 +00:00
|
|
|
#include <poll.h>
|
2016-09-15 11:13:23 +00:00
|
|
|
#include <signal.h>
|
2016-03-17 08:27:30 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include "osdep/io.h"
|
|
|
|
#include "osdep/threads.h"
|
|
|
|
|
|
|
|
#include "common/common.h"
|
|
|
|
#include "common/global.h"
|
|
|
|
#include "common/msg.h"
|
|
|
|
#include "input/input.h"
|
|
|
|
#include "libmpv/client.h"
|
2018-05-21 14:25:52 +00:00
|
|
|
#include "options/m_config.h"
|
2016-03-17 08:27:30 +00:00
|
|
|
#include "options/options.h"
|
|
|
|
#include "options/path.h"
|
|
|
|
#include "player/client.h"
|
|
|
|
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct mp_ipc_ctx {
|
|
|
|
struct mp_log *log;
|
|
|
|
struct mp_client_api *client_api;
|
|
|
|
const char *path;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread thread;
|
2016-03-17 08:27:30 +00:00
|
|
|
int death_pipe[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct client_arg {
|
|
|
|
struct mp_log *log;
|
|
|
|
struct mpv_handle *client;
|
|
|
|
|
2020-02-19 21:18:15 +00:00
|
|
|
const char *client_name;
|
2016-03-17 08:27:30 +00:00
|
|
|
int client_fd;
|
|
|
|
bool close_client_fd;
|
2020-05-14 19:40:43 +00:00
|
|
|
bool quit_on_close;
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
bool writable;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int ipc_write_str(struct client_arg *client, const char *buf)
|
|
|
|
{
|
|
|
|
size_t count = strlen(buf);
|
|
|
|
while (count > 0) {
|
|
|
|
ssize_t rc = send(client->client_fd, buf, count, MSG_NOSIGNAL);
|
|
|
|
if (rc <= 0) {
|
|
|
|
if (rc == 0)
|
|
|
|
return -1;
|
|
|
|
|
2018-10-31 04:34:21 +00:00
|
|
|
if (errno == EBADF || errno == ENOTSOCK) {
|
2016-03-17 08:27:30 +00:00
|
|
|
client->writable = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:35:51 +00:00
|
|
|
if (errno == EINTR || errno == EAGAIN)
|
2016-03-17 08:27:30 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
count -= rc;
|
|
|
|
buf += rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
static MP_THREAD_VOID client_thread(void *p)
|
2016-03-17 08:27:30 +00:00
|
|
|
{
|
2016-09-15 11:13:23 +00:00
|
|
|
// We don't use MSG_NOSIGNAL because the moldy fruit OS doesn't support it.
|
|
|
|
struct sigaction sa = { .sa_handler = SIG_IGN, .sa_flags = SA_RESTART };
|
|
|
|
sigfillset(&sa.sa_mask);
|
|
|
|
sigaction(SIGPIPE, &sa, NULL);
|
|
|
|
|
2016-03-17 08:27:30 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
struct client_arg *arg = p;
|
|
|
|
bstr client_msg = { talloc_strdup(NULL, ""), 0 };
|
|
|
|
|
2023-10-22 18:59:34 +00:00
|
|
|
char *tname = talloc_asprintf(NULL, "ipc/%s", arg->client_name);
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread_set_name(tname);
|
2023-10-22 18:59:34 +00:00
|
|
|
talloc_free(tname);
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
int pipe_fd = mpv_get_wakeup_pipe(arg->client);
|
|
|
|
if (pipe_fd < 0) {
|
|
|
|
MP_ERR(arg, "Could not get wakeup pipe\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
MP_VERBOSE(arg, "Client connected\n");
|
|
|
|
|
|
|
|
struct pollfd fds[2] = {
|
|
|
|
{.events = POLLIN, .fd = pipe_fd},
|
|
|
|
{.events = POLLIN, .fd = arg->client_fd},
|
|
|
|
};
|
|
|
|
|
|
|
|
fcntl(arg->client_fd, F_SETFL, fcntl(arg->client_fd, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rc = poll(fds, 2, 0);
|
2016-09-16 12:24:31 +00:00
|
|
|
if (rc == 0)
|
2016-03-17 08:27:30 +00:00
|
|
|
rc = poll(fds, 2, -1);
|
|
|
|
if (rc < 0) {
|
|
|
|
MP_ERR(arg, "Poll error\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[0].revents & POLLIN) {
|
2016-07-29 01:24:52 +00:00
|
|
|
mp_flush_wakeup_pipe(pipe_fd);
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
mpv_event *event = mpv_wait_event(arg->client, 0);
|
|
|
|
|
|
|
|
if (event->event_id == MPV_EVENT_NONE)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (event->event_id == MPV_EVENT_SHUTDOWN)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (!arg->writable)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
char *event_msg = mp_json_encode_event(event);
|
|
|
|
if (!event_msg) {
|
|
|
|
MP_ERR(arg, "Encoding error\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ipc_write_str(arg, event_msg);
|
|
|
|
talloc_free(event_msg);
|
|
|
|
if (rc < 0) {
|
|
|
|
MP_ERR(arg, "Write error (%s)\n", mp_strerror(errno));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 19:42:46 +00:00
|
|
|
if (fds[1].revents & (POLLIN | POLLHUP | POLLNVAL)) {
|
2016-03-17 08:27:30 +00:00
|
|
|
while (1) {
|
|
|
|
char buf[128];
|
|
|
|
bstr append = { buf, 0 };
|
|
|
|
|
|
|
|
ssize_t bytes = read(arg->client_fd, buf, sizeof(buf));
|
|
|
|
if (bytes < 0) {
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
break;
|
|
|
|
|
|
|
|
MP_ERR(arg, "Read error (%s)\n", mp_strerror(errno));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytes == 0) {
|
|
|
|
MP_VERBOSE(arg, "Client disconnected\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
append.len = bytes;
|
|
|
|
|
|
|
|
bstr_xappend(NULL, &client_msg, append);
|
|
|
|
|
|
|
|
while (bstrchr(client_msg, '\n') != -1) {
|
|
|
|
char *reply_msg = mp_ipc_consume_next_command(arg->client,
|
|
|
|
NULL, &client_msg);
|
|
|
|
|
|
|
|
if (reply_msg && arg->writable) {
|
|
|
|
rc = ipc_write_str(arg, reply_msg);
|
|
|
|
if (rc < 0) {
|
|
|
|
MP_ERR(arg, "Write error (%s)\n", mp_strerror(errno));
|
|
|
|
talloc_free(reply_msg);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
talloc_free(reply_msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (client_msg.len > 0)
|
|
|
|
MP_WARN(arg, "Ignoring unterminated command on disconnect.\n");
|
|
|
|
talloc_free(client_msg.start);
|
|
|
|
if (arg->close_client_fd)
|
|
|
|
close(arg->client_fd);
|
2020-05-14 19:40:43 +00:00
|
|
|
struct mpv_handle *h = arg->client;
|
|
|
|
bool quit = arg->quit_on_close;
|
2016-03-17 08:27:30 +00:00
|
|
|
talloc_free(arg);
|
2020-05-14 19:40:43 +00:00
|
|
|
if (quit) {
|
|
|
|
mpv_terminate_destroy(h);
|
|
|
|
} else {
|
|
|
|
mpv_destroy(h);
|
|
|
|
}
|
2023-10-21 02:55:41 +00:00
|
|
|
MP_THREAD_RETURN();
|
2016-03-17 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
2020-02-19 21:18:15 +00:00
|
|
|
static bool ipc_start_client(struct mp_ipc_ctx *ctx, struct client_arg *client,
|
|
|
|
bool free_on_init_fail)
|
2016-03-17 08:27:30 +00:00
|
|
|
{
|
2020-02-19 21:18:15 +00:00
|
|
|
if (!client->client)
|
|
|
|
client->client = mp_new_client(ctx->client_api, client->client_name);
|
2017-12-14 15:51:00 +00:00
|
|
|
if (!client->client)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
client->log = mp_client_get_log(client->client);
|
2016-03-17 08:27:30 +00:00
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread client_thr;
|
|
|
|
if (mp_thread_create(&client_thr, client_thread, client))
|
2017-12-14 15:51:00 +00:00
|
|
|
goto err;
|
2023-10-22 00:34:42 +00:00
|
|
|
mp_thread_detach(client_thr);
|
2017-12-14 15:51:00 +00:00
|
|
|
|
2020-02-19 21:18:15 +00:00
|
|
|
return true;
|
2017-12-14 15:51:00 +00:00
|
|
|
|
|
|
|
err:
|
2020-02-19 21:18:15 +00:00
|
|
|
if (free_on_init_fail) {
|
|
|
|
if (client->client)
|
|
|
|
mpv_destroy(client->client);
|
2017-12-14 15:51:00 +00:00
|
|
|
|
2020-02-19 21:18:15 +00:00
|
|
|
if (client->close_client_fd)
|
|
|
|
close(client->client_fd);
|
|
|
|
}
|
2017-12-14 15:51:00 +00:00
|
|
|
|
|
|
|
talloc_free(client);
|
2020-02-19 21:18:15 +00:00
|
|
|
return false;
|
2016-03-17 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ipc_start_client_json(struct mp_ipc_ctx *ctx, int id, int fd)
|
|
|
|
{
|
|
|
|
struct client_arg *client = talloc_ptrtype(NULL, client);
|
|
|
|
*client = (struct client_arg){
|
2020-04-08 23:05:51 +00:00
|
|
|
.client_name =
|
|
|
|
id >= 0 ? talloc_asprintf(client, "ipc-%d", id) : "ipc",
|
|
|
|
.client_fd = fd,
|
|
|
|
.close_client_fd = id >= 0,
|
2020-05-14 19:40:43 +00:00
|
|
|
.quit_on_close = id < 0,
|
2020-02-19 21:18:15 +00:00
|
|
|
.writable = true,
|
|
|
|
};
|
2016-03-17 08:27:30 +00:00
|
|
|
|
2020-02-19 21:18:15 +00:00
|
|
|
ipc_start_client(ctx, client, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_ipc_start_anon_client(struct mp_ipc_ctx *ctx, struct mpv_handle *h,
|
|
|
|
int out_fd[2])
|
|
|
|
{
|
|
|
|
int pair[2];
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair))
|
|
|
|
return false;
|
2020-05-15 14:18:01 +00:00
|
|
|
mp_set_cloexec(pair[0]);
|
|
|
|
mp_set_cloexec(pair[1]);
|
2020-02-19 21:18:15 +00:00
|
|
|
|
|
|
|
struct client_arg *client = talloc_ptrtype(NULL, client);
|
|
|
|
*client = (struct client_arg){
|
|
|
|
.client = h,
|
|
|
|
.client_name = mpv_client_name(h),
|
|
|
|
.client_fd = pair[1],
|
|
|
|
.close_client_fd = true,
|
2016-03-17 08:27:30 +00:00
|
|
|
.writable = true,
|
|
|
|
};
|
|
|
|
|
2020-02-19 21:18:15 +00:00
|
|
|
if (!ipc_start_client(ctx, client, false)) {
|
|
|
|
close(pair[0]);
|
|
|
|
close(pair[1]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_fd[0] = pair[0];
|
|
|
|
out_fd[1] = -1;
|
|
|
|
return true;
|
2016-03-17 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
static MP_THREAD_VOID ipc_thread(void *p)
|
2016-03-17 08:27:30 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
int ipc_fd;
|
|
|
|
struct sockaddr_un ipc_un = {0};
|
|
|
|
|
|
|
|
struct mp_ipc_ctx *arg = p;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread_set_name("ipc/socket");
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
MP_VERBOSE(arg, "Starting IPC master\n");
|
|
|
|
|
|
|
|
ipc_fd = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (ipc_fd < 0) {
|
|
|
|
MP_ERR(arg, "Could not create IPC socket\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
fchmod(ipc_fd, 0600);
|
|
|
|
|
|
|
|
size_t path_len = strlen(arg->path);
|
|
|
|
if (path_len >= sizeof(ipc_un.sun_path) - 1) {
|
|
|
|
MP_ERR(arg, "Could not create IPC socket\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_un.sun_family = AF_UNIX,
|
2018-10-31 01:03:22 +00:00
|
|
|
strncpy(ipc_un.sun_path, arg->path, sizeof(ipc_un.sun_path) - 1);
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
unlink(ipc_un.sun_path);
|
|
|
|
|
|
|
|
if (ipc_un.sun_path[0] == '@') {
|
|
|
|
ipc_un.sun_path[0] = '\0';
|
|
|
|
path_len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t addr_len = offsetof(struct sockaddr_un, sun_path) + 1 + path_len;
|
|
|
|
rc = bind(ipc_fd, (struct sockaddr *) &ipc_un, addr_len);
|
|
|
|
if (rc < 0) {
|
|
|
|
MP_ERR(arg, "Could not bind IPC socket\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = listen(ipc_fd, 10);
|
|
|
|
if (rc < 0) {
|
|
|
|
MP_ERR(arg, "Could not listen on IPC socket\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2016-09-29 14:47:19 +00:00
|
|
|
MP_VERBOSE(arg, "Listening to IPC socket.\n");
|
|
|
|
|
2016-03-17 08:27:30 +00:00
|
|
|
int client_num = 0;
|
|
|
|
|
|
|
|
struct pollfd fds[2] = {
|
|
|
|
{.events = POLLIN, .fd = arg->death_pipe[0]},
|
|
|
|
{.events = POLLIN, .fd = ipc_fd},
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
rc = poll(fds, 2, -1);
|
|
|
|
if (rc < 0) {
|
|
|
|
MP_ERR(arg, "Poll error\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fds[0].revents & POLLIN)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (fds[1].revents & POLLIN) {
|
|
|
|
int client_fd = accept(ipc_fd, NULL, NULL);
|
|
|
|
if (client_fd < 0) {
|
|
|
|
MP_ERR(arg, "Could not accept IPC client\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipc_start_client_json(arg, client_num++, client_fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (ipc_fd >= 0)
|
|
|
|
close(ipc_fd);
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
MP_THREAD_RETURN();
|
2016-03-17 08:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct mp_ipc_ctx *mp_init_ipc(struct mp_client_api *client_api,
|
|
|
|
struct mpv_global *global)
|
|
|
|
{
|
2019-11-28 23:16:52 +00:00
|
|
|
struct MPOpts *opts = mp_get_config_group(NULL, global, &mp_opt_root);
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
struct mp_ipc_ctx *arg = talloc_ptrtype(NULL, arg);
|
|
|
|
*arg = (struct mp_ipc_ctx){
|
|
|
|
.log = mp_log_new(arg, global->log, "ipc"),
|
|
|
|
.client_api = client_api,
|
|
|
|
.path = mp_get_user_path(arg, global, opts->ipc_path),
|
|
|
|
.death_pipe = {-1, -1},
|
|
|
|
};
|
|
|
|
|
2020-04-08 23:05:51 +00:00
|
|
|
if (opts->ipc_client && opts->ipc_client[0]) {
|
|
|
|
int fd = -1;
|
|
|
|
if (strncmp(opts->ipc_client, "fd://", 5) == 0) {
|
|
|
|
char *end;
|
|
|
|
unsigned long l = strtoul(opts->ipc_client + 5, &end, 0);
|
|
|
|
if (!end[0] && l <= INT_MAX)
|
|
|
|
fd = l;
|
|
|
|
}
|
|
|
|
if (fd < 0) {
|
|
|
|
MP_ERR(arg, "Invalid IPC client argument: '%s'\n", opts->ipc_client);
|
|
|
|
} else {
|
|
|
|
ipc_start_client_json(arg, -1, fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 14:25:52 +00:00
|
|
|
talloc_free(opts);
|
|
|
|
|
|
|
|
if (!arg->path || !arg->path[0])
|
2016-03-17 08:27:30 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (mp_make_wakeup_pipe(arg->death_pipe) < 0)
|
|
|
|
goto out;
|
|
|
|
|
2023-10-21 02:55:41 +00:00
|
|
|
if (mp_thread_create(&arg->thread, ipc_thread, arg))
|
2016-03-17 08:27:30 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
return arg;
|
|
|
|
|
|
|
|
out:
|
2017-06-27 12:22:28 +00:00
|
|
|
if (arg->death_pipe[0] >= 0) {
|
|
|
|
close(arg->death_pipe[0]);
|
|
|
|
close(arg->death_pipe[1]);
|
|
|
|
}
|
2016-03-17 08:27:30 +00:00
|
|
|
talloc_free(arg);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mp_uninit_ipc(struct mp_ipc_ctx *arg)
|
|
|
|
{
|
|
|
|
if (!arg)
|
|
|
|
return;
|
|
|
|
|
2016-06-07 11:39:43 +00:00
|
|
|
(void)write(arg->death_pipe[1], &(char){0}, 1);
|
2023-10-21 02:55:41 +00:00
|
|
|
mp_thread_join(arg->thread);
|
2016-03-17 08:27:30 +00:00
|
|
|
|
|
|
|
close(arg->death_pipe[0]);
|
|
|
|
close(arg->death_pipe[1]);
|
|
|
|
talloc_free(arg);
|
|
|
|
}
|