Blame tools/scmp_sys_resolver.c

Packit 56e23f
/**
Packit 56e23f
 * Syscall resolver
Packit 56e23f
 *
Packit 56e23f
 * Copyright (c) 2012 Red Hat <pmoore@redhat.com>
Packit 56e23f
 * Author: Paul Moore <paul@paul-moore.com>
Packit 56e23f
 */
Packit 56e23f
Packit 56e23f
/*
Packit 56e23f
 * This library is free software; you can redistribute it and/or modify it
Packit 56e23f
 * under the terms of version 2.1 of the GNU Lesser General Public License as
Packit 56e23f
 * published by the Free Software Foundation.
Packit 56e23f
 *
Packit 56e23f
 * This library is distributed in the hope that it will be useful, but WITHOUT
Packit 56e23f
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Packit 56e23f
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
Packit 56e23f
 * for more details.
Packit 56e23f
 *
Packit 56e23f
 * You should have received a copy of the GNU Lesser General Public License
Packit 56e23f
 * along with this library; if not, see <http://www.gnu.org/licenses>.
Packit 56e23f
 */
Packit 56e23f
Packit 56e23f
#include <errno.h>
Packit 56e23f
#include <stdlib.h>
Packit 56e23f
#include <stdio.h>
Packit 56e23f
#include <ctype.h>
Packit 56e23f
#include <string.h>
Packit 56e23f
#include <unistd.h>
Packit 56e23f
Packit 56e23f
#include <seccomp.h>
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * Print the usage information to stderr and exit
Packit 56e23f
 * @param program the name of the current program being invoked
Packit 56e23f
 *
Packit 56e23f
 * Print the usage information and exit with EINVAL.
Packit 56e23f
 *
Packit 56e23f
 */
Packit 56e23f
static void exit_usage(const char *program)
Packit 56e23f
{
Packit 56e23f
	fprintf(stderr,
Packit 56e23f
		"usage: %s [-h] [-a <arch>] [-t] <name>|<number>\n",
Packit 56e23f
		program);
Packit 56e23f
	exit(EINVAL);
Packit 56e23f
}
Packit 56e23f
Packit 56e23f
/**
Packit 56e23f
 * main
Packit 56e23f
 */
Packit 56e23f
int main(int argc, char *argv[])
Packit 56e23f
{
Packit 56e23f
	int opt;
Packit 56e23f
	int translate = 0;
Packit 56e23f
	uint32_t arch;
Packit 56e23f
	int sys_num;
Packit 56e23f
	const char *sys_name;
Packit 56e23f
Packit 56e23f
	arch = seccomp_arch_native();
Packit 56e23f
Packit 56e23f
	/* parse the command line */
Packit 56e23f
	while ((opt = getopt(argc, argv, "a:ht")) > 0) {
Packit 56e23f
		switch (opt) {
Packit 56e23f
		case 'a':
Packit 56e23f
			arch = seccomp_arch_resolve_name(optarg);
Packit 56e23f
			if (arch == 0)
Packit 56e23f
				exit_usage(argv[0]);
Packit 56e23f
			break;
Packit 56e23f
		case 't':
Packit 56e23f
			translate = 1;
Packit 56e23f
			break;
Packit 56e23f
		case 'h':
Packit 56e23f
		default:
Packit 56e23f
			/* usage information */
Packit 56e23f
			exit_usage(argv[0]);
Packit 56e23f
		}
Packit 56e23f
	}
Packit 56e23f
Packit 56e23f
	/* sanity checks */
Packit 56e23f
	if (optind >= argc)
Packit 56e23f
		exit_usage(argv[0]);
Packit 56e23f
Packit 56e23f
	/* perform the syscall lookup */
Packit 56e23f
	if (isdigit(argv[optind][0]) || argv[optind][0] == '-') {
Packit 56e23f
		sys_num = atoi(argv[optind]);
Packit 56e23f
		sys_name = seccomp_syscall_resolve_num_arch(arch, sys_num);
Packit 56e23f
		printf("%s\n", (sys_name ? sys_name : "UNKNOWN"));
Packit 56e23f
	} else if (translate) {
Packit 56e23f
		sys_num = seccomp_syscall_resolve_name_rewrite(arch,
Packit 56e23f
							       argv[optind]);
Packit 56e23f
		printf("%d\n", sys_num);
Packit 56e23f
	} else {
Packit 56e23f
		sys_num = seccomp_syscall_resolve_name_arch(arch, argv[optind]);
Packit 56e23f
		printf("%d\n", sys_num);
Packit 56e23f
	}
Packit 56e23f
Packit 56e23f
	return 0;
Packit 56e23f
}