liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/xattr.h>
#include "selinux_internal.h"
#include "policy.h"
 
int setfilecon_raw(const char *path, const char * context)
{
   int rc = setxattr(path, XATTR_NAME_SELINUX, context, strlen(context) + 1,
           0);
   if (rc < 0 && errno == ENOTSUP) {
       char * ccontext = NULL;
       int err = errno;
       if ((getfilecon_raw(path, &ccontext) >= 0) &&
           (strcmp(context,ccontext) == 0)) {
           rc = 0;
       } else {
           errno = err;
       }
       freecon(ccontext);
   }
   return rc;
}
 
hidden_def(setfilecon_raw)
 
int setfilecon(const char *path, const char *context)
{
   int ret;
   char * rcontext;
 
   if (selinux_trans_to_raw_context(context, &rcontext))
       return -1;
 
   ret = setfilecon_raw(path, rcontext);
 
   freecon(rcontext);
 
   return ret;
}