liberpal-skeleton-v2.c 1.2 KB
Newer Older
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/stat.h>
#include "liberpal-skeleton.h"

int pal_get_version(void)
{
	return 2;
}

int pal_init(pal_attr_t *attr)
{
	if (is_oot_driver) {
		fprintf(stderr, "Skeleton PAL API v2 doesn't support SGX OOT driver!\n");
		return -1;
	}

	return __pal_init(attr);
}

int pal_create_process(pal_create_process_args *args)
{
	if (args == NULL || args->path == NULL || args->argv == NULL || args->pid == NULL || args->stdio == NULL) {
		errno = EINVAL;
		return -1;
	}

	int pid;
	if ((pid = fork()) < 0)
		return -1;
	else if (pid == 0) {
		int exit_code, ret;

		ret = __pal_exec(args->path, args->argv, args->stdio, &exit_code);
		exit(ret ? ret : exit_code);
	} else
		*args->pid = pid;

	return 0;
}

int pal_exec(pal_exec_args *attr)
{
	if (attr == NULL || attr->exit_value == NULL) {
		errno = EINVAL;
		return -1;
	}

	int status;
	waitpid(attr->pid, &status, 0);

	if (WIFEXITED(status) || WIFSIGNALED(status))
		*attr->exit_value = WEXITSTATUS(status);

	return 0;
}

int pal_kill(int pid, int sig)
{
	/* No implementation */
	return 0;
}

int pal_destroy(void)
{
	return __pal_destory();
}