Patchwork [2/2] useradd.bbclass: retry useradd/groupadd commands to avoid lock race issues

login
register
mail settings
Submitter Scott Garman
Date March 23, 2012, 4:43 a.m.
Message ID <1e1a2ceb4c6afa22a71d3c902b7a3b18cdee10cf.1332453478.git.scott.a.garman@intel.com>
Download mbox | patch
Permalink /patch/24241/
State Accepted
Commit 68c589f1b5ee36f0aff151b728447ffdae14622c
Headers show

Comments

Scott Garman - March 23, 2012, 4:43 a.m.
A race condition can occur when adding users and groups to the
passwd and group files, causing errors like the following:

 ERROR: Function 'useradd_sysroot' failed
 Tried to access "/etc/group" but this was locked.

This fix will cause the useradd code to retry the useradd and
groupadd commands up to 10 times (with a 1s sleep in between
attempts) before failing.

This fixes [YOCTO #1794]

Signed-off-by: Scott Garman <scott.a.garman@intel.com>
---
 meta/classes/useradd.bbclass |   36 ++++++++++++++++++++++++++++++++++--
 1 files changed, 34 insertions(+), 2 deletions(-)
McClintock Matthew-B29882 - March 23, 2012, 9:12 p.m.
This should go in our edison 1.1.2 release as well.

-M

On Thu, Mar 22, 2012 at 11:43 PM, Scott Garman <scott.a.garman@intel.com> wrote:
> A race condition can occur when adding users and groups to the
> passwd and group files, causing errors like the following:
>
>  ERROR: Function 'useradd_sysroot' failed
>  Tried to access "/etc/group" but this was locked.
>
> This fix will cause the useradd code to retry the useradd and
> groupadd commands up to 10 times (with a 1s sleep in between
> attempts) before failing.
>
> This fixes [YOCTO #1794]
>
> Signed-off-by: Scott Garman <scott.a.garman@intel.com>
> ---
>  meta/classes/useradd.bbclass |   36 ++++++++++++++++++++++++++++++++++--
>  1 files changed, 34 insertions(+), 2 deletions(-)
>
> diff --git a/meta/classes/useradd.bbclass b/meta/classes/useradd.bbclass
> index 7981a68..0bbb371 100644
> --- a/meta/classes/useradd.bbclass
> +++ b/meta/classes/useradd.bbclass
> @@ -45,7 +45,23 @@ if test "x$GROUPADD_PARAM" != "x"; then
>                groupname=`echo "$opts" | awk '{ print $NF }'`
>                group_exists=`grep "^$groupname:" $SYSROOT/etc/group || true`
>                if test "x$group_exists" = "x"; then
> -                       eval $PSEUDO groupadd  $OPT $opts
> +                       count=1
> +                       while true; do
> +                               eval $PSEUDO groupadd $OPT $opts || true
> +                               group_exists=`grep "^$groupname:" $SYSROOT/etc/group || true`
> +                               if test "x$group_exists" = "x"; then
> +                                       # File locking issues can require us to retry the command
> +                                       echo "WARNING: groupadd command did not succeed. Retrying..."
> +                                       sleep 1
> +                               else
> +                                       break
> +                               fi
> +                               count=`expr $count + 1`
> +                               if test $count = 11; then
> +                                       echo "ERROR: tried running groupadd command 10 times without success, giving up"
> +                                       exit 1
> +                               fi
> +                       done
>                else
>                        echo "Note: group $groupname already exists, not re-creating it"
>                fi
> @@ -70,7 +86,23 @@ if test "x$USERADD_PARAM" != "x"; then
>                username=`echo "$opts" | awk '{ print $NF }'`
>                user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
>                if test "x$user_exists" = "x"; then
> -                       eval $PSEUDO useradd $OPT $opts
> +                       count=1
> +                       while true; do
> +                               eval $PSEUDO useradd $OPT $opts || true
> +                               user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
> +                               if test "x$user_exists" = "x"; then
> +                                       # File locking issues can require us to retry the command
> +                                       echo "WARNING: useradd command did not succeed. Retrying..."
> +                                       sleep 1
> +                               else
> +                                       break
> +                               fi
> +                               count=`expr $count + 1`
> +                               if test $count = 11; then
> +                                       echo "ERROR: tried running useradd command 10 times without success, giving up"
> +                                       exit 1
> +                               fi
> +                       done
>                else
>                        echo "Note: username $username already exists, not re-creating it"
>                fi
> --
> 1.7.5.4
>
>
> _______________________________________________
> Openembedded-core mailing list
> Openembedded-core@lists.openembedded.org
> http://lists.linuxtogo.org/cgi-bin/mailman/listinfo/openembedded-core

Patch

diff --git a/meta/classes/useradd.bbclass b/meta/classes/useradd.bbclass
index 7981a68..0bbb371 100644
--- a/meta/classes/useradd.bbclass
+++ b/meta/classes/useradd.bbclass
@@ -45,7 +45,23 @@  if test "x$GROUPADD_PARAM" != "x"; then
 		groupname=`echo "$opts" | awk '{ print $NF }'`
 		group_exists=`grep "^$groupname:" $SYSROOT/etc/group || true`
 		if test "x$group_exists" = "x"; then
-			eval $PSEUDO groupadd  $OPT $opts
+			count=1
+			while true; do
+				eval $PSEUDO groupadd $OPT $opts || true
+				group_exists=`grep "^$groupname:" $SYSROOT/etc/group || true`
+				if test "x$group_exists" = "x"; then
+					# File locking issues can require us to retry the command
+					echo "WARNING: groupadd command did not succeed. Retrying..."
+					sleep 1
+				else
+					break
+				fi
+				count=`expr $count + 1`
+				if test $count = 11; then
+					echo "ERROR: tried running groupadd command 10 times without success, giving up"
+					exit 1
+				fi
+			done		
 		else
 			echo "Note: group $groupname already exists, not re-creating it"
 		fi
@@ -70,7 +86,23 @@  if test "x$USERADD_PARAM" != "x"; then
 		username=`echo "$opts" | awk '{ print $NF }'`
 		user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
 		if test "x$user_exists" = "x"; then
-			eval $PSEUDO useradd $OPT $opts
+			count=1
+			while true; do
+				eval $PSEUDO useradd $OPT $opts || true
+				user_exists=`grep "^$username:" $SYSROOT/etc/passwd || true`
+				if test "x$user_exists" = "x"; then
+					# File locking issues can require us to retry the command
+					echo "WARNING: useradd command did not succeed. Retrying..."
+					sleep 1
+				else
+					break
+				fi
+				count=`expr $count + 1`
+				if test $count = 11; then
+					echo "ERROR: tried running useradd command 10 times without success, giving up"
+					exit 1
+				fi
+			done
 		else
 			echo "Note: username $username already exists, not re-creating it"
 		fi