Skip to content

Commit 2bb3623

Browse files
committed
userspace program
1 parent 7875cb6 commit 2bb3623

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

module/procdetails.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static int proc_procreadwrite_show (struct seq_file *m, void *v) {
4646
size_t spacing;
4747
char spacing_left[] = " ";
4848

49-
if(!proc_details.filename[0]) {
49+
if(!proc_details.filename[0] || !proc_details.dir_entry) {
5050
return -ENOENT;
5151
}
5252

@@ -60,12 +60,13 @@ static int proc_procreadwrite_show (struct seq_file *m, void *v) {
6060
"\n------------------------------------------------------------\n", spacing_left, proc_details.filename);
6161

6262
snprintf(buf, BUF, "%s> %-42s : %s\n", buf, "Module ", proc_details.modname);
63-
snprintf(buf, BUF, "%s> %-42s\n", buf, "Mode ");
64-
snprintf(buf, BUF, "%s %-38s : %o\n", buf, "Format ", (proc_details.dir_entry->mode & 0170000) / (8 * 8 * 8));
65-
snprintf(buf, BUF, "%s %-38s : %o\n", buf, "Permissions ", proc_details.dir_entry->mode & 0777);
66-
snprintf(buf, BUF, "%s> %-42s : %zd\n", buf, "Count ", (size_t)(proc_details.dir_entry->count.counter));
67-
snprintf(buf, BUF, "%s> %-42s : %zd\n", buf, "In use ", (size_t)(proc_details.dir_entry->in_use.counter));
68-
63+
if(proc_details.dir_entry) {
64+
snprintf(buf, BUF, "%s> %-42s\n", buf, "Mode ");
65+
snprintf(buf, BUF, "%s %-38s : %o\n", buf, "Format ", (proc_details.dir_entry->mode & 0170000) / (8 * 8 * 8));
66+
snprintf(buf, BUF, "%s %-38s : %o\n", buf, "Permissions ", proc_details.dir_entry->mode & 0777);
67+
snprintf(buf, BUF, "%s> %-42s : %zd\n", buf, "Count ", (size_t)(proc_details.dir_entry->count.counter));
68+
snprintf(buf, BUF, "%s> %-42s : %zd\n", buf, "In use ", (size_t)(proc_details.dir_entry->in_use.counter));
69+
}
6970
snprintf(buf, BUF, "%s> %-42s : %s\n", buf, "File Operations ", proc_details.fops[0] ? "Yes" : "No");
7071
if(proc_details.fops[0]) {
7172
snprintf(buf, BUF, "%s %s = {\n", buf, proc_details.fops);
@@ -78,6 +79,7 @@ static int proc_procreadwrite_show (struct seq_file *m, void *v) {
7879
}
7980

8081
seq_puts(m, buf);
82+
proc_details.filename[0] = 0;
8183
return 0;
8284
}
8385

@@ -136,6 +138,7 @@ static void get_details() {
136138
char *modname = NULL;
137139
char namebuf[128];
138140

141+
proc_details.dir_entry = NULL;
139142
spin_lock(subdir_lock);
140143
rv = xlate(proc_details.filename, &(proc_details.dir_entry), &fn);
141144
printk(KERN_INFO MOD "Ret: %d, Residual: %s\n", rv, fn);

userspace/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
all: procinfo.c
2+
gcc procinfo.c -o procinfo -Wall -Wextra
3+

userspace/procinfo.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
int main(int argc, char* argv[]) {
6+
char proc[256];
7+
char *line = NULL, *module = NULL;
8+
size_t len = 0, total = 0;
9+
ssize_t read;
10+
11+
if(argc != 2) {
12+
printf("Usage: %s <proc filename>\n", argv[0]);
13+
return -1;
14+
}
15+
16+
proc[255] = 0;
17+
if((module = strrchr(argv[1], '/')) != NULL) {
18+
strncpy(proc, module + 1, 255);
19+
} else {
20+
strncpy(proc, argv[1], 255);
21+
}
22+
23+
FILE* f = fopen("/proc/procdetails", "w");
24+
if(!f) {
25+
fprintf(stderr, "Could not find /proc/procdetails! Did you load the procdetails kernel module?\n");
26+
return -1;
27+
}
28+
fputs(proc, f);
29+
fclose(f);
30+
f = fopen("/proc/procdetails", "r");
31+
if(!f) {
32+
fprintf(stderr, "Something strange happened...\n");
33+
return -1;
34+
}
35+
36+
while ((read = getline(&line, &len, f)) != -1) {
37+
total += len;
38+
printf("%s", line);
39+
}
40+
free(line);
41+
fclose(f);
42+
43+
if(!total) {
44+
printf("Did not find any information\n");
45+
return -1;
46+
}
47+
return 0;
48+
}

0 commit comments

Comments
 (0)