/*
 * livepatch_main.c - kernel live patch main infrastructure
 *
 * Copyright (c) 2014 SUSE
 *  Author: Miroslav Benes <mbenes@suse.cz>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/livepatch.h>
#include <linux/module.h>
#include <linux/types.h>

#include "uname_patch/livepatch_uname.h"

/* Auto expanded KLP_PATCHES_INCLUDES: */
#include "bsc1259859/livepatch_bsc1259859.h"


static struct klp_object objs[] = {
	/* Auto expanded KLP_PATCHES_OBJS: */
	{
		.name = NULL,
		.funcs = (struct klp_func[]) {
			{
			  .old_name = __stringify(KLP_SYSCALL_SYM(newuname)),
			  .new_func = KLP_SYSCALL_SYM(klp_newuname),
			},
#ifdef KLP_ARCH_HAS_SYSCALL_COMPAT_STUBS
			{
			  .old_name = __stringify(KLP_SYSCALL_COMPAT_STUB_SYM(newuname)),
			  .new_func = KLP_SYSCALL_COMPAT_STUB_SYM(klp_newuname),
			},
#endif
			{ .old_name = __stringify(aa_unpack), .new_func = klpp_aa_unpack, },
			{ .old_name = __stringify(unpack_pdb), .new_func = klpp_unpack_pdb, },
			{ .old_name = __stringify(__aa_profile_list_release), .new_func = klpp___aa_profile_list_release, },
			{ .old_name = __stringify(aa_remove_profiles), .new_func = klpp_aa_remove_profiles, },
			{ .old_name = __stringify(aa_replace_profiles), .new_func = klpp_aa_replace_profiles, },
			{ .old_name = __stringify(__aa_create_ns), .new_func = klpp___aa_create_ns, },
			{ .old_name = __stringify(aa_dfa_match), .new_func = klpp_aa_dfa_match, },
			{ .old_name = __stringify(aa_dfa_match_len), .new_func = klpp_aa_dfa_match_len, },
			{ .old_name = __stringify(aa_dfa_unpack), .new_func = klpp_aa_dfa_unpack, },
			{ .old_name = __stringify(ns_mkdir_op), .new_func = klpp_ns_mkdir_op, },
			{ .old_name = __stringify(ns_rmdir_op), .new_func = klpp_ns_rmdir_op, },
			{ .old_name = __stringify(profile_load), .new_func = klpp_profile_load, },
			{ .old_name = __stringify(profile_remove), .new_func = klpp_profile_remove, },
			{ .old_name = __stringify(profile_replace), .new_func = klpp_profile_replace, },
			{ }
		}
	},
	{ }
};

static struct klp_patch patch = {
	.mod = THIS_MODULE,
	.objs = objs,
	.replace = true,
};

static int __init klp_patch_init(void)
{
	int retval;

	pr_info("livepatch: initializing\n");

	/* Auto expanded KLP_PATCHES_INIT_CALLS: */
	retval = livepatch_bsc1259859_init();
	if (retval)
		goto err_bsc1259859;

	retval = klp_enable_patch(&patch);
	if (!retval)
		return retval;

	/* Auto expanded KLP_PATCHES_INIT_ERR_HANDLERS: */
	livepatch_bsc1259859_cleanup();
err_bsc1259859:

	return retval;
}

static void __exit klp_patch_cleanup(void)
{
	pr_info("livepatch: removed\n");

	/* Auto expanded KLP_PATCHES_CLEANUP_CALLS: */
	livepatch_bsc1259859_cleanup();

}

module_init(klp_patch_init);
module_exit(klp_patch_cleanup);

MODULE_LICENSE("GPL");
MODULE_INFO(livepatch, "Y");
MODULE_INFO(klpgitrev, "a63a3cf0192faf11e0a1465d9e8103d3a6f62b62");
