util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ludwig Nussel <ludwig.nussel@suse.de>
To: util-linux@vger.kernel.org
Cc: Ludwig Nussel <ludwig.nussel@suse.de>
Subject: [PATCH 4/7] introduce xsetenv globally
Date: Fri,  1 Jun 2012 14:51:18 +0200	[thread overview]
Message-ID: <1338555081-2061-4-git-send-email-ludwig.nussel@suse.de> (raw)
In-Reply-To: <1338555081-2061-1-git-send-email-ludwig.nussel@suse.de>


Signed-off-by: Ludwig Nussel <ludwig.nussel@suse.de>
---
 include/env.h    |    7 +++++++
 login-utils/su.c |   36 +++++++++++-------------------------
 2 files changed, 18 insertions(+), 25 deletions(-)

diff --git a/include/env.h b/include/env.h
index bcd0f7e..9c6a3fd 100644
--- a/include/env.h
+++ b/include/env.h
@@ -4,5 +4,12 @@
 extern void sanitize_env (void);
 extern char *safe_getenv(const char *arg);
 
+static inline void
+xsetenv (char const *name, char const *val, int overwrite)
+{
+  if (setenv (name, val, overwrite) != 0)
+    err (EXIT_FAILURE,  _("failed to set the %s environment variable"), name);
+}
+
 #endif /* UTIL_LINUX_ENV_H */
 
diff --git a/login-utils/su.c b/login-utils/su.c
index 22b9ccc..cdaf31e 100644
--- a/login-utils/su.c
+++ b/login-utils/su.c
@@ -62,6 +62,7 @@ enum
 #include "xalloc.h"
 #include "nls.h"
 #include "pathnames.h"
+#include "env.h"
 
 /* name of the pam configuration files. separate configs for su and su -  */
 #define PAM_SERVICE_NAME "su"
@@ -111,21 +112,6 @@ static struct option const longopts[] =
   {NULL, 0, NULL, 0}
 };
 
-/* Add NAME=VAL to the environment, checking for out of memory errors.  */
-
-static void
-xsetenv (char const *name, char const *val)
-{
-  size_t namelen = strlen (name);
-  size_t vallen = strlen (val);
-  char *string = xmalloc (namelen + 1 + vallen + 1);
-  strcpy (string, name);
-  string[namelen] = '=';
-  strcpy (string + namelen + 1, val);
-  if (putenv (string) != 0)
-    error (EXIT_FAILURE, 0, _("out of memory"));
-}
-
 /* Log the fact that someone has run su to the user given by PW;
    if SUCCESSFUL is true, they gave the correct password, etc.  */
 
@@ -506,11 +492,11 @@ modify_environment (const struct passwd *pw, const char *shell)
       environ = xmalloc ((6 + !!term) * sizeof (char *));
       environ[0] = NULL;
       if (term)
-	xsetenv ("TERM", term);
-      xsetenv ("HOME", pw->pw_dir);
-      xsetenv ("SHELL", shell);
-      xsetenv ("USER", pw->pw_name);
-      xsetenv ("LOGNAME", pw->pw_name);
+	xsetenv ("TERM", term, 1);
+      xsetenv ("HOME", pw->pw_dir, 1);
+      xsetenv ("SHELL", shell, 1);
+      xsetenv ("USER", pw->pw_name, 1);
+      xsetenv ("LOGNAME", pw->pw_name, 1);
       set_path(pw);
     }
   else
@@ -519,8 +505,8 @@ modify_environment (const struct passwd *pw, const char *shell)
 	 USER and LOGNAME.  */
       if (change_environment)
         {
-          xsetenv ("HOME", pw->pw_dir);
-          xsetenv ("SHELL", shell);
+          xsetenv ("HOME", pw->pw_dir, 1);
+          xsetenv ("SHELL", shell, 1);
 	  if (getlogindefs_bool ("ALWAYS_SET_PATH", 0))
 	    set_path(pw);
 	  else
@@ -535,14 +521,14 @@ modify_environment (const struct passwd *pw, const char *shell)
 
 	      if (new)
 		{
-		  xsetenv ("PATH", new);
+		  xsetenv ("PATH", new, 1);
 		  free (new);
 		}
 	    }
           if (pw->pw_uid)
             {
-              xsetenv ("USER", pw->pw_name);
-              xsetenv ("LOGNAME", pw->pw_name);
+              xsetenv ("USER", pw->pw_name, 1);
+              xsetenv ("LOGNAME", pw->pw_name, 1);
             }
         }
     }
-- 
1.7.7


  parent reply	other threads:[~2012-06-01 12:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-01 12:51 [PATCH 1/7] don't use custom MAX macro Ludwig Nussel
2012-06-01 12:51 ` [PATCH 2/7] use EXIT_FAILURE consistently Ludwig Nussel
2012-06-01 12:51 ` [PATCH 3/7] use ENV_PATH resp ENV_SUPATH to be consistent with login Ludwig Nussel
2012-06-01 12:51 ` Ludwig Nussel [this message]
2012-06-05 13:27   ` [PATCH 4/7] introduce xsetenv globally Karel Zak
2012-06-01 12:51 ` [PATCH 5/7] replace PAM_BAIL_P macro with better solution Ludwig Nussel
2012-06-01 12:51 ` [PATCH 6/7] remove unused code Ludwig Nussel
2012-06-01 12:51 ` [PATCH 7/7] use BSD err function instead of gnu's error() Ludwig Nussel
2012-06-05 13:27 ` [PATCH 1/7] don't use custom MAX macro Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1338555081-2061-4-git-send-email-ludwig.nussel@suse.de \
    --to=ludwig.nussel@suse.de \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).