diff --git a/src/gst/gstpipewireformat.c b/src/gst/gstpipewireformat.c index f3d2926be..36f2221ef 100644 --- a/src/gst/gstpipewireformat.c +++ b/src/gst/gstpipewireformat.c @@ -527,7 +527,7 @@ builder_overflow (void *event_data, uint32_t size) b->size = SPA_ROUND_UP_N (size, 512); b->data = realloc (b->data, b->size); if (b->data == NULL) - return -ENOMEM; + return -errno; return 0; } diff --git a/src/modules/module-access.c b/src/modules/module-access.c index 613ab26d7..f5daf1316 100644 --- a/src/modules/module-access.c +++ b/src/modules/module-access.c @@ -222,7 +222,7 @@ int pipewire__module_init(struct pw_module *module, const char *args) impl = calloc(1, sizeof(struct impl)); if (impl == NULL) - return -ENOMEM; + return -errno; pw_log_debug("module %p: new %s", impl, args); diff --git a/src/modules/module-audio-dsp.c b/src/modules/module-audio-dsp.c index 6263df1f3..1b74b880d 100644 --- a/src/modules/module-audio-dsp.c +++ b/src/modules/module-audio-dsp.c @@ -205,7 +205,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie NULL), sizeof(*data)); if (factory == NULL) - return -ENOMEM; + return -errno; data = pw_factory_get_user_data(factory); data->this = factory; diff --git a/src/modules/module-client-device.c b/src/modules/module-client-device.c index 0302ffc31..8817bec71 100644 --- a/src/modules/module-client-device.c +++ b/src/modules/module-client-device.c @@ -66,26 +66,37 @@ static void *create_object(void *_data, struct pw_resource *device_resource; struct pw_global *parent; struct pw_client *client = pw_resource_get_client(resource); + int res; device_resource = pw_resource_new(client, new_id, PW_PERM_RWX, type, version, 0); - if (device_resource == NULL) - goto no_mem; + if (device_resource == NULL) { + res = -errno; + goto error_resource; + } parent = pw_client_get_global(client); result = pw_client_device_new(device_resource, parent, properties); - if (result == NULL) - goto no_mem; + if (result == NULL) { + res = -errno; + goto error_device; + } return result; - no_mem: - pw_log_error("can't create device"); - pw_resource_error(resource, -ENOMEM, "can't create device: no memory"); - goto done; - done: - if (properties) - pw_properties_free(properties); +error_resource: + pw_log_error("can't create resource: %s", spa_strerror(res)); + pw_resource_error(resource, res, "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_device: + pw_log_error("can't create device: %s", spa_strerror(res)); + pw_resource_error(resource, res, "can't create device: %s", spa_strerror(res)); + goto error_exit_free; + +error_exit_free: + pw_resource_destroy(device_resource); +error_exit: + errno = -res; return NULL; } @@ -128,7 +139,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie NULL), sizeof(*data)); if (factory == NULL) - return -ENOMEM; + return -errno; data = pw_factory_get_user_data(factory); data->this = factory; diff --git a/src/modules/module-client-node.c b/src/modules/module-client-node.c index 9e923325d..05a3fc6be 100644 --- a/src/modules/module-client-node.c +++ b/src/modules/module-client-node.c @@ -69,10 +69,13 @@ static void *create_object(void *_data, struct pw_resource *node_resource; struct pw_global *parent; struct pw_client *client = pw_resource_get_client(resource); + int res; node_resource = pw_resource_new(client, new_id, PW_PERM_RWX, type, version, 0); - if (node_resource == NULL) - goto no_mem; + if (node_resource == NULL) { + res = -errno; + goto error_resource; + } parent = pw_client_get_global(client); @@ -82,18 +85,24 @@ static void *create_object(void *_data, else { result = pw_client_node_new(node_resource, parent, properties, true); } - if (result == NULL) - goto no_mem; - + if (result == NULL) { + res = -errno; + goto error_node; + } return result; - no_mem: - pw_log_error("can't create node"); - pw_resource_error(resource, -ENOMEM, "can't create node: no memory"); - goto done; - done: - if (properties) - pw_properties_free(properties); +error_resource: + pw_log_error("can't create resource: %s", spa_strerror(res)); + pw_resource_error(resource, res, "can't create resource: %s", spa_strerror(res)); + goto error_exit; +error_node: + pw_log_error("can't create node: %s", spa_strerror(res)); + pw_resource_error(resource, res, "can't create node: %s", spa_strerror(res)); + goto error_exit_free; +error_exit_free: + pw_resource_destroy(node_resource); +error_exit: + errno = -res; return NULL; } @@ -135,7 +144,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie NULL, sizeof(*data)); if (factory == NULL) - return -ENOMEM; + return -errno; data = pw_factory_get_user_data(factory); data->this = factory; diff --git a/src/modules/module-client-node/client-node.c b/src/modules/module-client-node/client-node.c index 5d70b145e..577d30e94 100644 --- a/src/modules/module-client-node/client-node.c +++ b/src/modules/module-client-node/client-node.c @@ -225,6 +225,8 @@ static struct mem *ensure_mem(struct impl *impl, int fd, uint32_t type, uint32_t if (f == NULL) { m = pw_array_add(&impl->mems, sizeof(struct mem)); + if (m == NULL) + return NULL; m->id = pw_array_get_len(&impl->mems, struct mem) - 1; } else { @@ -469,6 +471,8 @@ static int impl_node_set_io(void *object, uint32_t id, void *data, size_t size) mem_offset += mem->offset; mem_size = size; m = ensure_mem(impl, mem->fd, SPA_DATA_MemFd, mem->flags); + if (m == NULL) + return -errno; memid = m->id; } else { @@ -776,6 +780,8 @@ static int do_port_set_io(struct impl *impl, mem_offset += mem->offset; m = ensure_mem(impl, mem->fd, SPA_DATA_MemFd, mem->flags); + if (m == NULL) + return -errno; memid = m->id; } else { @@ -879,6 +885,8 @@ do_port_use_buffers(struct impl *impl, } m = ensure_mem(impl, mem->fd, SPA_DATA_MemFd, mem->flags); + if (m == NULL) + return -errno; b->memid = m->id; mb[i].buffer = &b->buffer; @@ -900,6 +908,8 @@ do_port_use_buffers(struct impl *impl, if (d->type == SPA_DATA_DmaBuf || d->type == SPA_DATA_MemFd) { m = ensure_mem(impl, d->fd, d->type, d->flags); + if (m == NULL) + return -errno; b->buffer.datas[j].data = SPA_UINT32_TO_PTR(m->id); } else if (d->type == SPA_DATA_MemPtr) { spa_log_debug(this->log, "mem %d %zd", j, SPA_PTRDIFF(d->data, baseptr)); @@ -1278,6 +1288,10 @@ void pw_client_node_registered(struct pw_client_node *this, struct pw_global *gl impl->other_fds[1]); m = ensure_mem(impl, node->activation->fd, SPA_DATA_MemFd, node->activation->flags); + if (m == NULL) { + pw_log_debug("client-node %p: can't ensure mem: %m", this); + return; + } pw_client_node_resource_set_activation(this->resource, node_id, @@ -1362,6 +1376,8 @@ static int port_init_mix(void *data, struct pw_port_mix *mix) return -ENOMEM; mix->id = pw_map_insert_new(&impl->io_map, NULL); + if (mix->id == SPA_ID_INVALID) + return -errno; mix->io = SPA_MEMBER(impl->io_areas->ptr, mix->id * sizeof(struct spa_io_buffers), void); @@ -1577,6 +1593,10 @@ static void node_peer_added(void *data, struct pw_node *peer) return; m = ensure_mem(impl, peer->activation->fd, SPA_DATA_MemFd, peer->activation->flags); + if (m == NULL) { + pw_log_debug("client-node %p: can't ensure mem: %m", this); + return; + } pw_log_debug("client-node %p: peer %p %u added %u", &impl->this, peer, peer->info.id, m->id); diff --git a/src/modules/module-client-node/client-stream.c b/src/modules/module-client-node/client-stream.c index f03033c6a..f0f391a17 100644 --- a/src/modules/module-client-node/client-stream.c +++ b/src/modules/module-client-node/client-stream.c @@ -609,13 +609,13 @@ static int negotiate_buffers(struct impl *impl) in_alloc = false; } - if (spa_pod_parse_object(param, + if ((res = spa_pod_parse_object(param, SPA_TYPE_OBJECT_ParamBuffers, NULL, SPA_PARAM_BUFFERS_buffers, SPA_POD_Int(&buffers), SPA_PARAM_BUFFERS_blocks, SPA_POD_Int(&blocks), SPA_PARAM_BUFFERS_size, SPA_POD_Int(&size), - SPA_PARAM_BUFFERS_align, SPA_POD_Int(&align)) < 0) - return -EINVAL; + SPA_PARAM_BUFFERS_align, SPA_POD_Int(&align))) < 0) + return res; spa_log_debug(this->log, "%p: buffers %d, blocks %d, size %d, align %d", impl, buffers, blocks, size, align); @@ -635,7 +635,7 @@ static int negotiate_buffers(struct impl *impl) free(impl->buffers); impl->buffers = calloc(buffers, sizeof(struct spa_buffer *) + info.skel_size); if (impl->buffers == NULL) - return -ENOMEM; + return -errno; skel = SPA_MEMBER(impl->buffers, sizeof(struct spa_buffer *) * buffers, void); diff --git a/src/modules/module-client-node/remote-node.c b/src/modules/module-client-node/remote-node.c index cd16c33c1..763ab7675 100644 --- a/src/modules/module-client-node/remote-node.c +++ b/src/modules/module-client-node/remote-node.c @@ -342,6 +342,9 @@ static int client_node_add_mem(void *object, } m = pw_array_add(&data->mems, sizeof(struct mem)); + if (m == NULL) + return -errno; + pw_log_debug("add mem %u, fd %d, flags %d", mem_id, memfd, flags); m->id = mem_id; @@ -720,6 +723,11 @@ client_node_port_use_buffers(void *object, } bid = pw_array_add(&mix->buffers, sizeof(struct buffer)); + if (bid == NULL) { + res = -errno; + pw_proxy_error(proxy, res, "error allocating buffers : %m"); + goto cleanup; + } bid->id = i; bmem.mem_id = m->id; @@ -745,7 +753,7 @@ client_node_port_use_buffers(void *object, b = bid->buf = malloc(size); if (b == NULL) { - res = -ENOMEM; + res = -errno; pw_proxy_error(proxy, res, "can't alloc memory: %s", spa_strerror(res)); goto cleanup; } @@ -962,6 +970,8 @@ client_node_set_activation(void *object, if (ptr) { link = pw_array_add(&data->links, sizeof(struct link)); + if (link == NULL) + return -errno; link->node_id = node_id; link->mem_id = memid; link->target.activation = ptr; diff --git a/src/modules/module-link-factory.c b/src/modules/module-link-factory.c index 8ed6b427a..1fbb1fcad 100644 --- a/src/modules/module-link-factory.c +++ b/src/modules/module-link-factory.c @@ -126,6 +126,7 @@ static struct pw_port *get_port(struct pw_node *node, enum spa_direction directi if ((res = pw_port_add(p, node)) < 0) { pw_log_warn("can't add port: %s", spa_strerror(res)); + errno = -res; return NULL; } } @@ -158,15 +159,15 @@ static void *create_object(void *_data, core = pw_client_get_core(client); if (properties == NULL) - goto no_properties; + goto error_properties; if ((str = pw_properties_get(properties, PW_KEY_LINK_OUTPUT_NODE)) == NULL) - goto no_properties; + goto error_properties; output_node_id = pw_properties_parse_int(str); if ((str = pw_properties_get(properties, PW_KEY_LINK_INPUT_NODE)) == NULL) - goto no_properties; + goto error_properties; input_node_id = pw_properties_parse_int(str); @@ -178,13 +179,13 @@ static void *create_object(void *_data, global = pw_core_find_global(core, output_node_id); if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Node) - goto no_output; + goto error_output; output_node = pw_global_get_object(global); global = pw_core_find_global(core, input_node_id); if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Node) - goto no_input; + goto error_input; input_node = pw_global_get_object(global); @@ -194,31 +195,34 @@ static void *create_object(void *_data, else { global = pw_core_find_global(core, output_port_id); if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Port) - goto no_output_port; + goto error_output_port; outport = pw_global_get_object(global); } if (outport == NULL) - goto no_output_port; + goto error_output_port; if (input_port_id == SPA_ID_INVALID) inport = get_port(input_node, SPA_DIRECTION_INPUT); else { global = pw_core_find_global(core, input_port_id); if (global == NULL || pw_global_get_type(global) != PW_TYPE_INTERFACE_Port) - goto no_input_port; + goto error_input_port; inport = pw_global_get_object(global); } if (inport == NULL) - goto no_input_port; + goto error_input_port; str = pw_properties_get(properties, PW_KEY_OBJECT_LINGER); linger = str ? pw_properties_parse_bool(str) : false; link = pw_link_new(core, outport, inport, NULL, properties, sizeof(struct link_data)); - if (link == NULL) - goto no_mem; + properties = NULL; + if (link == NULL) { + res = -errno; + goto error_create_link; + } ld = pw_link_get_user_data(link); ld->data = d; @@ -226,61 +230,71 @@ static void *create_object(void *_data, spa_list_append(&d->link_list, &ld->l); pw_link_add_listener(link, &ld->link_listener, &link_events, ld); - pw_link_register(link, + if ((res = pw_link_register(link, linger ? NULL : client, linger ? NULL : pw_client_get_global(client), - NULL); - - properties = NULL; + NULL)) < 0) + goto error_link_register; ld->global = pw_link_get_global(link); pw_global_add_listener(ld->global, &ld->global_listener, &global_events, ld); res = pw_global_bind(ld->global, client, PW_PERM_RWX, PW_VERSION_LINK_PROXY, new_id); if (res < 0) - goto no_bind; + goto error_bind; if (!linger) { ld->resource = pw_client_find_resource(client, new_id); - if (ld->resource == NULL) - goto no_bind; + if (ld->resource == NULL) { + res = -ENOENT; + goto error_bind; + } pw_resource_add_listener(ld->resource, &ld->resource_listener, &resource_events, ld); } return link; - no_properties: +error_properties: + res = -EINVAL; pw_log_error("link-factory usage:" FACTORY_USAGE); - pw_resource_error(resource, -EINVAL, "no properties"); - goto done; - no_output: + pw_resource_error(resource, res, "no properties"); + goto error_exit; +error_output: + res = -EINVAL; pw_log_error("link-factory unknown output node %u", output_node_id); - pw_resource_error(resource, -EINVAL, "unknown output node %u", output_node_id); - goto done; - no_input: + pw_resource_error(resource, res, "unknown output node %u", output_node_id); + goto error_exit; +error_input: + res = -EINVAL; pw_log_error("link-factory unknown input node %u", input_node_id); - pw_resource_error(resource, -EINVAL, "unknown input node %u", input_node_id); - goto done; - no_output_port: + pw_resource_error(resource, res, "unknown input node %u", input_node_id); + goto error_exit; +error_output_port: + res = -EINVAL; pw_log_error("link-factory unknown output port %u", output_port_id); - pw_resource_error(resource, -EINVAL, "unknown output port %u", output_port_id); - goto done; - no_input_port: + pw_resource_error(resource, res, "unknown output port %u", output_port_id); + goto error_exit; +error_input_port: + res = -EINVAL; pw_log_error("link-factory unknown input port %u", input_port_id); - pw_resource_error(resource, -EINVAL, "unknown input port %u", input_port_id); - goto done; - no_mem: - res = -errno; + pw_resource_error(resource, res, "unknown input port %u", input_port_id); + goto error_exit; +error_create_link: pw_log_error("can't create link: %s", spa_strerror(res)); pw_resource_error(resource, res, "can't create link: %s", spa_strerror(res)); - goto done; - no_bind: - pw_resource_error(resource, res, "can't bind link"); - goto done; - done: + goto error_exit; +error_link_register: + pw_log_error("can't register link: %s", spa_strerror(res)); + pw_resource_error(resource, res, "can't register link: %s", spa_strerror(res)); + goto error_exit; +error_bind: + pw_resource_error(resource, res, "can't bind link: %s", spa_strerror(res)); + goto error_exit; +error_exit: if (properties) pw_properties_free(properties); + errno = -res; return NULL; } @@ -315,6 +329,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie struct pw_core *core = pw_module_get_core(module); struct pw_factory *factory; struct factory_data *data; + int res; factory = pw_factory_new(core, "link-factory", @@ -324,8 +339,10 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie PW_KEY_FACTORY_USAGE, FACTORY_USAGE, NULL), sizeof(*data)); - if (factory == NULL) - return -ENOMEM; + if (factory == NULL) { + res = -errno; + goto error_cleanup; + } data = pw_factory_get_user_data(factory); data->this = factory; @@ -338,13 +355,21 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie &impl_factory, data); - pw_factory_register(factory, NULL, pw_module_get_global(module), NULL); + if ((res = pw_factory_register(factory, NULL, pw_module_get_global(module), NULL)) < 0) + goto error_register; pw_module_add_listener(module, &data->module_listener, &module_events, data); pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props)); return 0; + +error_register: + pw_factory_destroy(factory); +error_cleanup: + if (properties) + pw_properties_free(properties); + return res; } SPA_EXPORT diff --git a/src/modules/module-protocol-native.c b/src/modules/module-protocol-native.c index ba71afb92..341cc5255 100644 --- a/src/modules/module-protocol-native.c +++ b/src/modules/module-protocol-native.c @@ -79,12 +79,12 @@ struct client { struct pw_properties *properties; struct spa_source *source; - struct pw_protocol_native_connection *connection; - struct spa_hook conn_listener; + struct pw_protocol_native_connection *connection; + struct spa_hook conn_listener; - bool disconnecting; - bool flush_signaled; - struct spa_source *flush_event; + struct spa_source *flush_event; + unsigned int disconnecting:1; + unsigned int flush_signaled:1; }; struct server { @@ -93,11 +93,11 @@ struct server { int fd_lock; struct sockaddr_un addr; char lock_addr[UNIX_PATH_MAX + LOCK_SUFFIXLEN]; - bool activated; struct pw_loop *loop; struct spa_source *source; struct spa_hook hook; + unsigned int activated:1; }; struct client_data { @@ -105,7 +105,7 @@ struct client_data { struct spa_hook client_listener; struct spa_source *source; struct pw_protocol_native_connection *connection; - bool busy; + unsigned int busy:1; }; static void diff --git a/src/modules/module-protocol-native/connection.c b/src/modules/module-protocol-native/connection.c index dbb6a3cf6..be5cae224 100644 --- a/src/modules/module-protocol-native/connection.c +++ b/src/modules/module-protocol-native/connection.c @@ -123,14 +123,18 @@ uint32_t pw_protocol_native_connection_add_fd(struct pw_protocol_native_connecti static void *connection_ensure_size(struct pw_protocol_native_connection *conn, struct buffer *buf, size_t size) { + int res; + if (buf->buffer_size + size > buf->buffer_maxsize) { buf->buffer_maxsize = SPA_ROUND_UP_N(buf->buffer_size + size, MAX_BUFFER_SIZE); buf->buffer_data = realloc(buf->buffer_data, buf->buffer_maxsize); if (buf->buffer_data == NULL) { + res = -errno; buf->buffer_maxsize = 0; spa_hook_list_call(&conn->listener_list, struct pw_protocol_native_connection_events, - error, 0, -ENOMEM); + error, 0, -res); + errno = -res; return NULL; } pw_log_warn("connection %p: resize buffer to %zd %zd %zd", @@ -335,7 +339,7 @@ pw_protocol_native_connection_get_next(struct pw_protocol_native_connection *con break; if (connection_ensure_size(conn, buf, len) == NULL) - return -ENOMEM; + return -errno; if ((res = refill_buffer(conn, buf)) < 0) return res; } @@ -362,7 +366,7 @@ static int builder_overflow(void *data, uint32_t size) b->size = SPA_ROUND_UP_N(size, 4096); if ((b->data = begin_write(&impl->this, b->size)) == NULL) - return -ENOMEM; + return -errno; return 0; } @@ -401,7 +405,7 @@ pw_protocol_native_connection_end(struct pw_protocol_native_connection *conn, int res; if ((p = connection_ensure_size(conn, buf, HDR_SIZE + size)) == NULL) - return -ENOMEM; + return -errno; p[0] = buf->msg.id; p[1] = (buf->msg.opcode << 24) | (size & 0xffffff); diff --git a/src/modules/spa/module-device-factory.c b/src/modules/spa/module-device-factory.c index 480f4cf13..257009636 100644 --- a/src/modules/spa/module-device-factory.c +++ b/src/modules/spa/module-device-factory.c @@ -83,13 +83,14 @@ static void *create_object(void *_data, struct pw_device *device; const char *factory_name, *name; struct device_data *nd; + int res; if (properties == NULL) - goto no_properties; + goto error_properties; factory_name = pw_properties_get(properties, SPA_KEY_FACTORY_NAME); if (factory_name == NULL) - goto no_properties; + goto error_properties; name = pw_properties_get(properties, PW_KEY_DEVICE_NAME); if (name == NULL) @@ -103,8 +104,10 @@ static void *create_object(void *_data, 0, properties, sizeof(struct device_data)); - if (device == NULL) - goto no_mem; + if (device == NULL) { + res = -errno; + goto error_device; + } nd = pw_spa_device_get_user_data(device); nd->device = device; @@ -120,17 +123,19 @@ static void *create_object(void *_data, return device; - no_properties: +error_properties: + res = -EINVAL; pw_log_error("factory %p: usage: " FACTORY_USAGE, data->this); - if (resource) { - pw_resource_error(resource, -EINVAL, "usage: " FACTORY_USAGE); - } - return NULL; - no_mem: - pw_log_error("can't create device: no memory"); - if (resource) { - pw_resource_error(resource, -ENOMEM, "no memory"); - } + if (resource) + pw_resource_error(resource, res, "usage: " FACTORY_USAGE); + goto error_exit; +error_device: + pw_log_error("can't create device: %s", spa_strerror(res)); + if (resource) + pw_resource_error(resource, res, "can't create device: %s", spa_strerror(res)); + goto error_exit; +error_exit: + errno = -res; return NULL; } @@ -174,6 +179,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie struct pw_core *core = pw_module_get_core(module); struct pw_factory *factory; struct factory_data *data; + int res; factory = pw_factory_new(core, "spa-device-factory", @@ -182,7 +188,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie NULL, sizeof(*data)); if (factory == NULL) - return -ENOMEM; + return -errno; data = pw_factory_get_user_data(factory); data->this = factory; @@ -198,9 +204,17 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props)); - pw_factory_register(factory, NULL, pw_module_get_global(module), NULL); + if ((res = pw_factory_register(factory, + NULL, + pw_module_get_global(module), + NULL)) < 0) + goto error_register; return 0; + +error_register: + pw_factory_destroy(factory); + return res; } SPA_EXPORT diff --git a/src/modules/spa/module-node-factory.c b/src/modules/spa/module-node-factory.c index fc30def3f..1966dde42 100644 --- a/src/modules/spa/module-node-factory.c +++ b/src/modules/spa/module-node-factory.c @@ -174,6 +174,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie struct pw_core *core = pw_module_get_core(module); struct pw_factory *factory; struct factory_data *data; + int res; factory = pw_factory_new(core, "spa-node-factory", @@ -182,7 +183,7 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie NULL, sizeof(*data)); if (factory == NULL) - return -ENOMEM; + return -errno; data = pw_factory_get_user_data(factory); data->this = factory; @@ -198,9 +199,15 @@ static int module_init(struct pw_module *module, struct pw_properties *propertie pw_module_update_properties(module, &SPA_DICT_INIT_ARRAY(module_props)); - pw_factory_register(factory, NULL, pw_module_get_global(module), NULL); + if ((res = pw_factory_register(factory, + NULL, pw_module_get_global(module), NULL)) < 0) + goto error_register; return 0; + +error_register: + pw_factory_destroy(factory); + return res; } SPA_EXPORT diff --git a/src/modules/spa/spa-device.c b/src/modules/spa/spa-device.c index 394049e7d..218130d67 100644 --- a/src/modules/spa/spa-device.c +++ b/src/modules/spa/spa-device.c @@ -52,7 +52,6 @@ struct impl { void *unload; struct spa_handle *handle; struct spa_device *device; - char *factory_name; struct spa_hook device_listener; @@ -69,7 +68,6 @@ static void device_destroy(void *data) spa_hook_remove(&impl->device_listener); if (impl->handle) pw_unload_spa_handle(impl->handle); - free(impl->factory_name); } static const struct pw_device_events device_events = { @@ -90,6 +88,7 @@ pw_spa_device_new(struct pw_core *core, { struct pw_device *this; struct impl *impl; + int res; this = pw_device_new(core, name, properties, sizeof(struct impl) + user_data_size); if (this == NULL) @@ -109,10 +108,16 @@ pw_spa_device_new(struct pw_core *core, pw_device_add_listener(this, &impl->device_listener, &device_events, impl); pw_device_set_implementation(this, impl->device); - if (!SPA_FLAG_CHECK(impl->flags, PW_SPA_DEVICE_FLAG_NO_REGISTER)) - pw_device_register(this, impl->owner, impl->parent, NULL); - + if (!SPA_FLAG_CHECK(impl->flags, PW_SPA_DEVICE_FLAG_NO_REGISTER)) { + if ((res = pw_device_register(this, impl->owner, impl->parent, NULL)) < 0) + goto error_register; + } return this; + +error_register: + pw_device_destroy(this); + errno = -res; + return NULL; } void *pw_spa_device_get_user_data(struct pw_device *device) @@ -131,7 +136,6 @@ struct pw_device *pw_spa_device_load(struct pw_core *core, size_t user_data_size) { struct pw_device *this; - struct impl *impl; struct spa_handle *handle; void *iface; int res; @@ -139,6 +143,7 @@ struct pw_device *pw_spa_device_load(struct pw_core *core, handle = pw_core_load_spa_handle(core, factory_name, properties ? &properties->dict : NULL); if (handle == NULL) { + res = -errno; pw_log_error("can't load device handle: %m"); goto exit; } @@ -151,17 +156,16 @@ struct pw_device *pw_spa_device_load(struct pw_core *core, this = pw_spa_device_new(core, owner, parent, name, flags, iface, handle, properties, user_data_size); if (this == NULL) { + res = -errno; pw_log_error("can't create device: %m"); - goto exit; + goto exit_unload; } - impl = this->user_data; - impl->factory_name = strdup(factory_name); - return this; exit_unload: pw_unload_spa_handle(handle); exit: + errno = -res; return NULL; } diff --git a/src/modules/spa/spa-monitor.c b/src/modules/spa/spa-monitor.c index a88ae0d6d..25367ab58 100644 --- a/src/modules/spa/spa-monitor.c +++ b/src/modules/spa/spa-monitor.c @@ -98,6 +98,9 @@ static struct monitor_object *add_object(struct pw_spa_monitor *this, uint32_t i else props = pw_properties_new(NULL, NULL); + if (props == NULL) + return NULL; + if ((name = pw_properties_get(props, PW_KEY_DEVICE_NAME)) == NULL) name = "unknown"; @@ -112,16 +115,21 @@ static struct monitor_object *add_object(struct pw_spa_monitor *this, uint32_t i handle = pw_core_load_spa_handle(core, info->factory_name, &props->dict); if (handle == NULL) { + res = -errno; pw_log_error("can't make factory instance: %m"); goto error_free_props; } if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) { - pw_log_error("can't get %d interface: %d", info->type, res); + pw_log_error("can't get %d interface: %s", info->type, spa_strerror(res)); goto error_free_handle; } obj = calloc(1, sizeof(struct monitor_object)); + if (obj == NULL) { + res = -errno; + goto error_free_handle; + } obj->id = id; obj->name = strdup(name); obj->handle = handle; @@ -139,6 +147,7 @@ static struct monitor_object *add_object(struct pw_spa_monitor *this, uint32_t i break; } default: + res = -ENOTSUP; pw_log_error("interface %d not implemented", obj->type); goto error_free_object; } @@ -154,6 +163,7 @@ error_free_handle: pw_unload_spa_handle(handle); error_free_props: pw_properties_free(props); + errno = -res; return NULL; } @@ -213,7 +223,7 @@ static int on_monitor_object_info(void *data, uint32_t id, } else if (obj == NULL) { obj = add_object(this, id, info, now_nsec); if (obj == NULL) - return -ENOMEM; + return -errno; } else { change_object(this, obj, info, now_nsec); } @@ -267,17 +277,21 @@ struct pw_spa_monitor *pw_spa_monitor_load(struct pw_core *core, handle = pw_core_load_spa_handle(core, factory_name, properties ? &properties->dict : NULL); - if (handle == NULL) + if (handle == NULL) { + res = -errno; goto exit; + } if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Monitor, &iface)) < 0) { - pw_log_error("can't get MONITOR interface: %d", res); + pw_log_error("can't get MONITOR interface: %s", spa_strerror(res)); goto exit_unload; } impl = calloc(1, sizeof(struct impl) + user_data_size); - if (impl == NULL) + if (impl == NULL) { + res = -errno; goto exit_unload; + } impl->core = core; impl->parent = parent; @@ -302,6 +316,7 @@ struct pw_spa_monitor *pw_spa_monitor_load(struct pw_core *core, exit_unload: pw_unload_spa_handle(handle); exit: + errno = -res; return NULL; } diff --git a/src/modules/spa/spa-node.c b/src/modules/spa/spa-node.c index 5885b7580..39a3ea216 100644 --- a/src/modules/spa/spa-node.c +++ b/src/modules/spa/spa-node.c @@ -139,9 +139,7 @@ pw_spa_node_new(struct pw_core *core, impl->user_data = SPA_MEMBER(impl, sizeof(struct impl), void); pw_node_add_listener(this, &impl->node_listener, &node_events, impl); - res = pw_node_set_implementation(this, impl->node); - - if (res < 0) + if ((res = pw_node_set_implementation(this, impl->node)) < 0) goto clean_node; if (flags & PW_SPA_NODE_FLAG_ASYNC) { @@ -153,6 +151,7 @@ pw_spa_node_new(struct pw_core *core, clean_node: pw_node_destroy(this); + errno = -res; return NULL; } @@ -180,7 +179,7 @@ setup_props(struct pw_core *core, struct spa_node *spa_node, struct pw_propertie &b); if (res != 1) { if (res < 0) - pw_log_debug("spa_node_get_props failed: %d", res); + pw_log_debug("spa_node_get_props failed: %s", spa_strerror(res)); return res; } @@ -230,7 +229,7 @@ setup_props(struct pw_core *core, struct spa_node *spa_node, struct pw_propertie } if ((res = spa_node_set_param(spa_node, SPA_PARAM_Props, 0, props)) < 0) { - pw_log_debug("spa_node_set_props failed: %d", res); + pw_log_debug("spa_node_set_props failed: %s", spa_strerror(res)); return res; } return 0; @@ -256,8 +255,10 @@ struct pw_node *pw_spa_node_load(struct pw_core *core, handle = pw_core_load_spa_handle(core, factory_name, properties ? &properties->dict : NULL); - if (handle == NULL) + if (handle == NULL) { + res = -errno; goto exit; + } if ((res = spa_handle_get_interface(handle, SPA_TYPE_INTERFACE_Node, &iface)) < 0) { pw_log_error("can't get node interface %d", res); @@ -270,14 +271,16 @@ struct pw_node *pw_spa_node_load(struct pw_core *core, if (properties != NULL) { if (setup_props(core, spa_node, properties) < 0) { - pw_log_debug("Unrecognized properties"); + pw_log_warn("can't setup properties: %s", spa_strerror(res)); } } this = pw_spa_node_new(core, owner, parent, name, flags, spa_node, handle, properties, user_data_size); - if (this == NULL) + if (this == NULL) { + res = -errno; goto exit; + } impl = this->user_data; impl->handle = handle; @@ -288,5 +291,6 @@ struct pw_node *pw_spa_node_load(struct pw_core *core, exit_unload: pw_unload_spa_handle(handle); exit: + errno = -res; return NULL; } diff --git a/src/pipewire/client.c b/src/pipewire/client.c index 73b283606..ae5e0973a 100644 --- a/src/pipewire/client.c +++ b/src/pipewire/client.c @@ -195,7 +195,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) - goto out; + goto error_resource; data = pw_resource_get_user_data(resource); data->client = this; @@ -219,7 +219,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, return 0; - out: +error_resource: pw_log_error("can't create client resource: %m"); return -errno; } @@ -259,10 +259,13 @@ struct pw_client *pw_client_new(struct pw_core *core, struct pw_client *this; struct impl *impl; struct pw_permission *p; + int res; impl = calloc(1, sizeof(struct impl) + user_data_size); - if (impl == NULL) - return NULL; + if (impl == NULL) { + res = -errno; + goto error_cleanup; + } this = &impl->this; pw_log_debug("client %p: new", this); @@ -271,11 +274,18 @@ struct pw_client *pw_client_new(struct pw_core *core, if (properties == NULL) properties = pw_properties_new(NULL, NULL); - if (properties == NULL) - return NULL; + if (properties == NULL) { + res = -errno; + goto error_free; + } pw_array_init(&impl->permissions, 1024); p = pw_array_add(&impl->permissions, sizeof(struct pw_permission)); + if (p == NULL) { + res = -errno; + goto error_clear_array; + } + p->id = SPA_ID_INVALID; p->permissions = 0; @@ -297,6 +307,16 @@ struct pw_client *pw_client_new(struct pw_core *core, pw_core_emit_check_access(core, this); return this; + +error_clear_array: + pw_array_clear(&impl->permissions); +error_free: + free(impl); +error_cleanup: + if (properties) + pw_properties_free(properties); + errno = -res; + return NULL; } static void global_destroy(void *object) @@ -522,6 +542,10 @@ int pw_client_update_permissions(struct pw_client *client, continue; } p = ensure_permissions(client, permissions[i].id); + if (p == NULL) { + pw_log_warn("client %p: can't ensure permission: %m", client); + continue; + } old_perm = p->permissions == SPA_ID_INVALID ? def->permissions : p->permissions; new_perm = permissions[i].permissions; diff --git a/src/pipewire/core.c b/src/pipewire/core.c index 00c55d503..575c9200b 100644 --- a/src/pipewire/core.c +++ b/src/pipewire/core.c @@ -74,33 +74,33 @@ static void * registry_bind(void *object, uint32_t id, uint32_t permissions, new_id = user_data_size; if ((global = pw_core_find_global(core, id)) == NULL) - goto no_id; + goto error_no_id; permissions = pw_global_get_permissions(global, client); if (!PW_PERM_IS_R(permissions)) - goto no_id; + goto error_no_id; if (global->type != type) - goto wrong_interface; + goto error_wrong_interface; pw_log_debug("global %p: bind global id %d, iface %s/%d to %d", global, id, spa_debug_type_find_name(pw_type_info(), type), version, new_id); if (pw_global_bind(global, client, permissions, version, new_id) < 0) - goto exit; + goto error_exit_clean; return NULL; - no_id: +error_no_id: pw_log_debug("registry %p: no global with id %u to bind to %u", resource, id, new_id); pw_resource_error(resource, -ENOENT, "no such global %u", id); - goto exit; - wrong_interface: + goto error_exit_clean; +error_wrong_interface: pw_log_debug("registry %p: global with id %u has no interface %u", resource, id, type); pw_resource_error(resource, -ENOENT, "no such interface %u", type); - goto exit; - exit: + goto error_exit_clean; +error_exit_clean: /* unmark the new_id the map, the client does not yet know about the failed * bind and will choose the next id, which we would refuse when we don't mark * new_id as 'used and freed' */ @@ -116,25 +116,34 @@ static int registry_destroy(void *object, uint32_t id) struct pw_core *core = resource->core; struct pw_global *global; uint32_t permissions; + int res; if ((global = pw_core_find_global(core, id)) == NULL) - goto no_id; + goto error_no_id; permissions = pw_global_get_permissions(global, client); + if (!PW_PERM_IS_R(permissions)) + goto error_no_id; + if (!PW_PERM_IS_X(permissions)) - goto no_id; + goto error_not_allowed; pw_log_debug("global %p: destroy global id %d", global, id); pw_global_destroy(global); return 0; - no_id: +error_no_id: pw_log_debug("registry %p: no global with id %u to destroy", resource, id); - goto exit; - exit: - return -EFAULT; + res = -ENOENT; + goto error_exit; +error_not_allowed: + pw_log_debug("registry %p: destroy of id %u not allowed", resource, id); + res = -EPERM; + goto error_exit; +error_exit: + return res; } static const struct pw_registry_proxy_methods registry_methods = { @@ -228,6 +237,7 @@ static struct pw_registry_proxy * core_get_registry(void *object, uint32_t versi struct pw_resource *registry_resource; struct resource_data *data; uint32_t new_id = user_data_size; + int res; registry_resource = pw_resource_new(client, new_id, @@ -235,8 +245,10 @@ static struct pw_registry_proxy * core_get_registry(void *object, uint32_t versi PW_TYPE_INTERFACE_Registry, version, sizeof(*data)); - if (registry_resource == NULL) - goto no_mem; + if (registry_resource == NULL) { + res = -errno; + goto error_resource; + } data = pw_resource_get_user_data(registry_resource); pw_resource_add_listener(registry_resource, @@ -266,13 +278,14 @@ static struct pw_registry_proxy * core_get_registry(void *object, uint32_t versi return (struct pw_registry_proxy *)registry_resource; - no_mem: - pw_log_error("can't create registry resource"); - pw_core_resource_error(client->core_resource, new_id, - client->recv_seq, -ENOMEM, "no memory"); +error_resource: + pw_log_error("can't create registry resource: %m"); + pw_core_resource_errorf(client->core_resource, new_id, + client->recv_seq, res, + "can't create registry resource: %s", spa_strerror(res)); pw_map_insert_at(&client->objects, new_id, NULL); pw_core_resource_remove_id(client->core_resource, new_id); - errno = ENOMEM; + errno = -res; return NULL; } @@ -333,12 +346,13 @@ core_create_object(void *object, no_properties: res = -errno; pw_log_error("can't create properties"); - pw_resource_error(resource, res, "no memory"); + pw_resource_error(resource, res, "can't create properties: %s", spa_strerror(res)); create_failed: res = -errno; error: pw_map_insert_at(&client->objects, new_id, NULL); pw_core_resource_remove_id(client->core_resource, new_id); + errno = -res; return NULL; } @@ -387,12 +401,16 @@ global_bind(void *_data, struct pw_global *global = this->global; struct pw_resource *resource; struct resource_data *data; + int res; resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); - if (resource == NULL) - goto no_mem; + if (resource == NULL) { + res = -errno; + goto error; + } data = pw_resource_get_user_data(resource); + pw_resource_add_listener(resource, &data->resource_listener, &core_resource_events, @@ -410,9 +428,9 @@ global_bind(void *_data, return 0; - no_mem: - pw_log_error("can't create core resource"); - return -ENOMEM; +error: + pw_log_error("core %p: can't create resource: %m", this); + return res; } static void global_destroy(void *object) @@ -449,8 +467,10 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, int res = 0; impl = calloc(1, sizeof(struct impl) + user_data_size); - if (impl == NULL) - return NULL; + if (impl == NULL) { + res = -errno; + goto error_cleanup; + } this = &impl->this; @@ -463,7 +483,7 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, properties = pw_properties_new(NULL, NULL); if (properties == NULL) { res = -errno; - goto out_free; + goto error_free; } this->properties = properties; @@ -471,16 +491,13 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, this->data_loop_impl = pw_data_loop_new(pw_properties_copy(properties)); if (this->data_loop_impl == NULL) { res = -errno; - goto out_free; + goto error_free; } this->data_loop = pw_data_loop_get_loop(this->data_loop_impl); this->data_system = this->data_loop->system; this->main_loop = main_loop; - pw_array_init(&this->factory_lib, 32); - pw_map_init(&this->globals, 128, 32); - n_support = pw_get_support(this->support, SPA_N_ELEMENTS(this->support)); this->support[n_support++] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_System, this->main_loop->system); this->support[n_support++] = SPA_SUPPORT_INIT(SPA_TYPE_INTERFACE_Loop, this->main_loop->loop); @@ -502,7 +519,11 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, } this->n_support = n_support; - pw_data_loop_start(this->data_loop_impl); + if ((res = pw_data_loop_start(this->data_loop_impl)) < 0) + goto error_free_loop; + + pw_array_init(&this->factory_lib, 32); + pw_map_init(&this->globals, 128, 32); spa_list_init(&this->protocol_list); spa_list_init(&this->remote_list); @@ -551,18 +572,23 @@ struct pw_core *pw_core_new(struct pw_loop *main_loop, this); if (this->global == NULL) { res = -errno; - goto out_cleanup; + goto error_free_loop; } pw_global_add_listener(this->global, &this->global_listener, &global_events, this); pw_global_register(this->global, NULL, NULL); + this->info.id = this->global->id; return this; - out_cleanup: - out_free: +error_free_loop: + pw_data_loop_destroy(this->data_loop_impl); +error_free: free(this); +error_cleanup: + if (properties) + pw_properties_free(properties); errno = -res; return NULL; } diff --git a/src/pipewire/data-loop.c b/src/pipewire/data-loop.c index c9ae1ad77..c06f8cfa2 100644 --- a/src/pipewire/data-loop.c +++ b/src/pipewire/data-loop.c @@ -67,33 +67,39 @@ struct pw_data_loop *pw_data_loop_new(struct pw_properties *properties) int res; this = calloc(1, sizeof(struct pw_data_loop)); - if (this == NULL) - return NULL; + if (this == NULL) { + res = -errno; + goto error_cleanup; + } pw_log_debug("data-loop %p: new", this); this->loop = pw_loop_new(properties); + properties = NULL; if (this->loop == NULL) { - pw_log_debug("data-loop %p: can't create loop: %m", this); res = -errno; - goto no_loop; + pw_log_debug("data-loop %p: can't create loop: %m", this); + goto error_free; } this->event = pw_loop_add_event(this->loop, do_stop, this); if (this->event == NULL) { - pw_log_debug("data-loop %p: can't add event: %m", this); res = -errno; - goto no_event; + pw_log_debug("data-loop %p: can't add event: %m", this); + goto error_loop_destroy; } spa_hook_list_init(&this->listener_list); return this; - no_event: +error_loop_destroy: pw_loop_destroy(this->loop); - no_loop: +error_free: free(this); +error_cleanup: + if (properties) + pw_properties_free(properties); errno = -res; return NULL; } diff --git a/src/pipewire/device.c b/src/pipewire/device.c index e9f7ef351..23c018836 100644 --- a/src/pipewire/device.c +++ b/src/pipewire/device.c @@ -82,8 +82,10 @@ struct pw_device *pw_device_new(struct pw_core *core, int res; impl = calloc(1, sizeof(struct impl) + user_data_size); - if (impl == NULL) - return NULL; + if (impl == NULL) { + res = -errno; + goto error_cleanup; + } this = &impl->this; @@ -91,7 +93,7 @@ struct pw_device *pw_device_new(struct pw_core *core, properties = pw_properties_new(NULL, NULL); if (properties == NULL) { res = -errno; - goto no_mem; + goto error_free; } this->core = core; @@ -111,8 +113,11 @@ struct pw_device *pw_device_new(struct pw_core *core, return this; - no_mem: +error_free: free(impl); +error_cleanup: + if (properties) + pw_properties_free(properties); errno = -res; return NULL; } @@ -296,7 +301,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) - goto no_mem; + goto error_resource; data = pw_resource_get_user_data(resource); data->device = this; @@ -319,8 +324,8 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, return 0; - no_mem: - pw_log_error("can't create device resource"); +error_resource: + pw_log_error("can't create device resource: %m"); return -errno; } diff --git a/src/pipewire/factory.c b/src/pipewire/factory.c index 5426e6e7f..f4c8618d3 100644 --- a/src/pipewire/factory.c +++ b/src/pipewire/factory.c @@ -111,7 +111,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) - goto no_mem; + goto error_resource; data = pw_resource_get_user_data(resource); pw_resource_add_listener(resource, &data->resource_listener, &resource_events, resource); @@ -126,7 +126,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, return 0; - no_mem: +error_resource: pw_log_error("can't create factory resource: %m"); return -errno; } diff --git a/src/pipewire/global.c b/src/pipewire/global.c index 7f794b785..3026d8b2e 100644 --- a/src/pipewire/global.c +++ b/src/pipewire/global.c @@ -89,6 +89,7 @@ pw_global_new(struct pw_core *core, { struct impl *impl; struct pw_global *this; + int res; impl = calloc(1, sizeof(struct impl)); if (impl == NULL) @@ -103,6 +104,11 @@ pw_global_new(struct pw_core *core, this->object = object; this->properties = properties; this->id = pw_map_insert_new(&core->globals, this); + if (this->id == SPA_ID_INVALID) { + res = -errno; + pw_log_error("global %p: can't allocate new id: %m", this); + goto error_clean; + } spa_list_init(&this->child_list); spa_list_init(&this->resource_list); @@ -113,6 +119,11 @@ pw_global_new(struct pw_core *core, this->id); return this; + +error_clean: + free(impl); + errno = -res; + return NULL; } /** register a global to the core registry diff --git a/src/pipewire/link.c b/src/pipewire/link.c index ec979e004..87d6ea342 100644 --- a/src/pipewire/link.c +++ b/src/pipewire/link.c @@ -1050,7 +1050,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) - goto no_mem; + goto error_resource; data = pw_resource_get_user_data(resource); pw_resource_add_listener(resource, &data->resource_listener, &resource_events, resource); @@ -1065,8 +1065,8 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, return 0; - no_mem: - pw_log_error("can't create link resource"); +error_resource: + pw_log_error("can't create link resource: %m"); return -errno; } diff --git a/src/pipewire/module.c b/src/pipewire/module.c index f2673134d..9d685375c 100644 --- a/src/pipewire/module.c +++ b/src/pipewire/module.c @@ -129,7 +129,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) - goto no_mem; + goto error_resource; data = pw_resource_get_user_data(resource); pw_resource_add_listener(resource, &data->resource_listener, &resource_events, resource); @@ -144,8 +144,8 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, return 0; - no_mem: - pw_log_error("can't create module resource"); +error_resource: + pw_log_error("can't create module resource: %m"); return -errno; } diff --git a/src/pipewire/node.c b/src/pipewire/node.c index 17622d070..b761ec784 100644 --- a/src/pipewire/node.c +++ b/src/pipewire/node.c @@ -438,7 +438,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) - goto no_mem; + goto error_resource; data = pw_resource_get_user_data(resource); data->node = this; @@ -460,8 +460,8 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, this->info.change_mask = 0; return 0; - no_mem: - pw_log_error("can't create node resource"); +error_resource: + pw_log_error("can't create node resource: %m"); return -errno; } @@ -1336,6 +1336,7 @@ uint32_t pw_node_get_free_port_id(struct pw_node *node, enum pw_direction direct uint32_t n_ports, max_ports; struct pw_map *portmap; uint32_t port_id; + int res; if (direction == PW_DIRECTION_INPUT) { max_ports = node->info.max_input_ports; @@ -1349,19 +1350,24 @@ uint32_t pw_node_get_free_port_id(struct pw_node *node, enum pw_direction direct pw_log_debug("node %p: direction %s n_ports:%u max_ports:%u", node, pw_direction_as_string(direction), n_ports, max_ports); - if (n_ports >= max_ports) - goto no_mem; + if (n_ports >= max_ports) { + res = -ENOSPC; + goto error; + } port_id = pw_map_insert_new(portmap, NULL); - if (port_id == SPA_ID_INVALID) - goto no_mem; + if (port_id == SPA_ID_INVALID) { + res = -errno; + goto error; + } pw_log_debug("node %p: free port %d", node, port_id); return port_id; - no_mem: - pw_log_warn("no more port available"); +error: + pw_log_warn("node %p: no more port available: %s", node, spa_strerror(res)); + errno = -res; return SPA_ID_INVALID; } diff --git a/src/pipewire/port.c b/src/pipewire/port.c index b14794aa6..07bcd429f 100644 --- a/src/pipewire/port.c +++ b/src/pipewire/port.c @@ -169,6 +169,8 @@ int pw_port_init_mix(struct pw_port *port, struct pw_port_mix *mix) int res = 0; port_id = pw_map_insert_new(&port->mix_port_map, mix); + if (port_id == SPA_ID_INVALID) + return -errno; mix->port.direction = port->direction; mix->port.port_id = port_id; @@ -602,7 +604,7 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, resource = pw_resource_new(client, id, permissions, global->type, version, sizeof(*data)); if (resource == NULL) { res = -errno; - goto no_mem; + goto error_resource; } data = pw_resource_get_user_data(resource); @@ -625,8 +627,8 @@ global_bind(void *_data, struct pw_client *client, uint32_t permissions, this->info.change_mask = 0; return 0; - no_mem: - pw_log_error("can't create port resource: %s", spa_strerror(res)); +error_resource: + pw_log_error("can't create port resource: %m"); return res; } @@ -674,6 +676,7 @@ int pw_port_add(struct pw_port *port, struct pw_node *node) struct pw_port *find; bool control; const char *str, *dir; + int res; if (port->node != NULL) return -EEXIST; @@ -690,6 +693,9 @@ int pw_port_add(struct pw_port *port, struct pw_node *node) if (find != NULL) return -EEXIST; + if ((res = pw_map_insert_at(portmap, port_id, port)) < 0) + return res; + port->node = node; pw_node_emit_port_init(node, port); @@ -734,7 +740,6 @@ int pw_port_add(struct pw_port *port, struct pw_node *node) pw_log_debug("port %p: %d add to node %p", port, port_id, node); spa_list_append(ports, &port->link); - pw_map_insert_at(portmap, port_id, port); if (port->direction == PW_DIRECTION_INPUT) { node->info.n_input_ports++; diff --git a/src/pipewire/properties.c b/src/pipewire/properties.c index c3a60e91c..09fa5e479 100644 --- a/src/pipewire/properties.c +++ b/src/pipewire/properties.c @@ -43,6 +43,9 @@ static int add_func(struct pw_properties *this, char *key, char *value) struct properties *impl = SPA_CONTAINER_OF(this, struct properties, this); item = pw_array_add(&impl->items, sizeof(struct spa_dict_item)); + if (item == NULL) + return -errno; + item->key = key; item->value = value; diff --git a/src/pipewire/proxy.c b/src/pipewire/proxy.c index 1edb74fef..0d4471b92 100644 --- a/src/pipewire/proxy.c +++ b/src/pipewire/proxy.c @@ -60,6 +60,7 @@ struct pw_proxy *pw_proxy_new(struct pw_proxy *factory, struct proxy *impl; struct pw_proxy *this; struct pw_remote *remote = factory->remote; + int res; impl = calloc(1, sizeof(struct proxy) + user_data_size); if (impl == NULL) @@ -68,16 +69,26 @@ struct pw_proxy *pw_proxy_new(struct pw_proxy *factory, this = &impl->this; this->remote = remote; + this->marshal = pw_protocol_get_marshal(remote->conn->protocol, type); + if (this->marshal == NULL) { + pw_log_error("proxy %p: no marshal for type %d", this, type); + res = -EPROTO; + goto error_clean; + } + + this->id = pw_map_insert_new(&remote->objects, this); + if (this->id == SPA_ID_INVALID) { + res = -errno; + pw_log_error("proxy %p: can't allocate new id: %m", this); + goto error_clean; + } + spa_hook_list_init(&this->listener_list); spa_hook_list_init(&this->object_listener_list); - this->id = pw_map_insert_new(&remote->objects, this); - if (user_data_size > 0) this->user_data = SPA_MEMBER(impl, sizeof(struct proxy), void); - this->marshal = pw_protocol_get_marshal(remote->conn->protocol, type); - this->impl = SPA_INTERFACE_INIT( type, this->marshal->version, @@ -91,6 +102,11 @@ struct pw_proxy *pw_proxy_new(struct pw_proxy *factory, remote, this->marshal); return this; + +error_clean: + free(impl); + errno = -res; + return NULL; } SPA_EXPORT diff --git a/src/pipewire/remote.c b/src/pipewire/remote.c index 3a47ebd0d..d8871781d 100644 --- a/src/pipewire/remote.c +++ b/src/pipewire/remote.c @@ -327,14 +327,14 @@ static int do_connect(struct pw_remote *remote) PW_TYPE_INTERFACE_Core, 0); if (remote->core_proxy == NULL) { res = -errno; - goto no_proxy; + goto error_disconnect; } remote->client_proxy = (struct pw_client_proxy*)pw_proxy_new(&dummy, PW_TYPE_INTERFACE_Client, 0); if (remote->client_proxy == NULL) { res = -errno; - goto clean_core_proxy; + goto error_clean_core_proxy; } pw_core_proxy_add_listener(remote->core_proxy, &impl->core_listener, &core_proxy_events, remote); @@ -345,12 +345,12 @@ static int do_connect(struct pw_remote *remote) return 0; - clean_core_proxy: +error_clean_core_proxy: ((struct pw_proxy*)remote->core_proxy)->removed = true; pw_proxy_destroy((struct pw_proxy*)remote->core_proxy); - no_proxy: +error_disconnect: pw_protocol_client_disconnect(remote->conn); - pw_remote_update_state(remote, PW_REMOTE_STATE_ERROR, "can't connect: no memory"); + pw_remote_update_state(remote, PW_REMOTE_STATE_ERROR, "can't connect: %s", spa_strerror(res)); return res; } @@ -421,8 +421,8 @@ int pw_remote_steal_fd(struct pw_remote *remote) int fd; fd = pw_protocol_client_steal_fd(remote->conn); - pw_remote_update_state(remote, PW_REMOTE_STATE_UNCONNECTED, NULL); - + if (fd >= 0) + pw_remote_update_state(remote, PW_REMOTE_STATE_UNCONNECTED, NULL); return fd; } diff --git a/src/pipewire/resource.c b/src/pipewire/resource.c index ddb3b5f37..f39824c8c 100644 --- a/src/pipewire/resource.c +++ b/src/pipewire/resource.c @@ -48,6 +48,7 @@ struct pw_resource *pw_resource_new(struct pw_client *client, { struct impl *impl; struct pw_resource *this; + int res; impl = calloc(1, sizeof(struct impl) + user_data_size); if (impl == NULL) @@ -63,18 +64,30 @@ struct pw_resource *pw_resource_new(struct pw_client *client, spa_hook_list_init(&this->listener_list); spa_hook_list_init(&this->object_listener_list); + this->marshal = pw_protocol_get_marshal(client->protocol, type); + if (this->marshal == NULL) { + pw_log_error("resource %p: no marshal for type %d", this, type); + res = -EPROTO; + goto error_clean; + } + if (id == SPA_ID_INVALID) { - id = pw_map_insert_new(&client->objects, this); - } else if (pw_map_insert_at(&client->objects, id, this) < 0) - goto in_use; + res = -EINVAL; + goto error_clean; + } + + if ((res = pw_map_insert_at(&client->objects, id, this)) < 0) { + res = -errno; + pw_log_error("resource %p: can't add id %u for client %p: %m", + this, id, client); + goto error_clean; + } this->id = id; if (user_data_size > 0) this->user_data = SPA_MEMBER(impl, sizeof(struct impl), void); - this->marshal = pw_protocol_get_marshal(client->protocol, type); - this->impl = SPA_INTERFACE_INIT( type, this->marshal->version, @@ -88,10 +101,9 @@ struct pw_resource *pw_resource_new(struct pw_client *client, return this; - in_use: - pw_log_debug("resource %p: id %u in use for client %p", this, id, client); +error_clean: free(impl); - errno = -EEXIST; + errno = -res; return NULL; }