Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2005 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 38 | #ifndef _GNU_SOURCE |
| 39 | #define _GNU_SOURCE |
| 40 | #endif |
| 41 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 42 | #include <vppinfra/error.h> |
| 43 | #include <vppinfra/os.h> |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 44 | #include <vppinfra/bitmap.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #include <vppinfra/unix.h> |
Damjan Marion | 40f4810 | 2023-08-07 01:07:09 +0200 | [diff] [blame] | 46 | #include <vppinfra/format.h> |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 47 | #ifdef __linux__ |
| 48 | #include <vppinfra/linux/sysfs.h> |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 49 | #include <sched.h> |
Tom Jones | f4c7996 | 2024-04-25 14:25:06 +0000 | [diff] [blame] | 50 | #elif defined(__FreeBSD__) |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 51 | #define _WANT_FREEBSD_BITSET |
| 52 | #include <sys/cdefs.h> |
Tom Jones | f4c7996 | 2024-04-25 14:25:06 +0000 | [diff] [blame] | 53 | #include <sys/param.h> |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 54 | #include <sys/types.h> |
| 55 | #include <sys/cpuset.h> |
| 56 | #include <sys/domainset.h> |
| 57 | #include <sys/sysctl.h> |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 58 | #endif |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | |
| 60 | #include <sys/stat.h> |
| 61 | #include <sys/types.h> |
| 62 | #include <sys/uio.h> /* writev */ |
| 63 | #include <fcntl.h> |
| 64 | #include <stdio.h> /* for sprintf */ |
Damjan Marion | 40f4810 | 2023-08-07 01:07:09 +0200 | [diff] [blame] | 65 | #include <limits.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 67 | __clib_export __thread uword __os_thread_index = 0; |
| 68 | __clib_export __thread uword __os_numa_index = 0; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 69 | |
Tom Jones | 828353f | 2024-08-14 09:23:11 +0000 | [diff] [blame] | 70 | __clib_export clib_bitmap_t *os_get_cpu_affinity_bitmap (int pid); |
| 71 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 72 | clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 73 | clib_file_n_bytes (char *file, uword * result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 74 | { |
| 75 | struct stat s; |
| 76 | |
| 77 | if (stat (file, &s) < 0) |
| 78 | return clib_error_return_unix (0, "stat `%s'", file); |
| 79 | |
| 80 | if (S_ISREG (s.st_mode)) |
| 81 | *result = s.st_size; |
| 82 | else |
| 83 | *result = 0; |
| 84 | |
| 85 | return /* no error */ 0; |
| 86 | } |
| 87 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 89 | clib_file_read_contents (char *file, u8 * result, uword n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | { |
| 91 | int fd = -1; |
| 92 | uword n_done, n_left; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 93 | clib_error_t *error = 0; |
| 94 | u8 *v = result; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 95 | |
| 96 | if ((fd = open (file, 0)) < 0) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 97 | return clib_error_return_unix (0, "open `%s'", file); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 98 | |
| 99 | n_left = n_bytes; |
| 100 | n_done = 0; |
| 101 | while (n_left > 0) |
| 102 | { |
| 103 | int n_read; |
| 104 | if ((n_read = read (fd, v + n_done, n_left)) < 0) |
| 105 | { |
| 106 | error = clib_error_return_unix (0, "open `%s'", file); |
| 107 | goto done; |
| 108 | } |
| 109 | |
| 110 | /* End of file. */ |
| 111 | if (n_read == 0) |
| 112 | break; |
| 113 | |
| 114 | n_left -= n_read; |
| 115 | n_done += n_read; |
| 116 | } |
| 117 | |
| 118 | if (n_left > 0) |
| 119 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 120 | error = |
| 121 | clib_error_return (0, |
| 122 | " `%s' expected to read %wd bytes; read only %wd", |
| 123 | file, n_bytes, n_bytes - n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 124 | goto done; |
| 125 | } |
| 126 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 127 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | close (fd); |
| 129 | return error; |
| 130 | } |
| 131 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 132 | __clib_export clib_error_t * |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 133 | clib_file_contents (char *file, u8 ** result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 134 | { |
| 135 | uword n_bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 136 | clib_error_t *error = 0; |
| 137 | u8 *v; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 138 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 139 | if ((error = clib_file_n_bytes (file, &n_bytes))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | return error; |
| 141 | |
| 142 | v = 0; |
| 143 | vec_resize (v, n_bytes); |
| 144 | |
Dave Barach | 59b2565 | 2017-09-10 15:04:27 -0400 | [diff] [blame] | 145 | error = clib_file_read_contents (file, v, n_bytes); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 146 | |
| 147 | if (error) |
| 148 | vec_free (v); |
| 149 | else |
| 150 | *result = v; |
| 151 | |
| 152 | return error; |
| 153 | } |
| 154 | |
Damjan Marion | 40f4810 | 2023-08-07 01:07:09 +0200 | [diff] [blame] | 155 | __clib_export u8 * |
| 156 | clib_file_get_resolved_basename (char *fmt, ...) |
| 157 | { |
| 158 | va_list va; |
| 159 | char *p, buffer[PATH_MAX]; |
| 160 | u8 *link, *s = 0; |
| 161 | int r; |
| 162 | |
| 163 | va_start (va, fmt); |
| 164 | link = va_format (0, fmt, &va); |
| 165 | va_end (va); |
| 166 | vec_add1 (link, 0); |
| 167 | |
| 168 | r = readlink ((char *) link, buffer, sizeof (buffer) - 1); |
| 169 | vec_free (link); |
| 170 | |
| 171 | if (r < 1) |
| 172 | return 0; |
| 173 | |
Dave Wallace | 05cc62d | 2023-10-09 18:05:47 -0400 | [diff] [blame] | 174 | buffer[r] = 0; |
Damjan Marion | 40f4810 | 2023-08-07 01:07:09 +0200 | [diff] [blame] | 175 | p = buffer + r - 1; |
| 176 | while (p > buffer && p[-1] != '/') |
| 177 | p--; |
| 178 | |
| 179 | while (p[0]) |
| 180 | vec_add1 (s, p++[0]); |
| 181 | |
Damjan Marion | 08600cc | 2023-10-04 18:03:18 +0200 | [diff] [blame] | 182 | vec_add1 (s, 0); |
Damjan Marion | 40f4810 | 2023-08-07 01:07:09 +0200 | [diff] [blame] | 183 | return s; |
| 184 | } |
| 185 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 186 | clib_error_t * |
| 187 | unix_proc_file_contents (char *file, u8 ** result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | { |
| 189 | u8 *rv = 0; |
| 190 | uword pos; |
| 191 | int bytes, fd; |
| 192 | |
| 193 | /* Unfortunately, stat(/proc/XXX) returns zero... */ |
| 194 | fd = open (file, O_RDONLY); |
| 195 | |
| 196 | if (fd < 0) |
| 197 | return clib_error_return_unix (0, "open `%s'", file); |
| 198 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 199 | vec_validate (rv, 4095); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 200 | pos = 0; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 201 | while (1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 202 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 203 | bytes = read (fd, rv + pos, 4096); |
| 204 | if (bytes < 0) |
| 205 | { |
| 206 | close (fd); |
| 207 | vec_free (rv); |
| 208 | return clib_error_return_unix (0, "read '%s'", file); |
| 209 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 210 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 211 | if (bytes == 0) |
| 212 | { |
Damjan Marion | 8bea589 | 2022-04-04 22:40:45 +0200 | [diff] [blame] | 213 | vec_set_len (rv, pos); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 214 | break; |
| 215 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | pos += bytes; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 217 | vec_validate (rv, pos + 4095); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 218 | } |
| 219 | *result = rv; |
| 220 | close (fd); |
| 221 | return 0; |
| 222 | } |
| 223 | |
Benoît Ganne | 448f08b | 2024-06-17 15:32:12 +0200 | [diff] [blame] | 224 | __clib_export __clib_weak void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 225 | os_panic (void) |
| 226 | { |
| 227 | abort (); |
| 228 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | |
Benoît Ganne | 448f08b | 2024-06-17 15:32:12 +0200 | [diff] [blame] | 230 | __clib_export __clib_weak void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 231 | os_exit (int code) |
| 232 | { |
| 233 | exit (code); |
| 234 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 235 | |
Benoît Ganne | 448f08b | 2024-06-17 15:32:12 +0200 | [diff] [blame] | 236 | __clib_export __clib_weak void |
| 237 | os_puts (u8 *string, uword string_length, uword is_error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 238 | { |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 239 | int cpu = os_get_thread_index (); |
| 240 | int nthreads = os_get_nthreads (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 241 | char buf[64]; |
| 242 | int fd = is_error ? 2 : 1; |
| 243 | struct iovec iovs[2]; |
| 244 | int n_iovs = 0; |
| 245 | |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 246 | if (nthreads > 1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 248 | snprintf (buf, sizeof (buf), "%d: ", cpu); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 249 | |
| 250 | iovs[n_iovs].iov_base = buf; |
| 251 | iovs[n_iovs].iov_len = strlen (buf); |
| 252 | n_iovs++; |
| 253 | } |
| 254 | |
| 255 | iovs[n_iovs].iov_base = string; |
| 256 | iovs[n_iovs].iov_len = string_length; |
| 257 | n_iovs++; |
| 258 | |
| 259 | if (writev (fd, iovs, n_iovs) < 0) |
| 260 | ; |
| 261 | } |
| 262 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 263 | __clib_export __clib_weak void |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 264 | os_out_of_memory (void) |
| 265 | { |
| 266 | os_panic (); |
| 267 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | |
Damjan Marion | dae1c7e | 2020-10-17 13:32:25 +0200 | [diff] [blame] | 269 | __clib_export __clib_weak uword |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 270 | os_get_nthreads (void) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 271 | { |
| 272 | return 1; |
| 273 | } |
| 274 | |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 275 | __clib_export clib_bitmap_t * |
| 276 | os_get_online_cpu_core_bitmap () |
| 277 | { |
| 278 | #if __linux__ |
Damjan Marion | 3eb6cbe | 2024-02-12 19:44:58 +0000 | [diff] [blame] | 279 | return clib_sysfs_read_bitmap ("/sys/devices/system/cpu/online"); |
Tom Jones | 828353f | 2024-08-14 09:23:11 +0000 | [diff] [blame] | 280 | #elif defined(__FreeBSD__) |
| 281 | return os_get_cpu_affinity_bitmap (0); |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 282 | #else |
| 283 | return 0; |
| 284 | #endif |
| 285 | } |
| 286 | |
| 287 | __clib_export clib_bitmap_t * |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 288 | os_get_cpu_affinity_bitmap (int pid) |
| 289 | { |
| 290 | #if __linux |
| 291 | int index, ret; |
| 292 | cpu_set_t cpuset; |
| 293 | uword *affinity_cpus; |
| 294 | |
| 295 | clib_bitmap_alloc (affinity_cpus, sizeof (cpu_set_t)); |
| 296 | clib_bitmap_zero (affinity_cpus); |
| 297 | |
Renato Botelho do Couto | 7f3c217 | 2024-06-21 14:00:10 +0000 | [diff] [blame] | 298 | CPU_ZERO_S (sizeof (cpu_set_t), &cpuset); |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 299 | |
| 300 | ret = sched_getaffinity (0, sizeof (cpu_set_t), &cpuset); |
| 301 | |
| 302 | if (ret < 0) |
| 303 | { |
| 304 | clib_bitmap_free (affinity_cpus); |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | for (index = 0; index < sizeof (cpu_set_t); index++) |
Renato Botelho do Couto | 7f3c217 | 2024-06-21 14:00:10 +0000 | [diff] [blame] | 309 | if (CPU_ISSET_S (index, sizeof (cpu_set_t), &cpuset)) |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 310 | clib_bitmap_set (affinity_cpus, index, 1); |
| 311 | return affinity_cpus; |
| 312 | #elif defined(__FreeBSD__) |
| 313 | cpuset_t mask; |
| 314 | uword *r = NULL; |
| 315 | |
Tom Jones | 828353f | 2024-08-14 09:23:11 +0000 | [diff] [blame] | 316 | clib_bitmap_alloc (r, sizeof (CPU_SETSIZE)); |
| 317 | clib_bitmap_zero (r); |
| 318 | |
Tom Jones | 2e87d94 | 2024-05-06 14:53:57 +0000 | [diff] [blame] | 319 | if (cpuset_getaffinity (CPU_LEVEL_CPUSET, CPU_WHICH_CPUSET, -1, |
| 320 | sizeof (mask), &mask) != 0) |
| 321 | { |
| 322 | clib_bitmap_free (r); |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | for (int bit = 0; bit < CPU_SETSIZE; bit++) |
| 327 | clib_bitmap_set (r, bit, CPU_ISSET (bit, &mask)); |
| 328 | |
| 329 | return r; |
| 330 | #else |
| 331 | return NULL; |
| 332 | #endif |
| 333 | } |
| 334 | |
| 335 | __clib_export clib_bitmap_t * |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 336 | os_get_online_cpu_node_bitmap () |
| 337 | { |
| 338 | #if __linux__ |
Damjan Marion | 3eb6cbe | 2024-02-12 19:44:58 +0000 | [diff] [blame] | 339 | return clib_sysfs_read_bitmap ("/sys/devices/system/node/online"); |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 340 | #else |
| 341 | return 0; |
| 342 | #endif |
| 343 | } |
Damjan Marion | 3eb6cbe | 2024-02-12 19:44:58 +0000 | [diff] [blame] | 344 | __clib_export clib_bitmap_t * |
| 345 | os_get_cpu_on_node_bitmap (int node) |
| 346 | { |
| 347 | #if __linux__ |
| 348 | return clib_sysfs_read_bitmap ("/sys/devices/system/node/node%u/cpulist", |
| 349 | node); |
| 350 | #else |
| 351 | return 0; |
| 352 | #endif |
| 353 | } |
| 354 | |
| 355 | __clib_export clib_bitmap_t * |
| 356 | os_get_cpu_with_memory_bitmap () |
| 357 | { |
| 358 | #if __linux__ |
| 359 | return clib_sysfs_read_bitmap ("/sys/devices/system/node/has_memory"); |
| 360 | #else |
| 361 | return 0; |
| 362 | #endif |
| 363 | } |
| 364 | |
| 365 | __clib_export int |
| 366 | os_get_cpu_phys_core_id (int cpu_id) |
| 367 | { |
| 368 | #if __linux |
| 369 | int core_id = -1; |
| 370 | clib_error_t *err; |
| 371 | u8 *p; |
| 372 | |
| 373 | p = |
| 374 | format (0, "/sys/devices/system/cpu/cpu%u/topology/core_id%c", cpu_id, 0); |
| 375 | err = clib_sysfs_read ((char *) p, "%d", &core_id); |
| 376 | vec_free (p); |
| 377 | if (err) |
| 378 | { |
| 379 | clib_error_free (err); |
| 380 | return -1; |
| 381 | } |
| 382 | return core_id; |
| 383 | #else |
| 384 | return -1; |
| 385 | #endif |
| 386 | } |
Damjan Marion | 4b66140 | 2024-02-29 16:14:27 +0100 | [diff] [blame] | 387 | |
Tom Jones | ab47993 | 2024-04-24 10:30:20 +0000 | [diff] [blame] | 388 | __clib_export u8 * |
| 389 | os_get_exec_path () |
| 390 | { |
| 391 | u8 *rv = 0; |
| 392 | #ifdef __linux__ |
| 393 | char tmp[PATH_MAX]; |
| 394 | ssize_t sz = readlink ("/proc/self/exe", tmp, sizeof (tmp)); |
| 395 | |
| 396 | if (sz <= 0) |
| 397 | return 0; |
| 398 | #else |
| 399 | char tmp[MAXPATHLEN]; |
| 400 | int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; |
| 401 | size_t sz = MAXPATHLEN; |
| 402 | |
| 403 | if (sysctl (mib, 4, tmp, &sz, NULL, 0) == -1) |
| 404 | return 0; |
| 405 | #endif |
| 406 | vec_add (rv, tmp, sz); |
| 407 | return rv; |
| 408 | } |
| 409 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 410 | /* |
| 411 | * fd.io coding-style-patch-verification: ON |
| 412 | * |
| 413 | * Local Variables: |
| 414 | * eval: (c-set-style "gnu") |
| 415 | * End: |
| 416 | */ |