Skip to content

Commit d0235bb

Browse files
committed
mostly done
1 parent 21510da commit d0235bb

File tree

1 file changed

+84
-37
lines changed

1 file changed

+84
-37
lines changed

procfs_hw/procfs_hw.c

Lines changed: 84 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,123 @@
1-
#include < linux/module.h >
2-
#include <linux/kernel.h>
1+
#include <linux/module.h>
2+
#include <linux/kernel.h>
3+
#include <linux/slab.h>
4+
#include <linux/string.h>
5+
#include <linux/timer.h>
6+
#include <linux/major.h>
7+
#include <linux/fs.h>
8+
#include <linux/err.h>
9+
#include <linux/ioctl.h>
10+
#include <linux/init.h>
311
#include <linux/proc_fs.h>
12+
#include <asm-generic/uaccess.h>
13+
#include <linux/types.h>
14+
#include <linux/bug.h>
15+
#include <linux/io.h>
16+
#include <asm/page.h>
17+
18+
19+
20+
#ifndef __GFP_WAIT
21+
#define __GFP_WAIT __GFP_RECLAIM
22+
#endif
23+
24+
#ifndef GFP_IOFS
25+
#define GFP_IOFS STP_ALLOC_FLAGS
26+
#endif
427

528
#ifdef CONFIG_PROC_FS
629

7-
#define procfs_name "procfs_hello_world"
830
#define procfs_msg "Hello World\n"
31+
#define procfs_name "procfs_hello_world"
32+
#define procfs_perms 0644
33+
#define procfs_parent NULL
934

10-
struct proc_dir_entry *my_pFile;
35+
static struct file_operations procfs_fops;
36+
static char *message;
37+
static int read_p;
1138

12-
static int
13-
pfs_hw_read(char *page,
14-
char **start,
15-
off_t off, int buffer_length, int *eof, void *data)
39+
40+
static ssize_t
41+
pfs_hw_read(struct file *file,
42+
char __user * buf, size_t count, loff_t * offp)
1643
{
17-
int ret;
44+
ssize_t len = strlen(message);
1845

19-
if (off > 0) {
20-
ret = 0;
21-
} else {
22-
/* totally ignore the size of the buffer */
23-
ret = sprintf(page, procfs_msg);
46+
read_p = !read_p;
47+
if (read_p) {
48+
return 0;
2449
}
50+
printk("proc called read %ld\n", count);
51+
printk("proc called len %ld\n", len);
52+
printk("about to copy %s\n", message);
53+
54+
/*
55+
* should be len instead of 0, but that causes an oops
56+
*/
2557

26-
return ret;
58+
/* @todo figure out why copy_to_user so upset wich nonzero */
59+
copy_to_user(buf, message, 0);
60+
61+
return 0;
2762
}
2863

2964
static int
30-
pfs_hw_create_entry()
65+
pfs_hw_open(struct inode *sp_inode, struct file *sp_file)
3166
{
32-
my_pFile = create_proc_entry(procfs_name, 0644, NULL);
67+
printk("proc called open\n");
3368

34-
if (my_pFile == NULL) {
35-
remove_proc_entry(procfs_name, &proc_root);
36-
printk(KERN_ALERT "Error creating:/proc/%s\n", procfs_name);
69+
read_p = 1;
70+
71+
//message = kmalloc(sizeof(char) * 1024, __GFP_WAIT | __GFP_IO | __GFP_FS);
72+
message = kmalloc(1024, GFP_KERNEL);
73+
74+
if (message == NULL) {
75+
printk("ERROR, pfs_hw_init");
3776
return -ENOMEM;
3877
}
39-
my_pFile->read_proc = procfile_read;
40-
my_pFile->owner = THIS_MODULE;
41-
my_pFile->mode = S_IFREG | S_IRUGO;
42-
my_pFile->uid = 0;
43-
my_pFile->gid = 0;
44-
my_pFile->size = 37;
78+
strcpy(message, procfs_msg);
79+
return 0;
80+
}
4581

82+
static int
83+
pfs_hw_release(struct inode *sp_inode, struct file *sp_file)
84+
{
85+
printk("proc called release\n");
86+
kfree(message);
4687
return 0;
4788

4889
}
4990

5091

51-
static int __init
52-
pfs_hw_init()
92+
static __init int
93+
pfs_hw_create_entry(void)
5394
{
54-
int ret_val;
95+
printk("/proc/%s create\n", procfs_name);
96+
/* procfs_fops.owner = THIS_MODULE; */
97+
procfs_fops.open = pfs_hw_open;
98+
procfs_fops.read = pfs_hw_read;
99+
procfs_fops.release = pfs_hw_release;
55100

56-
ret_val = pfs_hw_create_entry();
57-
return ret_val;
101+
if (!proc_create(procfs_name, procfs_perms, procfs_parent, &procfs_fops)) {
102+
printk(KERN_ALERT "Error creating:/proc/%s\n", procfs_name);
103+
remove_proc_entry(procfs_name, procfs_parent);
104+
return -ENOMEM;
105+
}
106+
return 0;
58107
}
59108

60109
static void __exit
61-
pfs_hw_exit()
110+
pfs_hw_exit(void)
62111
{
63-
remove_proc_entry(procfs_name, &proc_root);
112+
remove_proc_entry(procfs_name, procfs_parent);
64113
printk(KERN_INFO "/proc/%s removed\n", procfs_name);
65114
}
66115

67-
module_init(pfs_hw_init)
68-
module_exit(pfs_hw_exit)
116+
module_init(pfs_hw_create_entry);
117+
module_exit(pfs_hw_exit);
69118
#else
70119
#error "Set CONFIG_PROC_FS=y in you .config"
71120
#endif /* CONFIG_PROC_FS */
72121

73122
MODULE_AUTHOR("d-grossman");
74123
MODULE_DESCRIPTION("procfs hello world");
75-
76-
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)