treewide: add some examples for the spa_auto* macros

This commit is contained in:
Barnabás Pőcze 2023-07-03 20:53:08 +02:00
parent 65d949558b
commit fe45786a5d
16 changed files with 136 additions and 236 deletions

View file

@ -22,6 +22,7 @@
#include <glib.h>
#endif
#include <spa/utils/cleanup.h>
#include <spa/utils/result.h>
#include <pipewire/log.h>
@ -62,8 +63,8 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices)
{
#if defined(__linux__)
char root_path[2048];
int root_fd, info_fd, res;
struct stat stat_buf;
int res;
if (app_id)
*app_id = NULL;
@ -71,8 +72,9 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices)
*devices = NULL;
snprintf(root_path, sizeof(root_path), "/proc/%d/root", (int)pid);
root_fd = openat (AT_FDCWD, root_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY);
if (root_fd == -1) {
spa_autoclose int root_fd = openat(AT_FDCWD, root_path, O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_CLOEXEC | O_NOCTTY);
if (root_fd < 0) {
res = -errno;
if (res == -EACCES) {
struct statfs buf;
@ -90,9 +92,9 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices)
pw_log_info("failed to open \"%s\": %s", root_path, spa_strerror(res));
return res;
}
info_fd = openat (root_fd, ".flatpak-info", O_RDONLY | O_CLOEXEC | O_NOCTTY);
close (root_fd);
if (info_fd == -1) {
spa_autoclose int info_fd = openat(root_fd, ".flatpak-info", O_RDONLY | O_CLOEXEC | O_NOCTTY);
if (info_fd < 0) {
if (errno == ENOENT) {
pw_log_debug("no .flatpak-info, client on the host");
/* No file => on the host */
@ -128,7 +130,7 @@ static int pw_check_flatpak(pid_t pid, char **app_id, char **devices)
pw_log_error("PID %d .flatpak-info parsing failed: %s",
(int)pid, spa_strerror(res));
}
close(info_fd);
return 1;
#else
return 0;

View file

@ -4,6 +4,7 @@
#include <spa/utils/json.h>
#include <pipewire/cleanup.h>
#include <pipewire/utils.h>
#include "module.h"
@ -68,15 +69,14 @@ static int parse_cmd(void *user_data, const char *location,
{
struct impl *impl = user_data;
struct spa_json it[3];
char key[512], *s;
char key[512];
int res = 0;
s = strndup(str, len);
spa_autofree char *s = strndup(str, len);
spa_json_init(&it[0], s, len);
if (spa_json_enter_array(&it[0], &it[1]) < 0) {
pw_log_error("config file error: pulse.cmd is not an array");
res = -EINVAL;
goto exit;
return -EINVAL;
}
while (spa_json_enter_object(&it[1], &it[2]) > 0) {
@ -107,8 +107,7 @@ static int parse_cmd(void *user_data, const char *location,
if (res < 0)
break;
}
exit:
free(s);
return res;
}

View file

@ -11,6 +11,7 @@
#include <spa/utils/list.h>
#include <spa/utils/hook.h>
#include <spa/utils/string.h>
#include <pipewire/cleanup.h>
#include <pipewire/log.h>
#include <pipewire/map.h>
#include <pipewire/properties.h>
@ -117,7 +118,8 @@ int module_unload(struct module *module)
/** utils */
void module_args_add_props(struct pw_properties *props, const char *str)
{
char *s = strdup(str), *p = s, *e, f;
spa_autofree char *s = strdup(str);
char *p = s, *e, f;
const char *k, *v;
const struct str_map *map;
@ -160,7 +162,6 @@ void module_args_add_props(struct pw_properties *props, const char *str)
}
pw_properties_set(props, k, v);
}
free(s);
}
int module_args_to_audioinfo_keys(struct impl *impl, struct pw_properties *props,

View file

@ -28,6 +28,7 @@
#include <spa/utils/ringbuffer.h>
#include <spa/utils/json.h>
#include <pipewire/cleanup.h>
#include <pipewire/pipewire.h>
#include <pipewire/extensions/metadata.h>
@ -2516,12 +2517,12 @@ static int do_play_sample(struct client *client, uint32_t command, uint32_t tag,
uint32_t sink_index, volume;
struct sample *sample;
const char *sink_name, *name;
struct pw_properties *props = NULL;
spa_autoptr(pw_properties) props = NULL;
struct pw_manager_object *o;
int res;
if ((props = pw_properties_new(NULL, NULL)) == NULL)
goto error_errno;
return -errno;
if ((res = message_get(m,
TAG_U32, &sink_index,
@ -2529,13 +2530,13 @@ static int do_play_sample(struct client *client, uint32_t command, uint32_t tag,
TAG_U32, &volume,
TAG_STRING, &name,
TAG_INVALID)) < 0)
goto error_proto;
return -EPROTO;
if (client->version >= 13) {
if ((res = message_get(m,
TAG_PROPLIST, props,
TAG_INVALID)) < 0)
goto error_proto;
return -EPROTO;
}
pw_log_info("[%s] %s tag:%u sink_index:%u sink_name:%s name:%s",
@ -2545,35 +2546,19 @@ static int do_play_sample(struct client *client, uint32_t command, uint32_t tag,
pw_properties_update(props, &client->props->dict);
if (sink_index != SPA_ID_INVALID && sink_name != NULL)
goto error_inval;
return -EINVAL;
o = find_device(client, sink_index, sink_name, PW_DIRECTION_OUTPUT, NULL);
if (o == NULL)
goto error_noent;
return -ENOENT;
sample = find_sample(impl, SPA_ID_INVALID, name);
if (sample == NULL)
goto error_noent;
return -ENOENT;
pw_properties_setf(props, PW_KEY_TARGET_OBJECT, "%"PRIu64, o->serial);
return pending_sample_new(client, sample, props, tag);
error_errno:
res = -errno;
goto error;
error_proto:
res = -EPROTO;
goto error;
error_inval:
res = -EINVAL;
goto error;
error_noent:
res = -ENOENT;
goto error;
error:
pw_properties_free(props);
return res;
return pending_sample_new(client, sample, spa_steal_ptr(props), tag);
}
static int do_remove_sample(struct client *client, uint32_t command, uint32_t tag, struct message *m)
@ -3184,11 +3169,8 @@ static int do_set_stream_name(struct client *client, uint32_t command, uint32_t
static int do_update_proplist(struct client *client, uint32_t command, uint32_t tag, struct message *m)
{
uint32_t channel, mode;
struct stream *stream;
struct pw_properties *props;
int res;
props = pw_properties_new(NULL, NULL);
spa_autoptr(pw_properties) props = pw_properties_new(NULL, NULL);
if (props == NULL)
return -errno;
@ -3196,7 +3178,7 @@ static int do_update_proplist(struct client *client, uint32_t command, uint32_t
if (message_get(m,
TAG_U32, &channel,
TAG_INVALID) < 0)
goto error_protocol;
return -EPROTO;
} else {
channel = SPA_ID_INVALID;
}
@ -3208,12 +3190,12 @@ static int do_update_proplist(struct client *client, uint32_t command, uint32_t
TAG_U32, &mode,
TAG_PROPLIST, props,
TAG_INVALID) < 0)
goto error_protocol;
return -EPROTO;
if (command != COMMAND_UPDATE_CLIENT_PROPLIST) {
stream = pw_map_lookup(&client->streams, channel);
struct stream *stream = pw_map_lookup(&client->streams, channel);
if (stream == NULL || stream->type == STREAM_TYPE_UPLOAD)
goto error_noentity;
return -ENOENT;
pw_stream_update_properties(stream->stream, &props->dict);
} else {
@ -3223,29 +3205,17 @@ static int do_update_proplist(struct client *client, uint32_t command, uint32_t
pw_core_update_properties(client->core, &client->props->dict);
}
}
res = reply_simple_ack(client, tag);
exit:
pw_properties_free(props);
return res;
error_protocol:
res = -EPROTO;
goto exit;
error_noentity:
res = -ENOENT;
goto exit;
return reply_simple_ack(client, tag);
}
static int do_remove_proplist(struct client *client, uint32_t command, uint32_t tag, struct message *m)
{
uint32_t i, channel;
struct stream *stream;
struct pw_properties *props;
struct spa_dict dict;
struct spa_dict_item *items;
int res;
props = pw_properties_new(NULL, NULL);
spa_autoptr(pw_properties) props = pw_properties_new(NULL, NULL);
if (props == NULL)
return -errno;
@ -3253,7 +3223,7 @@ static int do_remove_proplist(struct client *client, uint32_t command, uint32_t
if (message_get(m,
TAG_U32, &channel,
TAG_INVALID) < 0)
goto error_protocol;
return -EPROTO;
} else {
channel = SPA_ID_INVALID;
}
@ -3267,7 +3237,7 @@ static int do_remove_proplist(struct client *client, uint32_t command, uint32_t
if (message_get(m,
TAG_STRING, &key,
TAG_INVALID) < 0)
goto error_protocol;
return -EPROTO;
if (key == NULL)
break;
pw_properties_set(props, key, key);
@ -3281,25 +3251,16 @@ static int do_remove_proplist(struct client *client, uint32_t command, uint32_t
}
if (command != COMMAND_UPDATE_CLIENT_PROPLIST) {
stream = pw_map_lookup(&client->streams, channel);
struct stream *stream = pw_map_lookup(&client->streams, channel);
if (stream == NULL || stream->type == STREAM_TYPE_UPLOAD)
goto error_noentity;
return -ENOENT;
pw_stream_update_properties(stream->stream, &dict);
} else {
pw_core_update_properties(client->core, &dict);
}
res = reply_simple_ack(client, tag);
exit:
pw_properties_free(props);
return res;
error_protocol:
res = -EPROTO;
goto exit;
error_noentity:
res = -ENOENT;
goto exit;
return reply_simple_ack(client, tag);
}
@ -3672,7 +3633,7 @@ static int fill_sink_info_proplist(struct message *m, const struct spa_dict *sin
const struct pw_manager_object *card)
{
struct pw_device_info *card_info = card ? card->info : NULL;
struct pw_properties *props = NULL;
spa_autoptr(pw_properties) props = NULL;
if (card_info && card_info->props) {
props = pw_properties_new_dict(sink_props);
@ -3682,9 +3643,8 @@ static int fill_sink_info_proplist(struct message *m, const struct spa_dict *sin
pw_properties_add(props, card_info->props);
sink_props = &props->dict;
}
message_put(m, TAG_PROPLIST, sink_props, TAG_INVALID);
pw_properties_free(props);
message_put(m, TAG_PROPLIST, sink_props, TAG_INVALID);
return 0;
}
@ -3882,7 +3842,7 @@ static int fill_source_info_proplist(struct message *m, const struct spa_dict *s
const struct pw_manager_object *card, const bool is_monitor)
{
struct pw_device_info *card_info = card ? card->info : NULL;
struct pw_properties *props = NULL;
spa_autoptr(pw_properties) props = NULL;
if ((card_info && card_info->props) || is_monitor) {
props = pw_properties_new_dict(source_props);
@ -3897,9 +3857,8 @@ static int fill_source_info_proplist(struct message *m, const struct spa_dict *s
source_props = &props->dict;
}
message_put(m, TAG_PROPLIST, source_props, TAG_INVALID);
pw_properties_free(props);
message_put(m, TAG_PROPLIST, source_props, TAG_INVALID);
return 0;
}
@ -5140,12 +5099,9 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3
const char *object_path = NULL;
const char *message = NULL;
const char *params = NULL;
char *response = NULL;
char *path = NULL;
struct message *reply;
struct pw_manager_object *o;
int len = 0;
int res;
if (message_get(m,
TAG_STRING, &object_path,
@ -5164,11 +5120,13 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3
len = strlen(object_path);
if (len > 0 && object_path[len - 1] == '/')
--len;
path = strndup(object_path, len);
spa_autofree char *path = strndup(object_path, len);
if (path == NULL)
return -ENOMEM;
res = -ENOENT;
spa_autofree char *response = NULL;
int res = -ENOENT;
spa_list_for_each(o, &manager->object_list, link) {
if (o->message_object_path && spa_streq(o->message_object_path, path)) {
@ -5180,7 +5138,6 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3
}
}
free(path);
if (res < 0)
return res;
@ -5188,7 +5145,7 @@ static int do_send_object_message(struct client *client, uint32_t command, uint3
reply = reply_new(client, tag);
message_put(reply, TAG_STRING, response, TAG_INVALID);
free(response);
return client_queue_message(client, reply);
}

View file

@ -28,6 +28,7 @@
#include <spa/utils/defs.h>
#include <spa/utils/json.h>
#include <spa/utils/result.h>
#include <pipewire/cleanup.h>
#include <pipewire/pipewire.h>
#include "client.h"
@ -404,7 +405,7 @@ on_connect(void *data, int fd, uint32_t mask)
client_access = server->client_access;
if (server->addr.ss_family == AF_UNIX) {
char *app_id = NULL, *devices = NULL;
spa_autofree char *app_id = NULL, *devices = NULL;
#ifdef SO_PRIORITY
val = 6;
@ -443,8 +444,6 @@ on_connect(void *data, int fd, uint32_t mask)
else
pw_properties_set(client->props, PW_KEY_MEDIA_CATEGORY, NULL);
}
free(devices);
free(app_id);
}
else if (server->addr.ss_family == AF_INET || server->addr.ss_family == AF_INET6) {

View file

@ -39,6 +39,7 @@
#include <spa/param/audio/raw.h>
#include <spa/param/latency-utils.h>
#include <pipewire/cleanup.h>
#include <pipewire/impl.h>
#include <pipewire/i18n.h>
@ -1211,7 +1212,7 @@ static int rtsp_do_announce(struct impl *impl)
char key[512*2];
char iv[16*2];
int res, frames, rsa_len, ip_version;
char *sdp;
spa_autofree char *sdp = NULL;
char local_ip[256];
host = pw_properties_get(impl->props, "raop.ip");
@ -1289,10 +1290,8 @@ static int rtsp_do_announce(struct impl *impl)
default:
return -ENOTSUP;
}
res = rtsp_send(impl, "ANNOUNCE", "application/sdp", sdp, rtsp_announce_reply);
free(sdp);
return res;
return rtsp_send(impl, "ANNOUNCE", "application/sdp", sdp, rtsp_announce_reply);
}
static int rtsp_auth_setup_reply(void *data, int status, const struct spa_dict *headers)
@ -1355,7 +1354,6 @@ static const char *find_attr(char **tokens, const char *key)
static int rtsp_do_auth(struct impl *impl, const struct spa_dict *headers)
{
const char *str, *realm, *nonce;
char **tokens;
int n_tokens;
if ((str = spa_dict_lookup(headers, "WWW-Authenticate")) == NULL)
@ -1368,9 +1366,9 @@ static int rtsp_do_auth(struct impl *impl, const struct spa_dict *headers)
pw_log_info("Auth: %s", str);
tokens = pw_split_strv(str, " ", INT_MAX, &n_tokens);
spa_auto(pw_strv) tokens = pw_split_strv(str, " ", INT_MAX, &n_tokens);
if (tokens == NULL || tokens[0] == NULL)
goto error;
return -EINVAL;
impl->auth_method = strdup(tokens[0]);
@ -1378,20 +1376,13 @@ static int rtsp_do_auth(struct impl *impl, const struct spa_dict *headers)
realm = find_attr(tokens, "realm");
nonce = find_attr(tokens, "nonce");
if (realm == NULL || nonce == NULL)
goto error;
return -EINVAL;
impl->realm = strdup(realm);
impl->nonce = strdup(nonce);
}
pw_free_strv(tokens);
rtsp_send(impl, "OPTIONS", NULL, NULL, rtsp_auth_reply);
return 0;
error:
pw_free_strv(tokens);
return -EINVAL;
return rtsp_send(impl, "OPTIONS", NULL, NULL, rtsp_auth_reply);
}
static int rtsp_options_reply(void *data, int status, const struct spa_dict *headers)

View file

@ -8,6 +8,7 @@
#include <getopt.h>
#include <limits.h>
#include <pipewire/cleanup.h>
#include <pipewire/impl.h>
#include "spa-device.h"
@ -51,12 +52,11 @@ SPA_EXPORT
int pipewire__module_init(struct pw_impl_module *module, const char *args)
{
struct pw_properties *props = NULL;
char **argv = NULL;
spa_auto(pw_strv) argv = NULL;
int n_tokens;
struct pw_context *context = pw_impl_module_get_context(module);
struct pw_impl_device *device;
struct device_data *data;
int res;
PW_LOG_TOPIC_INIT(mod_topic);
@ -69,10 +69,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
if (n_tokens == 2) {
props = pw_properties_new_string(argv[1]);
if (props == NULL) {
res = -errno;
goto error_exit_cleanup;
}
if (props == NULL)
return -errno;
}
device = pw_spa_device_load(context,
@ -80,12 +78,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
0,
props,
sizeof(struct device_data));
if (device == NULL) {
res = -errno;
goto error_exit_cleanup;
}
pw_free_strv(argv);
if (device == NULL)
return -errno;
data = pw_spa_device_get_user_data(device);
data->this = device;
@ -99,10 +93,6 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
return 0;
error_arguments:
res = -EINVAL;
pw_log_error("usage: module-spa-device " MODULE_USAGE);
goto error_exit_cleanup;
error_exit_cleanup:
pw_free_strv(argv);
return res;
return -EINVAL;
}

View file

@ -10,6 +10,7 @@
#include <getopt.h>
#include <limits.h>
#include <pipewire/cleanup.h>
#include <pipewire/impl.h>
#include "spa-node.h"
@ -52,8 +53,8 @@ SPA_EXPORT
int pipewire__module_init(struct pw_impl_module *module, const char *args)
{
struct pw_properties *props = NULL;
char **argv = NULL;
int n_tokens, res;
spa_auto(pw_strv) argv = NULL;
int n_tokens;
struct pw_context *context = pw_impl_module_get_context(module);
struct pw_impl_node *node;
struct node_data *data;
@ -69,10 +70,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
if (n_tokens == 2) {
props = pw_properties_new_string(argv[1]);
if (props == NULL) {
res = -errno;
goto error_exit_cleanup;
}
if (props == NULL)
return -errno;
}
node = pw_spa_node_load(context,
@ -81,12 +80,8 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
props,
sizeof(struct node_data));
if (node == NULL) {
res = -errno;
goto error_exit_cleanup;
}
pw_free_strv(argv);
if (node == NULL)
return -errno;
data = pw_spa_node_get_user_data(node);
data->this = node;
@ -101,10 +96,6 @@ int pipewire__module_init(struct pw_impl_module *module, const char *args)
return 0;
error_arguments:
res = -EINVAL;
pw_log_error("usage: module-spa-node " MODULE_USAGE);
goto error_exit_cleanup;
error_exit_cleanup:
pw_free_strv(argv);
return res;
return -EINVAL;
}