views:

270

answers:

1

Hi there,

I'm trying to check if a file has the setuid bit in Python.

The stat doc mentions a S_ISUID function but it only works with os.chmod(), not to actually read the setuid bit. It also lists S_IMODE, but I have no idea how to interpret it.

How can I easily check if a file as the setuid root bit set?

+4  A: 

stat.S_ISUID is the mode bit for 'setuid'. You compare the stat result's mode to see if it contains that bit:

>>> ping = os.stat('/bin/ping')
>>> ping.st_mode & stat.S_ISUID
2048
>>> echo = os.stat('/bin/echo')
>>> echo.st_mode & stat.S_ISUID
0
Thomas Wouters
Thank you very much. I find the example in the manual doesn't make this very clear as all the examples they give use things like `S_ISDIR(mode)` so I was trying `S_ISUID(mode)` and it didn't work.
Raphink