hush: HUSH_READONLY depends on HUSH
[oweals/busybox.git] / libbb / run_shell.c
index 6d084eead80ce5243ed6c801cc5c3b8350601818..b6b9360e8785759a28f9eb7307b575833fd88561 100644 (file)
  * SUCH DAMAGE.
  */
 
-#include <stdio.h>
-#include <errno.h>
-#include <unistd.h>
-#include <string.h>
-#include <stdlib.h>
-#include <syslog.h>
-#include <ctype.h>
 #include "libbb.h"
-#ifdef CONFIG_SELINUX
+#if ENABLE_SELINUX
 #include <selinux/selinux.h>  /* for setexeccon  */
 #endif
 
-#ifdef CONFIG_SELINUX
-static security_context_t current_sid=NULL;
+#if ENABLE_SELINUX
+static security_context_t current_sid;
 
-void
-renew_current_security_context(void)
+void FAST_FUNC renew_current_security_context(void)
 {
-  if  (current_sid)  
-    freecon(current_sid);  /* Release old context  */
-
-  getcon(&current_sid);  /* update */
-
-  return;
+       freecon(current_sid);  /* Release old context  */
+       getcon(&current_sid);  /* update */
 }
-void
-set_current_security_context(security_context_t sid)
+void FAST_FUNC set_current_security_context(security_context_t sid)
 {
-  if  (current_sid)  
-    freecon(current_sid);  /* Release old context  */
-
-  current_sid=sid;
-
-  return;
+       freecon(current_sid);  /* Release old context  */
+       current_sid = sid;
 }
 
 #endif
 
-/* Run SHELL, or DEFAULT_SHELL if SHELL is empty.
-   If COMMAND is nonzero, pass it to the shell with the -c option.
-   If ADDITIONAL_ARGS is nonzero, pass it to the shell as more
-   arguments.  */
-
-void run_shell ( const char *shell, int loginshell, const char *command, const char **additional_args)
+/* Run SHELL, or DEFAULT_SHELL if SHELL is "" or NULL.
+ * If ADDITIONAL_ARGS is not NULL, pass them to the shell.
+ */
+void FAST_FUNC run_shell(const char *shell, int loginshell, const char **additional_args)
 {
        const char **args;
-       int argno = 1;
-       int additional_args_cnt = 0;
-
-       for ( args = additional_args; args && *args; args++ )
-               additional_args_cnt++;
 
-               args = (const char **) xmalloc (sizeof (char *) * ( 4  + additional_args_cnt ));
+       args = additional_args;
+       while (args && *args)
+               args++;
 
-       args [0] = bb_get_last_path_component ( bb_xstrdup ( shell ));
+       args = xmalloc(sizeof(char*) * (2 + (args - additional_args)));
 
-       if ( loginshell ) 
-               args [0] = bb_xasprintf ("-%s", args [0]);
+       if (!shell || !shell[0])
+               shell = DEFAULT_SHELL;
 
-       if ( command ) {
-               args [argno++] = "-c";
-               args [argno++] = command;
+       args[0] = bb_get_last_path_component_nostrip(shell);
+       if (loginshell)
+               args[0] = xasprintf("-%s", args[0]);
+       args[1] = NULL;
+       if (additional_args) {
+               int cnt = 1;
+               for (;;)
+                       if ((args[cnt++] = *additional_args++) == NULL)
+                               break;
        }
-       if ( additional_args ) {
-               for ( ; *additional_args; ++additional_args )
-                       args [argno++] = *additional_args;
-       }
-       args [argno] = 0;
-#ifdef CONFIG_SELINUX
-       if ( (current_sid) && (!setexeccon(current_sid)) ) {
-           freecon(current_sid);
-           execve(shell, (char **) args, environ);
-       } else
+
+#if ENABLE_SELINUX
+       if (current_sid)
+               setexeccon(current_sid);
+       if (ENABLE_FEATURE_CLEAN_UP)
+               freecon(current_sid);
 #endif
-         execv ( shell, (char **) args );
-       bb_perror_msg_and_die ( "cannot run %s", shell );
+       execv(shell, (char **) args);
+       bb_perror_msg_and_die("can't execute '%s'", shell);
 }