Add another field to user_struct

I want to add a new field (to store the number of the finished process of this user) to user_struct in the 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?

+4
source share
1 answer

To add a new field to user_struct , you need to do 3 things:

  • The definition of user_struct is in the sched.h file (include / linux / sched.h)
    You must add your field to the struct .

     struct user_struct { atomic_t ready_processes; /* I added this line! */ /*Other fields*/ }; 
  • In the line user.c (kernel / user.c) 51, user_struct is created globally for root_user . 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 have finished initializing your field for the root user, but you must also initialize it for other users.
    For this purpose, in user.c, navigate to the alloc_uid function, where new users get allocated and initialized. For example, you see there the string atomic_set(&new->__count, 1); which initializes __count . Add to this the initialization code.

     atomic_set(&new->__count, 1); atomic_set(&new->ready_processes, 1); /* I added this line! */ 

NOTE. It works on Linux 2.6.32.62. I am not sure about the other versions, but I think that it should not be completely different.

+4
source

All Articles