Skip to content

Commit fe19fcd

Browse files
committed
initial version
0 parents  commit fe19fcd

9 files changed

Lines changed: 758 additions & 0 deletions

File tree

.devcontainer/Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Use Ubuntu 24.04 as base image (includes kernel 6.8+ headers)
2+
FROM ubuntu:24.04
3+
4+
# Avoid prompts from apt
5+
ENV DEBIAN_FRONTEND=noninteractive
6+
7+
# Set up timezone
8+
RUN ln -fs /usr/share/zoneinfo/UTC /etc/localtime
9+
10+
# Install essential tools and kernel headers
11+
RUN apt-get update && apt-get install -y \
12+
build-essential \
13+
linux-headers-generic \
14+
kmod \
15+
git \
16+
vim \
17+
sudo \
18+
curl \
19+
wget \
20+
ca-certificates \
21+
gnupg \
22+
lsb-release \
23+
gcc \
24+
make \
25+
bc \
26+
libelf-dev \
27+
libssl-dev \
28+
&& rm -rf /var/lib/apt/lists/*
29+
30+
# Ensure ubuntu user exists and has sudo privileges
31+
RUN if ! id -u ubuntu > /dev/null 2>&1; then \
32+
useradd -m -s /bin/bash ubuntu; \
33+
fi && \
34+
echo "ubuntu ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
35+
36+
# Set working directory
37+
WORKDIR /workspaces/kernel_module
38+
39+
# Switch to ubuntu user
40+
USER ubuntu
41+
42+
# Set environment
43+
ENV HOME=/home/ubuntu
44+
ENV SHELL=/bin/bash
45+
46+
CMD ["/bin/bash"]

.devcontainer/devcontainer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "kernel-module-devcontainer",
3+
"build": {
4+
"dockerfile": "Dockerfile",
5+
"context": ".."
6+
},
7+
"workspaceFolder": "/workspaces/kernel_module",
8+
"remoteUser": "ubuntu",
9+
"remoteEnv": {
10+
"HOME": "/home/ubuntu"
11+
},
12+
"mounts": [
13+
"source=${localEnv:HOME}${localEnv:USERPROFILE}/.ssh,target=/home/ubuntu/.ssh,type=bind,consistency=cached"
14+
],
15+
"features": {},
16+
"postCreateCommand": "bash -lc 'make --version && gcc --version'",
17+
"customizations": {
18+
"vscode": {
19+
"extensions": [
20+
"ms-vscode.cpptools",
21+
"ms-vscode.cpptools-extension-pack",
22+
"ms-vscode.makefile-tools"
23+
]
24+
}
25+
},
26+
"runArgs": [
27+
"--privileged"
28+
]
29+
}

.github/workflows/ci.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
build-and-test:
11+
name: Build Kernel Module and User Program
12+
runs-on: ubuntu-24.04 # Uses kernel 6.8+ for VMA iterator API support
13+
14+
steps:
15+
- name: Checkout code
16+
uses: actions/checkout@v4
17+
18+
- name: Install kernel headers and build tools
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y build-essential linux-headers-$(uname -r) kmod
22+
23+
- name: Display kernel version
24+
run: |
25+
uname -r
26+
ls -la /lib/modules/$(uname -r)/build || echo "Kernel headers path check"
27+
28+
- name: Build kernel module
29+
run: make module
30+
31+
- name: Build user program
32+
run: make user
33+
34+
- name: Verify build artifacts
35+
run: |
36+
ls -la build/
37+
file build/elf_det.ko
38+
file build/proc_elf_ctrl
39+
echo "Build artifacts verified successfully!"
40+
41+
- name: Upload build artifacts
42+
uses: actions/upload-artifact@v4
43+
if: always()
44+
with:
45+
name: kernel-module-build
46+
path: build/
47+
retention-days: 30
48+
49+
- name: Static analysis with sparse (if available)
50+
run: |
51+
sudo apt-get install -y sparse || true
52+
make clean
53+
make module C=1 || echo "Sparse analysis completed with warnings"
54+
continue-on-error: true

.gitignore

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Build artifacts
2+
build/
3+
*.o
4+
*.ko
5+
*.mod
6+
*.mod.c
7+
*.mod.o
8+
*.symvers
9+
*.order
10+
.*.cmd
11+
.tmp_versions/
12+
Module.symvers
13+
modules.order
14+
15+
# Note: Kbuild files are source files and should be tracked by git
16+
17+
# Editor and IDE files
18+
.vscode/
19+
.idea/
20+
*.swp
21+
*.swo
22+
*~
23+
.DS_Store
24+
25+
# Kernel module specific
26+
*.markers
27+
28+
# Debug files
29+
*.dSYM/
30+
31+
# Compiled user programs
32+
proc_elf_ctrl
33+
elf_det_ctrl
34+
35+
# Core dumps
36+
core
37+
core.*
38+
39+
# Dependency files
40+
*.d
41+
42+
# Archive files
43+
*.a
44+
*.so
45+
*.so.*
46+
47+
# Backup files
48+
*.bak
49+
*.backup
50+
*~

Makefile

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Kernel Module and User Program Makefile
2+
3+
# Kernel module name
4+
obj-m := elf_det.o
5+
6+
# Kernel build directory
7+
KDIR := /lib/modules/$(shell uname -r)/build
8+
9+
# Current directory
10+
PWD := $(shell pwd)
11+
12+
# User program
13+
USER_PROG := proc_elf_ctrl
14+
15+
# Source directory
16+
SRC_DIR := src
17+
18+
# Build directory for user program
19+
BUILD_DIR := build
20+
21+
.PHONY: all clean module user install uninstall test help
22+
23+
# Default target
24+
all: module user
25+
26+
# Build kernel module
27+
module:
28+
@echo "Building kernel module..."
29+
$(MAKE) -C $(KDIR) M=$(PWD)/$(SRC_DIR) modules
30+
@mkdir -p $(BUILD_DIR)
31+
@cp $(SRC_DIR)/*.ko $(BUILD_DIR)/ 2>/dev/null || true
32+
@echo "Kernel module built successfully!"
33+
34+
# Build user program
35+
user:
36+
@echo "Building user program..."
37+
@mkdir -p $(BUILD_DIR)
38+
gcc -Wall -o $(BUILD_DIR)/$(USER_PROG) $(SRC_DIR)/$(USER_PROG).c
39+
@echo "User program built successfully!"
40+
41+
# Install kernel module (requires root)
42+
install: module
43+
@echo "Installing kernel module..."
44+
sudo insmod $(BUILD_DIR)/elf_det.ko
45+
@echo "Module installed. Check with: lsmod | grep elf_det"
46+
47+
# Uninstall kernel module (requires root)
48+
uninstall:
49+
@echo "Uninstalling kernel module..."
50+
sudo rmmod elf_det 2>/dev/null || true
51+
@echo "Module uninstalled."
52+
53+
# Test: install module and run user program
54+
test: install
55+
@echo "Running user program..."
56+
@echo "Enter PID when prompted, or press Ctrl+C to exit"
57+
$(BUILD_DIR)/$(USER_PROG)
58+
59+
# Clean build artifacts
60+
clean:
61+
@echo "Cleaning build artifacts..."
62+
$(MAKE) -C $(KDIR) M=$(PWD)/$(SRC_DIR) clean
63+
rm -rf $(BUILD_DIR)
64+
rm -f $(SRC_DIR)/*.o $(SRC_DIR)/*.ko $(SRC_DIR)/*.mod.c $(SRC_DIR)/*.mod $(SRC_DIR)/.*.cmd
65+
rm -f $(SRC_DIR)/Module.symvers $(SRC_DIR)/modules.order
66+
rm -rf $(SRC_DIR)/.tmp_versions
67+
@echo "Clean complete!"
68+
69+
# Help target
70+
help:
71+
@echo "Linux Process Information Kernel Module - Build Targets:"
72+
@echo ""
73+
@echo " make all - Build both kernel module and user program (default)"
74+
@echo " make module - Build kernel module only"
75+
@echo " make user - Build user program only"
76+
@echo " make install - Install kernel module (requires root)"
77+
@echo " make uninstall - Remove kernel module (requires root)"
78+
@echo " make test - Install module and run user program"
79+
@echo " make clean - Remove all build artifacts"
80+
@echo " make help - Show this help message"
81+
@echo ""
82+
@echo "Note: Building the kernel module requires kernel headers to be installed."

0 commit comments

Comments
 (0)