Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <stdlib.h>
Chris PeBenito 473ea7
#include <errno.h>
Chris PeBenito 473ea7
#include <asm/page.h>
Chris PeBenito 473ea7
#include "selinux_internal.h"
Chris PeBenito 473ea7
#include "policy.h"
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getfscreatecon_raw(security_context_t *context)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char *buf;
Chris PeBenito 473ea7
	size_t size;
Chris PeBenito 473ea7
	int fd;
Chris PeBenito 473ea7
	ssize_t ret;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	fd = open("/proc/self/attr/fscreate", O_RDONLY);
Chris PeBenito 473ea7
	if (fd < 0)
Chris PeBenito 473ea7
		return -1;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	size = PAGE_SIZE;
Chris PeBenito 473ea7
	buf = malloc(size);
Chris PeBenito 473ea7
	if (!buf) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	memset(buf, 0, size);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	ret = read(fd, buf, size-1);
Chris PeBenito 473ea7
	if (ret < 0)
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (ret == 0) {
Chris PeBenito 473ea7
		*context = NULL;
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	*context = strdup(buf);
Chris PeBenito 473ea7
	if (!(*context)) {
Chris PeBenito 473ea7
		ret = -1;
Chris PeBenito 473ea7
		goto out2;
Chris PeBenito 473ea7
	}
Chris PeBenito 473ea7
	ret = 0;
Chris PeBenito 473ea7
out2:			
Chris PeBenito 473ea7
	free(buf);
Chris PeBenito 473ea7
out:
Chris PeBenito 473ea7
	close(fd);
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}
Chris PeBenito 473ea7
hidden_def(getfscreatecon_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getfscreatecon(security_context_t *context)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	int ret;
Chris PeBenito 473ea7
	security_context_t rcontext;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
 	ret = getfscreatecon_raw(&rcontext);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	if (context_translations && !ret) {
Chris PeBenito 473ea7
		if (raw_to_trans_context(rcontext, context)) {
Chris PeBenito 473ea7
			*context = NULL;
Chris PeBenito 473ea7
			ret = -1;
Chris PeBenito 473ea7
		}
Chris PeBenito 473ea7
		freecon(rcontext);
Chris PeBenito 473ea7
	} else if (!ret)
Chris PeBenito 473ea7
		*context = rcontext;
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	return ret;
Chris PeBenito 473ea7
}