Update contrib.
1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
20 #include <sys/syslimits.h>
29 // -----------------------------------------------------------------------------
30 // posix_spawn_file_actions_init
31 // Initialize the file actions structure
32 // -----------------------------------------------------------------------------
33 EXPORT_C int posix_spawn_file_actions_init(posix_spawn_file_actions_t* file_actions)
35 file_actions->_fa = new file_actions_t;
36 if (!file_actions->_fa)
43 // -----------------------------------------------------------------------------
44 // posix_spawn_file_actions_addopen
45 // Add an open action to the file actions structure
46 // -----------------------------------------------------------------------------
47 EXPORT_C int posix_spawn_file_actions_addopen(
48 posix_spawn_file_actions_t* file_actions,
54 if (fid < 0 || fid > OPEN_MAX)
59 TFileAction* fa = new TFileAction;
65 memset(fa, 0, sizeof(TFileAction));
71 int len = strlen(path)+1;
72 wchar_t* wpath = new wchar_t[len];
79 if (mbstowcs(wpath, path, len) == (size_t)-1)
86 fa->iPath = new TFileName;
87 fa->iPath->Copy((TText16*)wpath, len);
90 (file_actions->_fa->iActions).AddLast(*fa);
91 if (file_actions->_fa->iIter == NULL)
93 (file_actions->_fa->iIter).SetToFirst();
99 // -----------------------------------------------------------------------------
100 // posix_spawn_file_actions_adddup2
101 // Add a dup2 action to the file actions structure
102 // -----------------------------------------------------------------------------
103 EXPORT_C int posix_spawn_file_actions_adddup2(
104 posix_spawn_file_actions_t* file_actions,
108 if (fid1 < 0 || fid2 < 0 || fid1 > OPEN_MAX || fid2 > OPEN_MAX)
113 TFileAction* fa = new TFileAction;
119 memset(fa, 0, sizeof(TFileAction));
124 (file_actions->_fa->iActions).AddLast(*fa);
125 if (file_actions->_fa->iIter == NULL)
127 (file_actions->_fa->iIter).SetToFirst();
133 // -----------------------------------------------------------------------------
134 // posix_spawn_file_actions_addclose
135 // Add a close action to the file actions structure
136 // -----------------------------------------------------------------------------
137 EXPORT_C int posix_spawn_file_actions_addclose(
138 posix_spawn_file_actions_t* file_actions,
141 if (fid < 0 || fid > OPEN_MAX)
146 TFileAction* fa = new TFileAction;
152 memset(fa, 0, sizeof(TFileAction));
155 (file_actions->_fa->iActions).AddLast(*fa);
156 if (file_actions->_fa->iIter == NULL)
158 (file_actions->_fa->iIter).SetToFirst();
164 // -----------------------------------------------------------------------------
165 // posix_spawn_file_actions_destroy
166 // Empty and destroy the file actions structure
167 // -----------------------------------------------------------------------------
168 EXPORT_C int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* file_actions)
170 if (!file_actions || !file_actions->_fa)
175 if (file_actions->_fa->iActions.IsEmpty())
177 delete file_actions->_fa;
181 TFileAction* fa = (file_actions->_fa->iIter)++;
190 fa = (file_actions->_fa->iIter)++;
193 file_actions->_fa->iActions.Reset();
194 delete file_actions->_fa;
198 // -----------------------------------------------------------------------------
199 // posix_spawnattr_init
200 // Initialize the spawn attributes structure
201 // -----------------------------------------------------------------------------
202 EXPORT_C int posix_spawnattr_init(posix_spawnattr_t* attrp)
204 // we don't support these flags. simply set all attribs to 0.
209 memset(attrp, 0, sizeof(posix_spawnattr_t));
213 // -----------------------------------------------------------------------------
214 // posix_spawnattr_getsigdefault
215 // Returns the sigdefault attribute
216 // -----------------------------------------------------------------------------
217 EXPORT_C int posix_spawnattr_getsigdefault(
218 const posix_spawnattr_t* attrp,
219 sigset_t* sigdefault)
221 if (!attrp || !sigdefault)
226 *sigdefault = attrp->_sd;
230 // -----------------------------------------------------------------------------
231 // posix_spawnattr_getflags
232 // Return the flags attribute
233 // -----------------------------------------------------------------------------
234 EXPORT_C int posix_spawnattr_getflags(
235 const posix_spawnattr_t* attrp,
238 if (!attrp || !flags)
243 *flags = attrp->_flags;
247 // -----------------------------------------------------------------------------
248 // posix_spawnattr_getpgroup
249 // Return the process group attribute
250 // -----------------------------------------------------------------------------
251 EXPORT_C int posix_spawnattr_getpgroup(
252 const posix_spawnattr_t* attrp,
255 if (!attrp || !pgroup)
260 *pgroup = attrp->_pgrp;
264 // -----------------------------------------------------------------------------
265 // posix_spawnattr_getschedparam
266 // Return scheduling parameters attribute
267 // -----------------------------------------------------------------------------
268 EXPORT_C int posix_spawnattr_getschedparam(
269 const posix_spawnattr_t* attrp,
270 struct sched_param* schedparam)
272 if (!attrp || !schedparam)
277 *schedparam = attrp->_sp;
281 // -----------------------------------------------------------------------------
282 // posix_spawnattr_getschedpolicy
283 // Return the scheduling policy attribute
284 // -----------------------------------------------------------------------------
285 EXPORT_C int posix_spawnattr_getschedpolicy(
286 const posix_spawnattr_t* attrp,
289 if (!attrp || !policy)
294 *policy = attrp->_policy;
298 // -----------------------------------------------------------------------------
299 // posix_spawnattr_getsigmask
300 // Return the signal mask attribute
301 // -----------------------------------------------------------------------------
302 EXPORT_C int posix_spawnattr_getsigmask(
303 const posix_spawnattr_t* attrp,
306 if (!attrp || !sigmask)
311 *sigmask = attrp->_sm;
315 // -----------------------------------------------------------------------------
316 // posix_spawnattr_setsigdefault
317 // Sets the sigdefault attribute
318 // -----------------------------------------------------------------------------
319 EXPORT_C int posix_spawnattr_setsigdefault(
320 posix_spawnattr_t* attrp,
321 const sigset_t* sigdefault)
323 if (!attrp || !sigdefault)
328 attrp->_sd = *sigdefault;
332 // -----------------------------------------------------------------------------
333 // posix_spawnattr_setflags
334 // Sets the flags attribute
335 // -----------------------------------------------------------------------------
336 EXPORT_C int posix_spawnattr_setflags(posix_spawnattr_t* attrp, short flags)
343 attrp->_flags = flags;
347 // -----------------------------------------------------------------------------
348 // posix_spawnattr_setpgroup
349 // Sets the process group attribute
350 // -----------------------------------------------------------------------------
351 EXPORT_C int posix_spawnattr_setpgroup(posix_spawnattr_t* attrp, pid_t pgroup)
358 attrp->_pgrp = pgroup;
362 // -----------------------------------------------------------------------------
363 // posix_spawnattr_setschedparam
364 // Sets the scheduling parameters attribute
365 // -----------------------------------------------------------------------------
366 EXPORT_C int posix_spawnattr_setschedparam(
367 posix_spawnattr_t* attrp,
368 const struct sched_param* schedparam)
370 if (!attrp || !schedparam)
375 attrp->_sp = *schedparam;
379 // -----------------------------------------------------------------------------
380 // posix_spawnattr_setschedpolicy
381 // Sets the scheduling policy attribute
382 // -----------------------------------------------------------------------------
383 EXPORT_C int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attrp, int policy)
390 attrp->_policy = policy;
394 // -----------------------------------------------------------------------------
395 // posix_spawnattr_setsigdefault
396 // Sets the sigmask attribute
397 // -----------------------------------------------------------------------------
398 EXPORT_C int posix_spawnattr_setsigmask(
399 posix_spawnattr_t* attrp,
400 const sigset_t* sigmask)
402 if (!attrp || !sigmask)
407 attrp->_sm = *sigmask;
411 // -----------------------------------------------------------------------------
412 // posix_spawnattr_setsigdefault
413 // Empty and cleanup the spawn attributes structure
414 // -----------------------------------------------------------------------------
415 EXPORT_C int posix_spawnattr_destroy(posix_spawnattr_t* /*attrp*/)
421 // -----------------------------------------------------------------------------
423 // Launch a child process specified by path and obtain its pid
424 // This API allows the caller to specify command line arguments and envp for the child
425 // In addition, one can also specify a set of file operations that will be performed
426 // and a set of attributes that will be applied in the child before it enters its main.
427 // -----------------------------------------------------------------------------
428 EXPORT_C int posix_spawn(
431 const posix_spawn_file_actions_t* file_actions,
432 const posix_spawnattr_t* attrp,
436 if(path == NULL || *path == '\0')
441 int len = strlen(path) + 1;
442 wchar_t* wpath = new wchar_t[len];
443 if (mbstowcs(wpath, path, len) == (size_t)-1)
451 wchar_t* wargs = NULL;
452 wchar_t** wenvp = NULL;
457 // argv[0] is (or atleast should be) the exe name
458 for (int i = 1; argv[i]; ++i)
460 totlen += strlen(argv[i]) + 1;
463 wargs = new wchar_t[totlen+1];
472 // argv[0] is (or atleast should be) the exe name
473 for (int i = 1; argv[i]; ++i)
475 int len = strlen(argv[i]);
476 if (mbstowcs(wp, argv[i], len) == (size_t)-1)
485 // replace the last blank with a null character
492 for (; envp[count]; ++count) {}
498 wenvp = new wchar_t*[count+1];
505 for (int i = 0; i < count; ++i)
507 int len = strlen(envp[i]) + 1;
508 wenvp[i] = new wchar_t[len];
509 if (wenvp[i] == NULL)
515 if (mbstowcs(wenvp[i], envp[i], len) == (size_t)-1)
526 //coverity[leave_without_push]
528 ret = _posix_spawn_r(pid, wpath, file_actions, attrp, wargs, wenvp);
539 for (int i = 0; wenvp[i]; ++i)