|
| 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