Chris PeBenito 473ea7
#include <unistd.h>
Chris PeBenito 473ea7
#include <fcntl.h>
Chris PeBenito 473ea7
#include <string.h>
Chris PeBenito 473ea7
#include <stdio.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 getpidcon_raw(pid_t pid, security_context_t *context)
Chris PeBenito 473ea7
{
Chris PeBenito 473ea7
	char path[40];
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
	snprintf(path, sizeof path, "/proc/%d/attr/current", pid);
Chris PeBenito 473ea7
Chris PeBenito 473ea7
	fd = open(path, 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
	*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(getpidcon_raw)
Chris PeBenito 473ea7
Chris PeBenito 473ea7
int getpidcon(pid_t pid, 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 = getpidcon_raw(pid, &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
}