forked from e5l/au-linux-kernel-spring-2016
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ static void __init test_stack(void) | |
stack_entry_t *tos = NULL; | ||
const char *tos_data = NULL; | ||
const char* test_data[] = { "1", "2", "3", "4" }; | ||
unsigned long i = 0; | ||
long i = 0; | ||
|
||
pr_alert("Testing basic stack"); | ||
|
||
|
@@ -36,7 +36,25 @@ static void __init test_stack(void) | |
|
||
static void __init print_processes_backwards(void) | ||
{ | ||
// TODO | ||
struct task_struct *task_list; | ||
LIST_HEAD(proc_stack); | ||
long counter = 0; | ||
const char *process_name = NULL; | ||
stack_entry_t *stack_top = NULL; | ||
|
||
for_each_process(task_list) | ||
{ | ||
stack_push(&proc_stack, create_stack_entry((void*)(task_list->comm))); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
counter++; | ||
} | ||
|
||
for (; counter > 0; counter--) | ||
{ | ||
stack_top = stack_pop(&proc_stack); | ||
This comment has been minimized.
Sorry, something went wrong.
eabatalov
Collaborator
|
||
process_name = STACK_ENTRY_DATA(stack_top, const char*); | ||
printk(KERN_ALERT "%s\n", process_name); | ||
} | ||
|
||
} | ||
|
||
static int __init ll_init(void) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,33 @@ | ||
#include <linux/slab.h> | ||
#include "stack.h" | ||
|
||
stack_entry_t* create_stack_entry(void *data) | ||
{ | ||
// TODO | ||
(void)data; | ||
return NULL; | ||
stack_entry_t *entry = kmalloc(sizeof(stack_entry_t), GFP_KERNEL); | ||
if (!entry) | ||
return 0; | ||
entry->data = data; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
return entry; | ||
} | ||
|
||
void delete_stack_entry(stack_entry_t *entry) | ||
{ | ||
// TODO | ||
(void)entry; | ||
kfree(entry); | ||
} | ||
|
||
void stack_push(struct list_head *stack, stack_entry_t *entry) | ||
{ | ||
// TODO | ||
(void)stack; | ||
(void)entry; | ||
list_add(&(entry->lh), stack); | ||
} | ||
|
||
stack_entry_t* stack_pop(struct list_head *stack) | ||
{ | ||
// TODO | ||
(void)stack; | ||
return NULL; | ||
struct list_head *ret_val = stack->next; | ||
|
||
if (stack_empty(stack)) | ||
return 0; | ||
|
||
list_del_init(ret_val); | ||
|
||
return list_entry(ret_val, struct stack_entry, lh); | ||
} |
Not sooo fast :)
You can't save pointers from global kernel tasks list to your local data structure.
You should copy name of each task on each iteration into your not global buffer.
like this:
50 for_each_process(tsk) {
51 char comm_buf = kmalloc(sizeof(tsk->comm), GFP_KERNEL);
52 if (!comm_buf) {
53 ret = -ENOMEM;
54 break;
55 }
56
57 tos_tsk = create_stack_entry((void)get_task_comm(comm_buf, tsk));
58 if (!tos_tsk) {
59 kfree(comm_buf);
60 ret = -ENOMEM;
61 break;
62 }
63 stack_push(&process_stack, tos_tsk);
64 }
Also you should use mutual exclusive function get_task_comm for copying of the name.
Also you need to handle all the allocation errors kmalloc.
like it is written here: http://mit.spbau.ru/sewiki/index.php/Linux_kernel_2016#.D0.9E.D0.B1.D1.80.D0.B0.D0.B1.D0.BE.D1.82.D0.BA.D0.B0_.D0.BE.D1.88.D0.B8.D0.B1.D0.BE.D0.BA