static
int notify_thread_pipe(int wpipe)
{
- int ret;
+ ssize_t ret;
- do {
- ret = write(wpipe, "!", 1);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != 1) {
+ ret = lttng_write(wpipe, "!", 1);
+ if (ret < 1) {
PERROR("write poll pipe");
}
- return ret;
+ return (int) ret;
}
/*
static
void *thread_dispatcher(void *data)
{
- int ret, err = -1;
+ int err = -1;
+ ssize_t ret;
struct cds_wfq_node *node;
struct relay_command *relay_cmd = NULL;
* so we can be assured that the data will be read at some point in
* time or wait to the end of the world :)
*/
- do {
- ret = write(live_relay_cmd_pipe[1], relay_cmd,
- sizeof(*relay_cmd));
- } while (ret < 0 && errno == EINTR);
+ ret = lttng_write(live_relay_cmd_pipe[1], relay_cmd,
+ sizeof(*relay_cmd));
free(relay_cmd);
- if (ret < 0 || ret != sizeof(struct relay_command)) {
+ if (ret < sizeof(struct relay_command)) {
PERROR("write cmd pipe");
goto error;
}
stream->index_read_fd = ret;
DBG("Opening index file %s in read only, (fd: %d)", fullpath, ret);
- do {
- health_code_update();
- ret = read(stream->index_read_fd, &hdr, sizeof(hdr));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0) {
+ ret = lttng_read(stream->index_read_fd, &hdr, sizeof(hdr));
+ if (ret < sizeof(hdr)) {
PERROR("Reading index header");
goto error;
}
viewer_index.flags |= LTTNG_VIEWER_FLAG_NEW_METADATA;
}
- do {
- health_code_update();
- ret = read(vstream->index_read_fd, &packet_index,
- sizeof(packet_index));
- } while (ret < 0 && errno == EINTR);
+ ret = lttng_read(vstream->index_read_fd, &packet_index,
+ sizeof(packet_index));
if (ret < sizeof(packet_index)) {
PERROR("Relay reading index file");
viewer_index.status = htobe32(VIEWER_INDEX_ERR);
PERROR("lseek");
goto error;
}
- read_len = read(stream->read_fd, data, len);
- if (read_len < (ssize_t) len) {
+ read_len = lttng_read(stream->read_fd, data, len);
+ if (read_len < len) {
PERROR("Relay reading trace file, fd: %d, offset: %" PRIu64,
stream->read_fd, be64toh(get_packet_info.offset));
goto error;
goto error;
}
- read_len = read(stream->read_fd, data, len);
- if (read_len < (ssize_t) len) {
+ read_len = lttng_read(stream->read_fd, data, len);
+ if (read_len < len) {
PERROR("Relay reading metadata file");
goto error;
}
goto error;
}
- do {
- health_code_update();
- ret = read(fd, relay_connection, sizeof(*relay_connection));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret < sizeof(*relay_connection)) {
+ ret = lttng_read(fd, relay_connection, sizeof(*relay_connection));
+ if (ret < sizeof(*relay_connection)) {
PERROR("read relay cmd pipe");
goto error_read;
}
static
int notify_thread_pipe(int wpipe)
{
- int ret;
+ ssize_t ret;
- do {
- ret = write(wpipe, "!", 1);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != 1) {
+ ret = lttng_write(wpipe, "!", 1);
+ if (ret < 1) {
PERROR("write poll pipe");
}
static void notify_health_quit_pipe(int *pipe)
{
- int ret;
+ ssize_t ret;
- do {
- ret = write(pipe[1], "4", 1);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != 1) {
+ ret = lttng_write(pipe[1], "4", 1);
+ if (ret < 1) {
PERROR("write relay health quit");
}
}
static
void *relay_thread_dispatcher(void *data)
{
- int ret, err = -1;
+ int err = -1;
+ ssize_t ret;
struct cds_wfq_node *node;
struct relay_command *relay_cmd = NULL;
* call is blocking so we can be assured that the data will be read
* at some point in time or wait to the end of the world :)
*/
- do {
- ret = write(relay_cmd_pipe[1], relay_cmd,
- sizeof(struct relay_command));
- } while (ret < 0 && errno == EINTR);
+ ret = lttng_write(relay_cmd_pipe[1], relay_cmd,
+ sizeof(struct relay_command));
free(relay_cmd);
- if (ret < 0 || ret != sizeof(struct relay_command)) {
+ if (ret < sizeof(struct relay_command)) {
PERROR("write cmd pipe");
goto error;
}
*/
static int write_padding_to_file(int fd, uint32_t size)
{
- int ret = 0;
+ ssize_t ret = 0;
char *zeros;
if (size == 0) {
goto end;
}
- do {
- ret = write(fd, zeros, size);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != size) {
+ ret = lttng_write(fd, zeros, size);
+ if (ret < size) {
PERROR("write padding to file");
}
struct relay_command *cmd)
{
int ret = htobe32(LTTNG_OK);
+ ssize_t size_ret;
struct relay_session *session = cmd->session;
struct lttcomm_relayd_metadata_payload *metadata_struct;
struct relay_stream *metadata_stream;
goto end_unlock;
}
- do {
- ret = write(metadata_stream->fd, metadata_struct->payload,
- payload_size);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != payload_size) {
+ size_ret = lttng_write(metadata_stream->fd, metadata_struct->payload,
+ payload_size);
+ if (size_ret < payload_size) {
ERR("Relay error writing metadata on file");
ret = -1;
goto end_unlock;
int relay_process_data(struct relay_command *cmd)
{
int ret = 0, rotate_index = 0;
+ ssize_t size_ret;
struct relay_stream *stream;
struct lttcomm_relayd_data_hdr data_hdr;
uint64_t stream_id;
}
/* Write data to stream output fd. */
- do {
- ret = write(stream->fd, data_buffer, data_size);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != data_size) {
+ size_ret = lttng_write(stream->fd, data_buffer, data_size);
+ if (size_ret < data_size) {
ERR("Relay error writing data to file");
ret = -1;
goto end_rcu_unlock;
struct lttng_ht *relay_connections_ht)
{
struct relay_command *relay_connection;
- int ret;
+ ssize_t ret;
relay_connection = zmalloc(sizeof(struct relay_command));
if (relay_connection == NULL) {
PERROR("Relay command zmalloc");
goto error;
}
- do {
- ret = read(fd, relay_connection, sizeof(struct relay_command));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret < sizeof(struct relay_command)) {
+ ret = lttng_read(fd, relay_connection, sizeof(struct relay_command));
+ if (ret < sizeof(struct relay_command)) {
PERROR("read relay cmd pipe");
goto error_read;
}
void *thread_ht_cleanup(void *data)
{
int ret, i, pollfd, err = -1;
+ ssize_t size_ret;
uint32_t revents, nb_fd;
struct lttng_poll_event events;
goto error;
}
- do {
- /* Get socket from dispatch thread. */
- ret = read(ht_cleanup_pipe[0], &ht, sizeof(ht));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret < sizeof(ht)) {
+ /* Get socket from dispatch thread. */
+ size_ret = lttng_read(ht_cleanup_pipe[0], &ht,
+ sizeof(ht));
+ if (size_ret < sizeof(ht)) {
PERROR("ht cleanup notify pipe");
goto error;
}
static void *thread_manage_kernel(void *data)
{
int ret, i, pollfd, update_poll_flag = 1, err = -1;
+ ssize_t size_ret;
uint32_t revents, nb_fd;
char tmp;
struct lttng_poll_event events;
/* Check for data on kernel pipe */
if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
- do {
- ret = read(kernel_poll_pipe[0], &tmp, 1);
- } while (ret < 0 && errno == EINTR);
+ (void) lttng_read(kernel_poll_pipe[0],
+ &tmp, 1);
/*
* Ret value is useless here, if this pipe gets any actions an
* update is required anyway.
static void *thread_manage_apps(void *data)
{
int i, ret, pollfd, err = -1;
+ ssize_t size_ret;
uint32_t revents, nb_fd;
struct lttng_poll_event events;
int sock;
/* Empty pipe */
- do {
- ret = read(apps_cmd_pipe[0], &sock, sizeof(sock));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret < sizeof(sock)) {
+ size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
+ if (size_ret < sizeof(sock)) {
PERROR("read apps cmd pipe");
goto error;
}
*/
static int send_socket_to_thread(int fd, int sock)
{
- int ret;
+ ssize_t ret;
/*
* It's possible that the FD is set as invalid with -1 concurrently just
goto error;
}
- do {
- ret = write(fd, &sock, sizeof(sock));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != sizeof(sock)) {
+ ret = lttng_write(fd, &sock, sizeof(sock));
+ if (ret < sizeof(sock)) {
PERROR("write apps pipe %d", fd);
if (ret < 0) {
ret = -errno;
/* All good. Don't send back the write positive ret value. */
ret = 0;
error:
- return ret;
+ return (int) ret;
}
/*
void *ust_thread_manage_notify(void *data)
{
int i, ret, pollfd, err = -1;
+ ssize_t size_ret;
uint32_t revents, nb_fd;
struct lttng_poll_event events;
goto error;
}
- do {
- /* Get socket from dispatch thread. */
- ret = read(apps_cmd_notify_pipe[0], &sock, sizeof(sock));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret < sizeof(sock)) {
+ /* Get socket from dispatch thread. */
+ size_ret = lttng_read(apps_cmd_notify_pipe[0],
+ &sock, sizeof(sock));
+ if (size_ret < sizeof(sock)) {
PERROR("read apps notify pipe");
goto error;
}
*/
int notify_thread_pipe(int wpipe)
{
- int ret;
+ ssize_t ret;
/* Ignore if the pipe is invalid. */
if (wpipe < 0) {
return 0;
}
- do {
- ret = write(wpipe, "!", 1);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0) {
+ ret = lttng_write(wpipe, "!", 1);
+ if (ret < 1) {
PERROR("write poll pipe");
}
- return ret;
+ return (int) ret;
}
void ht_cleanup_push(struct lttng_ht *ht)
{
- int ret;
+ ssize_t ret;
int fd = ht_cleanup_pipe[1];
if (fd < 0)
return;
- do {
- ret = write(fd, &ht, sizeof(ht));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != sizeof(ht)) {
+ ret = lttng_write(fd, &ht, sizeof(ht));
+ if (ret < sizeof(ht)) {
PERROR("write ht cleanup pipe %d", fd);
if (ret < 0) {
ret = -errno;
void compat_epoll_set_max_size(void)
{
int ret, fd;
+ size_t size_ret;
char buf[64];
poll_max_size = DEFAULT_POLL_SIZE;
return;
}
- ret = read(fd, buf, sizeof(buf));
- if (ret < 0) {
+ size_ret = lttng_read(fd, buf, sizeof(buf));
+ /*
+ * Allow reading a file smaller than buf, but keep space for
+ * final \0.
+ */
+ if (size_ret < 0 || size_ret >= sizeof(buf)) {
PERROR("read set max size");
goto error;
}
+ buf[size_ret] = '\0';
poll_max_size = atoi(buf);
if (poll_max_size == 0) {
unsigned int offset, unsigned int len, char *data)
{
int ret = 0;
+ int size_ret;
struct consumer_metadata_cache *cache;
assert(channel);
cache->contiguous = cache->max_offset;
if (channel->monitor) {
- ret = write(channel->metadata_stream->ust_metadata_poll_pipe[1],
+ size_ret = lttng_write(channel->metadata_stream->ust_metadata_poll_pipe[1],
&dummy, 1);
- if (ret < 1) {
+ if (size_ret < 1) {
ERR("Wakeup UST metadata pipe");
+ ret = -1;
goto end;
}
}
ret = relayd_send_index(&relayd->control_sock, index,
stream->relayd_stream_id, stream->next_net_seq_num - 1);
} else {
- ret = index_write(stream->index_fd, index,
+ ssize_t size_ret;
+
+ size_ret = index_write(stream->index_fd, index,
sizeof(struct lttng_packet_index));
+ if (size_ret < sizeof(struct lttng_packet_index)) {
+ ret = -1;
+ } else {
+ ret = 0;
+ }
}
if (ret < 0) {
goto error;
static void notify_health_quit_pipe(int *pipe)
{
- int ret;
+ ssize_t ret;
- do {
- ret = write(pipe[1], "4", 1);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != 1) {
+ ret = lttng_write(pipe[1], "4", 1);
+ if (ret < 1) {
PERROR("write consumer health quit");
}
}
enum consumer_channel_action action)
{
struct consumer_channel_msg msg;
- int ret;
+ ssize_t ret;
memset(&msg, 0, sizeof(msg));
msg.action = action;
msg.chan = chan;
msg.key = key;
- do {
- ret = write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
- } while (ret < 0 && errno == EINTR);
+ ret = lttng_write(ctx->consumer_channel_pipe[1], &msg, sizeof(msg));
+ if (ret < sizeof(msg)) {
+ PERROR("notify_channel_pipe write error");
+ }
}
void notify_thread_del_channel(struct lttng_consumer_local_data *ctx,
enum consumer_channel_action *action)
{
struct consumer_channel_msg msg;
- int ret;
+ ssize_t ret;
- do {
- ret = read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
- } while (ret < 0 && errno == EINTR);
- if (ret > 0) {
- *action = msg.action;
- *chan = msg.chan;
- *key = msg.key;
+ ret = lttng_read(ctx->consumer_channel_pipe[0], &msg, sizeof(msg));
+ if (ret < sizeof(msg)) {
+ ret = -1;
+ goto error;
}
- return ret;
+ *action = msg.action;
+ *chan = msg.chan;
+ *key = msg.key;
+error:
+ return (int) ret;
}
/*
*/
void lttng_consumer_should_exit(struct lttng_consumer_local_data *ctx)
{
- int ret;
+ ssize_t ret;
+
consumer_quit = 1;
- do {
- ret = write(ctx->consumer_should_quit[1], "4", 1);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != 1) {
+ ret = lttng_write(ctx->consumer_should_quit[1], "4", 1);
+ if (ret < 1) {
PERROR("write consumer quit");
}
struct lttng_consumer_stream *stream,
struct consumer_relayd_sock_pair *relayd, unsigned long padding)
{
- int ret;
+ ssize_t ret;
struct lttcomm_relayd_metadata_payload hdr;
hdr.stream_id = htobe64(stream->relayd_stream_id);
hdr.padding_size = htobe32(padding);
- do {
- ret = write(fd, (void *) &hdr, sizeof(hdr));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0 || ret != sizeof(hdr)) {
+ ret = lttng_write(fd, (void *) &hdr, sizeof(hdr));
+ if (ret < sizeof(hdr)) {
/*
* This error means that the fd's end is closed so ignore the perror
* not to clubber the error output since this can happen in a normal
stream->relayd_stream_id, padding);
end:
- return ret;
+ return (int) ret;
}
/*
}
while (len > 0) {
- do {
- ret = write(outfd, mmap_base + mmap_offset, len);
- } while (ret < 0 && errno == EINTR);
+ ret = lttng_write(outfd, mmap_base + mmap_offset, len);
DBG("Consumer mmap write() ret %zd (len %lu)", ret, len);
- if (ret < 0) {
+ if (ret < len) {
/*
* This is possible if the fd is closed on the other side (outfd)
* or any write problem. It can be verbose a bit for a normal
pipe_len = lttng_pipe_read(ctx->consumer_metadata_pipe,
&stream, sizeof(stream));
- if (pipe_len < 0) {
- ERR("read metadata stream, ret: %zd", pipe_len);
+ if (pipe_len < sizeof(stream)) {
+ PERROR("read metadata stream");
/*
* Continue here to handle the rest of the streams.
*/
DBG("consumer_data_pipe wake up");
pipe_readlen = lttng_pipe_read(ctx->consumer_data_pipe,
&new_stream, sizeof(new_stream));
- if (pipe_readlen < 0) {
- ERR("Consumer data pipe ret %zd", pipe_readlen);
+ if (pipe_readlen < sizeof(new_stream)) {
+ PERROR("Consumer data pipe");
/* Continue so we can at least handle the current stream(s). */
continue;
}
uint64_t size, uint64_t count)
{
int ret, fd = -1;
+ ssize_t size_ret;
struct lttng_packet_index_file_hdr hdr;
char fullpath[PATH_MAX];
hdr.index_major = htobe32(INDEX_MAJOR);
hdr.index_minor = htobe32(INDEX_MINOR);
- do {
- ret = write(fd, &hdr, sizeof(hdr));
- } while (ret < 0 && errno == EINTR);
- if (ret < 0) {
+ size_ret = lttng_write(fd, &hdr, sizeof(hdr));
+ if (size_ret < sizeof(hdr)) {
PERROR("write index header");
+ ret = -1;
goto error;
}
/*
* Write index values to the given fd of size len.
*
- * Return 0 on success or else a negative value on error.
+ * Return "len" on success or else < len on error. errno contains error
+ * details.
*/
-int index_write(int fd, struct lttng_packet_index *index, size_t len)
+ssize_t index_write(int fd, struct lttng_packet_index *index, size_t len)
{
- int ret;
+ ssize_t ret;
assert(fd >= 0);
assert(index);
- do {
- ret = write(fd, index, len);
- } while (ret < 0 && errno == EINTR);
- if (ret < 0) {
+ ret = lttng_write(fd, index, len);
+ if (ret < len) {
PERROR("writing index file");
}
int index_create_file(char *path_name, char *stream_name, int uid, int gid,
uint64_t size, uint64_t count);
-int index_write(int fd, struct lttng_packet_index *index, size_t len);
+ssize_t index_write(int fd, struct lttng_packet_index *index, size_t len);
#endif /* _INDEX_H */
/*
* Read on a lttng pipe and put the data in buf of at least size count.
*
- * Return 0 on success or else a negative errno message from read(2).
+ * Return "count" on success. Return < count on error. errno can be used
+ * to check the actual error.
*/
ssize_t lttng_pipe_read(struct lttng_pipe *pipe, void *buf, size_t count)
{
- ssize_t ret, read_len, read_left, index;
+ ssize_t ret;
assert(pipe);
assert(buf);
lock_read_side(pipe);
-
if (!lttng_pipe_is_read_open(pipe)) {
- ret = -EBADF;
+ ret = -1;
+ errno = EBADF;
goto error;
}
-
- read_left = count;
- index = 0;
- do {
- read_len = read(pipe->fd[0], buf + index, read_left);
- if (read_len < 0) {
- ret = -errno;
- if (errno == EINTR) {
- /* Read again. */
- continue;
- } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
- /*
- * Return the number of bytes read up to this point if any.
- */
- if (index) {
- ret = index;
- }
- goto error;
- } else {
- PERROR("lttng pipe read");
- goto error;
- }
- }
- read_left -= read_len;
- index += read_len;
- } while (read_left > 0);
-
- /* Everything went fine. */
- ret = index;
-
+ ret = lttng_read(pipe->fd[0], buf, count);
error:
unlock_read_side(pipe);
return ret;
/*
* Write on a lttng pipe using the data in buf and size of count.
*
- * Return 0 on success or else a negative errno message from write(2).
+ * Return "count" on success. Return < count on error. errno can be used
+ * to check the actual error.
*/
ssize_t lttng_pipe_write(struct lttng_pipe *pipe, const void *buf,
size_t count)
{
- ssize_t ret, write_len, write_left, index;
+ ssize_t ret;
assert(pipe);
assert(buf);
lock_write_side(pipe);
-
if (!lttng_pipe_is_write_open(pipe)) {
- ret = -EBADF;
+ ret = -1;
+ errno = EBADF;
goto error;
}
-
- write_left = count;
- index = 0;
- do {
- write_len = write(pipe->fd[1], buf + index, write_left);
- if (write_len < 0) {
- ret = -errno;
- if (errno == EINTR) {
- /* Read again. */
- continue;
- } else if (errno == EAGAIN || errno == EWOULDBLOCK) {
- /*
- * Return the number of bytes read up to this point if any.
- */
- if (index) {
- ret = index;
- }
- goto error;
- } else {
- PERROR("lttng pipe write");
- goto error;
- }
- }
- write_left -= write_len;
- index += write_len;
- } while (write_left > 0);
-
- /* Everything went fine. */
- ret = index;
-
+ ret = lttng_write(pipe->fd[1], buf, count);
error:
unlock_write_side(pipe);
return ret;
*/
#include <unistd.h>
+#include <errno.h>
+#include <assert.h>
#include "readwrite.h"
/*
ssize_t ret;
do {
- ret = read(fd, &buf[i], count - i);
+ ret = read(fd, buf + i, count - i);
if (ret < 0) {
if (errno == EINTR) {
continue; /* retry operation */
ssize_t ret;
do {
- ret = write(fd, &buf[i], count - i);
+ ret = write(fd, buf + i, count - i);
if (ret < 0) {
if (errno == EINTR) {
continue; /* retry operation */
int ret;
struct run_as_data *data = _data;
ssize_t writelen;
- size_t writeleft, index;
- union {
- int i;
- char c[sizeof(int)];
- } sendret;
+ int sendret;
/*
* Child: it is safe to drop egid and euid while sharing the
ret = setegid(data->gid);
if (ret < 0) {
PERROR("setegid");
- sendret.i = -1;
+ sendret = -1;
goto write_return;
}
}
ret = seteuid(data->uid);
if (ret < 0) {
PERROR("seteuid");
- sendret.i = -1;
+ sendret = -1;
goto write_return;
}
}
* Also set umask to 0 for mkdir executable bit.
*/
umask(0);
- sendret.i = (*data->cmd)(data->data);
+ sendret = (*data->cmd)(data->data);
write_return:
/* send back return value */
- writeleft = sizeof(sendret);
- index = 0;
- do {
- do {
- writelen = write(data->retval_pipe, &sendret.c[index],
- writeleft);
- } while (writelen < 0 && errno == EINTR);
- if (writelen < 0) {
- PERROR("write");
- return EXIT_FAILURE;
- }
- writeleft -= writelen;
- index += writelen;
- } while (writeleft > 0);
- return EXIT_SUCCESS;
+ writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
+ if (writelen < sizeof(sendret)) {
+ PERROR("lttng_write error");
+ return EXIT_FAILURE;
+ } else {
+ return EXIT_SUCCESS;
+ }
}
static
{
struct run_as_data run_as_data;
int ret = 0;
+ ssize_t readlen;
int status;
pid_t pid;
int retval_pipe[2];
- ssize_t readlen, readleft, index;
void *child_stack;
- union {
- int i;
- char c[sizeof(int)];
- } retval;
+ int retval;
/*
* If we are non-root, we can only deal with our own uid.
ret = pipe(retval_pipe);
if (ret < 0) {
PERROR("pipe");
- retval.i = ret;
+ retval = ret;
goto end;
}
run_as_data.data = data;
-1, 0);
if (child_stack == MAP_FAILED) {
PERROR("mmap");
- retval.i = -ENOMEM;
+ retval = -ENOMEM;
goto close_pipe;
}
/*
&run_as_data);
if (pid < 0) {
PERROR("clone");
- retval.i = pid;
+ retval = pid;
goto unmap_stack;
}
/* receive return value */
- readleft = sizeof(retval);
- index = 0;
- do {
- readlen = read(retval_pipe[0], &retval.c[index], readleft);
- if (readlen < 0) {
- PERROR("read");
- ret = -1;
- break;
- }
- readleft -= readlen;
- index += readlen;
- } while (readleft > 0);
+ readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
+ if (readlen < sizeof(retval)) {
+ ret = -1;
+ }
/*
* Parent: wait for child to return, in which case the
pid = waitpid(pid, &status, 0);
if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
PERROR("wait");
- retval.i = -1;
+ retval = -1;
}
unmap_stack:
ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
if (ret < 0) {
PERROR("munmap");
- retval.i = ret;
+ retval = ret;
}
close_pipe:
ret = close(retval_pipe[0]);
PERROR("close");
}
end:
- return retval.i;
+ return retval;
}
/*
static unsigned long read_proc_value(const char *path)
{
int ret, fd;
+ ssize_t size_ret;
long r_val;
unsigned long val = 0;
char buf[64];
goto error;
}
- ret = read(fd, buf, sizeof(buf));
- if (ret < 0) {
+ size_ret = lttng_read(fd, buf, sizeof(buf));
+ /*
+ * Allow reading a file smaller than buf, but keep space for
+ * final \0.
+ */
+ if (size_ret < 0 || size_ret >= sizeof(buf)) {
PERROR("read proc failed");
goto error_close;
}
+ buf[size_ret] = '\0';
errno = 0;
r_val = strtol(buf, NULL, 10);
/* Ease our life for what's next. */
ustream = stream->ustream;
- /* We can consume the 1 byte written into the wait_fd by UST */
+ /*
+ * We can consume the 1 byte written into the wait_fd by UST.
+ * Don't trigger error if we cannot read this one byte (read
+ * returns 0), or if the error is EAGAIN or EWOULDBLOCK.
+ */
if (stream->monitor && !stream->hangup_flush_done) {
ssize_t readlen;
- do {
- readlen = read(stream->wait_fd, &dummy, 1);
- } while (readlen == -1 && errno == EINTR);
- if (readlen == -1 && errno != EAGAIN && errno != EWOULDBLOCK) {
+ readlen = lttng_read(stream->wait_fd, &dummy, 1);
+ if (readlen < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
ret = readlen;
goto end;
}