Changeset 1ed69dc for lib


Ignore:
Timestamp:
06/28/18 11:26:07 (2 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
cachetimestamps, develop, master, rc-4.0.4, ringdecrementfix, ringperformance
Children:
055a2c9, b06b894
Parents:
47d4f8c
Message:

Be less casual with error checking in SCB data structure

  • ftruncate can fail, so actually check for that during init.
  • If this does happen, return an error from libtrace_scb_init() -- this means that libtrace_scb_init() now returns an int rather than void.
  • Update other API functions to make sure that they don't die horribly if the error returned by init is ignored.
Location:
lib/data-struct
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • lib/data-struct/simple_circular_buffer.c

    re8e9052 r1ed69dc  
    1010#include <fcntl.h>
    1111#include <sys/socket.h>
     12#include <errno.h>
    1213
    1314#include "simple_circular_buffer.h"
    1415
    15 DLLEXPORT void libtrace_scb_init(libtrace_scb_t *buf, uint32_t size,
     16DLLEXPORT int libtrace_scb_init(libtrace_scb_t *buf, uint32_t size,
    1617                uint16_t id) {
    1718
     
    2829        buf->fd = shm_open(anonname, O_RDWR | O_CREAT, 0600);
    2930#endif
    30         ftruncate(buf->fd, size);
    31 
    32         buf->address = mmap(NULL, 2 * size, PROT_NONE,
    33                         MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    34         mmap(buf->address, size, PROT_READ | PROT_WRITE,
    35                         MAP_SHARED | MAP_FIXED, buf->fd, 0);
    36         mmap(buf->address + size, size, PROT_READ | PROT_WRITE,
    37                         MAP_SHARED | MAP_FIXED, buf->fd, 0);
     31        if (ftruncate(buf->fd, size) < 0) {
     32                perror("ftruncate in libtrace_scb_init");
     33                close(buf->fd);
     34                buf->fd = -1;
     35                buf->address = NULL;
     36        } else {
     37                buf->address = mmap(NULL, 2 * size, PROT_NONE,
     38                                MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
     39                mmap(buf->address, size, PROT_READ | PROT_WRITE,
     40                                MAP_SHARED | MAP_FIXED, buf->fd, 0);
     41                mmap(buf->address + size, size, PROT_READ | PROT_WRITE,
     42                                MAP_SHARED | MAP_FIXED, buf->fd, 0);
     43        }
    3844        buf->read_offset = 0;
    3945        buf->write_offset = 0;
    4046        buf->count_bytes = size;
     47
     48        if (buf->address) {
     49                return 0;
     50        }
     51        return -1;
    4152}
    4253
     
    4455        /* TODO shm_unlink the file name if we used shm_open? */
    4556
    46         munmap(buf->address, buf->count_bytes * 2);
    47         close(buf->fd);
     57        if (buf->address) {
     58                munmap(buf->address, buf->count_bytes * 2);
     59        }
     60        if (buf->fd != -1) {
     61                close(buf->fd);
     62        }
    4863}
    4964
     
    5267        int space = buf->count_bytes - (buf->write_offset - buf->read_offset);
    5368        int ret;
     69
     70        if (buf->address == NULL) {
     71                return -1;
     72        }
    5473
    5574        if (space == 0) {
     
    6887                uint32_t *available) {
    6988
     89        if (buf->address == NULL) {
     90                return NULL;
     91        }
    7092        *available = buf->write_offset - buf->read_offset;
    7193        return buf->address + buf->read_offset;
  • lib/data-struct/simple_circular_buffer.h

    rb94478f r1ed69dc  
    1313
    1414
    15 DLLEXPORT void libtrace_scb_init(libtrace_scb_t *buf, uint32_t size,
     15DLLEXPORT int libtrace_scb_init(libtrace_scb_t *buf, uint32_t size,
    1616                uint16_t id);
    1717DLLEXPORT void libtrace_scb_destroy(libtrace_scb_t *buf);
Note: See TracChangeset for help on using the changeset viewer.