File: stap_kmodule.c

package info (click to toggle)
systemtap 3.1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,860 kB
  • ctags: 12,513
  • sloc: cpp: 58,610; ansic: 58,189; exp: 37,322; sh: 10,633; xml: 7,771; perl: 2,252; python: 2,066; tcl: 1,305; makefile: 969; lisp: 105; java: 100; awk: 94; asm: 91; sed: 16
file content (77 lines) | stat: -rw-r--r-- 1,755 bytes parent folder | download | duplicates (3)
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
/* -*- linux-c -*- 
 * Systemtap Test Module 1
 * Copyright (C) 2007 Red Hat Inc.
 *
 * This file is part of systemtap, and is free software.  You can
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License (GPL); either version 2, or (at your option) any
 * later version.
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/compiler.h>
#include <asm/uaccess.h>

/*
 * The purpose of this module is to provide a function that can be
 * triggered from user context via a /proc file.  Systemtap scripts
 * set probes on the function and run tests to see if the expected
 * output is received. This is better than using the kernel's modules
 * because kernel internals frequently change.
 */

/************ Below are the functions to create this module ************/

static struct proc_dir_entry *stm_ctl = NULL;

static int last_cmd = -1;

static ssize_t stm_write_cmd (struct file *file, const char __user *buf,
			       size_t count, loff_t *ppos)
{
	char type;

	if (get_user(type, (char __user *)buf))
		return -EFAULT;

	switch (type) {
	case '0':
		last_cmd = 0;
		break;
	case '1':
		last_cmd = 1;
		break;
	case '2':
		last_cmd = 2;
		break;
	default:
		printk ("stap_kmodule: invalid command type %d\n", (int)type);
		return -EINVAL;
	}
  
	return count;
}

static struct file_operations stm_fops_cmd = {
	.owner = THIS_MODULE,
	.write = stm_write_cmd,
};

int init_module(void)
{
	stm_ctl = proc_create ("stap_kmodule_cmd", 0666, NULL, &stm_fops_cmd);
	if (stm_ctl == NULL) 
		return -1;
	return 0;
}

void cleanup_module(void)
{
	if (stm_ctl)
		remove_proc_entry ("stap_kmodule_cmd", NULL);
}

MODULE_DESCRIPTION("systemtap test module");
MODULE_LICENSE("GPL");