|
| 1 | +/*- |
| 2 | + * SPDX-License-Identifier: BSD-2-Clause |
| 3 | + * |
| 4 | + * Copyright (c) 2025 SRI International |
| 5 | + * |
| 6 | + * This software was developed by SRI International, the University of |
| 7 | + * Cambridge Computer Laboratory (Department of Computer Science and |
| 8 | + * Technology), and Capabilities Limited under Defense Advanced Research |
| 9 | + * Projects Agency (DARPA) Contract No. FA8750-24-C-B047 ("DEC"). |
| 10 | + */ |
| 11 | + |
| 12 | +#include <sys/cdefs.h> |
| 13 | + |
| 14 | +#include <dlfcn.h> |
| 15 | +#include <err.h> |
| 16 | +#include <libutil.h> |
| 17 | +#include <limits.h> |
| 18 | +#include <stdbool.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <stdio.h> |
| 21 | +#include <unistd.h> |
| 22 | + |
| 23 | +static bool verbose = false; |
| 24 | +static int seconds = 999999; |
| 25 | +static int dlopen_mode = RTLD_NOW; |
| 26 | +static int dlopen_local_global = 0; /* dlopen(3)'s default is RTLD_LOCAL */ |
| 27 | + |
| 28 | +static void _Noreturn |
| 29 | +usage(void) |
| 30 | +{ |
| 31 | + printf("usage: dlopen [-L|N] [-g|l] [-s <seconds>] [-v] <library> [...]\n"); |
| 32 | + exit (1); |
| 33 | +} |
| 34 | + |
| 35 | +int |
| 36 | +main(int argc, char **argv) |
| 37 | +{ |
| 38 | + int ch; |
| 39 | + uint64_t num; |
| 40 | + |
| 41 | + while((ch = getopt(argc, argv, "gLlNs:v")) != -1) { |
| 42 | + switch (ch) { |
| 43 | + case 'g': |
| 44 | + dlopen_local_global = RTLD_GLOBAL; |
| 45 | + break; |
| 46 | + case 'l': |
| 47 | + dlopen_local_global = RTLD_LOCAL; |
| 48 | + break; |
| 49 | + case 'L': |
| 50 | + dlopen_mode = RTLD_LAZY; |
| 51 | + break; |
| 52 | + case 'N': |
| 53 | + dlopen_mode = RTLD_NOW; |
| 54 | + break; |
| 55 | + case 's': |
| 56 | + if (expand_number(optarg, &num) != 0) |
| 57 | + err(1, "bad number '%s'", optarg); |
| 58 | + if (num > INT_MAX) |
| 59 | + seconds = INT_MAX; |
| 60 | + else |
| 61 | + seconds = num; |
| 62 | + break; |
| 63 | + case 'v': |
| 64 | + verbose = true; |
| 65 | + break; |
| 66 | + case '?': |
| 67 | + default: |
| 68 | + usage(); |
| 69 | + } |
| 70 | + } |
| 71 | + argc -= optind; |
| 72 | + argv += optind; |
| 73 | + |
| 74 | + for (int i = 0; i < argc; i++) { |
| 75 | + if (dlopen(argv[i], dlopen_mode | dlopen_local_global) == NULL) |
| 76 | + errx(1, "dlopen(%s)", argv[i]); |
| 77 | + if (verbose) |
| 78 | + printf("loaded %s\n", argv[i]); |
| 79 | + } |
| 80 | + if (seconds != 0 && verbose) |
| 81 | + printf("sleeping for %d seconds\n", seconds); |
| 82 | + sleep(seconds); |
| 83 | + return (0); |
| 84 | +} |
0 commit comments