]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/seccomp-util.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / shared / seccomp-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2014 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <seccomp.h>
23
24 #include "string-util.h"
25 #include "util.h"
26 #include "seccomp-util.h"
27
28 const char* seccomp_arch_to_string(uint32_t c) {
29
30 if (c == SCMP_ARCH_NATIVE)
31 return "native";
32 if (c == SCMP_ARCH_X86)
33 return "x86";
34 if (c == SCMP_ARCH_X86_64)
35 return "x86-64";
36 if (c == SCMP_ARCH_X32)
37 return "x32";
38 if (c == SCMP_ARCH_ARM)
39 return "arm";
40
41 return NULL;
42 }
43
44 int seccomp_arch_from_string(const char *n, uint32_t *ret) {
45 if (!n)
46 return -EINVAL;
47
48 assert(ret);
49
50 if (streq(n, "native"))
51 *ret = SCMP_ARCH_NATIVE;
52 else if (streq(n, "x86"))
53 *ret = SCMP_ARCH_X86;
54 else if (streq(n, "x86-64"))
55 *ret = SCMP_ARCH_X86_64;
56 else if (streq(n, "x32"))
57 *ret = SCMP_ARCH_X32;
58 else if (streq(n, "arm"))
59 *ret = SCMP_ARCH_ARM;
60 else
61 return -EINVAL;
62
63 return 0;
64 }
65
66 int seccomp_add_secondary_archs(scmp_filter_ctx *c) {
67
68 #if defined(__i386__) || defined(__x86_64__)
69 int r;
70
71 /* Add in all possible secondary archs we are aware of that
72 * this kernel might support. */
73
74 r = seccomp_arch_add(c, SCMP_ARCH_X86);
75 if (r < 0 && r != -EEXIST)
76 return r;
77
78 r = seccomp_arch_add(c, SCMP_ARCH_X86_64);
79 if (r < 0 && r != -EEXIST)
80 return r;
81
82 r = seccomp_arch_add(c, SCMP_ARCH_X32);
83 if (r < 0 && r != -EEXIST)
84 return r;
85
86 #endif
87
88 return 0;
89
90 }