/* read.c -- File views without mmap.1Copyright (C) 2012-2021 Free Software Foundation, Inc.2Written by Ian Lance Taylor, Google.34Redistribution and use in source and binary forms, with or without5modification, are permitted provided that the following conditions are6met:78(1) Redistributions of source code must retain the above copyright9notice, this list of conditions and the following disclaimer.1011(2) Redistributions in binary form must reproduce the above copyright12notice, this list of conditions and the following disclaimer in13the documentation and/or other materials provided with the14distribution.1516(3) The name of the author may not be used to17endorse or promote products derived from this software without18specific prior written permission.1920THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR21IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,24INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR26SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,28STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING29IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE30POSSIBILITY OF SUCH DAMAGE. */3132#include "config.h"3334#include <errno.h>35#include <stdlib.h>36#include <sys/types.h>37#include <unistd.h>3839#include "backtrace.h"40#include "internal.h"4142/* This file implements file views when mmap is not available. */4344/* Create a view of SIZE bytes from DESCRIPTOR at OFFSET. */4546int47backtrace_get_view (struct backtrace_state *state, int descriptor,48off_t offset, uint64_t size,49backtrace_error_callback error_callback,50void *data, struct backtrace_view *view)51{52uint64_t got;53ssize_t r;54if (_lseeki64 (descriptor, offset, SEEK_SET) < 0)55{56error_callback (data, "lseek", errno);57return 0;58}5960view->base = backtrace_alloc (state, size, error_callback, data);61if (view->base == NULL)62return 0;63view->data = view->base;64view->len = size;6566got = 0;67void *ptr = view->base;68while (got < size)69{70uint64_t sz = size - got;71if (sz > INT_MAX)72sz = INT_MAX;73r = _read (descriptor, ptr, sz);74if (r < 0)75{76error_callback (data, "read", errno);77free (view->base);78return 0;79}80if (r == 0)81break;82got += (uint64_t) r;83ptr += r;84}8586if (got < size)87{88error_callback (data, "file too short", 0);89free (view->base);90return 0;91}9293return 1;94}9596/* Release a view read by backtrace_get_view. */9798void99backtrace_release_view (struct backtrace_state *state,100struct backtrace_view *view,101backtrace_error_callback error_callback,102void *data)103{104backtrace_free (state, view->base, view->len, error_callback, data);105view->data = NULL;106view->base = NULL;107}108109110