Skip to content

Commit e4b2263

Browse files
Rtoaxdkruces
authored andcommitted
libbpf-tools/mountsnoop: Use union to pass arguments
Ready to introduce fsopen(2),fsconfig(2),fsmount(2),move_mount(2) syscall support. Signed-off-by: Rong Tao <[email protected]>
1 parent c65ee7d commit e4b2263

File tree

3 files changed

+89
-43
lines changed

3 files changed

+89
-43
lines changed

libbpf-tools/mountsnoop.bpf.c

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,21 @@ static int probe_entry(const char *src, const char *dest, const char *fs,
3131
return 0;
3232

3333
arg.ts = bpf_ktime_get_ns();
34-
arg.flags = flags;
35-
arg.src = src;
36-
arg.dest = dest;
37-
arg.fs = fs;
38-
arg.data= data;
3934
arg.op = op;
35+
36+
switch (op) {
37+
case MOUNT:
38+
arg.mount.flags = flags;
39+
arg.mount.src = src;
40+
arg.mount.dest = dest;
41+
arg.mount.fs = fs;
42+
arg.mount.data= data;
43+
break;
44+
case UMOUNT:
45+
arg.umount.flags = flags;
46+
arg.umount.dest = dest;
47+
break;
48+
}
4049
bpf_map_update_elem(&args, &tid, &arg, BPF_ANY);
4150
return 0;
4251
};
@@ -60,29 +69,26 @@ static int probe_exit(void *ctx, int ret)
6069

6170
task = (struct task_struct *)bpf_get_current_task();
6271
eventp->delta = bpf_ktime_get_ns() - argp->ts;
63-
eventp->flags = argp->flags;
72+
eventp->op = argp->op;
6473
eventp->pid = pid;
6574
eventp->tid = tid;
6675
eventp->mnt_ns = BPF_CORE_READ(task, nsproxy, mnt_ns, ns.inum);
6776
eventp->ret = ret;
68-
eventp->op = argp->op;
6977
bpf_get_current_comm(&eventp->comm, sizeof(eventp->comm));
70-
if (argp->src)
71-
bpf_probe_read_user_str(eventp->src, sizeof(eventp->src), argp->src);
72-
else
73-
eventp->src[0] = '\0';
74-
if (argp->dest)
75-
bpf_probe_read_user_str(eventp->dest, sizeof(eventp->dest), argp->dest);
76-
else
77-
eventp->dest[0] = '\0';
78-
if (argp->fs)
79-
bpf_probe_read_user_str(eventp->fs, sizeof(eventp->fs), argp->fs);
80-
else
81-
eventp->fs[0] = '\0';
82-
if (argp->data)
83-
bpf_probe_read_user_str(eventp->data, sizeof(eventp->data), argp->data);
84-
else
85-
eventp->data[0] = '\0';
78+
79+
switch (argp->op) {
80+
case MOUNT:
81+
eventp->mount.flags = argp->mount.flags;
82+
bpf_probe_read_user_str(eventp->mount.src, sizeof(eventp->mount.src), argp->mount.src);
83+
bpf_probe_read_user_str(eventp->mount.dest, sizeof(eventp->mount.dest), argp->mount.dest);
84+
bpf_probe_read_user_str(eventp->mount.fs, sizeof(eventp->mount.fs), argp->mount.fs);
85+
bpf_probe_read_user_str(eventp->mount.data, sizeof(eventp->mount.data), argp->mount.data);
86+
break;
87+
case UMOUNT:
88+
eventp->umount.flags = argp->umount.flags;
89+
bpf_probe_read_user_str(eventp->umount.dest, sizeof(eventp->umount.dest), argp->umount.dest);
90+
break;
91+
}
8692

8793
submit_buf(ctx, eventp, sizeof(*eventp));
8894

libbpf-tools/mountsnoop.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,20 @@ static const char *gen_call(const struct event *e)
185185
static char call[10240];
186186

187187
memset(call, 0, sizeof(call));
188-
if (e->op == UMOUNT) {
188+
switch (e->op) {
189+
case UMOUNT:
189190
snprintf(call, sizeof(call), "umount(\"%s\", %s) = %s",
190-
e->dest, strflags(e->flags), strerrno(e->ret));
191-
} else {
191+
e->umount.dest, strflags(e->umount.flags),
192+
strerrno(e->ret));
193+
break;
194+
case MOUNT:
192195
snprintf(call, sizeof(call), "mount(\"%s\", \"%s\", \"%s\", %s, \"%s\") = %s",
193-
e->src, e->dest, e->fs, strflags(e->flags), e->data, strerrno(e->ret));
196+
e->mount.src, e->mount.dest, e->mount.fs,
197+
strflags(e->mount.flags), e->mount.data,
198+
strerrno(e->ret));
199+
break;
200+
default:
201+
break;
194202
}
195203
return call;
196204
}
@@ -230,11 +238,21 @@ static int handle_event(void *ctx, void *data, size_t len)
230238
printf("%sRET: %s\n", indent, strerrno(e->ret));
231239
printf("%sLAT: %lldus\n", indent, e->delta / 1000);
232240
printf("%sMNT_NS: %u\n", indent, e->mnt_ns);
233-
printf("%sFS: %s\n", indent, e->fs);
234-
printf("%sSOURCE: %s\n", indent, e->src);
235-
printf("%sTARGET: %s\n", indent, e->dest);
236-
printf("%sDATA: %s\n", indent, e->data);
237-
printf("%sFLAGS: %s\n", indent, strflags(e->flags));
241+
switch (e->op) {
242+
case MOUNT:
243+
printf("%sFS: %s\n", indent, e->mount.fs);
244+
printf("%sSOURCE: %s\n", indent, e->mount.src);
245+
printf("%sTARGET: %s\n", indent, e->mount.dest);
246+
printf("%sDATA: %s\n", indent, e->mount.data);
247+
printf("%sFLAGS: %s\n", indent, strflags(e->mount.flags));
248+
break;
249+
case UMOUNT:
250+
printf("%sTARGET: %s\n", indent, e->umount.dest);
251+
printf("%sFLAGS: %s\n", indent, strflags(e->umount.flags));
252+
break;
253+
default:
254+
break;
255+
}
238256
printf("\n");
239257

240258
return 0;

libbpf-tools/mountsnoop.h

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,55 @@
88
#define PATH_MAX 4096
99

1010
enum op {
11+
OP_MIN, /* skip 0 */
1112
MOUNT,
1213
UMOUNT,
1314
};
1415

1516
struct arg {
1617
__u64 ts;
17-
__u64 flags;
18-
const char *src;
19-
const char *dest;
20-
const char *fs;
21-
const char *data;
2218
enum op op;
19+
20+
union {
21+
/* op=MOUNT */
22+
struct {
23+
__u64 flags;
24+
const char *src;
25+
const char *dest;
26+
const char *fs;
27+
const char *data;
28+
} mount;
29+
/* op=UMOUNT */
30+
struct {
31+
__u64 flags;
32+
const char *dest;
33+
} umount;
34+
};
2335
};
2436

2537
struct event {
2638
__u64 delta;
27-
__u64 flags;
2839
__u32 pid;
2940
__u32 tid;
3041
unsigned int mnt_ns;
3142
int ret;
32-
char comm[TASK_COMM_LEN];
33-
char fs[FS_NAME_LEN];
34-
char src[PATH_MAX];
35-
char dest[PATH_MAX];
36-
char data[DATA_LEN];
3743
enum op op;
44+
char comm[TASK_COMM_LEN];
45+
union {
46+
/* op=MOUNT */
47+
struct {
48+
__u64 flags;
49+
char fs[FS_NAME_LEN];
50+
char src[PATH_MAX];
51+
char dest[PATH_MAX];
52+
char data[DATA_LEN];
53+
} mount;
54+
/* op=UMOUNT */
55+
struct {
56+
__u64 flags;
57+
char dest[PATH_MAX];
58+
} umount;
59+
};
3860
};
3961

4062
#endif /* __MOUNTSNOOP_H */

0 commit comments

Comments
 (0)