Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/unix/native/libjava/posix_spawn.c
41119 views
1
// From https://android.googlesource.com/platform/external/dhcpcd-6.8.2/+/refs/heads/pie-dr1-release/compat/posix_spawn.c
2
/*
3
* dhcpcd - DHCP client daemon
4
* Copyright (c) 2006-2012 Roy Marples <[email protected]>
5
* All rights reserved
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*/
27
28
/* This implementation of posix_spawn is only suitable for the needs of dhcpcd
29
* but it could easily be extended to other applications. */
30
31
#include <sys/types.h>
32
#include <sys/wait.h>
33
#include <errno.h>
34
#include <signal.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <unistd.h>
38
39
#include "posix_spawn.h"
40
41
#ifndef _NSIG
42
#ifdef _SIG_MAXSIG
43
#define _NSIG _SIG_MAXSIG + 1
44
#else
45
/* Guess */
46
#define _NSIG SIGPWR + 1
47
#endif
48
#endif
49
50
extern char **environ;
51
52
static int
53
posix_spawnattr_handle(const posix_spawnattr_t *attrp)
54
{
55
struct sigaction sa;
56
int i;
57
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGMASK)
58
sigprocmask(SIG_SETMASK, &attrp->posix_attr_sigmask, NULL);
59
if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGDEF) {
60
memset(&sa, 0, sizeof(sa));
61
sa.sa_handler = SIG_DFL;
62
for (i = 1; i < _NSIG; i++) {
63
if (sigismember(&attrp->posix_attr_sigdefault, i)) {
64
if (sigaction(i, &sa, NULL) == -1)
65
return -1;
66
}
67
}
68
}
69
return 0;
70
}
71
72
inline static int
73
is_vfork_safe(short int flags)
74
{
75
return !(flags & (POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK));
76
}
77
78
int
79
posix_spawn(pid_t *pid, const char *path,
80
const posix_spawn_file_actions_t *file_actions,
81
const posix_spawnattr_t *attrp,
82
char *const argv[], char *const envp[])
83
{
84
short int flags;
85
pid_t p;
86
volatile int error;
87
error = 0;
88
flags = attrp ? attrp->posix_attr_flags : 0;
89
if (file_actions == NULL && is_vfork_safe(flags))
90
p = vfork();
91
else
92
#ifdef THERE_IS_NO_FORK
93
return ENOSYS;
94
#else
95
p = fork();
96
#endif
97
switch (p) {
98
case -1:
99
return errno;
100
case 0:
101
if (attrp) {
102
error = posix_spawnattr_handle(attrp);
103
if (error)
104
_exit(127);
105
}
106
execve(path, argv, envp);
107
error = errno;
108
_exit(127);
109
default:
110
if (error != 0)
111
waitpid(p, NULL, WNOHANG);
112
else if (pid != NULL)
113
*pid = p;
114
return error;
115
}
116
}
117
118
int
119
posix_spawnattr_init(posix_spawnattr_t *attr)
120
{
121
memset(attr, 0, sizeof(*attr));
122
attr->posix_attr_flags = 0;
123
sigprocmask(0, NULL, &attr->posix_attr_sigmask);
124
sigemptyset(&attr->posix_attr_sigdefault);
125
return 0;
126
}
127
128
int
129
posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags)
130
{
131
attr->posix_attr_flags = flags;
132
return 0;
133
}
134
135
int
136
posix_spawnattr_setsigmask(posix_spawnattr_t *attr, const sigset_t *sigmask)
137
{
138
attr->posix_attr_sigmask = *sigmask;
139
return 0;
140
}
141
142
int
143
posix_spawnattr_setsigdefault(posix_spawnattr_t *attr, const sigset_t *sigmask)
144
{
145
attr->posix_attr_sigdefault = *sigmask;
146
return 0;
147
}
148
149