Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add another field to 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 ?

like image 840
Esterlinkof Avatar asked Dec 21 '14 14:12

Esterlinkof


1 Answers

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

  1. Definition of user_struct is in file sched.h(include/linux/sched.h)
    You should add your field in that struct.

    struct user_struct {
        atomic_t ready_processes; /* I added this line! */
        /*Other fields*/
    };
    
  2. 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,    
    };
    
  3. 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! */
    

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.

like image 84
Ali Seyedi Avatar answered Oct 10 '22 03:10

Ali Seyedi