File: devtrace.c

package info (click to toggle)
ibcs 971113-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,096 kB
  • ctags: 2,079
  • sloc: ansic: 14,910; makefile: 310; sh: 203; asm: 47; perl: 18; pascal: 2
file content (163 lines) | stat: -rw-r--r-- 3,731 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <linux/config.h>

#include <linux/module.h>
#include <linux/version.h>

#include <linux/types.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/major.h>
#include <asm/segment.h>
#ifndef KERNEL_DS
#include <linux/segment.h>
#endif
#define __KERNEL__ 1

#include <linux/kernel.h>
#include <linux/net.h>
#include <linux/sched.h>
#include <linux/signal.h>
#include <linux/socket.h>
#include <linux/utsname.h>
#include <linux/time.h>
#include <linux/termios.h>
#include <linux/sys.h>


/* Zero for auto allocation. The major number allocated can be found
 * by looking in /proc/devices after the module is loaded. Auto
 * allocation of major numbers is a fairly new kernel feature. If you
 * aren't using a recent 1.1.x kernel you may have to set an explicit
 * major number here.
 */
#define DEVTRACE_MAJOR	0


char kernel_version[] = UTS_RELEASE;


static int
devtrace_lseek(struct inode *inode, struct file *file, off_t offset, int whence)
{
	printk(KERN_DEBUG "devtrace: [%d] %lx: lseek to 0x%08lx whence=%d\n",
		current->pid, (unsigned long)file,
		offset, whence);

	/* Easy way out. Most devices we wish to trace are either
	 * STREAMS devices or character devices which aren't seekable
	 * anyway.
	 */
	return -ESPIPE;
}


static int
devtrace_read(struct inode *inode, struct file *file, char *buf, int size)
{
	printk(KERN_DEBUG "devtrace: [%d] %lx: read %d bytes to 0x%lx\n",
		current->pid, (unsigned long)file,
		size, (unsigned long)buf);

	return size;
}


static int
devtrace_write(struct inode *inode, struct file *file, char *buf, int size)
{
	printk(KERN_DEBUG "devtrace: [%d] %lx: write %d bytes from 0x%lx\n",
		current->pid, (unsigned long)file,
		size, (unsigned long)buf);

	return size;
}


static int
devtrace_ioctl(struct inode *inode, struct file *file,
		unsigned int cmd, unsigned long arg)
{
	static char *code[] = {
		"",		"I_NREAD",	"I_PUSH",	"I_POP",
		"I_LOOK",	"I_FLUSH",	"I_SRDOPT",	"I_GRDOPT",
		"I_STR",	"I_SETSIG",	"I_GETSIG",	"I_FIND",
		"I_LINK",	"I_UNLINK",	"I_PEEK",	"I_FDINSERT",
		"I_SENDFD",	"I_RECVFD"
	};

	printk(KERN_DEBUG "devtrace: [%d] %lx: ioctl 0x%x (%s) with argument 0x%lx requested\n",
		current->pid, (unsigned long)file,
		cmd, (cmd & 0xff) <= 18 ? code[(cmd & 0xff)] : "????",
		(unsigned long)arg);

	return 0;
}


static int
devtrace_open(struct inode *ino, struct file *filep)
{
	MOD_INC_USE_COUNT;

	printk(KERN_DEBUG "devtrace: [%d] %lx opening\n",
		current->pid, (unsigned long)filep);

	return 0;
}


static void
devtrace_close(struct inode *ino, struct file *filep)
{
	MOD_DEC_USE_COUNT;

	printk(KERN_DEBUG "devtrace: [%d] %lx closed\n",
		current->pid, (unsigned long)filep);
}


static struct file_operations devtrace_fops = {
	devtrace_lseek,		/* lseek */
	devtrace_read,		/* read */
	devtrace_write,		/* write */
	NULL,			/* readdir */
	NULL,			/* select */
	devtrace_ioctl,		/* ioctl */
	NULL,			/* mmap */
	devtrace_open,		/* open */
	devtrace_close,		/* close */
	NULL			/* fsync */
};


static int devtrace_major;


int
init_module(void)
{
	devtrace_major = register_chrdev(DEVTRACE_MAJOR, "devtrace", &devtrace_fops);
	if (devtrace_major < 0) {
		printk(KERN_INFO "iBCS: couldn't register devtrace on character major %d\n",
			DEVTRACE_MAJOR);
		return 1;
	} else {
		if (!devtrace_major)
			devtrace_major = DEVTRACE_MAJOR;
		printk(KERN_INFO "iBCS: devtrace registered on character major %d\n", devtrace_major);

		return 0;
	}
}


void
cleanup_module(void)
{
	if (MOD_IN_USE)
		printk(KERN_INFO "iBCS: devtrace module is in use, remove delayed\n");

	if (devtrace_major > 0 && unregister_chrdev(devtrace_major, "devtrace") != 0)
		printk(KERN_NOTICE "iBCS: couldn't unregister devtrace device!\n");
}