| 1 |
/* sysprof-platform.c |
| 2 |
* |
| 3 |
* Copyright 2016-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 <stdio.h> |
| 60 |
#include <stdlib.h> |
| 61 |
#include <sys/syscall.h> |
| 62 |
#include <unistd.h> |
| 63 |
|
| 64 |
#include "sysprof-capture-util-private.h" |
| 65 |
#include "sysprof-platform.h" |
| 66 |
|
| 67 |
#ifndef __NR_memfd_create |
| 68 |
static const char * |
| 69 |
get_tmpdir (void) |
| 70 |
{ |
| 71 |
const char *tmpdir = NULL; |
| 72 |
|
| 73 |
if (tmpdir == NULL || *tmpdir == '\0') |
| 74 |
tmpdir = getenv ("TMPDIR"); |
| 75 |
|
| 76 |
#ifdef P_tmpdir |
| 77 |
if (tmpdir == NULL || *tmpdir == '\0') |
| 78 |
tmpdir = P_tmpdir; |
| 79 |
#endif |
| 80 |
|
| 81 |
if (tmpdir == NULL || *tmpdir == '\0') |
| 82 |
tmpdir = "/tmp"; |
| 83 |
|
| 84 |
return tmpdir; |
| 85 |
} |
| 86 |
#endif /* !__NR_memfd_create */ |
| 87 |
|
| 88 |
/** |
| 89 |
* sysprof_memfd_create: |
| 90 |
* @name: (nullable): A descriptive name for the memfd or %NULL |
| 91 |
* |
| 92 |
* Creates a new memfd using the memfd_create syscall if available. |
| 93 |
* Otherwise, a tmpfile is used. Currently, no attempt is made to |
| 94 |
* ensure the tmpfile is on a tmpfs backed mount. |
| 95 |
* |
| 96 |
* Returns: An fd if successful; otherwise -1 and errno is set. |
| 97 |
*/ |
| 98 |
int |
| 99 |
sysprof_memfd_create (const char *name) |
| 100 |
{ |
| 101 |
#ifdef __NR_memfd_create |
| 102 |
if (name == NULL) |
| 103 |
name = "[sysprof]"; |
| 104 |
return syscall (__NR_memfd_create, name, 0); |
| 105 |
#else |
| 106 |
int fd; |
| 107 |
int flags; |
| 108 |
const char *tmpdir; |
| 109 |
char *template = NULL; |
| 110 |
size_t template_len = 0; |
| 111 |
|
| 112 |
/* |
| 113 |
* TODO: It would be nice to ensure tmpfs |
| 114 |
* |
| 115 |
* It is not strictly required that the preferred temporary directory |
| 116 |
* will be mounted as tmpfs. We should look through the mounts and ensure |
| 117 |
* that the tmpfile we open is on tmpfs so that we get anonymous backed |
| 118 |
* pages after unlinking. |
| 119 |
*/ |
| 120 |
|
| 121 |
tmpdir = get_tmpdir (); |
| 122 |
template_len = strlen (tmpdir) + 1 + strlen ("sysprof-XXXXXX") + 1; |
| 123 |
template = sysprof_malloc0 (template_len); |
| 124 |
if (template == NULL) |
| 125 |
{ |
| 126 |
errno = ENOMEM; |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
snprintf (template, template_len, "%s/sysprof-XXXXXX", tmpdir); |
| 131 |
|
| 132 |
#ifdef __APPLE__ |
| 133 |
flags = 0; |
| 134 |
#else |
| 135 |
flags = O_BINARY | O_CLOEXEC; |
| 136 |
#endif |
| 137 |
|
| 138 |
fd = TEMP_FAILURE_RETRY (mkostemp (template, flags)); |
| 139 |
if (fd < 0) |
| 140 |
{ |
| 141 |
free (template); |
| 142 |
return -1; |
| 143 |
} |
| 144 |
|
| 145 |
unlink (template); |
| 146 |
free (template); |
| 147 |
|
| 148 |
return fd; |
| 149 |
#endif |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* sysprof_getpagesize: |
| 154 |
* |
| 155 |
* Gets the page size using platform specific APIs. |
| 156 |
* |
| 157 |
* Returns: the system page size |
| 158 |
* |
| 159 |
* Since: 3.36 |
| 160 |
*/ |
| 161 |
size_t |
| 162 |
sysprof_getpagesize (void) |
| 163 |
{ |
| 164 |
return _sysprof_getpagesize (); |
| 165 |
} |