向user_struct添加另一个字段 [英] add another field to user_struct
问题描述
我想在文件linux-source/kernel/user.c中向user_struct添加新字段(以存储该用户的就绪进程数)
I want to add new field ( to store number of ready process of this user ) to user_struct in file linux-source/kernel/user.c
struct user_struct {
atomic_t ready_processes; /* I add this field */
/* not important fields */
}
在哪里正确初始化此字段?
where to initialize this field correctly ?
推荐答案
为了向user_struct
添加一个新字段,您需要做三件事:
In order to add a new field to user_struct
, you need to do 3 things:
-
user_struct
的定义在文件sched.h(include/linux/sched.h)中
您应该将字段添加到该struct
.
Definition of
user_struct
is in file sched.h(include/linux/sched.h)
You should add your field in thatstruct
.
struct user_struct {
atomic_t ready_processes; /* I added this line! */
/*Other fields*/
};
在user.c(内核/user.c)第51行中,全局为root_user
实例化了user_struct
.在此处为您的字段赋值.
In user.c (kernel/user.c) line 51, user_struct
is instantiated for root_user
globally. Give your field a value here.
struct user_struct root_user = {
.ready_processes = ATOMIC_INIT(1), /* I added this line! */
.__count = ATOMIC_INIT(2),
.processes = ATOMIC_INIT(1),
.files = ATOMIC_INIT(0),
.sigpending = ATOMIC_INIT(0),
.locked_shm = 0,
.user_ns = &init_user_ns,
};
您已经完成了为root用户初始化字段的操作,但还应该为其他用户初始化字段.
为此,请在user.c中转到函数alloc_uid
,在该函数中分配并初始化新用户.例如,您看到有一行atomic_set(&new->__count, 1);
初始化了__count
.在此旁边添加您的初始化代码.
You're done with initializing your field for root user but you should also initialize it for other users.
For this aim, in user.c, go to the function alloc_uid
where new users get allocated and initialized. For example you see there's a line atomic_set(&new->__count, 1);
that initializes __count
. Add your initialization code beside this.
atomic_set(&new->__count, 1);
atomic_set(&new->ready_processes, 1); /* I added this line! */
注意:它在Linux 2.6.32.62中工作.我不确定其他版本,但我认为应该没什么不同.
NOTE: It works in linux 2.6.32.62. I'm not sure about other versions but I think it shouldn't be very different.
这篇关于向user_struct添加另一个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!