forked from nicupavel/emlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemlog.c
493 lines (405 loc) · 14.8 KB
/
emlog.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* EMLOG: the EMbedded-device LOGger
*
* Jeremy Elson <[email protected]>
* USC/Information Sciences Institute
*
* Modified By:
* Andreas Neustifter <andreas.neustifter at gmail.com>
* Andriy Stepanov <stanv at altlinux.ru>
* Nicu Pavel <npavel at mini-box.com>
* Darien Kindlund <kindlund at mitre.org>
*
* This code is released under the GPL
*
* This is emlog version 0.41, released 13 August 2001, modified 25 July 2006
* For more information see http://www.circlemud.org/~jelson/software/emlog
*
* $Id: emlog.c,v 1.7 2001/08/13 21:29:20 jelson Exp $
*/
/* pr_fmt() taken from infiniband/core/netlink.c */
#define pr_fmt(fmt) "%s:%s: " fmt, KBUILD_MODNAME, __func__
/* to have pr_debug() output in case DYNAMIC_DEBUG is disabled */
#define DEBUG 1
#ifdef MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33)
#include <linux/autoconf.h>
#else
#include <generated/autoconf.h>
#endif
#include <linux/stddef.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/workqueue.h>
#include <linux/wait.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/poll.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
#include <asm/uaccess.h>
#else
#include <linux/uaccess.h>
#endif
/* older kernels don't have pr_{info,err,debug}() at all or lacks pr_fmt() expansion */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 28)
#undef pr_err
#undef pr_info
#undef pr_debug
/* copied from 3.14 kernel's printk.h */
#define pr_err(fmt, ...) \
printk(KERN_ERR pr_fmt(fmt), ##__VA_ARGS__)
#define pr_info(fmt, ...) \
printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
#define pr_debug(fmt, ...) \
printk(KERN_DEBUG pr_fmt(fmt), ##__VA_ARGS__)
#endif
#include "emlog.h"
static struct emlog_info *emlog_info_list = NULL;
static bool emlog_autofree;
static bool emlog_debug;
static dev_t emlog_dev_type = 0;
#define EMLOG_MINOR_BASE 1
#define EMLOG_MINOR_COUNT 127 /* keep in sync with EMLOG_MAX_SIZE */
static struct cdev *emlog_cdev = NULL;
static struct class *emlog_class = NULL;
static struct device *emlog_dev_reg;
module_param(emlog_autofree, bool, 0644);
module_param(emlog_debug, bool, 0644);
/* find the emlog-info structure associated with an inode. returns a
* pointer to the structure if found, NULL if not found */
static struct emlog_info *get_einfo(const struct inode *inode)
{
struct emlog_info *einfo;
if (inode == NULL)
return NULL;
for (einfo = emlog_info_list; einfo != NULL; einfo = einfo->next)
if (einfo->i_ino == inode->i_ino && einfo->i_rdev == inode->i_rdev)
return einfo;
return NULL;
}
/* create a new emlog buffer and its associated info structure.
* returns an errno on failure, or 0 on success. on success, the
* pointer to the new struct is passed back using peinfo */
static int create_einfo(const struct inode *inode, int minor,
struct emlog_info **peinfo)
{
struct emlog_info *einfo;
/* make sure the memory requirement is legal */
if (minor < 1 || minor > EMLOG_MAX_SIZE)
return -EINVAL;
/* allocate space for our metadata and initialize it */
if ((einfo = kzalloc(sizeof(struct emlog_info), GFP_KERNEL)) == NULL)
goto struct_malloc_failed;
einfo->i_ino = inode->i_ino;
einfo->i_rdev = inode->i_rdev;
init_waitqueue_head(EMLOG_READQ(einfo));
/* figure out how much of a buffer this should be and allocate the buffer */
einfo->size = 1024 * minor;
if ((einfo->data = vmalloc(sizeof(char) * einfo->size)) == NULL)
goto data_malloc_failed;
/* add it to our linked list */
einfo->next = emlog_info_list;
emlog_info_list = einfo;
if (emlog_debug)
pr_debug("allocating resources associated with inode %ld.\n", einfo->i_ino);
/* pass the struct back */
*peinfo = einfo;
return 0;
#if 0
other_failure: /* if we check for other errors later, jump here */
#endif
vfree(einfo->data);
data_malloc_failed:
kfree(einfo);
struct_malloc_failed:
return -ENOMEM;
}
/* this frees all data associated with an emlog_info buffer, including
* the struct that you pass to the function. don't dereference this
* structure after calling free_einfo! */
static void free_einfo(struct emlog_info *einfo)
{
struct emlog_info **ptr;
if (einfo == NULL) {
pr_err("null passed to free_einfo.\n");
return;
}
if (emlog_debug)
pr_debug("freeing resources associated with inode %ld.\n", einfo->i_ino);
vfree(einfo->data);
/* now delete the 'einfo' structure from the linked list. 'ptr' is
* the pointer that needs to be changed... which is either the list
* head or one of the 'next' pointers on the list. */
ptr = &emlog_info_list;
while (*ptr != einfo) {
if (!*ptr) {
pr_err("corrupt einfo list.\n");
break;
} else
ptr = &((**ptr).next);
}
*ptr = einfo->next;
}
/************************ File Interface Functions ************************/
static int emlog_open(struct inode *inode, struct file *file)
{
int minor = MINOR(inode->i_rdev);
struct emlog_info *einfo = NULL;
int retval;
if ((einfo = get_einfo(inode)) == NULL) {
/* never heard of this inode before... create a new record */
if ((retval = create_einfo(inode, minor, &einfo)) < 0)
return retval;
}
if (einfo == NULL) {
pr_err("can not fetch einfo for inode %ld.\n", inode->i_ino);
return -EIO;
}
einfo->refcount++;
if (!try_module_get(THIS_MODULE)) {
pr_err("cannot get module\n");
einfo->refcount--;
return -ENODEV;
}
return 0;
}
/* this is called when a file is closed */
static int emlog_release(struct inode *inode, struct file *file)
{
struct emlog_info *einfo;
int retval = 0;
/* get the buffer info */
if ((einfo = get_einfo(inode)) == NULL) {
pr_err("can not fetch einfo for inode %ld.\n", inode->i_ino);
retval = EIO; goto out;
}
/* decrement the reference count. if no one has this file open and
* it's not holding any data or if autofree is set, delete the
* record. */
einfo->refcount--;
if (einfo->refcount == 0 && (emlog_autofree || EMLOG_QLEN(einfo) == 0))
free_einfo(einfo);
out:
module_put(THIS_MODULE);
return retval;
}
/* read_from_emlog reads bytes out of a circular buffer with
* wraparound. returns char * pointer to data read, which the
* caller must free. length is (a pointer to) the number of bytes to
* be read, which will be set by this function to be the number of
* bytes actually returned */
static char * read_from_emlog(struct emlog_info * einfo, size_t * length,
loff_t * offset)
{
char *retval;
int bytes_copied = 0, n, start_point;
size_t remaining;
/* is the user trying to read data that has already scrolled off? */
if (*offset < einfo->offset)
*offset = einfo->offset;
/* is the user trying to read past EOF? */
if (*offset >= EMLOG_FIRST_EMPTY_BYTE(einfo))
return NULL;
/* find the smaller of the total bytes we have available and what
* the user is asking for */
*length = min_t(size_t, *length, EMLOG_FIRST_EMPTY_BYTE(einfo) - *offset);
remaining = *length;
/* figure out where to start based on user's offset */
start_point = einfo->read_point + (*offset - einfo->offset);
start_point = start_point % einfo->size;
/* allocate memory to return */
if ((retval = kmalloc(sizeof(char) * remaining, GFP_KERNEL)) == NULL)
return NULL;
/* copy the (possibly noncontiguous) data to our buffer */
while (remaining) {
n = min(remaining, einfo->size - start_point);
memcpy(retval + bytes_copied, einfo->data + start_point, n);
bytes_copied += n;
remaining -= n;
start_point = (start_point + n) % einfo->size;
}
/* advance user's file pointer */
*offset += *length;
return retval;
}
static ssize_t emlog_read(struct file *file, char __user *buffer, /* The buffer to fill with data */
size_t length, /* The length of the buffer */
loff_t * offset)
{ /* Our offset in the file */
int retval;
char *data_to_return;
struct emlog_info *einfo;
/* get the metadata about this emlog */
if ((einfo = get_einfo(file->f_path.dentry->d_inode)) == NULL) {
pr_err("can not fetch einfo for inode %ld.\n", (long)(file->f_path.dentry->d_inode->i_ino));
return -EIO;
}
/* wait until there's data available (unless we do nonblocking reads) */
// if (*offset >= EMLOG_FIRST_EMPTY_BYTE(einfo))
// return -EAGAIN;
//
// wait_event_interruptible((einfo)->read_q,
// *offset <
// (einfo)->offset + EMLOG_QLEN(einfo));
//
// /* see if a signal woke us up */
// if (signal_pending(current))
// return -ERESTARTSYS;
if ((data_to_return = read_from_emlog(einfo, &length, offset)) == NULL)
return 0;
if (copy_to_user(buffer, data_to_return, length) > 0)
retval = -EFAULT;
else
retval = length;
kfree(data_to_return);
return retval;
}
/* write_to_emlog writes to a circular buffer with wraparound. in the
* case of an overflow, it overwrites the oldest unread data. */
static void write_to_emlog(struct emlog_info *einfo, char *buf, size_t length)
{
int bytes_copied = 0;
int overflow = 0;
int n;
if (length + EMLOG_QLEN(einfo) >= (einfo->size - 1)) {
overflow = 1;
/* in case of overflow, figure out where the new buffer will
* begin. we start by figuring out where the current buffer ENDS:
* einfo->offset + EMLOG_QLEN. we then advance the end-offset
* by the length of the current write, and work backwards to
* figure out what the oldest unoverwritten data will be (i.e.,
* size of the buffer). was that all quite clear? :-) */
einfo->offset = einfo->offset + EMLOG_QLEN(einfo) + length
- einfo->size + 1;
}
while (length) {
/* how many contiguous bytes are available from the write point to
* the end of the circular buffer? */
n = min(length, einfo->size - einfo->write_point);
memcpy(einfo->data + einfo->write_point, buf + bytes_copied, n);
bytes_copied += n;
length -= n;
einfo->write_point = (einfo->write_point + n) % einfo->size;
}
/* if there is an overflow, reset the read point to read whatever is
* the oldest data that we have, that has not yet been
* overwritten. */
if (overflow)
einfo->read_point = (einfo->write_point + 1) % einfo->size;
}
static ssize_t emlog_write(struct file *file,
const char __user *buffer,
size_t length, loff_t * offset)
{
char *message = NULL;
size_t n;
struct emlog_info *einfo;
/* get the metadata about this emlog */
if ((einfo = get_einfo(file->f_path.dentry->d_inode)) == NULL)
return -EIO;
/* if the message is longer than the buffer, just take the beginning
* of it, in hopes that the reader (if any) will have time to read
* before we wrap around and obliterate it */
n = min(length, einfo->size - 1);
/* make sure we have the memory for it */
if ((message = kmalloc(n, GFP_KERNEL)) == NULL)
return -ENOMEM;
/* copy into our temp buffer */
if (copy_from_user(message, buffer, n) > 0) {
kfree(message);
return -EFAULT;
}
/* now copy it into the circular buffer and free our temp copy */
write_to_emlog(einfo, message, n);
kfree(message);
/* wake up any readers that might be waiting for the data. we call
* schedule in the vague hope that a reader will run before the
* writer's next write, to avoid losing data. */
wake_up_interruptible(EMLOG_READQ(einfo));
return n;
}
static unsigned int emlog_poll(struct file *file, struct poll_table_struct * wait)
{
struct emlog_info *einfo;
/* get the metadata about this emlog */
if ((einfo = get_einfo(file->f_path.dentry->d_inode)) == NULL)
return -EIO;
poll_wait(file, EMLOG_READQ(einfo), wait);
if (file->f_pos < EMLOG_FIRST_EMPTY_BYTE(einfo))
return POLLIN | POLLRDNORM;
else
return 0;
}
static const struct file_operations emlog_fops = {
.read = emlog_read,
.write = emlog_write,
.open = emlog_open,
.release = emlog_release,
.poll = emlog_poll,
.llseek = no_llseek, /* no_llseek by default introduced at v2.6.37-rc1 */
.owner = THIS_MODULE,
};
static int __init emlog_init(void)
{
int ret_val;
ret_val = alloc_chrdev_region(&emlog_dev_type, EMLOG_MINOR_BASE, EMLOG_MINOR_COUNT, DEVICE_NAME);
if (ret_val < 0) {
pr_err("Can not alloc_chrdev_region, error code %d.\n", ret_val);
return -1;
}
emlog_cdev = cdev_alloc();
if (emlog_cdev == NULL) {
pr_err("Can not cdev_alloc.\n");
ret_val = -2; goto emlog_init_error;
}
emlog_cdev->ops = &emlog_fops;
emlog_cdev->owner = THIS_MODULE;
ret_val = cdev_add(emlog_cdev, emlog_dev_type, EMLOG_MINOR_COUNT);
if (ret_val < 0) {
pr_err("Can not cdev_add, error code %d.\n", ret_val);
ret_val = -3; goto emlog_init_error;
}
pr_info("version %s running, major is %u, MINOR is %u.\n", EMLOG_VERSION, (unsigned)MAJOR(emlog_dev_type), (unsigned)MINOR(emlog_dev_type));
emlog_class = class_create(THIS_MODULE, DEVICE_NAME);
if (emlog_class == NULL) {
pr_err("Can not class_create.\n");
ret_val = -4; goto emlog_init_error;
}
emlog_dev_reg = device_create(emlog_class, NULL, emlog_dev_type, NULL, DEVICE_NAME);
if (emlog_dev_reg == NULL) {
pr_err("Can not device_create.\n");
ret_val = -5; goto emlog_init_error;
}
goto emlog_init_okay;
emlog_init_error:
if (emlog_dev_reg) device_destroy(emlog_class, emlog_dev_type);
if (emlog_class) class_destroy(emlog_class);
if (emlog_cdev) cdev_del(emlog_cdev);
if (emlog_dev_type) unregister_chrdev_region(emlog_dev_type, EMLOG_MINOR_COUNT);
emlog_init_okay:
return ret_val;
}
static void __exit emlog_remove(void)
{
/* clean up any still-allocated memory */
while (emlog_info_list != NULL)
free_einfo(emlog_info_list);
device_destroy(emlog_class, emlog_dev_type);
class_destroy(emlog_class);
cdev_del(emlog_cdev);
unregister_chrdev_region(emlog_dev_type, EMLOG_MINOR_COUNT);
pr_info("unloaded.\n");
}
module_init(emlog_init);
module_exit(emlog_remove);
MODULE_LICENSE("GPL v2");