2 Copyright © 2008-2012, The AROS Development Team. All rights reserved.
5 Support functions for POSIX exec*() functions.
7 #include <exec/types.h>
11 #include <aros/libcall.h>
23 #include "__arosc_privdata.h"
30 #include <aros/debug.h>
32 static BOOL
containswhite(const char *str
);
33 static char *escape(const char *str
);
34 static char *appendarg(char *argptr
, int *argptrsize
, const char *arg
, APTR pool
);
35 static char *appendargs(char *argptr
, int *argptrsize
, char *const args
[], APTR pool
);
36 static void __exec_cleanup(struct aroscbase
*aroscbase
);
37 static void close_on_exec(struct aroscbase
*aroscbase
);
39 /* Public functions */
40 /********************/
42 APTR
__exec_prepare(const char *filename
, int searchpath
, char *const argv
[], char *const envp
[])
44 struct aroscbase
*aroscbase
= __GM_GetBase();
45 char *filename2
= NULL
;
49 D(bug("Entering __exec_prepare(\"%s\", %d, %x, %x)\n",
50 filename
, searchpath
, argv
, envp
53 if (filename
== NULL
|| filename
[0] == '\0' || argv
== NULL
)
59 /* Use own memory to allocate so that no arosstdc.library functions need to be called
60 exec_pool can also be allocated in __exec_valist2array
62 if (!aroscbase
->acb_exec_pool
)
63 aroscbase
->acb_exec_pool
= CreatePool(MEMF_PUBLIC
, 1024, 512);
64 if (!aroscbase
->acb_exec_pool
)
70 aroscbase
->acb_exec_args
= AllocPooled(aroscbase
->acb_exec_pool
, argssize
);
71 aroscbase
->acb_exec_args
[0] = '\0';
73 /* Search path if asked and no directory separator is present in the file */
74 if (searchpath
&& index(filename
, '/') == NULL
&& index(filename
, ':') == NULL
)
77 char *path
= NULL
, *path_ptr
, *path_item
;
82 for (i
=0; environ
[i
]; i
++)
84 if (strncmp(environ
[i
], "PATH=", 5) == 0)
86 path
= &environ
[i
][5];
93 path
= getenv("PATH");
96 path
= ":/bin:/usr/bin";
98 D(bug("__exec_prepare: PATH('%s')\n", path
));
100 path_ptr
= AllocPooled(aroscbase
->acb_exec_pool
, strlen(path
) + 1);
101 strcpy(path_ptr
, path
);
104 D(bug("__exec_prepare: PATH('%s')\n", path
));
107 filename2
= AllocPooled(aroscbase
->acb_exec_pool
, size
);
114 for(path_ptr
= path
, lock
= (BPTR
)NULL
, path_item
= strsep(&path_ptr
, ",:");
115 lock
== (BPTR
)NULL
&& path_item
!= NULL
;
116 path_item
= strsep(&path_ptr
, ",:")
119 if(path_item
[0] == '\0')
122 len
= strlen(path_item
) + strlen(filename
) + 2;
126 FreePooled(aroscbase
->acb_exec_pool
, filename2
, size
);
128 filename2
= AllocPooled(aroscbase
->acb_exec_pool
, size
);
136 strcpy(filename2
, path_item
);
137 strcat(filename2
, "/");
138 strcat(filename2
, filename
);
139 lock
= Lock(__path_u2a(filename2
), SHARED_LOCK
);
140 D(bug("__exec_prepare: Lock(\"%s\") == %x\n", filename2
, (APTR
)lock
));
143 if(lock
!= (BPTR
)NULL
)
152 filename2
= (char *)filename
;
155 if (aroscbase
->acb_flags
& PRETEND_CHILD
)
157 struct vfork_data
*udata
= aroscbase
->acb_vfork_data
;
159 udata
->exec_filename
= filename2
;
160 udata
->exec_argv
= argv
;
161 udata
->exec_envp
= envp
;
163 /* Set this so the child knows that __exec_prepare was called */
164 udata
->child_executed
= 1;
166 D(bug("__exec_prepare: Calling child\n"));
167 /* Now call child process, so it will call __exec_prepare */
168 Signal(udata
->child
, 1 << udata
->child_signal
);
170 D(bug("__exec_prepare: Waiting for child to finish __exec_prepare\n"));
171 /* __exec_prepare should be finished now on child */
172 Wait(1 << udata
->parent_signal
);
176 D(bug("__exec_prepare: Continue child immediately on error\n"));
177 Signal(udata
->child
, 1 << udata
->child_signal
);
180 D(bug("__exec_prepare: Exiting from forked __exec_prepare id=%x, errno=%d\n",
181 udata
->exec_id
, udata
->child_errno
187 D(bug("__exec_prepare: Not running as PRETEND_CHILD\n"));
188 aroscbase
->acb_exec_args
= AllocPooled(aroscbase
->acb_exec_pool
, argssize
);
189 aroscbase
->acb_exec_args
[0] = '\0';
191 /* Let's check if it's a script */
192 BPTR fh
= Open((CONST_STRPTR
)__path_u2a(filename2
), MODE_OLDFILE
);
195 if(FGetC(fh
) == '#' && FGetC(fh
) == '!')
197 char firstline
[128], *linebuf
, *inter
, *interargs
= NULL
;
199 /* It is a script, let's read the first line */
200 if(FGets(fh
, (STRPTR
)firstline
, sizeof(firstline
) - 1))
202 /* delete end of line if present */
203 if(firstline
[0] && firstline
[strlen(firstline
)-1] == '\n')
204 firstline
[strlen(firstline
)-1] = '\0';
206 while(isblank(*linebuf
)) linebuf
++;
209 /* Interpreter name is here */
211 while(*linebuf
!= '\0' && !isblank(*linebuf
)) linebuf
++;
215 while(isblank(*linebuf
)) linebuf
++;
217 /* Interpreter arguments are here */
221 /* Add interpeter args and the script name to command line args */
222 char *args
[] = {NULL
, NULL
, NULL
};
232 aroscbase
->acb_exec_args
= appendargs(
233 aroscbase
->acb_exec_args
, &argssize
, args
, aroscbase
->acb_exec_pool
235 if (!aroscbase
->acb_exec_args
)
241 /* Set file to execute as the script interpreter */
242 filename2
= AllocPooled(aroscbase
->acb_exec_pool
, strlen(inter
) + 1);
243 strcpy(filename2
, inter
);
251 /* Simply assume it doesn't exist */
252 errno
= __arosc_ioerr2errno(IoErr());
256 /* Add arguments to command line args */
257 aroscbase
->acb_exec_args
= appendargs(aroscbase
->acb_exec_args
, &argssize
, argv
+ 1, aroscbase
->acb_exec_pool
);
258 if (!aroscbase
->acb_exec_args
)
264 /* End command line args with '\n' */
265 if(strlen(aroscbase
->acb_exec_args
) > 0)
266 aroscbase
->acb_exec_args
[strlen(aroscbase
->acb_exec_args
) - 1] = '\n';
268 strcat(aroscbase
->acb_exec_args
, "\n");
270 /* let's make some sanity tests */
272 if(stat(filename2
, &st
) == 0)
274 if(!(st
.st_mode
& S_IXUSR
) && !(st
.st_mode
& S_IXGRP
) && !(st
.st_mode
& S_IXOTH
))
276 /* file is not executable */
280 if(st
.st_mode
& S_IFDIR
)
282 /* it's a directory */
289 /* couldn't stat file */
294 aroscbase
->acb_exec_taskname
= AllocPooled(aroscbase
->acb_exec_pool
, strlen(filename2
) + 1);
295 if (!aroscbase
->acb_exec_taskname
)
300 strcpy(aroscbase
->acb_exec_taskname
, filename2
);
302 /* Load file to execute */
303 aroscbase
->acb_exec_seglist
= LoadSeg((CONST_STRPTR
)__path_u2a(filename2
));
304 if (!aroscbase
->acb_exec_seglist
)
310 me
= (struct Process
*)FindTask(NULL
);
312 if (envp
&& envp
!= environ
)
314 struct MinList tempenv
;
315 struct LocalVar
*lv
, *lv2
;
319 /* Remember previous environment variables so they can be put back
320 * if something goes wrong
323 ForeachNodeSafe(&me
->pr_LocalVars
, lv
, lv2
)
325 Remove((struct Node
*)lv
);
326 AddTail((struct List
*)&tempenv
, (struct Node
*)lv
);
329 NEWLIST(&me
->pr_LocalVars
);
332 for (envit
= envp
; *envit
&& env_ok
; envit
++)
333 env_ok
= putenv(*envit
) == 0;
336 /* Old vars may be freed */
337 ForeachNodeSafe(&tempenv
, lv
, lv2
)
339 Remove((struct Node
*)lv
);
340 FreeMem(lv
->lv_Value
, lv
->lv_Len
);
346 ForeachNodeSafe(&me
->pr_LocalVars
, lv
, lv2
)
348 Remove((struct Node
*)lv
);
349 FreeMem(lv
->lv_Value
, lv
->lv_Len
);
353 /* Put old ones back */
354 ForeachNodeSafe(&tempenv
, lv
, lv2
)
356 Remove((struct Node
*)lv
);
357 AddTail((struct List
*)&me
->pr_LocalVars
, (struct Node
*)lv
);
365 /* Set standard files to the standard files from arosc */
366 fdesc
*in
= __getfdesc(STDIN_FILENO
), *out
= __getfdesc(STDOUT_FILENO
),
367 *err
= __getfdesc(STDERR_FILENO
);
370 aroscbase
->acb_exec_oldin
= SelectInput(in
->fcb
->fh
);
372 aroscbase
->acb_exec_oldout
= SelectOutput(out
->fcb
->fh
);
375 aroscbase
->acb_exec_olderr
= me
->pr_CES
;
376 me
->pr_CES
= err
->fcb
->fh
;
380 return (APTR
)aroscbase
;
383 __exec_cleanup(aroscbase
);
389 void __exec_do(APTR id
)
391 struct aroscbase
*aroscbase
= __GM_GetBase();
393 struct CommandLineInterface
*cli
= Cli();
394 struct Task
*self
= FindTask(NULL
);
397 D(bug("[__exec_do] Entering, id(%x)\n", id
));
401 /* When exec is not called under vfork condition id == __GM_GetBase()
402 When exec is called under vfork condition we need to use __GM_GetBase() in the
403 parent to check for PRETEND_CHILD and find the udata for signaling the child
406 if (aroscbase
->acb_flags
& PRETEND_CHILD
)
408 struct vfork_data
*udata
= aroscbase
->acb_vfork_data
;
410 D(bug("[__exec_do] PRETEND_CHILD\n"));
412 close_on_exec(aroscbase
);
414 D(bug("Notify child to call __exec_do\n"));
416 /* Signal child that __exec_do is called */
417 Signal(udata
->child
, 1 << udata
->child_signal
);
419 /* Clean up in parent */
420 __exec_cleanup(aroscbase
);
422 /* Continue as parent process */
425 assert(0); /* Should not be reached */
429 D(bug("[__exec_do] !PRETEND_CHILD\n"));
431 aroscbase
->acb_flags
|= EXEC_PARENT
;
433 oldtaskname
= self
->tc_Node
.ln_Name
;
434 self
->tc_Node
.ln_Name
= aroscbase
->acb_exec_taskname
;
435 SetProgramName((STRPTR
)aroscbase
->acb_exec_taskname
);
437 returncode
= RunCommand(
438 aroscbase
->acb_exec_seglist
,
439 cli
->cli_DefaultStack
* CLI_DEFAULTSTACK_UNIT
,
440 (STRPTR
)aroscbase
->acb_exec_args
,
441 strlen(aroscbase
->acb_exec_args
)
444 self
->tc_Node
.ln_Name
= oldtaskname
;
445 SetProgramName((STRPTR
)oldtaskname
);
447 __exec_cleanup(aroscbase
);
449 D(bug("[__exec_do] exiting from non-forked\n"));
454 char *const *__exec_valist2array(const char *arg1
, va_list list
)
456 struct aroscbase
*aroscbase
= __GM_GetBase();
458 static char *no_arg
[] = {NULL
};
462 assert(aroscbase
->acb_exec_tmparray
== NULL
);
464 va_copy(list2
, list
);
469 for (argit
= va_arg(list
, char *), argc
= 1;
471 argit
= va_arg(list
, char *)
475 if (!(aroscbase
->acb_exec_tmparray
= malloc((argc
+1)*(sizeof(char *)))))
477 D(bug("__exec_valist2array: Memory allocation failed\n"));
482 aroscbase
->acb_exec_tmparray
[0] = (char *)arg1
;
483 for (argit
= va_arg(list2
, char *), i
= 1;
484 i
<= argc
; /* i == argc will copy the NULL pointer */
485 argit
= va_arg(list2
, char *), i
++
488 D(bug("arg %d: %x\n", i
, argit
));
489 aroscbase
->acb_exec_tmparray
[i
] = argit
;
494 return aroscbase
->acb_exec_tmparray
;
498 void __exec_cleanup_array()
500 struct aroscbase
*aroscbase
= __GM_GetBase();
501 if (aroscbase
->acb_exec_tmparray
)
503 free((void *)aroscbase
->acb_exec_tmparray
);
504 aroscbase
->acb_exec_tmparray
= NULL
;
509 /* Support functions */
510 /*********************/
512 /* Return TRUE if there are any white spaces in the string */
513 static BOOL
containswhite(const char *str
)
516 if(isspace(*str
++)) return TRUE
;
520 /* Escape the string and quote it */
521 static char *escape(const char *str
)
523 const char *strptr
= str
;
524 char *escaped
, *escptr
;
525 /* Additional two characters for '"', and one for '\0' */
526 int bufsize
= strlen(str
) + 3;
527 /* Take into account characters to ecape */
528 while(*strptr
!= '\0')
538 escptr
= escaped
= (char*) malloc(bufsize
);
542 for(strptr
= str
; *strptr
!= '\0'; strptr
++)
550 *escptr
++ = (*strptr
== '\n' ? 'N' : *strptr
);
562 /* Append arg string to argptr increasing argptr if needed */
563 static char *appendarg(char *argptr
, int *argptrsize
, const char *arg
, APTR pool
)
565 while(strlen(argptr
) + strlen(arg
) + 2 > *argptrsize
)
568 int argptrsize2
= 2*(*argptrsize
);
570 argptr2
= AllocPooled(pool
, argptrsize2
);
573 FreePooled(pool
, argptr
, *argptrsize
);
576 strcpy(argptr2
, argptr
);
577 FreePooled(pool
, argptr
, *argptrsize
);
579 *argptrsize
= argptrsize2
;
587 static char *appendargs(char *argptr
, int *argssizeptr
, char *const args
[], APTR pool
)
591 for (argsit
= args
; *argsit
&& argptr
; argsit
++)
593 if(containswhite(*argsit
))
595 char *escaped
= escape(*argsit
);
599 argptr
= appendarg(argptr
, argssizeptr
, escaped
, pool
);
603 argptr
= appendarg(argptr
, argssizeptr
, *argsit
, pool
);
609 static void __exec_cleanup(struct aroscbase
*aroscbase
)
611 D(bug("__exec_cleanup: me(%x)\n", FindTask(NULL
)));
613 if(aroscbase
->acb_exec_oldin
)
615 SelectInput(aroscbase
->acb_exec_oldin
);
616 aroscbase
->acb_exec_oldin
= (BPTR
)NULL
;
618 if(aroscbase
->acb_exec_oldout
)
620 SelectOutput(aroscbase
->acb_exec_oldout
);
621 aroscbase
->acb_exec_oldout
= (BPTR
)NULL
;
623 if(aroscbase
->acb_exec_olderr
)
625 struct Process
*me
= (struct Process
*)FindTask(NULL
);
627 me
->pr_CES
= aroscbase
->acb_exec_olderr
;
628 aroscbase
->acb_exec_olderr
= BNULL
;
631 if (aroscbase
->acb_exec_pool
)
633 DeletePool(aroscbase
->acb_exec_pool
);
634 aroscbase
->acb_exec_pool
= NULL
;
636 if (aroscbase
->acb_exec_seglist
)
638 UnLoadSeg(aroscbase
->acb_exec_seglist
);
639 aroscbase
->acb_exec_seglist
= (BPTR
)NULL
;
643 static void close_on_exec(struct aroscbase
*aroscbase
)
648 for (i
= __getfdslots() - 1; i
>= 0; i
--)
650 if ((fd
= __getfdesc(i
)) != NULL
)
652 D(bug("close_on_exec: checking fd %d\n", i
));
653 if (fd
->fdflags
& FD_CLOEXEC
)
655 D(bug("close_on_exec: closing fd %d\n", i
));
656 assert(close(i
) == 0);