spawn.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /*++
  2. Copyright (c) 2016 Minoca Corp.
  3. This file is licensed under the terms of the GNU Lesser General Public
  4. License version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details.
  6. Module Name:
  7. spawn.h
  8. Abstract:
  9. This header contains definitions for the posix_spawn
  10. Author:
  11. Evan Green 20-Jul-2016
  12. --*/
  13. #ifndef _SPAWN_H
  14. #define _SPAWN_H
  15. //
  16. // ------------------------------------------------------------------- Includes
  17. //
  18. #include <libcbase.h>
  19. #include <sched.h>
  20. #include <signal.h>
  21. #include <sys/types.h>
  22. //
  23. // ---------------------------------------------------------------- Definitions
  24. //
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. //
  29. // Set this flag to have the child's effective user and group IDs set back to
  30. // its real IDs.
  31. //
  32. #define POSIX_SPAWN_RESETIDS 0x00000001
  33. //
  34. // Set this flag to have the child's process group ID set as defined in the
  35. // attributes. If no attribute was set and this flag is set, the process group
  36. // ID will be set to the process ID of the new child.
  37. //
  38. #define POSIX_SPAWN_SETPGROUP 0x00000002
  39. //
  40. // Set this flag to set the scheduling parameter of the child process.
  41. //
  42. #define POSIX_SPAWN_SETSCHEDPARAM 0x00000004
  43. //
  44. // Set this flag to set the scheduling policy of the child process.
  45. //
  46. #define POSIX_SPAWN_SETSCHEDULER 0x00000008
  47. //
  48. // Set this flag to reset the signals defined in the setsigdefault attribute
  49. // back to their default disposition.
  50. //
  51. #define POSIX_SPAWN_SETSIGDEF 0x00000010
  52. //
  53. // Set this flag to set the signal mask specified in the setsigmask attribute
  54. // in the child process.
  55. //
  56. #define POSIX_SPAWN_SETSIGMASK 0x00000020
  57. //
  58. // ------------------------------------------------------ Data Type Definitions
  59. //
  60. //
  61. // Define the opaque attribute types. Users should use the init/destroy and
  62. // get/set methods defined below in this file to manipulate these types.
  63. //
  64. typedef struct __posix_spawnattr_t *posix_spawnattr_t;
  65. typedef struct __posix_spawn_file_actions_t *posix_spawn_file_actions_t;
  66. //
  67. // -------------------------------------------------------------------- Globals
  68. //
  69. //
  70. // -------------------------------------------------------- Function Prototypes
  71. //
  72. LIBC_API
  73. int
  74. posix_spawn (
  75. pid_t *ChildPid,
  76. const char *Path,
  77. const posix_spawn_file_actions_t *FileActions,
  78. const posix_spawnattr_t *Attributes,
  79. char *const Arguments[],
  80. char *const Environment[]
  81. );
  82. /*++
  83. Routine Description:
  84. This routine spawns a new child process.
  85. Arguments:
  86. ChildPid - Supplies an optional pointer where the child process ID will be
  87. returned on success.
  88. Path - Supplies a pointer to the file path to execute.
  89. FileActions - Supplies an optional pointer to the file actions to execute
  90. in the child.
  91. Attributes - Supplies an optional pointer to the spawn attributes that
  92. affect various properties of the child.
  93. Arguments - Supplies the arguments to pass to the new child.
  94. Environment - Supplies an optional pointer to the environment to pass
  95. to the new child.
  96. Return Value:
  97. 0 on success.
  98. Returns an error number on failure.
  99. --*/
  100. LIBC_API
  101. int
  102. posix_spawnp (
  103. pid_t *ChildPid,
  104. const char *File,
  105. const posix_spawn_file_actions_t *FileActions,
  106. const posix_spawnattr_t *Attributes,
  107. char *const Arguments[],
  108. char *const Environment[]
  109. );
  110. /*++
  111. Routine Description:
  112. This routine spawns a new child process. It is identical to posix_spawn
  113. except the path is searched to find the file argument.
  114. Arguments:
  115. ChildPid - Supplies an optional pointer where the child process ID will be
  116. returned on success.
  117. File - Supplies a pointer to the file to execute. If this path contains a
  118. slash, it will be used as a relative path from the current directory
  119. or an absolute path. If this path does not contain a slash, it will
  120. use the PATH environment variable from the new child environment
  121. to attempt to find the path.
  122. FileActions - Supplies an optional pointer to the file actions to execute
  123. in the child.
  124. Attributes - Supplies an optional pointer to the spawn attributes that
  125. affect various properties of the child.
  126. Arguments - Supplies the arguments to pass to the new child.
  127. Environment - Supplies an optional pointer to the environment to pass
  128. to the new child.
  129. Return Value:
  130. 0 on success.
  131. Returns an error number on failure.
  132. --*/
  133. //
  134. // File action functions
  135. //
  136. LIBC_API
  137. int
  138. posix_spawn_file_actions_init (
  139. posix_spawn_file_actions_t *FileActions
  140. );
  141. /*++
  142. Routine Description:
  143. This routine initializes a set of posix spawn file action.
  144. Arguments:
  145. FileActions - Supplies a pointer to the file actions to initialize.
  146. Return Value:
  147. 0 on success. The caller must call the corresponding destroy routine to
  148. avoid leaking resources.
  149. Returns an error number on failure. The caller should not call destroy on
  150. this object.
  151. --*/
  152. LIBC_API
  153. int
  154. posix_spawn_file_actions_destroy (
  155. posix_spawn_file_actions_t *FileActions
  156. );
  157. /*++
  158. Routine Description:
  159. This routine destroys a set of posix spawn file actions.
  160. Arguments:
  161. FileActions - Supplies a pointer to the file actions to destroy.
  162. Return Value:
  163. 0 always.
  164. --*/
  165. LIBC_API
  166. int
  167. posix_spawn_file_actions_addopen (
  168. posix_spawn_file_actions_t *FileActions,
  169. int FileDescriptor,
  170. const char *Path,
  171. int OpenFlags,
  172. mode_t CreatePermissions
  173. );
  174. /*++
  175. Routine Description:
  176. This routine adds an open call to the set of file attributes. The spawn
  177. function will attempt to open the given file in the child.
  178. Arguments:
  179. FileActions - Supplies a pointer to the initialized file actions.
  180. FileDescriptor - Supplies the descriptor number to set the open file to.
  181. Path - Supplies a pointer to the path to open.
  182. OpenFlags - Supplies the set of open flags to use when opening the file.
  183. See O_* flags or the definition of the open function for details.
  184. CreatePermissions - Supplies the permissions to set on the new file if it
  185. is creates. See S_I* definitions or the definition of the open function
  186. for details.
  187. Return Value:
  188. 0 on success.
  189. Returns an error number on failure.
  190. --*/
  191. LIBC_API
  192. int
  193. posix_spawn_file_actions_adddup2 (
  194. posix_spawn_file_actions_t *FileActions,
  195. int FileDescriptor,
  196. int DestinationDescriptor
  197. );
  198. /*++
  199. Routine Description:
  200. This routine adds a dup2 call to the set of file attributes. The spawn
  201. function will attempt to duplicate the given descriptor in the child.
  202. Arguments:
  203. FileActions - Supplies a pointer to the initialized file actions.
  204. FileDescriptor - Supplies the descriptor to copy.
  205. DestinationDescriptor - Supplies the descriptor number to copy the
  206. descriptor to.
  207. Return Value:
  208. 0 on success.
  209. Returns an error number on failure.
  210. --*/
  211. LIBC_API
  212. int
  213. posix_spawn_file_actions_addclose (
  214. posix_spawn_file_actions_t *FileActions,
  215. int FileDescriptor
  216. );
  217. /*++
  218. Routine Description:
  219. This routine adds a close call to the set of file attributes. The spawn
  220. function will attempt to close the given descriptor in the child.
  221. Arguments:
  222. FileActions - Supplies a pointer to the initialized file actions.
  223. FileDescriptor - Supplies the descriptor to close.
  224. Return Value:
  225. 0 on success.
  226. Returns an error number on failure.
  227. --*/
  228. //
  229. // Spawn attribute functions
  230. //
  231. LIBC_API
  232. int
  233. posix_spawnattr_init (
  234. posix_spawnattr_t *Attributes
  235. );
  236. /*++
  237. Routine Description:
  238. This routine initializes a set of spawn attributes.
  239. Arguments:
  240. Attributes - Supplies a pointer to the attributes to initialize.
  241. Return Value:
  242. 0 on success. The caller must call the corresponding destroy routine to
  243. avoid leaking resources.
  244. Returns an error number on failure. The caller should not call destroy in
  245. this case.
  246. --*/
  247. LIBC_API
  248. int
  249. posix_spawnattr_destroy (
  250. posix_spawnattr_t *Attributes
  251. );
  252. /*++
  253. Routine Description:
  254. This routine destroys a set of spawn attributes.
  255. Arguments:
  256. Attributes - Supplies a pointer to the attributes to destroy.
  257. Return Value:
  258. 0 always.
  259. --*/
  260. //
  261. // Spawn attribute get functions
  262. //
  263. LIBC_API
  264. int
  265. posix_spawnattr_getflags (
  266. const posix_spawnattr_t *Attributes,
  267. short *Flags
  268. );
  269. /*++
  270. Routine Description:
  271. This routine returns the current flags on a set of spawn attributes.
  272. Arguments:
  273. Attributes - Supplies a pointer to the initialized attributes.
  274. Flags - Supplies a pointer where the flags will be returned on success.
  275. Return Value:
  276. 0 on success (always).
  277. Returns an error number on failure.
  278. --*/
  279. LIBC_API
  280. int
  281. posix_spawnattr_getpgroup (
  282. const posix_spawnattr_t *Attributes,
  283. pid_t *ProcessGroup
  284. );
  285. /*++
  286. Routine Description:
  287. This routine returns the current process group on a set of spawn attributes.
  288. Arguments:
  289. Attributes - Supplies a pointer to the initialized attributes.
  290. ProcessGroup - Supplies a pointer where the process group will be returned
  291. on success.
  292. Return Value:
  293. 0 on success (always).
  294. Returns an error number on failure.
  295. --*/
  296. LIBC_API
  297. int
  298. posix_spawnattr_getschedparam (
  299. const posix_spawnattr_t *Attributes,
  300. struct sched_param *Parameters
  301. );
  302. /*++
  303. Routine Description:
  304. This routine returns the current scheduling parameters on a set of spawn
  305. attributes.
  306. Arguments:
  307. Attributes - Supplies a pointer to the initialized attributes.
  308. Parameters - Supplies a pointer where the scheduling parameters will be
  309. returned on success.
  310. Return Value:
  311. 0 on success (always).
  312. Returns an error number on failure.
  313. --*/
  314. LIBC_API
  315. int
  316. posix_spawnattr_getschedpolicy (
  317. const posix_spawnattr_t *Attributes,
  318. int *Policy
  319. );
  320. /*++
  321. Routine Description:
  322. This routine returns the current scheduling policy on a set of spawn
  323. attributes.
  324. Arguments:
  325. Attributes - Supplies a pointer to the initialized attributes.
  326. Policy - Supplies a pointer where the scheduling policy will be returned on
  327. success.
  328. Return Value:
  329. 0 on success (always).
  330. Returns an error number on failure.
  331. --*/
  332. LIBC_API
  333. int
  334. posix_spawnattr_getsigdefault (
  335. const posix_spawnattr_t *Attributes,
  336. sigset_t *DefaultSignals
  337. );
  338. /*++
  339. Routine Description:
  340. This routine returns the current default signal set on a set of spawn
  341. attributes.
  342. Arguments:
  343. Attributes - Supplies a pointer to the initialized attributes.
  344. DefaultSignals - Supplies a pointer where the set of signals to be returned
  345. to their default dispositions will be returned on success.
  346. Return Value:
  347. 0 on success (always).
  348. Returns an error number on failure.
  349. --*/
  350. LIBC_API
  351. int
  352. posix_spawnattr_getsigmask (
  353. const posix_spawnattr_t *Attributes,
  354. sigset_t *Mask
  355. );
  356. /*++
  357. Routine Description:
  358. This routine returns the current signal mask on a set of spawn attributes.
  359. Arguments:
  360. Attributes - Supplies a pointer to the initialized attributes.
  361. Mask - Supplies a pointer where the signal mask to be set on the child
  362. process will be returned on success.
  363. Return Value:
  364. 0 on success (always).
  365. Returns an error number on failure.
  366. --*/
  367. //
  368. // Spawn attribute set functions
  369. //
  370. LIBC_API
  371. int
  372. posix_spawnattr_setflags (
  373. posix_spawnattr_t *Attributes,
  374. short Flags
  375. );
  376. /*++
  377. Routine Description:
  378. This routine sets the current flags on a set of spawn attributes.
  379. Arguments:
  380. Attributes - Supplies a pointer to the initialized attributes.
  381. Flags - Supplies the new flags to set.
  382. Return Value:
  383. 0 on success (always).
  384. Returns an error number on failure.
  385. --*/
  386. LIBC_API
  387. int
  388. posix_spawnattr_setpgroup (
  389. posix_spawnattr_t *Attributes,
  390. pid_t ProcessGroup
  391. );
  392. /*++
  393. Routine Description:
  394. This routine sets the current process group on a set of spawn attributes.
  395. Arguments:
  396. Attributes - Supplies a pointer to the initialized attributes.
  397. ProcessGroup - Supplies the process group to set the child to.
  398. Return Value:
  399. 0 on success (always).
  400. Returns an error number on failure.
  401. --*/
  402. LIBC_API
  403. int
  404. posix_spawnattr_setschedparam (
  405. posix_spawnattr_t *Attributes,
  406. const struct sched_param *Parameters
  407. );
  408. /*++
  409. Routine Description:
  410. This routine sets the current scheduling parameters on a set of spawn
  411. attributes.
  412. Arguments:
  413. Attributes - Supplies a pointer to the initialized attributes.
  414. Parameters - Supplies a pointer to the scheduling parameters to set in the
  415. child.
  416. Return Value:
  417. 0 on success (always).
  418. Returns an error number on failure.
  419. --*/
  420. LIBC_API
  421. int
  422. posix_spawnattr_setschedpolicy (
  423. posix_spawnattr_t *Attributes,
  424. int Policy
  425. );
  426. /*++
  427. Routine Description:
  428. This routine sets the current scheduling policy on a set of spawn
  429. attributes.
  430. Arguments:
  431. Attributes - Supplies a pointer to the initialized attributes.
  432. Policy - Supplies the scheduling policy to set in the child.
  433. Return Value:
  434. 0 on success (always).
  435. Returns an error number on failure.
  436. --*/
  437. LIBC_API
  438. int
  439. posix_spawnattr_setsigdefault (
  440. posix_spawnattr_t *Attributes,
  441. const sigset_t *DefaultSignals
  442. );
  443. /*++
  444. Routine Description:
  445. This routine sets the current default signal set on a set of spawn
  446. attributes.
  447. Arguments:
  448. Attributes - Supplies a pointer to the initialized attributes.
  449. DefaultSignals - Supplies a pointer to the set of signals to return to
  450. their default dispositions in the child.
  451. Return Value:
  452. 0 on success (always).
  453. Returns an error number on failure.
  454. --*/
  455. LIBC_API
  456. int
  457. posix_spawnattr_setsigmask (
  458. posix_spawnattr_t *Attributes,
  459. const sigset_t *Mask
  460. );
  461. /*++
  462. Routine Description:
  463. This routine sets the current signal mask on a set of spawn attributes.
  464. Arguments:
  465. Attributes - Supplies a pointer to the initialized attributes.
  466. Mask - Supplies a pointer to the signal mask to set in the child.
  467. Return Value:
  468. 0 on success (always).
  469. Returns an error number on failure.
  470. --*/
  471. #ifdef __cplusplus
  472. }
  473. #endif
  474. #endif