[PATCH] shell: Add rtems_shell_dup_current_env to get a default env.

Chris Johns chrisj at rtems.org
Thu May 29 05:10:44 UTC 2014


Remove rtems_current_shell_env as this is dangerous because
the env can be NULL if used outside of a valid shell with the
POSIX key to an env set up.

Clean up the usage of rtems_current_shell_env.
---
 cpukit/libmisc/shell/login_check.c |  4 +++-
 cpukit/libmisc/shell/main_logoff.c |  5 +++-
 cpukit/libmisc/shell/shell.c       | 49 +++++++++++++++++++++++++-------------
 cpukit/libmisc/shell/shell.h       |  2 +-
 4 files changed, 40 insertions(+), 20 deletions(-)

diff --git a/cpukit/libmisc/shell/login_check.c b/cpukit/libmisc/shell/login_check.c
index 4efed77..8be5be2 100644
--- a/cpukit/libmisc/shell/login_check.c
+++ b/cpukit/libmisc/shell/login_check.c
@@ -43,11 +43,13 @@ bool rtems_shell_login_check(
 
   /* Valid user? */
   if (pw != NULL && strcmp( pw->pw_passwd, "!") != 0) {
+    rtems_shell_env_t *env = rtems_shell_get_current_env();
     setuid( pw->pw_uid);
     setgid( pw->pw_gid);
     rtems_current_user_env->euid = 0;
     rtems_current_user_env->egid = 0;
-    chown( rtems_current_shell_env->devname, pw->pw_uid, 0);
+    if (env)
+      chown( env->devname, pw->pw_uid, 0);
     rtems_current_user_env->euid = pw->pw_uid;
     rtems_current_user_env->egid = pw->pw_gid;
     if (strcmp( pw->pw_passwd, "*") == 0) {
diff --git a/cpukit/libmisc/shell/main_logoff.c b/cpukit/libmisc/shell/main_logoff.c
index 0535d09..e902744 100644
--- a/cpukit/libmisc/shell/main_logoff.c
+++ b/cpukit/libmisc/shell/main_logoff.c
@@ -25,9 +25,12 @@ static int rtems_shell_main_logoff(
   char *argv[] __attribute__((unused))
 )
 {
+  rtems_shell_env_t *env = rtems_shell_get_current_env();
+
   printf("logoff from the system...");
 
-  rtems_current_shell_env->exit_shell = true;
+  if (env)
+    env->exit_shell = true;
 
   return 0;
 }
diff --git a/cpukit/libmisc/shell/shell.c b/cpukit/libmisc/shell/shell.c
index 0f31f2a..a94724f 100644
--- a/cpukit/libmisc/shell/shell.c
+++ b/cpukit/libmisc/shell/shell.c
@@ -114,9 +114,25 @@ static void rtems_shell_current_env_make_key(void)
  */
 rtems_shell_env_t *rtems_shell_get_current_env(void)
 {
-  void *ptr = pthread_getspecific(rtems_shell_current_env_key);
-  assert (ptr != NULL);
-  return (rtems_shell_env_t *) ptr;
+  return (rtems_shell_env_t *) pthread_getspecific(rtems_shell_current_env_key);
+}
+
+/*
+ *  Duplication the current shell environment and if none is set
+ *  clear it.
+ */
+void rtems_shell_dup_current_env(rtems_shell_env_t *copy)
+{
+  rtems_shell_env_t *env = rtems_shell_get_current_env();
+  if (env) {
+    *copy = *env;
+  }
+  else {
+    memset(copy, 0, sizeof(rtems_shell_env_t));
+    copy->magic    = rtems_build_name('S', 'E', 'N', 'V');
+    copy->devname  = CONSOLE_DEVICE_NAME;
+    copy->taskname = "RTSH";
+  }
 }
 
 /*
@@ -511,9 +527,13 @@ static void rtems_shell_init_issue(void)
 }
 
 static bool rtems_shell_login(FILE * in,FILE * out) {
-  FILE          *fd;
-  int            c;
-  time_t         t;
+  rtems_shell_env_t *env;
+  FILE              *fd;
+  int               c;
+  time_t            t;
+
+  env = rtems_shell_get_current_env();
+  assert(env != NULL);
 
   rtems_shell_init_issue();
   setuid(0);
@@ -522,16 +542,16 @@ static bool rtems_shell_login(FILE * in,FILE * out) {
   rtems_current_user_env->egid =0;
 
   if (out) {
-    if ((rtems_current_shell_env->devname[5]!='p')||
-        (rtems_current_shell_env->devname[6]!='t')||
-        (rtems_current_shell_env->devname[7]!='y')) {
+    if ((env->devname[5]!='p')||
+        (env->devname[6]!='t')||
+        (env->devname[7]!='y')) {
       fd = fopen("/etc/issue","r");
       if (fd) {
         while ((c=fgetc(fd))!=EOF) {
           if (c=='@')  {
             switch(c=fgetc(fd)) {
               case 'L':
-                fprintf(out,"%s",rtems_current_shell_env->devname);
+                fprintf(out,"%s", env->devname);
                 break;
               case 'B':
                 fprintf(out,"0");
@@ -578,7 +598,7 @@ static bool rtems_shell_login(FILE * in,FILE * out) {
           if (c=='%')  {
             switch(c=fgetc(fd)) {
               case 't':
-                fprintf(out,"%s",rtems_current_shell_env->devname);
+                fprintf(out,"%s", env->devname);
                 break;
               case 'h':
                 fprintf(out,"0");
@@ -617,12 +637,7 @@ static bool rtems_shell_login(FILE * in,FILE * out) {
     }
   }
 
-  return rtems_shell_login_prompt(
-    in,
-    out,
-    rtems_current_shell_env->devname,
-    rtems_current_shell_env->login_check
-  );
+  return rtems_shell_login_prompt(in, out, env->devname, env->login_check);
 }
 
 #if defined(SHELL_DEBUG)
diff --git a/cpukit/libmisc/shell/shell.h b/cpukit/libmisc/shell/shell.h
index 76b34c7..726a8ee 100644
--- a/cpukit/libmisc/shell/shell.h
+++ b/cpukit/libmisc/shell/shell.h
@@ -208,7 +208,7 @@ bool rtems_shell_main_loop(
 extern const rtems_shell_env_t rtems_global_shell_env;
 
 rtems_shell_env_t *rtems_shell_get_current_env(void);
-#define rtems_current_shell_env rtems_shell_get_current_env()
+void rtems_shell_dup_current_env(rtems_shell_env_t *);
 
 /*
  * The types of file systems we can mount. We have them broken out
-- 
1.9.0




More information about the devel mailing list