#include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/mm.h> #include <linux/sched.h> #include <asm/uaccess.h> #include <linux/slab.h> #define TASK_COMM_LEN 16 #define TMP_DATA_LEN 50 typedef struct my_task_struct { volatile long state; int prio, static_prio, normal_prio; pid_t pid; pid_t tgid; char comm[TASK_COMM_LEN]; struct my_task_struct *next; }my_struct_t, *my_struct_p; my_struct_p get_data
(void) { my_struct_p p, head, h; struct task_struct *task = NULL; head
= (my_struct_p)kmalloc(sizeof(my_struct_t), GFP_ATOMIC); head->next = NULL; h = head; for_each_process
(task) { p = (my_struct_p)kmalloc(sizeof(my_struct_t), GFP_ATOMIC); p
->state = task->state; p->prio = task->prio; p->static_prio = task->static_prio; p->normal_prio = task->normal_prio; p->pid = task->pid; p->tgid = task->tgid;
memset(p->comm, '\0', sizeof(p->comm)); strncpy(p->comm, task->comm, TASK_COMM_LEN-1); p
->next = h->next; h->next = p; h = p; } return head; } int filewrite(const char * filename, my_struct_p head) { struct file *filp; mm_segment_t fs; my_struct_p data; char *change_line = "\t"; char *menu_line = "state\tprio\tstatic_prio\tnormal_prio\tpid\ttgid\tcomm\n"; char tmpdata[TMP_DATA_LEN]; data
= head->next; filp = filp_open(filename, O_RDWR|O_APPEND|O_CREAT, 0644); if(IS_ERR(filp)) { printk("open error!\n"); return 1; } fs = get_fs(); set_fs(KERNEL_DS); filp->f_op->write(filp, menu_line, strlen(menu_line), &filp->f_pos);
while(NULL != data) { memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->state), "%ld", data->state); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos);
memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->prio), "%d", data->prio); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos);
memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->static_prio), "%d", data->static_prio); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos);
memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->normal_prio), "%d", data->normal_prio); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos);
memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->pid), "%d", data->pid); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos);
memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->tgid), "%d", data->tgid); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos);
memset(tmpdata, '\0', TMP_DATA_LEN); snprintf(tmpdata, sizeof(data->comm), "%s", data->comm); strcat(tmpdata, change_line); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos); memset(tmpdata, '\0', TMP_DATA_LEN); strcpy(tmpdata, "\n"); filp->f_op->write(filp, tmpdata, strlen(tmpdata), &filp->f_pos); data
= data->next; } set_fs(fs); filp_close(filp, NULL); return 0; } int data_flush(my_struct_p head) { my_struct_p data; data = head; while(NULL != data) { head = head->next; kfree(data); data = head; } return 0; } asmlinkage my_struct_p sys_getdata
(void) { my_struct_p res; res = get_data(); return res; } asmlinkage
int sys_datawrite(const char *filename, my_struct_p head) { return filewrite(filename, head); } asmlinkage
int sys_dataflush(my_struct_p head) { return data_flush(head); }
|