aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/src/util_spawn.c
blob: 09c740d89c13209f2205c5b02b7e1c47544c2170 (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sched.h>
#include <sys/wait.h>
#include <sys/mman.h>

#include <d2tk/util.h>

typedef struct _clone_data_t clone_data_t;

struct _clone_data_t {
	char **argv;
};

static int
_clone(void *data)
{
	clone_data_t *clone_data = data;

	execvp(clone_data->argv[0], clone_data->argv);
	_exit(EXIT_FAILURE);

	return 0;
}

D2TK_API int
d2tk_util_spawn(char **argv)
{
	clone_data_t clone_data = {
		.argv = argv
	};

#if D2TK_CLONE == 1
#	define STACK_SIZE (1024 * 1024)
	uint8_t *stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
		MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
	if(stack == MAP_FAILED)
	{
		return -1;
	}

	uint8_t *stack_top = stack + STACK_SIZE;
	const int flags = CLONE_FS | CLONE_IO | CLONE_VFORK | CLONE_VM;
	const int pid = clone(_clone, stack_top, flags, &clone_data);
#	undef STACK_SIZE
#elif D2TK_VFORK == 1
	const int pid = vfork();
#else
	const int pid = fork();
#endif

	switch(pid)
	{
		case -1:
		{
		} return -1;

#if D2TK_CLONE == 0
    case 0: //child
		{
			// everything is done in _clone
		} return _clone(&clone_data);
#endif

		default: // parent
		{
		} return pid;
	}
}

D2TK_API int
d2tk_util_kill(int *kid)
{
	if(*kid <= 0)
	{
		return 0;
	}

	kill(*kid, SIGKILL);

	if(waitpid(*kid, NULL, 0) == *kid)
	{
		*kid = -1;
		return 0;
	}

	return 1;
}

D2TK_API int
d2tk_util_wait(int *kid)
{
	if(*kid <= 0)
	{
		return 0;
	}

	if(waitpid(*kid, NULL, WNOHANG) == *kid)
	{
		*kid = -1;
		return 0;
	}

	return 1;
}