Reading the context of a resource

It is, of course, also important to obtain the context of a resource if the application is SELinux-aware. This could be for logging purposes or to decide which domain to transition to (based on the resource context, current context, username, and so on).

How to do it…

To read the context of a resource, the following methods are available:

  1. Given a file path, the following call to getfilecon() will provide the context of the file:
    security_context_t filecon = 0;
    char * path = "/etc/passwd";
    rc = getfilecon(path, &filecon);
    if (rc < 0) {
      … // Call failed
    };
    … // Do stuff with the context
    freecon(filecon);
  2. To get the context of a process, assuming the pid variable (of the pid_t type) has the proper process ID in it, the ...

Get SELinux Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.