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