clinuxlinux-capabilities

Test for linux CAP_FOWNER capability in C?


Before invoking chmod() on a directory, if the caller does not own the directory, I would like to test that the caller has the CAP_FOWNER capability.

From searching, it seems that I should be able to test for the CAP_FOWNER capability by calling capable(CAP_FOWNER) -- but capable() is not among my man pages and does not seem to be exported by <linux/capability.h>.

What's the right include file for capable(), or alternatively, what's the simplest/best way to test for a linux capability?


Solution

  • I think that capable() is available within the kernel sources, but not for general use. If you are writing a device driver or module then it should be available.

    If you are writing a user space program, then you might be able to use functions provided by libcap; see man capabilities(7) and man libcap(3). I'd suggest #include <sys/capability.h> and use cap_get_proc() and possibly CAP_IS_SUPPORTED(CAP_FOWNER).

    If that's no good the obvious workaround is to attempt chmod() on the directory and handle possible failure.