From 99dfe2f520a1d9c7f20f6e9a0f4a3566ff6b34f0 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sat, 4 Apr 2020 17:32:52 +0100 Subject: [PATCH] Extract common data structures to ksudoku header file --- .gitignore | 1 + ksudoku.c | 64 +++++++++++++++++++------------------ ksudoku.h | 76 ++++++++++++++++++++++---------------------- tools/boot_kernel.sh | 10 ++++++ tools/create_fs.sh | 12 +++++++ 5 files changed, 94 insertions(+), 69 deletions(-) create mode 100755 tools/boot_kernel.sh create mode 100755 tools/create_fs.sh diff --git a/.gitignore b/.gitignore index d917823..2efdb2e 100644 --- a/.gitignore +++ b/.gitignore @@ -92,3 +92,4 @@ extra_certificates signing_key.priv signing_key.x509 x509.genkey +*.mod \ No newline at end of file diff --git a/ksudoku.c b/ksudoku.c index a39c4f8..1e35d37 100644 --- a/ksudoku.c +++ b/ksudoku.c @@ -1,34 +1,27 @@ -#include -#include -#include -#include -#include +/* + * KSudoku subsystem for Linux kernel to solve sudoku with different + * algorithms. + * Copyright (C) 2020 Sameer Rahmani + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ +#include "ksudoku.h" #define KSUDOKU_VERSION "0.1" #define KSUDOKU_DESC "Kernel Sudoku Module" -#define KSUDOKU_READY 0 -#define KSUDOKU_BUSY 1 - -struct ksudoku { - struct kobject kobj; - const struct attribute_group *attr_group; - char *matrix; - atomic_t status; -}; -#define to_ksudoku(obj) container_of(obj, struct ksudoku, kobj) - - -struct ksudoku_attribute { - struct attribute attr; - ssize_t (*show)(struct ksudoku *s, struct ksudoku_attribute *attr, - char *buf); - ssize_t (*store)(struct ksudoku *s, struct ksudoku_attribute *attr, - const char *buf, size_t count); - bool value; -}; -#define to_ksudoku_attr(_attr) container_of(_attr, struct ksudoku_attribute, attr) - /* * The default show callback that is called by sysfs when ever a read * operation happens on any attribute on ksudoku subsystem. @@ -165,7 +158,11 @@ static struct kobj_type ksudoku_type = { static struct kset *ksudoku_set; -static struct ksudoku *create_ksudoku(const char *name) +/** + * ksudoku_create_sudoku - create new sudoku with under ksudoku subsystem. + * @name: name of the sudoku to create. + */ +struct ksudoku *ksudoku_create_sudoku(const char *name) { struct ksudoku *s; int result; @@ -190,12 +187,17 @@ static struct ksudoku *create_ksudoku(const char *name) kobject_uevent(&s->kobj, KOBJ_ADD); return s; } +EXPORT_SYMBOL_GPL(ksudoku_create_sudoku); -static void destroy_ksudoku(struct ksudoku *s) +/** + * ksudoku_destroy_ksudoku - destroys the given ksudoku instance. + * @sudoku: Pointer to a ksudoku instance to destroy. + */ +void ksudoku_destroy_ksudoku(struct ksudoku *sudoku) { - kobject_put(&s->kobj); + kobject_put(&sudoku->kobj); } - +EXPORT_SYMBOL_GPL(ksudoku_destroy_ksudoku); static int ksudoku_init(void) { diff --git a/ksudoku.h b/ksudoku.h index 6bede36..de3cfe7 100644 --- a/ksudoku.h +++ b/ksudoku.h @@ -1,3 +1,22 @@ +/* + * KSudoku subsystem for Linux kernel to solve sudoku with different + * algorithms. + * Copyright (C) 2020 Sameer Rahmani + * + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ #ifndef _KSUDOKU_H_ #define _KSUDOKU_H_ @@ -5,50 +24,31 @@ #include #include #include +#include -struct ksudoku; - -#define to_ksudoku(obj) container_of(obj, struct ksudoku, kobj) -#define to_ksudoku_attr(_attr) container_of(_attr, struct kobj_attribute, attr) - -struct sudoku_attribute { - struct attribute attr; - ssize_t (*show)(struct kobject *kobj, struct attribute *attr, - char *buf); - ssize_t (*store)(struct kobject *kobj, struct kobj_attribute *attr, - const char *buf, size_t count); - bool value; -}; - -/* - * The type of sudoku, "struct sudoku" is embedded in. A class - * or bus can only contain one sudoku type. - * This identifies the sudoku type and carries type-specific - * information, equivalent to the kobj_type of a kobject. - * Since we have only one type it always will be the same button - * we're designing a subsystem so we have to make sysfs happy. - * If "name" is specified, the uevent will contain it in - * the DEVTYPE variable. - */ -/* struct sudoku_type { */ -/* const char *name; */ -/* const struct attribute_group **groups; */ -/* int (*uevent)(struct sudoku *s, struct kobj_uevent_env *env); */ -/* void (*release)(struct kobject *kobj); */ -/* const struct sysfs_ops *sysfs_ops; */ -/* }; */ +#define KSUDOKU_READY 0 +#define KSUDOKU_BUSY 1 struct ksudoku { - struct kobject *kobj; + struct kobject kobj; const struct attribute_group *attr_group; - char matrix[81]; + char *matrix; atomic_t status; -} ; +}; +#define to_ksudoku(obj) container_of(obj, struct ksudoku, kobj) -#define SUDOKU_ATTR_RO(_name) \ - struct kobj_attribute sudoku_##_name = __ATTR_RO(_name) -#define SUDOKU_ATTR_WO(_name) \ - struct kobj_attribute sudoku_##_name = __ATTR_WO(_name) +struct ksudoku_attribute { + struct attribute attr; + ssize_t (*show)(struct ksudoku *s, struct ksudoku_attribute *attr, + char *buf); + ssize_t (*store)(struct ksudoku *s, struct ksudoku_attribute *attr, + const char *buf, size_t count); +}; + +#define to_ksudoku_attr(_attr) container_of(_attr, struct ksudoku_attribute, attr) + +extern struct ksudoku *ksudoku_create_sudoku(const char *name); +extern void ksudoku_destroy_ksudoku(struct ksudoku *sudoku); #endif diff --git a/tools/boot_kernel.sh b/tools/boot_kernel.sh new file mode 100755 index 0000000..8f57fa4 --- /dev/null +++ b/tools/boot_kernel.sh @@ -0,0 +1,10 @@ +#! /bin/bash + +qemu-system-x86_64 \ + -kernel /boot/vmlinuz-`uname -r` \ + -nographic \ + -append "console=ttyS0 nokaslr init=/bin/bash" \ + -initrd `pwd`/ram.disk \ + -m 512 \ + --enable-kvm \ + -cpu host diff --git a/tools/create_fs.sh b/tools/create_fs.sh new file mode 100755 index 0000000..9da2f03 --- /dev/null +++ b/tools/create_fs.sh @@ -0,0 +1,12 @@ +#! /bin/bash + +IMG=`pwd`/qemu-image.img +DIR=`pwd`/mounted_fs + +qemu-img create $IMG 1g +sudo mkfs.ext2 $IMG +mkdir $DIR +sudo mount -o loop $IMG $DIR +sudo debootstrap --arch amd64 buster $DIR +sudo umount $DIR +rmdir $DIR