2017-05-23 19:15:33 +02:00
|
|
|
/* PipeWire
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2016 Axis Communications <dev-gstreamer@axis.com>
|
|
|
|
|
* @author Linus Svensson <linus.svensson@axis.com>
|
|
|
|
|
* Copyright © 2018 Wim Taymans
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
|
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
#include <errno.h>
|
2016-08-29 18:29:07 +02:00
|
|
|
#include <string.h>
|
2017-07-12 18:04:00 +02:00
|
|
|
#include <stdio.h>
|
2018-10-02 09:17:07 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <limits.h>
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-07-11 15:57:20 +02:00
|
|
|
#include <pipewire/utils.h>
|
|
|
|
|
#include <pipewire/module.h>
|
2019-06-20 11:09:58 +02:00
|
|
|
#include <pipewire/private.h>
|
2016-08-29 18:29:07 +02:00
|
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** \cond */
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
static struct pw_command *parse_command_help(struct pw_properties *properties, const char *line, char **err);
|
|
|
|
|
static struct pw_command *parse_command_set_prop(struct pw_properties *properties, const char *line, char **err);
|
|
|
|
|
static struct pw_command *parse_command_add_spa_lib(struct pw_properties *properties, const char *line, char **err);
|
|
|
|
|
static struct pw_command *parse_command_module_load(struct pw_properties *properties, const char *line, char **err);
|
|
|
|
|
static struct pw_command *parse_command_exec(struct pw_properties *properties, const char *line, char **err);
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
struct impl {
|
2017-05-26 08:05:01 +02:00
|
|
|
struct pw_command this;
|
2017-05-23 19:15:33 +02:00
|
|
|
};
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
typedef struct pw_command *(*pw_command_parse_func_t) (struct pw_properties *properties, const char *line, char **err);
|
2017-09-15 14:54:52 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
struct command_parse {
|
|
|
|
|
const char *name;
|
2017-09-15 14:54:52 +02:00
|
|
|
const char *description;
|
2017-05-26 08:05:01 +02:00
|
|
|
pw_command_parse_func_t func;
|
2017-05-23 19:15:33 +02:00
|
|
|
};
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-23 19:15:33 +02:00
|
|
|
static const struct command_parse parsers[] = {
|
2017-09-15 14:54:52 +02:00
|
|
|
{"help", "Show this help", parse_command_help},
|
2019-06-20 15:19:28 +02:00
|
|
|
{"set-prop", "Set a property", parse_command_set_prop},
|
2019-05-31 15:03:56 +02:00
|
|
|
{"add-spa-lib", "Add a library that provides a spa factory name regex", parse_command_add_spa_lib},
|
2017-09-15 14:54:52 +02:00
|
|
|
{"load-module", "Load a module", parse_command_module_load},
|
2018-10-02 09:17:07 +02:00
|
|
|
{"exec", "Execute a program", parse_command_exec},
|
2017-09-15 14:54:52 +02:00
|
|
|
{NULL, NULL, NULL }
|
2016-08-29 18:29:07 +02:00
|
|
|
};
|
|
|
|
|
|
2016-11-24 17:00:42 +01:00
|
|
|
static const char whitespace[] = " \t";
|
2017-05-30 19:46:51 +02:00
|
|
|
/** \endcond */
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
static int
|
2017-09-15 14:54:52 +02:00
|
|
|
execute_command_help(struct pw_command *command, struct pw_core *core, char **err)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
fputs("Available commands:\n", stdout);
|
|
|
|
|
for (i = 0; parsers[i].name; i++)
|
|
|
|
|
fprintf(stdout, " %20.20s\t%s\n", parsers[i].name, parsers[i].description);
|
|
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
return 0;
|
2017-09-15 14:54:52 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
static struct pw_command *parse_command_help(struct pw_properties *properties, const char *line, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
struct impl *impl;
|
2017-09-15 14:54:52 +02:00
|
|
|
struct pw_command *this;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
goto no_mem;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-09-15 14:54:52 +02:00
|
|
|
this = &impl->this;
|
|
|
|
|
this->func = execute_command_help;
|
|
|
|
|
this->args = pw_split_strv(line, whitespace, 1, &this->n_args);
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-09-15 14:54:52 +02:00
|
|
|
return this;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
no_mem:
|
|
|
|
|
asprintf(err, "alloc failed: %m");
|
2017-05-26 08:05:01 +02:00
|
|
|
return NULL;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
static int
|
|
|
|
|
execute_command_set_prop(struct pw_command *command, struct pw_core *core, char **err)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct pw_command *parse_command_set_prop(struct pw_properties *properties, const char *line, char **err)
|
|
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct pw_command *this;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
goto error_alloc;
|
|
|
|
|
|
|
|
|
|
this = &impl->this;
|
|
|
|
|
this->func = execute_command_set_prop;
|
|
|
|
|
this->args = pw_split_strv(line, whitespace, 4, &this->n_args);
|
|
|
|
|
|
|
|
|
|
if (this->n_args < 3)
|
|
|
|
|
goto error_arguments;
|
|
|
|
|
|
|
|
|
|
pw_log_debug("set property: '%s' = '%s'", this->args[1], this->args[2]);
|
|
|
|
|
pw_properties_set(properties, this->args[1], this->args[2]);
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
|
|
error_arguments:
|
|
|
|
|
asprintf(err, "%s requires <property-name> <value>", this->args[0]);
|
|
|
|
|
pw_free_strv(this->args);
|
|
|
|
|
return NULL;
|
|
|
|
|
error_alloc:
|
|
|
|
|
asprintf(err, "alloc failed: %m");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-31 15:03:56 +02:00
|
|
|
static int
|
|
|
|
|
execute_command_add_spa_lib(struct pw_command *command, struct pw_core *core, char **err)
|
|
|
|
|
{
|
|
|
|
|
int res;
|
|
|
|
|
|
|
|
|
|
res = pw_core_add_spa_lib(core, command->args[1], command->args[2]);
|
|
|
|
|
if (res < 0) {
|
|
|
|
|
asprintf(err, "could not add spa library \"%s\"", command->args[1]);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
static struct pw_command *parse_command_add_spa_lib(struct pw_properties *properties, const char *line, char **err)
|
2019-05-31 15:03:56 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct pw_command *this;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
goto no_mem;
|
|
|
|
|
|
|
|
|
|
this = &impl->this;
|
|
|
|
|
this->func = execute_command_add_spa_lib;
|
|
|
|
|
this->args = pw_split_strv(line, whitespace, 4, &this->n_args);
|
|
|
|
|
|
|
|
|
|
if (this->n_args < 3)
|
|
|
|
|
goto no_library;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
no_library:
|
2019-05-31 15:03:56 +02:00
|
|
|
asprintf(err, "%s requires <factory-regex> <library-name>", this->args[0]);
|
|
|
|
|
pw_free_strv(this->args);
|
|
|
|
|
return NULL;
|
2019-06-20 11:04:34 +02:00
|
|
|
no_mem:
|
|
|
|
|
asprintf(err, "alloc failed: %m");
|
2019-05-31 15:03:56 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-18 11:38:30 +01:00
|
|
|
static int
|
2017-05-26 08:05:01 +02:00
|
|
|
execute_command_module_load(struct pw_command *command, struct pw_core *core, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-09-15 14:54:52 +02:00
|
|
|
struct pw_module *module;
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2018-01-23 15:25:48 +01:00
|
|
|
module = pw_module_load(core, command->args[1], command->args[2], NULL, NULL, NULL);
|
2017-09-15 14:54:52 +02:00
|
|
|
if (module == NULL) {
|
2019-07-08 18:23:22 +02:00
|
|
|
asprintf(err, "could not load module \"%s\": %m", command->args[1]);
|
|
|
|
|
return -errno;
|
2017-07-19 12:44:10 +02:00
|
|
|
}
|
2017-12-18 11:38:30 +01:00
|
|
|
return 0;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
static struct pw_command *parse_command_module_load(struct pw_properties *properties, const char *line, char **err)
|
2017-09-15 14:54:52 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct pw_command *this;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
goto no_mem;
|
|
|
|
|
|
|
|
|
|
this = &impl->this;
|
|
|
|
|
this->func = execute_command_module_load;
|
|
|
|
|
this->args = pw_split_strv(line, whitespace, 3, &this->n_args);
|
|
|
|
|
|
|
|
|
|
if (this->n_args < 2)
|
|
|
|
|
goto no_module;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
no_module:
|
2017-09-15 14:54:52 +02:00
|
|
|
asprintf(err, "%s requires a module name", this->args[0]);
|
|
|
|
|
pw_free_strv(this->args);
|
|
|
|
|
return NULL;
|
2019-06-20 11:04:34 +02:00
|
|
|
no_mem:
|
|
|
|
|
asprintf(err, "alloc failed: %m");
|
2017-09-15 14:54:52 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-02 09:17:07 +02:00
|
|
|
static int
|
|
|
|
|
execute_command_exec(struct pw_command *command, struct pw_core *core, char **err)
|
|
|
|
|
{
|
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
|
pw_log_info("exec %s", command->args[1]);
|
|
|
|
|
execv(command->args[1], command->args);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
pw_log_info("exec got pid %d", pid);
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-20 15:19:28 +02:00
|
|
|
static struct pw_command *parse_command_exec(struct pw_properties *properties, const char *line, char **err)
|
2018-10-02 09:17:07 +02:00
|
|
|
{
|
|
|
|
|
struct impl *impl;
|
|
|
|
|
struct pw_command *this;
|
|
|
|
|
|
|
|
|
|
impl = calloc(1, sizeof(struct impl));
|
|
|
|
|
if (impl == NULL)
|
|
|
|
|
goto no_mem;
|
|
|
|
|
|
|
|
|
|
this = &impl->this;
|
|
|
|
|
this->func = execute_command_exec;
|
|
|
|
|
this->args = pw_split_strv(line, whitespace, INT_MAX, &this->n_args);
|
|
|
|
|
|
|
|
|
|
if (this->n_args < 1)
|
|
|
|
|
goto no_executable;
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
2019-06-20 11:04:34 +02:00
|
|
|
no_executable:
|
2018-10-02 09:17:07 +02:00
|
|
|
asprintf(err, "requires an executable name");
|
|
|
|
|
pw_free_strv(this->args);
|
|
|
|
|
return NULL;
|
2019-06-20 11:04:34 +02:00
|
|
|
no_mem:
|
|
|
|
|
asprintf(err, "alloc failed: %m");
|
2018-10-02 09:17:07 +02:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Free command
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-05-30 19:46:51 +02:00
|
|
|
* \param command a command to free
|
|
|
|
|
*
|
|
|
|
|
* Free all resources assicated with \a command.
|
|
|
|
|
*
|
|
|
|
|
* \memberof pw_command
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2019-02-06 13:24:41 +01:00
|
|
|
SPA_EXPORT
|
2017-05-26 08:05:01 +02:00
|
|
|
void pw_command_free(struct pw_command *command)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
struct impl *impl = SPA_CONTAINER_OF(command, struct impl, this);
|
2016-08-29 18:29:07 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
spa_list_remove(&command->link);
|
2017-09-15 14:54:52 +02:00
|
|
|
pw_free_strv(command->args);
|
2017-05-26 08:05:01 +02:00
|
|
|
free(impl);
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Parses a command line
|
|
|
|
|
* \param line command line to parse
|
|
|
|
|
* \param[out] err Return location for an error
|
|
|
|
|
* \return The command or NULL when \a err is set.
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-05-30 19:46:51 +02:00
|
|
|
* Parses a command line, \a line, and return the parsed command.
|
|
|
|
|
* A command can later be executed with \ref pw_command_run()
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-05-30 19:46:51 +02:00
|
|
|
* \memberof pw_command
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2019-02-06 13:24:41 +01:00
|
|
|
SPA_EXPORT
|
2019-06-20 15:19:28 +02:00
|
|
|
struct pw_command *pw_command_parse(struct pw_properties *properties, const char *line, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-05-26 08:05:01 +02:00
|
|
|
struct pw_command *command = NULL;
|
|
|
|
|
const struct command_parse *parse;
|
|
|
|
|
char *name;
|
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
|
|
len = strcspn(line, whitespace);
|
|
|
|
|
|
|
|
|
|
name = strndup(line, len);
|
|
|
|
|
|
|
|
|
|
for (parse = parsers; parse->name != NULL; parse++) {
|
|
|
|
|
if (strcmp(name, parse->name) == 0) {
|
2019-06-20 15:19:28 +02:00
|
|
|
command = parse->func(properties, line, err);
|
2017-05-26 08:05:01 +02:00
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
asprintf(err, "Command \"%s\" does not exist", name);
|
2019-06-20 11:04:34 +02:00
|
|
|
out:
|
2017-05-26 08:05:01 +02:00
|
|
|
free(name);
|
|
|
|
|
return command;
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-30 19:46:51 +02:00
|
|
|
/** Run a command
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-12-18 11:38:30 +01:00
|
|
|
* \param command A \ref pw_command
|
|
|
|
|
* \param core A \ref pw_core
|
|
|
|
|
* \param err Return location for an error string, or NULL
|
|
|
|
|
* \return 0 on success, < 0 on error
|
2016-08-29 18:29:07 +02:00
|
|
|
*
|
2017-05-30 19:46:51 +02:00
|
|
|
* \memberof pw_command
|
2016-08-29 18:29:07 +02:00
|
|
|
*/
|
2019-02-06 13:24:41 +01:00
|
|
|
SPA_EXPORT
|
2017-12-18 11:38:30 +01:00
|
|
|
int pw_command_run(struct pw_command *command, struct pw_core *core, char **err)
|
2016-08-29 18:29:07 +02:00
|
|
|
{
|
2017-09-15 14:54:52 +02:00
|
|
|
return command->func(command, core, err);
|
2016-08-29 18:29:07 +02:00
|
|
|
}
|