| 1 |
/* sysprof-capture-util.c |
| 2 |
* |
| 3 |
* Copyright 2019 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions are |
| 7 |
* met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright notice, |
| 10 |
* this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* Subject to the terms and conditions of this license, each copyright holder |
| 17 |
* and contributor hereby grants to those receiving rights under this license |
| 18 |
* a perpetual, worldwide, non-exclusive, no-charge, royalty-free, |
| 19 |
* irrevocable (except for failure to satisfy the conditions of this license) |
| 20 |
* patent license to make, have made, use, offer to sell, sell, import, and |
| 21 |
* otherwise transfer this software, where such license applies only to those |
| 22 |
* patent claims, already acquired or hereafter acquired, licensable by such |
| 23 |
* copyright holder or contributor that are necessarily infringed by: |
| 24 |
* |
| 25 |
* (a) their Contribution(s) (the licensed copyrights of copyright holders |
| 26 |
* and non-copyrightable additions of contributors, in source or binary |
| 27 |
* form) alone; or |
| 28 |
* |
| 29 |
* (b) combination of their Contribution(s) with the work of authorship to |
| 30 |
* which such Contribution(s) was added by such copyright holder or |
| 31 |
* contributor, if, at the time the Contribution is added, such addition |
| 32 |
* causes such combination to be necessarily infringed. The patent license |
| 33 |
* shall not apply to any other combinations which include the |
| 34 |
* Contribution. |
| 35 |
* |
| 36 |
* Except as expressly stated above, no rights or licenses from any copyright |
| 37 |
* holder or contributor is granted under this license, whether expressly, by |
| 38 |
* implication, estoppel or otherwise. |
| 39 |
* |
| 40 |
* DISCLAIMER |
| 41 |
* |
| 42 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 43 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 44 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 45 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR |
| 46 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 47 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 48 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 49 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 50 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 51 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 52 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 53 |
* |
| 54 |
* SPDX-License-Identifier: BSD-2-Clause-Patent |
| 55 |
*/ |
| 56 |
|
| 57 |
#include "config.h" |
| 58 |
|
| 59 |
#include <assert.h> |
| 60 |
#include <errno.h> |
| 61 |
#include <stdint.h> |
| 62 |
#include <unistd.h> |
| 63 |
|
| 64 |
#ifdef _WIN32 |
| 65 |
# include <process.h> |
| 66 |
# define WIN32_LEAN_AND_MEAN |
| 67 |
# include <windows.h> |
| 68 |
#endif |
| 69 |
|
| 70 |
#include "sysprof-capture-util-private.h" |
| 71 |
#include "sysprof-macros.h" |
| 72 |
|
| 73 |
#ifdef _WIN32 |
| 74 |
static void *_sysprof_io_sync_lock = SRWLOCK_INIT; |
| 75 |
#endif |
| 76 |
|
| 77 |
#ifndef __linux__ |
| 78 |
size_t |
| 79 |
(_sysprof_getpagesize) (void) |
| 80 |
{ |
| 81 |
static size_t pgsz = 0; |
| 82 |
|
| 83 |
if SYSPROF_UNLIKELY (pgsz == 0) |
| 84 |
{ |
| 85 |
#ifdef _WIN32 |
| 86 |
SYSTEM_INFO system_info; |
| 87 |
GetSystemInfo (&system_info); |
| 88 |
pgsz = system_info.dwPageSize; |
| 89 |
#else |
| 90 |
pgsz = sysconf (_SC_PAGESIZE); |
| 91 |
#endif |
| 92 |
} |
| 93 |
|
| 94 |
return pgsz; |
| 95 |
} |
| 96 |
|
| 97 |
ssize_t |
| 98 |
(_sysprof_pread) (int fd, |
| 99 |
void *buf, |
| 100 |
size_t count, |
| 101 |
off_t offset) |
| 102 |
{ |
| 103 |
#ifdef _WIN32 |
| 104 |
ssize_t ret = -1; |
| 105 |
|
| 106 |
AcquireSRWLockExclusive (_sysprof_io_sync_lock); |
| 107 |
errno = 0; |
| 108 |
if (lseek (fd, offset, SEEK_SET) != -1) |
| 109 |
ret = read (fd, buf, count); |
| 110 |
ReleaseSRWLockExclusive (_sysprof_io_sync_lock); |
| 111 |
|
| 112 |
return ret; |
| 113 |
#else |
| 114 |
errno = 0; |
| 115 |
return pread (fd, buf, count, offset); |
| 116 |
#endif |
| 117 |
} |
| 118 |
|
| 119 |
ssize_t |
| 120 |
(_sysprof_pwrite) (int fd, |
| 121 |
const void *buf, |
| 122 |
size_t count, |
| 123 |
off_t offset) |
| 124 |
{ |
| 125 |
#ifdef _WIN32 |
| 126 |
ssize_t ret = -1; |
| 127 |
|
| 128 |
AcquireSRWLockExclusive (_sysprof_io_sync_lock); |
| 129 |
errno = 0; |
| 130 |
if (lseek (fd, offset, SEEK_SET) != -1) |
| 131 |
ret = write (fd, buf, count); |
| 132 |
ReleaseSRWLockExclusive (_sysprof_io_sync_lock); |
| 133 |
|
| 134 |
return ret; |
| 135 |
#else |
| 136 |
errno = 0; |
| 137 |
return pwrite (fd, buf, count, offset); |
| 138 |
#endif |
| 139 |
} |
| 140 |
|
| 141 |
ssize_t |
| 142 |
(_sysprof_write) (int fd, |
| 143 |
const void *buf, |
| 144 |
size_t count) |
| 145 |
{ |
| 146 |
#ifdef _WIN32 |
| 147 |
ssize_t ret = -1; |
| 148 |
|
| 149 |
AcquireSRWLockExclusive (_sysprof_io_sync_lock); |
| 150 |
errno = 0; |
| 151 |
ret = write (fd, buf, count); |
| 152 |
ReleaseSRWLockExclusive (_sysprof_io_sync_lock); |
| 153 |
|
| 154 |
return ret; |
| 155 |
#else |
| 156 |
errno = 0; |
| 157 |
return write (fd, buf, count); |
| 158 |
#endif |
| 159 |
} |
| 160 |
|
| 161 |
int32_t |
| 162 |
(_sysprof_getpid) (void) |
| 163 |
{ |
| 164 |
#ifdef _WIN32 |
| 165 |
return _getpid (); |
| 166 |
#else |
| 167 |
return getpid (); |
| 168 |
#endif |
| 169 |
} |
| 170 |
|
| 171 |
ssize_t |
| 172 |
(_sysprof_sendfile) (int out_fd, |
| 173 |
int in_fd, |
| 174 |
off_t *offset, |
| 175 |
size_t count) |
| 176 |
{ |
| 177 |
ssize_t total = 0; |
| 178 |
off_t wpos = 0; |
| 179 |
off_t rpos = 0; |
| 180 |
|
| 181 |
errno = 0; |
| 182 |
|
| 183 |
if (offset != NULL && *offset > 0) |
| 184 |
wpos = rpos = *offset; |
| 185 |
|
| 186 |
while (count > 0) |
| 187 |
{ |
| 188 |
unsigned char buf[4096*4]; |
| 189 |
ssize_t n_written = 0; |
| 190 |
ssize_t n_read; |
| 191 |
off_t off = 0; |
| 192 |
size_t to_read; |
| 193 |
|
| 194 |
/* Try to page align */ |
| 195 |
if ((rpos % 4096) != 0) |
| 196 |
to_read = 4096 - rpos; |
| 197 |
else |
| 198 |
to_read = sizeof buf; |
| 199 |
|
| 200 |
if (to_read > count) |
| 201 |
to_read = count; |
| 202 |
|
| 203 |
errno = 0; |
| 204 |
n_read = _sysprof_pread (in_fd, buf, to_read, rpos); |
| 205 |
|
| 206 |
if (n_read <= 0) |
| 207 |
return -1; |
| 208 |
|
| 209 |
assert (count >= n_read); |
| 210 |
|
| 211 |
count -= n_read; |
| 212 |
rpos += n_read; |
| 213 |
|
| 214 |
while (wpos < rpos) |
| 215 |
{ |
| 216 |
assert (off < sizeof buf); |
| 217 |
|
| 218 |
errno = 0; |
| 219 |
n_written = write (out_fd, &buf[off], rpos - wpos); |
| 220 |
|
| 221 |
if (n_written <= 0) |
| 222 |
return -1; |
| 223 |
|
| 224 |
wpos += n_written; |
| 225 |
off += n_written; |
| 226 |
total += n_written; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
assert (count == 0); |
| 231 |
|
| 232 |
if (offset != NULL) |
| 233 |
*offset = rpos; |
| 234 |
|
| 235 |
errno = 0; |
| 236 |
return total; |
| 237 |
} |
| 238 |
#endif /* #ifndef __linux__ */ |
| 239 |
|
| 240 |
size_t |
| 241 |
(_sysprof_strlcpy) (char *dest, |
| 242 |
const char *src, |
| 243 |
size_t dest_size) |
| 244 |
{ |
| 245 |
size_t i = 0; |
| 246 |
|
| 247 |
if (dest_size > 0) |
| 248 |
{ |
| 249 |
for (; i < dest_size - 1 && src[i] != '\0'; i++) |
| 250 |
dest[i] = src[i]; |
| 251 |
dest[i] = '\0'; |
| 252 |
} |
| 253 |
|
| 254 |
for (; src[i] != '\0'; i++) |
| 255 |
; |
| 256 |
|
| 257 |
return i; |
| 258 |
} |
| 259 |
|
| 260 |
void * |
| 261 |
(_sysprof_reallocarray) (void *ptr, |
| 262 |
size_t m, |
| 263 |
size_t n) |
| 264 |
{ |
| 265 |
if (n && m > (size_t)(-1) / n) { |
| 266 |
errno = ENOMEM; |
| 267 |
return NULL; |
| 268 |
} |
| 269 |
return realloc(ptr, m * n); |
| 270 |
} |