- Which of the following code snippets correctly defines a Linux kernel module in C?
a)
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int __init my_module_init(void) {
printk(KERN_INFO "Hello, Linux kernel!\n");
return 0;
}
static void __exit my_module_exit(void) {
printk(KERN_INFO "Goodbye, Linux kernel!\n");
}
module_init(my_module_init);
module_exit(my_module_exit);
b)
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int init_module(void) {
printk(KERN_INFO "Hello, Linux kernel!\n");
return 0;
}
static void cleanup_module(void) {
printk(KERN_INFO "Goodbye, Linux kernel!\n");
}
c)
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
void my_module_init(void) {
printk(KERN_INFO "Hello, Linux kernel!\n");
}
void my_module_exit(void) {
printk(KERN_INFO "Goodbye, Linux kernel!\n");
}
module_init(my_module_init);
module_exit(my_module_exit);
d)
#include <linux/module.h>
MODULE_LICENSE("GPL");
static int my_module_init(void) {
printk(KERN_INFO "Hello, Linux kernel!\n");
return 0;
}
static void my_module_exit(void) {
printk(KERN_INFO "Goodbye, Linux kernel!\n");
}
module_init(my_module_init);
module_exit(my_module_exit);
Answer: a)
- Which of the following options correctly initializes a character device in a Linux kernel module written in C?
a)
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = my_read,
.write = my_write,
};
int major_number;
major_number = register_chrdev(0, "mydevice", &fops);
b)
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = my_open,
.release = my_release,
};
int major_number;
major_number = allocate_chrdev_region(&major_number, 0, 1, "mydevice");
c)
static struct file_operations fops = {
.owner = THIS_MODULE,
.read = my_read,
.write = my_write,
};
int major_number;
major_number = alloc_chrdev_region(0, 1, "mydevice");
d)
static struct file_operations fops = {
.owner = THIS_MODULE,
.open = my_open,
.release = my_release,
};
int major_number;
major_number = register_chrdev_region(MKDEV(0, 0), 1, "mydevice");
Answer: a)
- Which of the following C code snippets demonstrates the correct way to allocate memory for a kernel buffer?
a)
char *buffer = (char *)vmalloc(PAGE_SIZE * 2);
b)
char *buffer = kmalloc(PAGE_SIZE * 2, GFP_KERNEL);
c)
char *buffer = (char *)malloc(PAGE_SIZE * 2);
d)
char *buffer = __get_free_pages(GFP_KERNEL, 1);
Answer: b)
- In Linux kernel module development using C, which function should be used to read data from a user-space buffer into kernel memory?
a) copy_from_user
b) read_user_buffer
c) copy_to_user
d) write_user_buffer
Answer: a) copy_from_user
- Which of the following code snippets illustrates the correct way to define a global variable in a Linux kernel module written in C?
a)
int my_global_variable;
b)
static int my_global_variable;
c)
extern int my_global_variable;
d)
register int my_global_variable;
Answer: b)
- In Rust, what does the
#[no_mangle] attribute signify when applied to a function in a Linux kernel module?
a) It indicates that the function should not be linked against other modules.
b) It indicates that the function should be publicly accessible by C code.
c) It disables name mangling for the function.
d) It enables runtime optimization for the function.
Answer: b)
- Which of the following Rust code examples demonstrates the correct way to call a C function from Rust in a Linux kernel module?
a)
extern "C" {
fn my_c_function(arg: i32) -> i32;
}
fn main() {
let result = unsafe { my_c_function(42) };
println!("Result: {}", result);
}
b)
extern "C" {
fn my_c_function(arg: i32);
}
fn main() {
unsafe { my_c_function(42) };
}
c)
extern "C" {
fn my_c_function(arg: i32) -> u16;
}
fn main() {
let result: u16 = unsafe { my_c_function(42) };
println!("Result: {}", result);
}
d)
extern "C" {
fn my_c_function(arg: i32) -> u32;
}
fn main() {
let result: u32 = unsafe { my_c_function(42) };
println!("Result: {}", result);
}
Answer: b)
- In Linux kernel module development using C, which of the following is used to register a callback function to handle a specific interrupt?
a) register_irq_handler
b) request_isr
c) setup_irq
d) request_irq
Answer: d) request_irq
- Which of the following options demonstrates the correct way to unregister a character device in a Linux kernel module written in C?
a)
unregister_chrdev(major_number, "mydevice");
b)
free_chrdev(major_number);
c)
unregister_chrdev_region(major_number, 1);
d)
remove_chrdev(major_number);
Answer: c)
- In Rust, what is the purpose of the
man module in Linux kernel module development?
a) It provides access to Linux manual pages within the kernel.
b) It handles manual page generation for the kernel modules.
c) It represents the manual section within the Linux kernel code.
d) There is no specific man module in Linux kernel module development.
Answer: d)
- Which of the following code snippets demonstrates the correct usage of Linux kernel cryptographic APIs in a Linux kernel module written in C?
a)
#include <linux/crypto.h>
void my_module_init(void) {
struct crypto_skcipher *skcipher;
skcipher = crypto_alloc_skcipher("cbc(aes)", 0, 0);
// ... rest of the code
}
b)
#include <linux/crypto.h>
void my_module_init(void) {
struct crypto_tfm *tfm;
tfm = crypto_alloc_tfm("cbc(aes)", 0);
// ... rest of the code
}
c)
#include <linux/crypto.h>
void my_module_init(void) {
struct crypto_cipher *cipher;
cipher = crypto_alloc_cipher("cbc(aes)", 0, 0);
// ... rest of the code
}
d)
#include <linux/crypto.h>
void my_module_init(void) {
struct crypto_rfc3686_ctx *ctx;
ctx = crypto_alloc_rfc3686_ctx("cbc(aes)", 0, 0);
// ... rest of the code
}
Answer: a)
- Which of the following C code snippets demonstrates the correct way to acquire a spinlock in a Linux kernel module?
a)
spin_lock_init(&my_spinlock);
spin_lock(&my_spinlock);
// ... critical section
spin_unlock(&my_spinlock);
spin_lock_destroy(&my_spinlock);
b)
my_spinlock = SPIN_LOCK_UNLOCKED;
spin_lock(&my_spinlock);
// ... critical section
spin_unlock(&my_spinlock);
spin_lock_destroy(&my_spinlock);
c)
spin_lock_init(my_spinlock);
spin_lock(my_spinlock);
// ... critical section
spin_unlock(my_spinlock);
spin_lock_destroy(my_spinlock);
d)
my_spinlock = SPIN_LOCK_INIT;
spin_lock(my_spinlock);
// ... critical section
spin_unlock(my_spinlock);
spin_lock_destroy(my_spinlock);
Answer: a)
- Which of the following options demonstrates the correct code for defining a struct in Rust that represents a Linux character device?
a)
#[repr(C)]
struct MyDevice {
dev: cdev,
}
b)
#[repr(C)]
struct MyDevice {
cdev: cdev,
}
c)
#[repr(C)]
struct MyDevice {
dev: class_device,
}
d)
#[repr(C)]
struct MyDevice {
cdev: class_device,
}
Answer: b