问题
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 ?
回答1:
In order to add a new field to user_struct, you need to do 3 things:
Definition of
user_structis 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*/ };In user.c (kernel/user.c) line 51,
user_structis instantiated forroot_userglobally. 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, };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 functionalloc_uidwhere new users get allocated and initialized. For example you see there's a lineatomic_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! */
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.
来源:https://stackoverflow.com/questions/27594865/add-another-field-to-user-struct