[lxc-devel] [PATCH 1/3] lxc-attach: Default to /bin/sh if shell cannot be determined or exec'd

Christian Seiler christian at iwakd.de
Mon Mar 4 19:20:22 UTC 2013


If the NSS implementation of the host and the container is
incompatible, getpwuid() will fail and the shell of the user in the
container cannot be determined. In that case, don't simply fail, but
rather default to /bin/sh. Since this code path is only executed when
attaching to a container without a command argument, this makes the
default behavior of lxc-attach a lot more robust.

Signed-off-by: Christian Seiler <christian at iwakd.de>
---
 src/lxc/lxc_attach.c |   22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c
index 1f60266..292b5b5 100644
--- a/src/lxc/lxc_attach.c
+++ b/src/lxc/lxc_attach.c
@@ -438,15 +438,26 @@ int main(int argc, char *argv[])
 		uid = getuid();
 
 		passwd = getpwuid(uid);
-		if (!passwd) {
-			SYSERROR("failed to get passwd "		\
-				 "entry for uid '%d'", uid);
-			return -1;
+
+		if (passwd) {
+			char *const args[] = {
+				passwd->pw_shell,
+				NULL,
+			};
+
+			execvp(args[0], args);
 		}
 
+		/* executed if either no passwd entry or execvp fails,
+		 * we will fall back on /bin/sh as a default shell
+		 *
+		 * this will make lxc-attach work better out of the box,
+		 * esp. when attaching to a container that has an
+		 * incompatible nss implementation
+		 */
 		{
 			char *const args[] = {
-				passwd->pw_shell,
+				"/bin/sh",
 				NULL,
 			};
 
@@ -454,7 +465,6 @@ int main(int argc, char *argv[])
 			SYSERROR("failed to exec '%s'", args[0]);
 			return -1;
 		}
-
 	}
 
 	return 0;
-- 
1.7.10.4





More information about the lxc-devel mailing list