|
| 1 | +/** |
| 2 | + * D header file for spawn.h. |
| 3 | + * |
| 4 | + * Copyright: Copyright (C) 2018 by The D Language Foundation, All Rights Reserved |
| 5 | + * Authors: Petar Kirov |
| 6 | + * License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 | + * Source: $(LINK2 https://github.com/dlang/druntime/blob/master/src/core/sys/posix/spawn.d, _spawn.d) |
| 8 | + * Standards: The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition |
| 9 | + */ |
| 10 | +module core.sys.posix.spawn; |
| 11 | + |
| 12 | +/* |
| 13 | +Based on the following system headers: |
| 14 | +
|
| 15 | +Glibc: https://sourceware.org/git/?p=glibc.git;a=blob;f=posix/spawn.h;hb=HEAD |
| 16 | +
|
| 17 | +Bionic libc: https://android.googlesource.com/platform/bionic.git/+/master/libc/include/spawn.h |
| 18 | +
|
| 19 | +Musl libc: https://git.musl-libc.org/cgit/musl/tree/include/spawn.h |
| 20 | +
|
| 21 | +uClibc: https://git.uclibc.org/uClibc/tree/include/spawn.h |
| 22 | +
|
| 23 | +Darwin XNU: |
| 24 | +https://opensource.apple.com/source/xnu/xnu-4570.71.2/libsyscall/wrappers/spawn/spawn.h.auto.html |
| 25 | +https://opensource.apple.com/source/xnu/xnu-4570.71.2/bsd/sys/spawn.h.auto.html |
| 26 | +https://github.com/opensource-apple/xnu (GitHub mirror) |
| 27 | +
|
| 28 | +FreeBSD: https://github.com/freebsd/freebsd/blob/master/include/spawn.h |
| 29 | +
|
| 30 | +NetBSD: https://github.com/NetBSD/src/blob/trunk/sys/sys/spawn.h |
| 31 | +
|
| 32 | +OpenBSD: https://github.com/openbsd/src/blob/master/include/spawn.h |
| 33 | +
|
| 34 | +DragonFlyBSD: https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/include/spawn.h |
| 35 | +
|
| 36 | +Solaris: https://github.com/illumos/illumos-gate/blob/master/usr/src/head/spawn.h |
| 37 | +*/ |
| 38 | + |
| 39 | +version (OSX) // macOS and iOS only as this API is prohibited on WatchOS and TVOS |
| 40 | + version = Darwin; |
| 41 | +else version (iOS) |
| 42 | + version = Darwin; |
| 43 | + |
| 44 | +version (Posix): |
| 45 | +public import core.sys.posix.sys.types : mode_t, pid_t; |
| 46 | +public import core.sys.posix.signal : sigset_t; |
| 47 | +public import core.sys.posix.sched : sched_param; |
| 48 | + |
| 49 | +extern(C): |
| 50 | +@nogc: |
| 51 | +nothrow: |
| 52 | + |
| 53 | +int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t*, int); |
| 54 | +int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t*, int, int); |
| 55 | +int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t*, int, const char*, int, mode_t); |
| 56 | +int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t*); |
| 57 | +int posix_spawn_file_actions_init(posix_spawn_file_actions_t*); |
| 58 | +int posix_spawnattr_destroy(posix_spawnattr_t*); |
| 59 | +int posix_spawnattr_getflags(const posix_spawnattr_t*, short*); |
| 60 | +int posix_spawnattr_getpgroup(const posix_spawnattr_t*, pid_t*); |
| 61 | + |
| 62 | +version (Darwin) |
| 63 | +{ } // Not supported |
| 64 | +else |
| 65 | +{ |
| 66 | + int posix_spawnattr_getschedparam(const posix_spawnattr_t*, sched_param*); |
| 67 | + int posix_spawnattr_getschedpolicy(const posix_spawnattr_t*, int*); |
| 68 | + int posix_spawnattr_setschedparam(posix_spawnattr_t*, const sched_param*); |
| 69 | + int posix_spawnattr_setschedpolicy(posix_spawnattr_t*, int); |
| 70 | +} |
| 71 | + |
| 72 | +int posix_spawnattr_getsigdefault(const posix_spawnattr_t*, sigset_t*); |
| 73 | +int posix_spawnattr_getsigmask(const posix_spawnattr_t*, sigset_t*); |
| 74 | +int posix_spawnattr_init(posix_spawnattr_t*); |
| 75 | +int posix_spawnattr_setflags(posix_spawnattr_t*, short); |
| 76 | +int posix_spawnattr_setpgroup(posix_spawnattr_t*, pid_t); |
| 77 | +int posix_spawnattr_setsigdefault(posix_spawnattr_t*, const sigset_t*); |
| 78 | +int posix_spawnattr_setsigmask(posix_spawnattr_t*, const sigset_t*); |
| 79 | +int posix_spawn(pid_t*pid, const char* path, |
| 80 | + const posix_spawn_file_actions_t* file_actions, |
| 81 | + const posix_spawnattr_t* attrp, |
| 82 | + const char** argv, const char** envp); |
| 83 | +int posix_spawnp(pid_t* pid, const char* file, |
| 84 | + const posix_spawn_file_actions_t* file_actions, |
| 85 | + const posix_spawnattr_t* attrp, |
| 86 | + const char** argv, const char** envp); |
| 87 | + |
| 88 | +version (linux) |
| 89 | +{ |
| 90 | + version (CRuntime_Glibc) |
| 91 | + { |
| 92 | + // Source: https://sourceware.org/git/?p=glibc.git;a=blob;f=posix/spawn.h;hb=HEAD |
| 93 | + enum |
| 94 | + { |
| 95 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 96 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 97 | + POSIX_SPAWN_SETSIGDEF = 0x04, |
| 98 | + POSIX_SPAWN_SETSIGMASK = 0x08, |
| 99 | + POSIX_SPAWN_SETSCHEDPARAM = 0x10, |
| 100 | + POSIX_SPAWN_SETSCHEDULER = 0x20 |
| 101 | + } |
| 102 | + import core.sys.posix.config : __USE_GNU; |
| 103 | + static if (__USE_GNU) |
| 104 | + { |
| 105 | + enum |
| 106 | + { |
| 107 | + POSIX_SPAWN_USEVFORK = 0x40, |
| 108 | + POSIX_SPAWN_SETSID = 0x80 |
| 109 | + } |
| 110 | + } |
| 111 | + struct posix_spawnattr_t |
| 112 | + { |
| 113 | + short __flags; |
| 114 | + pid_t __pgrp; |
| 115 | + sigset_t __sd; |
| 116 | + sigset_t __ss; |
| 117 | + sched_param __sp; |
| 118 | + int __policy; |
| 119 | + int[16] __pad; |
| 120 | + } |
| 121 | + struct __spawn_action; |
| 122 | + struct posix_spawn_file_actions_t |
| 123 | + { |
| 124 | + int __allocated; |
| 125 | + int __used; |
| 126 | + __spawn_action* __actions; |
| 127 | + int[16] __pad; |
| 128 | + } |
| 129 | + } |
| 130 | + else version (CRuntime_Bionic) |
| 131 | + { |
| 132 | + // Source: https://android.googlesource.com/platform/bionic.git/+/master/libc/include/spawn.h |
| 133 | + enum |
| 134 | + { |
| 135 | + POSIX_SPAWN_RESETIDS = 1, |
| 136 | + POSIX_SPAWN_SETPGROUP = 2, |
| 137 | + POSIX_SPAWN_SETSIGDEF = 4, |
| 138 | + POSIX_SPAWN_SETSIGMASK = 8, |
| 139 | + POSIX_SPAWN_SETSCHEDPARAM = 16, |
| 140 | + POSIX_SPAWN_SETSCHEDULER = 32 |
| 141 | + } |
| 142 | + import core.sys.posix.config : __USE_GNU; |
| 143 | + static if (__USE_GNU) |
| 144 | + { |
| 145 | + enum |
| 146 | + { |
| 147 | + POSIX_SPAWN_USEVFORK = 64, |
| 148 | + POSIX_SPAWN_SETSID = 128 |
| 149 | + } |
| 150 | + } |
| 151 | + alias posix_spawnattr_t = __posix_spawnattr*; |
| 152 | + alias posix_spawn_file_actions_t = __posix_spawn_file_actions*; |
| 153 | + struct __posix_spawnattr; |
| 154 | + struct __posix_spawn_file_actions; |
| 155 | + } |
| 156 | + else version (CRuntime_Musl) |
| 157 | + { |
| 158 | + // Source: https://git.musl-libc.org/cgit/musl/tree/include/spawn.h |
| 159 | + enum |
| 160 | + { |
| 161 | + POSIX_SPAWN_RESETIDS = 1, |
| 162 | + POSIX_SPAWN_SETPGROUP = 2, |
| 163 | + POSIX_SPAWN_SETSIGDEF = 4, |
| 164 | + POSIX_SPAWN_SETSIGMASK = 8, |
| 165 | + POSIX_SPAWN_SETSCHEDPARAM = 16, |
| 166 | + POSIX_SPAWN_SETSCHEDULER = 32, |
| 167 | + POSIX_SPAWN_USEVFORK = 64, |
| 168 | + POSIX_SPAWN_SETSID = 128 |
| 169 | + } |
| 170 | + struct posix_spawnattr_t |
| 171 | + { |
| 172 | + int __flags; |
| 173 | + pid_t __pgrp; |
| 174 | + sigset_t __def, __mask; |
| 175 | + int __prio, __pol; |
| 176 | + void* __fn; |
| 177 | + char[64 - (void*).sizeof] __pad; |
| 178 | + } |
| 179 | + struct posix_spawn_file_actions_t |
| 180 | + { |
| 181 | + int[2] __pad0; |
| 182 | + void* __actions; |
| 183 | + int[16] __pad; |
| 184 | + } |
| 185 | + } |
| 186 | + else version (CRuntime_UClibc) |
| 187 | + { |
| 188 | + // Source: https://git.uclibc.org/uClibc/tree/include/spawn.h |
| 189 | + enum |
| 190 | + { |
| 191 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 192 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 193 | + POSIX_SPAWN_SETSIGDEF = 0x04, |
| 194 | + POSIX_SPAWN_SETSIGMASK = 0x08, |
| 195 | + POSIX_SPAWN_SETSCHEDPARAM = 0x10, |
| 196 | + POSIX_SPAWN_SETSCHEDULER = 0x20 |
| 197 | + } |
| 198 | + import core.sys.posix.config : __USE_GNU; |
| 199 | + static if (__USE_GNU) |
| 200 | + { |
| 201 | + enum |
| 202 | + { |
| 203 | + POSIX_SPAWN_USEVFORK = 0x40, |
| 204 | + } |
| 205 | + } |
| 206 | + struct posix_spawnattr_t |
| 207 | + { |
| 208 | + short __flags; |
| 209 | + pid_t __pgrp; |
| 210 | + sigset_t __sd; |
| 211 | + sigset_t __ss; |
| 212 | + sched_param __sp; |
| 213 | + int __policy; |
| 214 | + int[16] __pad; |
| 215 | + } |
| 216 | + struct posix_spawn_file_actions_t |
| 217 | + { |
| 218 | + int __allocated; |
| 219 | + int __used; |
| 220 | + __spawn_action* __actions; |
| 221 | + int[16] __pad; |
| 222 | + } |
| 223 | + } |
| 224 | + else |
| 225 | + static assert(0, "Unsupported Linux libc"); |
| 226 | +} |
| 227 | +else version (Darwin) |
| 228 | +{ |
| 229 | + // Sources: |
| 230 | + // https://opensource.apple.com/source/xnu/xnu-4570.71.2/libsyscall/wrappers/spawn/spawn.h.auto.html |
| 231 | + // https://opensource.apple.com/source/xnu/xnu-4570.71.2/bsd/sys/spawn.h.auto.html |
| 232 | + enum |
| 233 | + { |
| 234 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 235 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 236 | + POSIX_SPAWN_SETSIGDEF = 0x04, |
| 237 | + POSIX_SPAWN_SETSIGMASK = 0x08, |
| 238 | + // POSIX_SPAWN_SETSCHEDPARAM = 0x10, // not supported |
| 239 | + // POSIX_SPAWN_SETSCHEDULER = 0x20, // ditto |
| 240 | + POSIX_SPAWN_SETEXEC = 0x40, |
| 241 | + POSIX_SPAWN_START_SUSPENDED = 0x80 |
| 242 | + } |
| 243 | + alias posix_spawnattr_t = void*; |
| 244 | + alias posix_spawn_file_actions_t = void*; |
| 245 | +} |
| 246 | +else version (FreeBSD) |
| 247 | +{ |
| 248 | + // Source: https://github.com/freebsd/freebsd/blob/master/include/spawn.h |
| 249 | + enum |
| 250 | + { |
| 251 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 252 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 253 | + POSIX_SPAWN_SETSCHEDPARAM = 0x04, |
| 254 | + POSIX_SPAWN_SETSCHEDULER = 0x08, |
| 255 | + POSIX_SPAWN_SETSIGDEF = 0x10, |
| 256 | + POSIX_SPAWN_SETSIGMASK = 0x20 |
| 257 | + } |
| 258 | + alias posix_spawnattr_t = void*; |
| 259 | + alias posix_spawn_file_actions_t = void*; |
| 260 | +} |
| 261 | +else version (NetBSD) |
| 262 | +{ |
| 263 | + // Source: https://github.com/NetBSD/src/blob/trunk/sys/sys/spawn.h |
| 264 | + enum |
| 265 | + { |
| 266 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 267 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 268 | + POSIX_SPAWN_SETSCHEDPARAM = 0x04, |
| 269 | + POSIX_SPAWN_SETSCHEDULER = 0x08, |
| 270 | + POSIX_SPAWN_SETSIGDEF = 0x10, |
| 271 | + POSIX_SPAWN_SETSIGMASK = 0x20, |
| 272 | + POSIX_SPAWN_RETURNERROR = 0x40 // NetBSD specific |
| 273 | + } |
| 274 | + struct posix_spawnattr |
| 275 | + { |
| 276 | + short sa_flags; |
| 277 | + pid_t sa_pgroup; |
| 278 | + sched_param sa_schedparam; |
| 279 | + int sa_schedpolicy; |
| 280 | + sigset_t sa_sigdefault; |
| 281 | + sigset_t sa_sigmask; |
| 282 | + } |
| 283 | + struct posix_spawn_file_actions_entry_t; |
| 284 | + struct posix_spawn_file_actions |
| 285 | + { |
| 286 | + uint size; |
| 287 | + uint len; |
| 288 | + posix_spawn_file_actions_entry_t* fae; |
| 289 | + } |
| 290 | +} |
| 291 | +else version (OpenBSD) |
| 292 | +{ |
| 293 | + // Source: https://github.com/openbsd/src/blob/master/include/spawn.h |
| 294 | + enum |
| 295 | + { |
| 296 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 297 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 298 | + POSIX_SPAWN_SETSCHEDPARAM = 0x04, |
| 299 | + POSIX_SPAWN_SETSCHEDULER = 0x08, |
| 300 | + POSIX_SPAWN_SETSIGDEF = 0x10, |
| 301 | + POSIX_SPAWN_SETSIGMASK = 0x20 |
| 302 | + } |
| 303 | + alias posix_spawnattr_t = __posix_spawnattr*; |
| 304 | + alias posix_spawn_file_actions_t = __posix_spawn_file_actions*; |
| 305 | + struct __posix_spawnattr; |
| 306 | + struct __posix_spawn_file_actions; |
| 307 | +} |
| 308 | +else version (DragonFlyBSD) |
| 309 | +{ |
| 310 | + // Source: https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/include/spawn.h |
| 311 | + enum |
| 312 | + { |
| 313 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 314 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 315 | + POSIX_SPAWN_SETSCHEDPARAM = 0x04, |
| 316 | + POSIX_SPAWN_SETSCHEDULER = 0x08, |
| 317 | + POSIX_SPAWN_SETSIGDEF = 0x10, |
| 318 | + POSIX_SPAWN_SETSIGMASK = 0x20 |
| 319 | + } |
| 320 | + alias posix_spawnattr_t = __posix_spawnattr*; |
| 321 | + alias posix_spawn_file_actions_t = __posix_spawn_file_actions*; |
| 322 | + struct __posix_spawnattr; |
| 323 | + struct __posix_spawn_file_actions; |
| 324 | +} |
| 325 | +else version (Solaris) |
| 326 | +{ |
| 327 | + // Source: https://github.com/illumos/illumos-gate/blob/master/usr/src/head/spawn.h |
| 328 | + enum |
| 329 | + { |
| 330 | + POSIX_SPAWN_RESETIDS = 0x01, |
| 331 | + POSIX_SPAWN_SETPGROUP = 0x02, |
| 332 | + POSIX_SPAWN_SETSIGDEF = 0x04, |
| 333 | + POSIX_SPAWN_SETSIGMASK = 0x08, |
| 334 | + POSIX_SPAWN_SETSCHEDPARAM = 0x10, |
| 335 | + POSIX_SPAWN_SETSCHEDULER = 0x20, |
| 336 | + } |
| 337 | + version (none) |
| 338 | + { |
| 339 | + // Non-portable Solaris extensions. |
| 340 | + enum |
| 341 | + { |
| 342 | + POSIX_SPAWN_SETSIGIGN_NP = 0x0800, |
| 343 | + POSIX_SPAWN_NOSIGCHLD_NP = 0x1000, |
| 344 | + POSIX_SPAWN_WAITPID_NP = 0x2000, |
| 345 | + POSIX_SPAWN_NOEXECERR_NP = 0x4000, |
| 346 | + } |
| 347 | + } |
| 348 | + struct posix_spawnattr_t |
| 349 | + { |
| 350 | + void* __spawn_attrp; |
| 351 | + } |
| 352 | + struct posix_spawn_file_actions_t |
| 353 | + { |
| 354 | + void* __file_attrp; |
| 355 | + } |
| 356 | + version (none) |
| 357 | + { |
| 358 | + // Non-portable Solaris extensions. |
| 359 | + alias boolean_t = int; |
| 360 | + int posix_spawn_file_actions_addclosefrom_np(posix_spawn_file_actions_t* file_actions, |
| 361 | + int lowfiledes); |
| 362 | + int posix_spawn_pipe_np(pid_t* pidp, int* fdp, const char* cmd, boolean_t write, |
| 363 | + posix_spawn_file_actions_t* fact, |
| 364 | + posix_spawnattr_t* attr); |
| 365 | + int posix_spawnattr_getsigignore_np(const posix_spawnattr_t* attr, sigset_t* sigignore); |
| 366 | + int posix_spawnattr_setsigignore_np(posix_spawnattr_t* attr, const sigset_t* sigignore); |
| 367 | + } |
| 368 | +} |
| 369 | +else |
| 370 | + static assert(0, "Unsupported OS"); |
0 commit comments