Repository navigation
Expand file tree
/
Copy pathcode.c
More file actions
178 lines (138 loc) · 3.89 KB
/
Copy pathcode.c
File metadata and controls
178 lines (138 loc) · 3.89 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Read-Write
Archive of Reversing.ID
Tested on FreeBSD 12.0-RELEASE
Menangani operasi baca dan tulis pada device.
Build:
$ make
List module:
$ kldstat
Load:
$ kldload ./revid_kld.ko
Testing
Unload:
$ kldunload revid_kld
*/
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/uio.h>
#include <sys/malloc.h>
/* ****************** Type Definition & Data Declaration ****************** */
#define DEV_NAME "revid"
#define BUFF_LEN 256
struct devdata_t
{
struct cdev * dev;
size_t length;
char buffer[BUFF_LEN];
};
struct devdata_t device;
/* ********************* Internal Functions Prototype ********************* */
static d_open_t dev_open;
static d_close_t dev_close;
static d_read_t dev_read;
static d_write_t dev_write;
static struct cdevsw fops = {
.d_version = D_VERSION,
.d_open = dev_open,
.d_close = dev_close,
.d_read = dev_read,
.d_write = dev_write,
.d_name = "revid"
};
/* *********************** Kernel Module Initialize *********************** */
static int event_handler (struct module * m, int evt_type, void *arg)
{
int retval = 0;
switch (evt_type)
{
case MOD_LOAD:
device.dev = make_dev (&fops, 0, UID_ROOT, GID_WHEEL, 0600, "revid");
uprintf("Driver loaded\n");
break;
case MOD_UNLOAD:
destroy_dev (device.dev);
uprintf("Driver unloaded\n");
break;
default:
retval = EOPNOTSUPP;
break;
}
return retval;
}
/*
Informasi tentang module disimpan ke dalam struktur moduledata_t.
*/
static moduledata_t kld_data = {
"revid_kld", /* class name atau nama module */
event_handler, /* event handler */
NULL /* pointer ke private data */
};
/*
Register event handler (dalam moduledata_t)
DECLARE_MODULE(name, data, sub, order);
sub menyatakan subsystem kernel, dalam hal ini module merupakan driver
order menyatakan urutan inisialisasi dalam subsystem.
*/
DECLARE_MODULE(revid_kld, kld_data, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
/* ******************* Internal Functions Implementation ******************* */
/*
Dipanggil untuk menginisialisasi perangkat dan resource.
*/
static int
dev_open (struct cdev * dev, int oflags, int devtype, struct thread * td)
{
/* Letakkan kode spesifik di sini */
/* return 0 jika open sukses */
return 0;
}
/*
Dipanggil untuk membebaskan / melepas resource yang digunakan.
Dalam hal ini tidak ada resource yang dialokasikan.
*/
static int
dev_close (struct cdev * dev, int fflags, int devtype, struct thread * td)
{
/* Letakkan kode spesifik di sini */
/* return 0 jika release sukses */
return 0;
}
/*
Dipanggil untuk menangani proses pembacaan data dari file.
Data dikirimkan dari device ke user.
*/
static int
dev_read (struct cdev * dev, struct uio * uio, int ioflag)
{
int retval;
int length = MIN(uio->uio_resid,
(device.length - uio->uio_offset > 0 ?
device.length - uio->uio_offset : 0));
retval = uiomove (device.buffer + uio->uio_offset, length, uio);
if (retval != 0)
uprintf ("Write failed.\n");
return retval;
}
/*
Dipanggil untuk menangani proses penulisan data ke file.
Data dikirimkan dari user ke device.
*/
static int
dev_write (struct cdev * dev, struct uio * uio, int ioflag)
{
int retval;
int length = MIN(uio->uio_iov->iov_len, BUFF_LEN - 1);
/* salin data dari user-space ke buffer di kernel-space */
retval = copyin (uio->uio_iov->iov_base, device.buffer, length);
if (retval != 0)
{
uprintf ("Write failed.\n");
return retval;
}
device.buffer[length] = 0;
device.length = length;
return retval;
}