Skip to content

Commit 5fb093c

Browse files
committed
dlopen(1): a program that dlopens its arguments
Attempts to dlopen each command line argument then sleeps. This allows trivial demonstration of the impact of loading various libraries without the need to write custom code.
1 parent 6f550c5 commit 5fb093c

File tree

4 files changed

+157
-0
lines changed

4 files changed

+157
-0
lines changed

bin/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ SUBDIR= cat \
99
date \
1010
dd \
1111
df \
12+
dlopen \
1213
domainname \
1314
echo \
1415
ed \

bin/dlopen/Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PACKAGE=runtime
2+
PROG= dlopen
3+
4+
LIBADD= util
5+
6+
.include <bsd.prog.mk>

bin/dlopen/dlopen.1

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
.\"-
2+
.\" SPDX-License-Identifier: BSD-2-Clause
3+
.\"
4+
.\" Copyright (c) 2025 (holder)
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+
.Dd February 25, 2025
12+
.Dt DLOPEN 1
13+
.Os
14+
.Sh Name
15+
.Nm dlopen
16+
.Nd load libraries with dlopen(3) and sleep for analysis
17+
.Sh SYNOPSIS
18+
.Nm
19+
.Op Brq Fl L | Fl N
20+
.Op Brq Fl g | Fl l
21+
.Op Fl v
22+
.Op Fl s Ar seconds
23+
.Ar library ...
24+
.Sh DESCRIPTION
25+
The
26+
.Nm
27+
utility supports analysis of the linkage and loading behavior of dynamiclly
28+
loaded libraries without the need for custom software.
29+
It loads each library listed on the command line and then sleeps for a
30+
number of second specified by the
31+
.Fl s
32+
argument (999999 by default).
33+
.Pp
34+
The
35+
.Fl L
36+
and
37+
.Fl N
38+
flags control the use of
39+
.Dv RTLD_LAZY
40+
vs
41+
.Dv RTLD_NOW
42+
flags.
43+
.Dv RTLD_NOW is
44+
the
45+
.Nm
46+
utility's default.
47+
Likewise
48+
.Fl g
49+
and
50+
.Fl l
51+
control the use of
52+
.Dv RTLD_GLOBAL
53+
or
54+
.Dv RTLD_LOCAL .
55+
The
56+
.Fl v
57+
flag enables verbose mode.
58+
.Sh EXIT STATUS
59+
.Ex -std
60+
.Sh AUTHORS
61+
This software and this manual page were
62+
developed by SRI International, the University of Cambridge Computer
63+
Laboratory (Department of Computer Science and Technology), and
64+
Capabilities Limited under contract
65+
.Pq FA8750-24-C-B047
66+
.Pq Do DEC Dc .

bin/dlopen/dlopen.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)