Skip to content

Commit e34ddae

Browse files
committed
Embed manpages into the binaries
With this invoking one of the utilities with `-h` will display the manpage. Since quark is not a "system utility", we can't expect people to go to the trouble of installing manpages, and typing `man ./quark-mon.8` is a pain. It's also likely that the binary is sent somewhere and thus the man relationship was lost. I had to write a mini-utility to embed the manpage, it's a bit sad that there isn't a `mandoc -T c` to export the manpage as a C struct. Arm64 cross compilation gets a little bit trickier: In order to build quark-mon, quark-btf and quark-test, it needs to build manpage.h, and to build manpage.h it needs to build man-embedder. But it can't build man-embedder for arm64, since we must be able to execute it! We fix it by cheating the order build, we force manpages.h to be evaluated before `all` in the host system. Which pulls man-embedder for the host and generates manpage.h. The only drawback is that man-embedder will stay built for the host system, which is fine since it's a build-system executable.
1 parent 51a3b02 commit e34ddae

13 files changed

+225
-23
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ init
1313
libquark.a
1414
libquark_big.a
1515
bpf_prog_skel.h
16+
# manpages.h is autogenerated
17+
manpages.h
18+
man-embedder
1619
manhtml/*.html
1720
elftoolchain/libelf/libelf_pic.a
1821
initramfs/

Makefile

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ EEBPF_INCLUDES:= -Ielastic-ebpf/GPL/Events -Ielastic-ebpf/contrib/vmlinux/$(ARCH
7474

7575
# LIBQUARK
7676
LIBQUARK_DEPS:= $(wildcard *.h) bpf_prog_skel.h $(EEBPF_FILES) include
77+
LIBQUARK_DEPS:= $(filter-out manpages.h, $(LIBQUARK_DEPS))
7778
LIBQUARK_SRCS:= \
7879
bpf_queue.c \
7980
btf.c \
@@ -198,7 +199,7 @@ docker: docker-image clean-all
198199
$(call msg,DOCKER-RUN,Dockerfile)
199200
$(Q)$(DOCKER) run $(DOCKER_RUN_ARGS) /bin/bash -c "make -C $(PWD)"
200201

201-
docker-cross-arm64: clean-all docker-image
202+
docker-cross-arm64: clean-all docker-image manpages.h
202203
$(call msg,DOCKER-RUN,Dockerfile)
203204
$(Q)$(DOCKER) run \
204205
-e ARCH=arm64 \
@@ -278,29 +279,52 @@ init: init.c
278279
$(call msg,CC,$@)
279280
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -static -o $@ $^
280281

281-
quark-mon: quark-mon.c $(LIBQUARK_STATIC_BIG)
282+
quark-mon: quark-mon.c manpages.h $(LIBQUARK_STATIC_BIG)
282283
$(call msg,CC,$@)
283-
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -o $@ $^
284+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
285+
-o $@ $< $(LIBQUARK_STATIC_BIG)
284286

285-
quark-btf: quark-btf.c $(LIBQUARK_STATIC_BIG)
287+
quark-btf: quark-btf.c manpages.h $(LIBQUARK_STATIC_BIG)
286288
$(call msg,CC,$@)
287-
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -o $@ $^
289+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
290+
-o $@ $< $(LIBQUARK_STATIC_BIG)
288291

289-
quark-test: quark-test.c $(LIBQUARK_STATIC_BIG)
292+
quark-test: quark-test.c manpages.h $(LIBQUARK_STATIC_BIG)
290293
$(call msg,CC,$@)
291-
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -o $@ $^
294+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
295+
-o $@ $< $(LIBQUARK_STATIC_BIG)
292296

293-
quark-mon-static: quark-mon.c $(LIBQUARK_STATIC_BIG)
297+
quark-mon-static: quark-mon.c manpages.h $(LIBQUARK_STATIC_BIG)
294298
$(call msg,CC,$@)
295-
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -DNO_PRIVDROP $(CDIAGFLAGS) -static -o $@ $^
299+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) -DNO_PRIVDROP $(CDIAGFLAGS) \
300+
-static -o $@ $< $(LIBQUARK_STATIC_BIG)
296301

297-
quark-btf-static: quark-btf.c $(LIBQUARK_STATIC_BIG)
302+
quark-btf-static: quark-btf.c manpages.h $(LIBQUARK_STATIC_BIG)
298303
$(call msg,CC,$@)
299-
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -static -o $@ $^
304+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
305+
-static -o $@ $< $(LIBQUARK_STATIC_BIG)
300306

301-
quark-test-static: quark-test.c $(LIBQUARK_STATIC_BIG)
307+
quark-test-static: quark-test.c manpages.h $(LIBQUARK_STATIC_BIG)
302308
$(call msg,CC,$@)
303-
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -static -o $@ $^
309+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) \
310+
-static -o $@ $< $(LIBQUARK_STATIC_BIG)
311+
312+
man-embedder: man-embedder.c
313+
$(call msg,CC,$@)
314+
$(Q)$(CC) $(CFLAGS) $(CPPFLAGS) $(CDIAGFLAGS) -o $@ $^
315+
316+
manpages.h: man-embedder display_man.c quark-btf.8 quark-mon.8 quark-test.8
317+
$(Q)echo '// SPDX-License-Identifier: Apache-2.0' > $@
318+
$(Q)echo '/* Copyright (c) 2024 Elastic NV */' >> $@
319+
$(Q)echo '' >> $@
320+
$(call msg,MAN-EMB,quark-btf.8)
321+
$(Q)./man-embedder quark-btf.8 MAN_QUARK_BTF >> $@
322+
$(call msg,MAN-EMB,quark-mon.8)
323+
$(Q)./man-embedder quark-mon.8 MAN_QUARK_MON >> $@
324+
$(call msg,MAN-EMB,quark-test.8)
325+
$(Q)./man-embedder quark-test.8 MAN_QUARK_TEST >> $@
326+
$(call msg,MAN-EMB,display_man.c)
327+
$(Q)cat display_man.c >> $@
304328

305329
docs/index.html: docs/quark.7.html
306330
$(call msg,CP,index.html)
@@ -336,6 +360,8 @@ clean:
336360
$(Q)rm -f \
337361
*.o \
338362
*.a \
363+
man-embedder \
364+
manpages.h \
339365
quark-mon \
340366
quark-mon-static \
341367
quark-btf \

display_man.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This code is stashed in the end of manpage.h
3+
*/
4+
static void
5+
display_man(void)
6+
{
7+
int fd, status;
8+
char template[] = "/tmp/quark-man-display.XXXXXX";
9+
pid_t pid;
10+
11+
fd = mkstemp(template);
12+
if (fd == -1)
13+
err(1, "mkstemp");
14+
if (qwrite(fd, manpage_bin, sizeof(manpage_bin)) != 0)
15+
err(1, "qwrite");
16+
close(fd);
17+
18+
if ((pid = fork()) == -1)
19+
err(1, "fork");
20+
21+
/* child */
22+
if (pid == 0)
23+
exit(execlp("man", "man", template, NULL));
24+
/* parent */
25+
if (waitpid(pid, &status, 0) == -1)
26+
err(1, "waitpid");
27+
if (unlink(template) != 0)
28+
warn("unlink");
29+
if (WIFEXITED(status))
30+
exit(WEXITSTATUS(status));
31+
32+
exit(1);
33+
}

docs/quark-btf.8.html

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ <h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a><
4646
</tr>
4747
</table>
4848
<br/>
49+
<table class="Nm">
50+
<tr>
51+
<td><code class="Nm">quark-btf</code></td>
52+
<td><code class="Fl">-h</code></td>
53+
</tr>
54+
</table>
55+
<br/>
4956
<table class="Nm">
5057
<tr>
5158
<td><code class="Nm">quark-btf <code class="Fl">-V</code></code></td>
@@ -80,9 +87,12 @@ <h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIP
8087
<a class="permalink" href="#uname"><i class="Em">uname
8188
-r</i></a>.</dd>
8289
</dl>
83-
This option only used to generate <span class="Pa">btfhub.c</span> via
84-
<span class="Pa">genbtf.sh</span>, chances are you'll never need
85-
this.</dd>
90+
<p class="Pp">This option is only used to generate
91+
<span class="Pa">btfhub.c</span> via <span class="Pa">genbtf.sh</span>,
92+
chances are you'll never need this.</p>
93+
</dd>
94+
<dt id="h"><a class="permalink" href="#h"><code class="Fl">-h</code></a></dt>
95+
<dd>Display this manpage.</dd>
8696
<dt id="l"><a class="permalink" href="#l"><code class="Fl">-l</code></a>
8797
<var class="Ar">version</var></dt>
8898
<dd>Lookup the kernel
@@ -155,7 +165,7 @@ <h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
155165
</div>
156166
<table class="foot">
157167
<tr>
158-
<td class="foot-date">October 25, 2024</td>
168+
<td class="foot-date">October 28, 2024</td>
159169
<td class="foot-os">Linux</td>
160170
</tr>
161171
</table>

docs/quark-mon.8.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ <h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a><
3232
</tr>
3333
</table>
3434
<br/>
35+
<table class="Nm">
36+
<tr>
37+
<td><code class="Nm">quark-mon</code></td>
38+
<td><code class="Fl">-h</code></td>
39+
</tr>
40+
</table>
41+
<br/>
3542
<table class="Nm">
3643
<tr>
3744
<td><code class="Nm">quark-mon <code class="Fl">-V</code></code></td>
@@ -78,6 +85,8 @@ <h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIP
7885
<dd>Use minimal aggregation, fork, exec and exit will
7986
<a class="permalink" href="#not"><i class="Em" id="not">not</i></a> be
8087
aggregated.</dd>
88+
<dt id="h"><a class="permalink" href="#h"><code class="Fl">-h</code></a></dt>
89+
<dd>Display this manpage.</dd>
8190
<dt id="k"><a class="permalink" href="#k"><code class="Fl">-k</code></a></dt>
8291
<dd>Attempt kprobe as the backend.</dd>
8392
<dt id="l"><a class="permalink" href="#l"><code class="Fl">-l</code></a>
@@ -171,7 +180,7 @@ <h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
171180
</div>
172181
<table class="foot">
173182
<tr>
174-
<td class="foot-date">October 25, 2024</td>
183+
<td class="foot-date">October 28, 2024</td>
175184
<td class="foot-os">Linux</td>
176185
</tr>
177186
</table>

docs/quark-test.8.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ <h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a><
2929
</tr>
3030
</table>
3131
<br/>
32+
<table class="Nm">
33+
<tr>
34+
<td><code class="Nm">quark-test</code></td>
35+
<td><code class="Fl">-h</code></td>
36+
</tr>
37+
</table>
38+
<br/>
3239
<table class="Nm">
3340
<tr>
3441
<td><code class="Nm">quark-test <code class="Fl">-l</code></code></td>
@@ -64,6 +71,8 @@ <h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIP
6471
<dl class="Bl-tag">
6572
<dt id="b"><a class="permalink" href="#b"><code class="Fl">-b</code></a></dt>
6673
<dd>Run only EBPF tests.</dd>
74+
<dt id="h"><a class="permalink" href="#h"><code class="Fl">-h</code></a></dt>
75+
<dd>Display this manpage.</dd>
6776
<dt id="k"><a class="permalink" href="#k"><code class="Fl">-k</code></a></dt>
6877
<dd>Run only KPROBE tests.</dd>
6978
<dt id="l"><a class="permalink" href="#l"><code class="Fl">-l</code></a></dt>
@@ -120,7 +129,7 @@ <h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
120129
</div>
121130
<table class="foot">
122131
<tr>
123-
<td class="foot-date">October 25, 2024</td>
132+
<td class="foot-date">October 28, 2024</td>
124133
<td class="foot-os">Linux</td>
125134
</tr>
126135
</table>

man-embedder.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
/* Copyright (c) 2024 Elastic NV */
3+
4+
#include <err.h>
5+
#include <errno.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
#include <strings.h>
10+
#include <unistd.h>
11+
12+
static void
13+
usage(void)
14+
{
15+
fprintf(stderr, "usage: %s input_file ifdef_name\n",
16+
program_invocation_short_name);
17+
18+
exit(1);
19+
}
20+
21+
static int
22+
embed(const char *input_path, const char *ifdef_name)
23+
{
24+
FILE *input;
25+
int ch, line_wrap;
26+
27+
if ((input = fopen(input_path, "r")) == NULL)
28+
err(1, "fopen");
29+
30+
if (ifdef_name != NULL)
31+
printf("#ifdef %s\n", ifdef_name);
32+
printf("const char manpage_bin[] = {\n");
33+
34+
line_wrap = 0;
35+
while ((ch = fgetc(input)) != EOF) {
36+
if (line_wrap == 0)
37+
putchar('\t');
38+
printf("0x%02x, ", ch);
39+
if (++line_wrap == 10) {
40+
putchar('\n');
41+
line_wrap = 0;
42+
}
43+
}
44+
if (ferror(input))
45+
errx(1, "input error");
46+
fclose(input);
47+
48+
printf("\n};\n");
49+
if (ifdef_name != NULL)
50+
printf("#endif /* %s */", ifdef_name);
51+
putchar('\n');
52+
53+
return (0);
54+
}
55+
56+
int
57+
main(int argc, char *argv[])
58+
{
59+
if (argc != 3)
60+
usage();
61+
62+
return (embed(argv[1], argv[2]));
63+
}

quark-btf.8

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
.Nm quark-btf
1717
.Op Fl v
1818
.Fl g Ar btf_file name version
19+
.Nm quark-btf
20+
.Fl h
1921
.Nm quark-btf Fl V
2022
.Sh DESCRIPTION
2123
The
@@ -45,11 +47,14 @@ is a human identifier, like ubuntu-22.
4547
is the kernel version as returned by
4648
.Em uname -r .
4749
.El
48-
This option only used to generate
50+
.Pp
51+
This option is only used to generate
4952
.Pa btfhub.c
5053
via
5154
.Pa genbtf.sh ,
5255
chances are you'll never need this.
56+
.It Fl h
57+
Display this manpage.
5358
.It Fl l Ar version
5459
Lookup the kernel
5560
.Em version

quark-btf.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@
99
#include <strings.h>
1010
#include <unistd.h>
1111

12+
#include <sys/wait.h>
13+
1214
#include "quark.h"
1315

16+
#define MAN_QUARK_BTF
17+
#include "manpages.h"
18+
1419
static int bflag;
1520
static int fflag;
1621
static int gflag;
@@ -29,6 +34,8 @@ disply_version(void)
2934
static void
3035
usage(void)
3136
{
37+
fprintf(stderr, "usage: %s -h\n",
38+
program_invocation_short_name);
3239
fprintf(stderr, "usage: %s [-bv] [targets...]\n",
3340
program_invocation_short_name);
3441
fprintf(stderr, "usage: %s [-bv] [-f btf_file]\n",
@@ -229,14 +236,20 @@ main(int argc, char *argv[])
229236
int ch;
230237
const char *path = NULL;
231238

232-
while ((ch = getopt(argc, argv, "bf:glvV")) != -1) {
239+
while ((ch = getopt(argc, argv, "bf:ghlvV")) != -1) {
233240
switch (ch) {
234241
case 'b':
235242
bflag = 1;
236243
break;
237244
case 'g':
238245
gflag = 1;
239246
break;
247+
case 'h':
248+
if (isatty(STDOUT_FILENO))
249+
display_man();
250+
else
251+
usage();
252+
break; /* NOTREACHED */
240253
case 'l':
241254
lflag = 1;
242255
break;

quark-mon.8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
.Op Fl C Ar filename
1111
.Op Fl l Ar maxlength
1212
.Op Fl m Ar maxnodes
13+
.Nm quark-mon
14+
.Fl h
1315
.Nm quark-mon Fl V
1416
.Sh DESCRIPTION
1517
The
@@ -56,6 +58,8 @@ it is Elastic/ECS specific.
5658
Use minimal aggregation, fork, exec and exit will
5759
.Em not
5860
be aggregated.
61+
.It Fl h
62+
Display this manpage.
5963
.It Fl k
6064
Attempt kprobe as the backend.
6165
.It Fl l Ar maxlength

0 commit comments

Comments
 (0)