1

I just wonder, how can we determine if the current process is running as root or not. after searching on google, I found out that Linux has a field called "current" that I can use to determine who is running a particular process. I try to use

current->uid == 0

However, when I try to compile my code I get this error

struct task_structâ has no member named âuidâ

did I do something wrong? additionally, is it true if the process run as root, the uid always equal to zero? thanks

eldwin h
  • 57
  • 5
  • 3
    The `current->***` variable is only visible from kernel-space. In user space you should use `getuid()` – wildplasser Aug 09 '13 at 20:56
  • 3
    Just use `getuid() == 0` or `geteuid() == 0`. –  Aug 09 '13 at 20:56
  • 2
    BTW: the getuid() and geteuid() functions are actually systemcalls that inspect the current->[e]uid fields (for the current process), and report them back to the user process. – wildplasser Aug 09 '13 at 21:12

1 Answers1

3

For a process ,it have 3 kind of user ID:

1.Actual user ID

--> most of time is who we are, the man who login , while the root process has some way to change it.

Use getuid() to get this user ID.

2.Effective user ID

-->This ID decide the access limits. exce function can set this ID ,and if it didn't, this user ID is the same as actual user ID.

Use geteuid() to get this ID

3.Saved settings user ID

--> it copyed from the effective user ID by exec function. No function to get this ID's current value.At least , I don't know.

So

how can we determine if the current process is running as root or not

If you mean running as root ,use geteuid() == 0 is better, just my opinion.

Lidong Guo
  • 2,817
  • 2
  • 19
  • 31