]> granicus.if.org Git - strace/commitdiff
seccomp.c: use print_array function
authorDmitry V. Levin <ldv@altlinux.org>
Sat, 7 May 2016 23:11:52 +0000 (23:11 +0000)
committerDmitry V. Levin <ldv@altlinux.org>
Sat, 7 May 2016 23:37:53 +0000 (23:37 +0000)
* seccomp.c (decode_filter, decode_seccomp_fprog): Remove.
(print_bpf_filter): New function.
(print_seccomp_fprog): Use it via print_array.
* tests/seccomp-filter-v.c (main): Update.

seccomp.c
tests/seccomp-filter-v.c

index e62f99a0c711429486060306dace58ff6a924e77..5d972f97f858d4f22ab4c8b4c76051ddeda38ce8 100644 (file)
--- a/seccomp.c
+++ b/seccomp.c
@@ -108,6 +108,8 @@ decode_bpf_code(uint16_t code)
 
 }
 
+#endif /* HAVE_LINUX_FILTER_H */
+
 static void
 decode_bpf_stmt(const struct bpf_filter *filter)
 {
@@ -146,54 +148,44 @@ decode_bpf_jump(const struct bpf_filter *filter)
 #endif /* HAVE_LINUX_FILTER_H */
 }
 
-static void
-decode_filter(const struct bpf_filter *filter)
-{
-       if (filter->jt || filter->jf)
-               decode_bpf_jump(filter);
-       else
-               decode_bpf_stmt(filter);
-}
-
-#endif /* HAVE_LINUX_FILTER_H */
-
 #ifndef BPF_MAXINSNS
 # define BPF_MAXINSNS 4096
 #endif
 
-static void
-decode_seccomp_fprog(struct tcb *tcp, unsigned short len, unsigned long addr)
+static bool
+print_bpf_filter(struct tcb *tcp, void *elem_buf, size_t elem_size, void *data)
 {
-       struct bpf_filter filter;
-       const unsigned long start_addr = addr;
-       unsigned int i = 0;
-
-       for (; addr >= start_addr && i < len; ++i, addr += sizeof(filter)) {
-               if (i) {
-                       tprints(", ");
-                       if (i >= BPF_MAXINSNS) {
-                               tprints("...");
-                               break;
-                       }
-               }
-               if (umove_or_printaddr(tcp, addr, &filter))
-                       break;
-               if (!i)
-                       tprints("[");
-               decode_filter(&filter);
+       const struct bpf_filter *filter = elem_buf;
+       unsigned int *pn = data;
+
+       if ((*pn)++ >= BPF_MAXINSNS) {
+               tprints("...");
+               return false;
        }
-       if (i)
-               tprints("]");
+
+       if (filter->jt || filter->jf)
+               decode_bpf_jump(filter);
+       else
+               decode_bpf_stmt(filter);
+
+       return true;
 }
 
 static void
-print_seccomp_fprog(struct tcb *tcp, unsigned short len, unsigned long addr)
+print_seccomp_fprog(struct tcb *tcp, unsigned long addr, unsigned short len)
 {
        tprintf("{len=%u, filter=", len);
-       if (abbrev(tcp) || !len)
+
+       if (abbrev(tcp)) {
                printaddr(addr);
-       else
-               decode_seccomp_fprog(tcp, len, addr);
+       } else {
+               unsigned int insns = 0;
+               struct bpf_filter filter;
+
+               print_array(tcp, addr, len, &filter, sizeof(filter),
+                           umoven_or_printaddr, print_bpf_filter, &insns);
+       }
+
        tprints("}");
 }
 
@@ -205,7 +197,7 @@ print_seccomp_filter(struct tcb *tcp, unsigned long addr)
        struct seccomp_fprog fprog;
 
        if (fetch_seccomp_fprog(tcp, addr, &fprog))
-               print_seccomp_fprog(tcp, fprog.len, fprog.filter);
+               print_seccomp_fprog(tcp, fprog.filter, fprog.len);
 }
 
 static void
index 9a7065853d361d3af62e8821a7490cce41308291..ef69c0dcac7b272c182e309804544de51c1b29af 100644 (file)
@@ -128,8 +128,8 @@ main(void)
 
        prog->len = 0;
        syscall(__NR_seccomp, SECCOMP_SET_MODE_FILTER, 0, prog);
-       tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=0, filter=%p})"
-               " = -1 EINVAL (%m)\n", prog->filter);
+       tprintf("seccomp(SECCOMP_SET_MODE_FILTER, 0, {len=0, filter=[]})"
+               " = -1 EINVAL (%m)\n");
 
        unsigned int i;
        for (i = 0; i <= BPF_MAXINSNS; ++i) {