]>
iEval git - linux-seccomp.git/blob - libseccomp/tests/util.c
9c069d600df256d597708920987ab583f368c666
2 * Seccomp Library utility code for tests
4 * Copyright (c) 2012 Red Hat <eparis@redhat.com>
5 * Author: Eric Paris <eparis@redhat.com>
9 * This library is free software; you can redistribute it and/or modify it
10 * under the terms of version 2.1 of the GNU Lesser General Public License as
11 * published by the Free Software Foundation.
13 * This library is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this library; if not, see <http://www.gnu.org/licenses>.
29 #include <sys/types.h>
37 * SIGSYS signal handler
38 * @param nr the signal number
39 * @param info siginfo_t pointer
40 * @param void_context handler context
42 * Simple signal handler for SIGSYS which exits with error code 161.
45 static void _trap_handler(int signal
, siginfo_t
*info
, void *ctx
)
51 * Parse the arguments passed to main
52 * @param argc the argument count
53 * @param argv the argument pointer
54 * @param opts the options structure
56 * This function parses the arguments passed to the test from the command line.
57 * Returns zero on success and negative values on failure.
60 int util_getopt(int argc
, char *argv
[], struct util_options
*opts
)
67 memset(opts
, 0, sizeof(*opts
));
69 int c
, option_index
= 0;
70 const struct option long_options
[] = {
71 {"bpf", no_argument
, &(opts
->bpf_flg
), 1},
72 {"pfc", no_argument
, &(opts
->bpf_flg
), 0},
76 c
= getopt_long(argc
, argv
, "bp",
77 long_options
, &option_index
);
96 if (rc
== -EINVAL
|| optind
< argc
) {
97 fprintf(stderr
, "usage %s: [--bpf,-b] [--pfc,-p]\n", argv
[0]);
105 * Output the filter in either BPF or PFC
106 * @param opts the options structure
107 * @param ctx the filter context
109 * This function outputs the seccomp filter to stdout in either BPF or PFC
110 * format depending on the test paramaeters supplied by @opts.
113 int util_filter_output(const struct util_options
*opts
,
114 const scmp_filter_ctx ctx
)
122 rc
= seccomp_export_bpf(ctx
, STDOUT_FILENO
);
124 rc
= seccomp_export_pfc(ctx
, STDOUT_FILENO
);
130 * Install a TRAP action signal handler
132 * This function installs the TRAP action signal handler and is based on
133 * examples from Will Drewry and Kees Cook. Returns zero on success, negative
137 int util_trap_install(void)
139 struct sigaction signal_handler
;
140 sigset_t signal_mask
;
142 memset(&signal_handler
, 0, sizeof(signal_handler
));
143 sigemptyset(&signal_mask
);
144 sigaddset(&signal_mask
, SIGSYS
);
146 signal_handler
.sa_sigaction
= &_trap_handler
;
147 signal_handler
.sa_flags
= SA_SIGINFO
;
148 if (sigaction(SIGSYS
, &signal_handler
, NULL
) < 0)
150 if (sigprocmask(SIG_UNBLOCK
, &signal_mask
, NULL
))
157 * Parse a filter action string into an action value
158 * @param action the action string
160 * Parse a seccomp action string into the associated integer value. Returns
161 * the correct value on success, -1 on failure.
164 int util_action_parse(const char *action
)
169 if (strcasecmp(action
, "KILL") == 0)
170 return SCMP_ACT_KILL
;
171 else if (strcasecmp(action
, "TRAP") == 0)
172 return SCMP_ACT_TRAP
;
173 else if (strcasecmp(action
, "ERRNO") == 0)
174 return SCMP_ACT_ERRNO(163);
175 else if (strcasecmp(action
, "TRACE") == 0)
176 return -1; /* not yet supported */
177 else if (strcasecmp(action
, "ALLOW") == 0)
178 return SCMP_ACT_ALLOW
;
184 * Write a string to a file
185 * @param path the file path
187 * Open the specified file, write a string to the file, and close the file.
188 * Return zero on success, negative values on error.
191 int util_file_write(const char *path
)
194 const char buf
[] = "testing";
195 ssize_t buf_len
= strlen(buf
);
197 fd
= open(path
, O_WRONLY
| O_CREAT
, S_IRUSR
| S_IWUSR
);
200 if (write(fd
, buf
, buf_len
) < buf_len
) {
This page took 0.052175 seconds and 4 git commands to generate.