From 00127eab993911df24aeeb0bf5eee56259b29620 Mon Sep 17 00:00:00 2001 From: seantywork Date: Wed, 19 Nov 2025 08:09:21 +0000 Subject: [PATCH] proc --- kproc/kproc.c | 2 -- kproc/kproc.h | 1 + kproc/user/Makefile | 5 ++- kproc/user/user.c | 74 ++++++++++++++++++++++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 4 deletions(-) diff --git a/kproc/kproc.c b/kproc/kproc.c index 8647557..0cd7a9d 100644 --- a/kproc/kproc.c +++ b/kproc/kproc.c @@ -16,8 +16,6 @@ #include "kproc.h" -#define DEVICE_NAME "kproc_f" - static struct work_struct job; static int dev_major; diff --git a/kproc/kproc.h b/kproc/kproc.h index 0c7da2b..d11d559 100644 --- a/kproc/kproc.h +++ b/kproc/kproc.h @@ -1,6 +1,7 @@ #ifndef _KPROC_H_ #define _KPROC_H_ +#define DEVICE_NAME "kproc_f" #define KPROC_BUFF_LEN 128 #endif \ No newline at end of file diff --git a/kproc/user/Makefile b/kproc/user/Makefile index 18ffa8a..7716b82 100644 --- a/kproc/user/Makefile +++ b/kproc/user/Makefile @@ -1,4 +1,7 @@ + +INCLUDES := -I../ + all: - gcc -g -o user.out user.c -lpthread + gcc -g $(INCLUDES) -o user.out user.c -lpthread clean: rm -rf *.out *.o \ No newline at end of file diff --git a/kproc/user/user.c b/kproc/user/user.c index 9009319..02fe7b2 100644 --- a/kproc/user/user.c +++ b/kproc/user/user.c @@ -2,9 +2,81 @@ #include #include #include +#include +#include +#include -int main(){ +#include "kproc.h" +FILE* fp = NULL; +pthread_mutex_t mut; +int keepalive = 1; + +void sig_hdl(int sig){ + printf("SIG: %d\n", sig); + keepalive =0; +} + +void* another_thread(void* varg){ + while(keepalive){ + pthread_mutex_lock(&mut); + + pthread_mutex_unlock(&mut); + sleep(1); + } + pthread_exit(NULL); +} + +int main(int argc, char** argv){ + pthread_t tid; + int result = -1; + char fbuf[1024] = {0}; + char* tbuf = NULL; + size_t rlen = 0; + int major = -1; + fp = fopen("/proc/devices", "r"); + if(fp == NULL){ + printf("failed to open proc devices\n"); + return -1; + } + printf("devices:\n"); + while(fgets(fbuf, 1024, fp)){ + printf("%s", fbuf); + if((tbuf = strstr(fbuf, "loop")) != NULL){ + tbuf -= 1; + *tbuf = 0; + tbuf -= 3; + printf("target major: %s\n", tbuf); + break; + } + memset(fbuf, 0, 1024 * sizeof(char)); + } + fclose(fp); + if(result < 0){ + printf("target not found\n"); + return -1; + } + result = -1; + signal(SIGINT, sig_hdl); + fp = fopen(DEVICE_NAME, "r+"); + if(fp == NULL){ + printf("failed to open\n"); + return -1; + } + pthread_mutex_init(&mut, NULL); + result = pthread_create(&tid, NULL, another_thread, NULL); + if(result < 0){ + printf("failed to create thread\n"); + return -1; + } + while(keepalive){ + pthread_mutex_lock(&mut); + + + pthread_mutex_unlock(&mut); + sleep(1); + } + fclose(fp); return 0; } \ No newline at end of file -- 2.43.0