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