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
10int 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
29int setfilecon(const char *path, const char *context)
30{
31	int ret;
32	char * rcontext;
33
34	if (selinux_trans_to_raw_context(context, &rcontext))
35		return -1;
36
37	ret = setfilecon_raw(path, rcontext);
38
39	freecon(rcontext);
40
41	return ret;
42}
43