system: make system functions return error on error

Return -errno from system functions instead of -1 in errors. This
makes it easier to pass along the result without having to go to
errno etc..
This commit is contained in:
Wim Taymans 2019-06-20 17:31:29 +02:00
parent 03eeb945f3
commit 5b7e95c71c
9 changed files with 154 additions and 157 deletions

View file

@ -34,12 +34,11 @@ static int spa_alsa_open(struct state *state)
SND_PCM_NO_AUTO_RESAMPLE |
SND_PCM_NO_AUTO_CHANNELS | SND_PCM_NO_AUTO_FORMAT), "open failed");
state->timerfd = spa_system_timerfd_create(state->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
if (state->timerfd == -1) {
err = -errno;
goto err_close;
}
if ((err = spa_system_timerfd_create(state->data_system,
CLOCK_MONOTONIC, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_exit_close;
state->timerfd = err;
state->opened = true;
state->sample_count = 0;
@ -47,7 +46,7 @@ static int spa_alsa_open(struct state *state)
return 0;
err_close:
error_exit_close:
snd_pcm_close(state->hndl);
return err;
}
@ -1010,7 +1009,7 @@ static void alsa_on_timeout_event(struct spa_source *source)
int res;
if (state->started && spa_system_timerfd_read(state->data_system, state->timerfd, &expire) < 0)
spa_log_warn(state->log, "error reading timerfd: %s", strerror(errno));
spa_log_warn(state->log, "error reading timerfd: %m");
if (state->position)
state->threshold = state->position->size;

View file

@ -237,11 +237,12 @@ impl_init(const struct spa_handle_factory *factory,
this->source.fd = spa_system_eventfd_create(this->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
this->source.mask = SPA_IO_IN;
this->source.rmask = 0;
if (this->source.fd != -1) {
if (this->source.fd < 0) {
fprintf(stderr, "Warning: failed to create eventfd: %m");
} else {
spa_loop_add_source(loop, &this->source);
this->have_source = true;
} else {
fprintf(stderr, "Warning: failed to create eventfd: %m");
}
}

View file

@ -102,34 +102,20 @@ struct source_impl {
static int loop_add_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
source->loop = &impl->loop;
if (SPA_UNLIKELY(source->fd == -1))
return 0;
return spa_system_pollfd_add(impl->system, impl->poll_fd, source->fd, source->mask, source);
}
static int loop_update_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
if (SPA_UNLIKELY(source->fd == -1))
return 0;
return spa_system_pollfd_mod(impl->system, impl->poll_fd, source->fd, source->mask, source);
}
static int loop_remove_source(void *object, struct spa_source *source)
{
struct impl *impl = object;
source->loop = NULL;
if (SPA_UNLIKELY(source->fd == -1))
return 0;
return spa_system_pollfd_del(impl->system, impl->poll_fd, source->fd);
}
@ -280,18 +266,16 @@ static int loop_iterate(void *object, int timeout)
struct impl *impl = object;
struct spa_loop *loop = &impl->loop;
struct spa_poll_event ep[32];
int i, nfds, res = 0;
int i, nfds;
spa_loop_control_hook_before(&impl->hooks_list);
nfds = spa_system_pollfd_wait(impl->system, impl->poll_fd, ep, SPA_N_ELEMENTS(ep), timeout);
if (SPA_UNLIKELY(nfds < 0))
res = -errno;
spa_loop_control_hook_after(&impl->hooks_list);
if (SPA_UNLIKELY(nfds < 0))
return -res;
return nfds;
/* first we set all the rmasks, then call the callbacks. The reason is that
* some callback might also want to look at other sources it manages and
@ -323,11 +307,11 @@ static struct spa_source *loop_add_io(void *object,
{
struct impl *impl = object;
struct source_impl *source;
struct spa_source *res = NULL;
int res;
source = calloc(1, sizeof(struct source_impl));
if (source == NULL)
goto out;
goto error_exit;
source->source.loop = &impl->loop;
source->source.func = source_io_func;
@ -338,13 +322,18 @@ static struct spa_source *loop_add_io(void *object,
source->close = close;
source->func.io = func;
loop_add_source(impl, &source->source);
if ((res = loop_add_source(impl, &source->source)) < 0)
goto error_exit_free;
spa_list_insert(&impl->source_list, &source->link);
res = &source->source;
out:
return res;
return &source->source;
error_exit_free:
free(source);
errno = -res;
error_exit:
return NULL;
}
static int loop_update_io(void *object, struct spa_source *source, uint32_t mask)
@ -384,42 +373,41 @@ static struct spa_source *loop_add_idle(void *object,
{
struct impl *impl = object;
struct source_impl *source;
struct spa_source *res = NULL;
int err;
int res;
source = calloc(1, sizeof(struct source_impl));
if (source == NULL)
goto out;
goto error_exit;
if ((res = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_exit_free;
source->source.loop = &impl->loop;
source->source.func = source_idle_func;
source->source.data = data;
source->source.fd = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
if (source->source.fd == -1) {
err = -errno;
goto err_free;
}
source->source.fd = res;
source->impl = impl;
source->close = true;
source->source.mask = SPA_IO_IN;
source->func.idle = func;
loop_add_source(impl, &source->source);
if ((res = loop_add_source(impl, &source->source)) < 0)
goto error_exit_close;
spa_list_insert(&impl->source_list, &source->link);
if (enabled)
loop_enable_idle(impl, &source->source, true);
res = &source->source;
out:
return res;
return &source->source;
err_free:
error_exit_close:
spa_system_close(impl->system, source->source.fd);
error_exit_free:
free(source);
errno = -err;
goto out;
errno = -res;
error_exit:
return NULL;
}
static void source_event_func(struct spa_source *source)
@ -440,39 +428,38 @@ static struct spa_source *loop_add_event(void *object,
{
struct impl *impl = object;
struct source_impl *source;
struct spa_source *res = NULL;
int err;
int res;
source = calloc(1, sizeof(struct source_impl));
if (source == NULL)
goto out;
goto error_exit;
if ((res = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_exit_free;
source->source.loop = &impl->loop;
source->source.func = source_event_func;
source->source.data = data;
source->source.fd = spa_system_eventfd_create(impl->system, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
if (source->source.fd == -1) {
err = -errno;
goto err_free;
}
source->source.fd = res;
source->source.mask = SPA_IO_IN;
source->impl = impl;
source->close = true;
source->func.event = func;
loop_add_source(impl, &source->source);
if ((res = loop_add_source(impl, &source->source)) < 0)
goto error_exit_close;
spa_list_insert(&impl->source_list, &source->link);
res = &source->source;
return &source->source;
out:
return res;
err_free:
error_exit_close:
spa_system_close(impl->system, source->source.fd);
error_exit_free:
free(source);
errno = -err;
goto out;
errno = -res;
error_exit:
return NULL;
}
static int loop_signal_event(void *object, struct spa_source *source)
@ -505,39 +492,39 @@ static struct spa_source *loop_add_timer(void *object,
{
struct impl *impl = object;
struct source_impl *source;
struct spa_source *res = NULL;
int err;
int res;
source = calloc(1, sizeof(struct source_impl));
if (source == NULL)
goto out;
goto error_exit;
if ((res = spa_system_timerfd_create(impl->system, CLOCK_MONOTONIC,
SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_exit_free;
source->source.loop = &impl->loop;
source->source.func = source_timer_func;
source->source.data = data;
source->source.fd = spa_system_timerfd_create(impl->system, CLOCK_MONOTONIC,
SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
if (source->source.fd == -1) {
err = -errno;
goto err_free;
}
source->source.fd = res;
source->source.mask = SPA_IO_IN;
source->impl = impl;
source->close = true;
source->func.timer = func;
loop_add_source(impl, &source->source);
if ((res = loop_add_source(impl, &source->source)) < 0)
goto error_exit_close;
spa_list_insert(&impl->source_list, &source->link);
res = &source->source;
out:
return res;
return &source->source;
err_free:
error_exit_close:
spa_system_close(impl->system, source->source.fd);
error_exit_free:
free(source);
errno = -err;
goto out;
errno = -res;
error_exit:
return NULL;
}
static int
@ -546,7 +533,7 @@ loop_update_timer(void *object, struct spa_source *source,
{
struct impl *impl = object;
struct itimerspec its;
int flags = 0;
int flags = 0, res;
spa_zero(its);
if (value) {
@ -560,8 +547,8 @@ loop_update_timer(void *object, struct spa_source *source,
if (absolute)
flags |= SPA_FD_TIMER_ABSTIME;
if (spa_system_timerfd_settime(impl->system, source->fd, flags, &its, NULL) < 0)
return errno;
if ((res = spa_system_timerfd_settime(impl->system, source->fd, flags, &its, NULL)) < 0)
return res;
return 0;
}
@ -584,41 +571,39 @@ static struct spa_source *loop_add_signal(void *object,
{
struct impl *impl = object;
struct source_impl *source;
struct spa_source *res = NULL;
int err;
int res;
source = calloc(1, sizeof(struct source_impl));
if (source == NULL)
goto out;
goto error_exit;
if ((res = spa_system_signalfd_create(impl->system,
signal_number, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK)) < 0)
goto error_exit_free;
source->source.loop = &impl->loop;
source->source.func = source_signal_func;
source->source.data = data;
source->source.fd = spa_system_signalfd_create(impl->system,
signal_number, SPA_FD_CLOEXEC | SPA_FD_NONBLOCK);
if (source->source.fd == -1) {
err = -errno;
goto err_free;
}
source->source.fd = res;
source->source.mask = SPA_IO_IN;
source->impl = impl;
source->close = true;
source->func.signal = func;
loop_add_source(impl, &source->source);
if ((res = loop_add_source(impl, &source->source)) < 0)
goto error_exit_close;
spa_list_insert(&impl->source_list, &source->link);
res = &source->source;
out:
return res;
return &source->source;
err_free:
error_exit_close:
spa_system_close(impl->system, source->source.fd);
error_exit_free:
free(source);
errno = -err;
goto out;
errno = -res;
error_exit:
return NULL;
}
static void loop_destroy_source(void *object, struct spa_source *source)
@ -764,15 +749,15 @@ impl_init(const struct spa_handle_factory *factory,
if (impl->system == NULL) {
spa_log_error(impl->log, NAME " %p: a System is needed", impl);
res = -EINVAL;
goto err;
goto error_exit;
}
impl->poll_fd = spa_system_pollfd_create(impl->system, SPA_FD_CLOEXEC);
if (impl->poll_fd < 0) {
res = -errno;
spa_log_error(impl->log, NAME " %p: can't create pollfd: %m", impl);
goto err;
if ((res = spa_system_pollfd_create(impl->system, SPA_FD_CLOEXEC)) < 0) {
spa_log_error(impl->log, NAME " %p: can't create pollfd: %s",
impl, spa_strerror(res));
goto error_exit;
}
impl->poll_fd = res;
spa_list_init(&impl->source_list);
spa_list_init(&impl->destroy_list);
@ -784,24 +769,25 @@ impl_init(const struct spa_handle_factory *factory,
if (impl->wakeup == NULL) {
res = -errno;
spa_log_error(impl->log, NAME " %p: can't create wakeup event: %m", impl);
goto err_free_poll;
goto error_exit_free_poll;
}
impl->ack_fd = spa_system_eventfd_create(impl->system,
SPA_FD_EVENT_SEMAPHORE | SPA_FD_CLOEXEC);
if (impl->ack_fd < 0) {
res = -errno;
spa_log_error(impl->log, NAME " %p: can't create ack event: %m", impl);
goto err_free_wakeup;
if ((res = spa_system_eventfd_create(impl->system,
SPA_FD_EVENT_SEMAPHORE | SPA_FD_CLOEXEC)) < 0) {
spa_log_error(impl->log, NAME " %p: can't create ack event: %s",
impl, spa_strerror(res));
goto error_exit_free_wakeup;
}
impl->ack_fd = res;
spa_log_debug(impl->log, NAME " %p: initialized", impl);
return 0;
err_free_wakeup:
error_exit_free_wakeup:
loop_destroy_source(impl, impl->wakeup);
err_free_poll:
error_exit_free_poll:
spa_system_close(impl->system, impl->poll_fd);
err:
error_exit:
return res;
}

View file

@ -49,12 +49,14 @@ struct impl {
static ssize_t impl_read(void *object, int fd, void *buf, size_t count)
{
return read(fd, buf, count);
ssize_t res = read(fd, buf, count);
return res < 0 ? -errno : res;
}
static ssize_t impl_write(void *object, int fd, const void *buf, size_t count)
{
return write(fd, buf, count);
ssize_t res = write(fd, buf, count);
return res < 0 ? -errno : res;
}
static int impl_ioctl(void *object, int fd, unsigned long request, ...)
@ -68,25 +70,28 @@ static int impl_ioctl(void *object, int fd, unsigned long request, ...)
res = ioctl(fd, request, arg);
va_end(ap);
return res;
return res < 0 ? -errno : res;
}
static int impl_close(void *object, int fd)
{
return close(fd);
int res = close(fd);
return res < 0 ? -errno : res;
}
/* clock */
static int impl_clock_gettime(void *object,
int clockid, struct timespec *value)
{
return clock_gettime(clockid, value);
int res = clock_gettime(clockid, value);
return res < 0 ? -errno : res;
}
static int impl_clock_getres(void *object,
int clockid, struct timespec *res)
{
return clock_getres(clockid, res);
int r = clock_getres(clockid, res);
return r < 0 ? -errno : r;
}
/* poll */
@ -124,37 +129,43 @@ static inline uint32_t spa_epoll_to_io(uint32_t events)
static int impl_pollfd_create(void *object, int flags)
{
int fl = 0;
int fl = 0, res;
if (flags & SPA_FD_CLOEXEC)
fl |= EPOLL_CLOEXEC;
return epoll_create1(fl);
res = epoll_create1(fl);
return res < 0 ? -errno : res;
}
static int impl_pollfd_add(void *object, int pfd, int fd, uint32_t events, void *data)
{
struct epoll_event ep;
int res;
spa_zero(ep);
ep.events = spa_io_to_epoll(events);
ep.data.ptr = data;
return epoll_ctl(pfd, EPOLL_CTL_ADD, fd, &ep);
res = epoll_ctl(pfd, EPOLL_CTL_ADD, fd, &ep);
return res < 0 ? -errno : res;
}
static int impl_pollfd_mod(void *object, int pfd, int fd, uint32_t events, void *data)
{
struct epoll_event ep;
int res;
spa_zero(ep);
ep.events = spa_io_to_epoll(events);
ep.data.ptr = data;
return epoll_ctl(pfd, EPOLL_CTL_MOD, fd, &ep);
res = epoll_ctl(pfd, EPOLL_CTL_MOD, fd, &ep);
return res < 0 ? -errno : res;
}
static int impl_pollfd_del(void *object, int pfd, int fd)
{
return epoll_ctl(pfd, EPOLL_CTL_DEL, fd, NULL);
int res = epoll_ctl(pfd, EPOLL_CTL_DEL, fd, NULL);
return res < 0 ? -errno : res;
}
static int impl_pollfd_wait(void *object, int pfd,
@ -164,7 +175,7 @@ static int impl_pollfd_wait(void *object, int pfd,
int i, nfds;
if (SPA_UNLIKELY((nfds = epoll_wait(pfd, ep, SPA_N_ELEMENTS(ep), timeout)) < 0))
return nfds;
return -errno;
for (i = 0; i < nfds; i++) {
ev[i].events = spa_epoll_to_io(ep[i].events);
@ -176,12 +187,13 @@ static int impl_pollfd_wait(void *object, int pfd,
/* timers */
static int impl_timerfd_create(void *object, int clockid, int flags)
{
int fl = 0;
int fl = 0, res;
if (flags & SPA_FD_CLOEXEC)
fl |= TFD_CLOEXEC;
if (flags & SPA_FD_NONBLOCK)
fl |= TFD_NONBLOCK;
return timerfd_create(clockid, fl);
res = timerfd_create(clockid, fl);
return res < 0 ? -errno : res;
}
static int impl_timerfd_settime(void *object,
@ -189,18 +201,20 @@ static int impl_timerfd_settime(void *object,
const struct itimerspec *new_value,
struct itimerspec *old_value)
{
int fl = 0;
int fl = 0, res;
if (flags & SPA_FD_TIMER_ABSTIME)
fl |= TFD_TIMER_ABSTIME;
if (flags & SPA_FD_TIMER_CANCEL_ON_SET)
fl |= TFD_TIMER_CANCEL_ON_SET;
return timerfd_settime(fd, fl, new_value, old_value);
res = timerfd_settime(fd, fl, new_value, old_value);
return res < 0 ? -errno : res;
}
static int impl_timerfd_gettime(void *object,
int fd, struct itimerspec *curr_value)
{
return timerfd_gettime(fd, curr_value);
int res = timerfd_gettime(fd, curr_value);
return res < 0 ? -errno : res;
}
static int impl_timerfd_read(void *object, int fd, uint64_t *expirations)
@ -213,14 +227,15 @@ static int impl_timerfd_read(void *object, int fd, uint64_t *expirations)
/* events */
static int impl_eventfd_create(void *object, int flags)
{
int fl = 0;
int fl = 0, res;
if (flags & SPA_FD_CLOEXEC)
fl |= EFD_CLOEXEC;
if (flags & SPA_FD_NONBLOCK)
fl |= EFD_NONBLOCK;
if (flags & SPA_FD_EVENT_SEMAPHORE)
fl |= EFD_SEMAPHORE;
return eventfd(0, fl);
res = eventfd(0, fl);
return res < 0 ? -errno : res;
}
static int impl_eventfd_write(void *object, int fd, uint64_t count)
@ -253,7 +268,7 @@ static int impl_signalfd_create(void *object, int signal, int flags)
res = signalfd(-1, &mask, fl);
sigprocmask(SIG_BLOCK, &mask, NULL);
return res;
return res < 0 ? -errno : res;
}
static int impl_signalfd_read(void *object, int fd, int *signal)