useradd_base.bbclass: replace retry logic with flock

When perform useradd during populate sysroot, it locks files passwd.lock
and group.lock at same time. And then it meets a dead lock issue
randomly.

Use flock to reslove it by using an universal lock directory for all the
user and group related operations.

[YOCTO #9022]

(From OE-Core rev: 2ebf697b46c42cee8bfa6d2e6087397f8cce385c)

Signed-off-by: Kai Kang <kai.kang@windriver.com>
Signed-off-by: Ross Burton <ross.burton@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Kai Kang 2016-02-29 17:19:32 +08:00 committed by Richard Purdie
parent 5d06f00471
commit 57a525ca72
2 changed files with 46 additions and 125 deletions

View File

@ -57,7 +57,7 @@ if test "x`echo $GROUPADD_PARAM | tr -d '[:space:]'`" != "x"; then
opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1` opts=`echo "$GROUPADD_PARAM" | cut -d ';' -f 1`
remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-` remaining=`echo "$GROUPADD_PARAM" | cut -d ';' -f 2-`
while test "x$opts" != "x"; do while test "x$opts" != "x"; do
perform_groupadd "$SYSROOT" "$OPT $opts" 10 perform_groupadd "$SYSROOT" "$OPT $opts"
if test "x$opts" = "x$remaining"; then if test "x$opts" = "x$remaining"; then
break break
fi fi
@ -73,7 +73,7 @@ if test "x`echo $USERADD_PARAM | tr -d '[:space:]'`" != "x"; then
opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1` opts=`echo "$USERADD_PARAM" | cut -d ';' -f 1`
remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-` remaining=`echo "$USERADD_PARAM" | cut -d ';' -f 2-`
while test "x$opts" != "x"; do while test "x$opts" != "x"; do
perform_useradd "$SYSROOT" "$OPT $opts" 10 perform_useradd "$SYSROOT" "$OPT $opts"
if test "x$opts" = "x$remaining"; then if test "x$opts" = "x$remaining"; then
break break
fi fi
@ -89,7 +89,7 @@ if test "x`echo $GROUPMEMS_PARAM | tr -d '[:space:]'`" != "x"; then
opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1` opts=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 1`
remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-` remaining=`echo "$GROUPMEMS_PARAM" | cut -d ';' -f 2-`
while test "x$opts" != "x"; do while test "x$opts" != "x"; do
perform_groupmems "$SYSROOT" "$OPT $opts" 10 perform_groupmems "$SYSROOT" "$OPT $opts"
if test "x$opts" = "x$remaining"; then if test "x$opts" = "x$remaining"; then
break break
fi fi

View File

@ -4,7 +4,7 @@
# The following functions basically have similar logic. # The following functions basically have similar logic.
# *) Perform necessary checks before invoking the actual command # *) Perform necessary checks before invoking the actual command
# *) Invoke the actual command, make retries if necessary # *) Invoke the actual command with flock
# *) Error out if an error occurs. # *) Error out if an error occurs.
# Note that before invoking these functions, make sure the global variable # Note that before invoking these functions, make sure the global variable
@ -13,26 +13,16 @@
perform_groupadd () { perform_groupadd () {
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing groupadd with [$opts]"
bbnote "${PN}: Performing groupadd with [$opts] and $retries times of retry"
local groupname=`echo "$opts" | awk '{ print $NF }'` local groupname=`echo "$opts" | awk '{ print $NF }'`
local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
if test "x$group_exists" = "x"; then if test "x$group_exists" = "x"; then
local count=0 opts=`echo $opts | sed s/\'/\"/g`
while true; do eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupadd $opts\' || true
eval $PSEUDO groupadd $opts || true group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" if test "x$group_exists" = "x"; then
if test "x$group_exists" = "x"; then bbfatal "${PN}: groupadd command did not succeed."
bbwarn "${PN}: groupadd command did not succeed. Retrying..." fi
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
bbfatal "${PN}: Tried running groupadd command $retries times without success, giving up"
fi
sleep $count
done
else else
bbnote "${PN}: group $groupname already exists, not re-creating it" bbnote "${PN}: group $groupname already exists, not re-creating it"
fi fi
@ -41,26 +31,16 @@ perform_groupadd () {
perform_useradd () { perform_useradd () {
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing useradd with [$opts]"
bbnote "${PN}: Performing useradd with [$opts] and $retries times of retry"
local username=`echo "$opts" | awk '{ print $NF }'` local username=`echo "$opts" | awk '{ print $NF }'`
local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
if test "x$user_exists" = "x"; then if test "x$user_exists" = "x"; then
local count=0 opts=`echo $opts | sed s/\'/\"/g`
while true; do eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO useradd $opts\' || true
eval $PSEUDO useradd $opts || true user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" if test "x$user_exists" = "x"; then
if test "x$user_exists" = "x"; then bbfatal "${PN}: useradd command did not succeed."
bbwarn "${PN}: useradd command did not succeed. Retrying..." fi
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
bbfatal "${PN}: Tried running useradd command $retries times without success, giving up"
fi
sleep $count
done
else else
bbnote "${PN}: user $username already exists, not re-creating it" bbnote "${PN}: user $username already exists, not re-creating it"
fi fi
@ -69,8 +49,7 @@ perform_useradd () {
perform_groupmems () { perform_groupmems () {
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing groupmems with [$opts]"
bbnote "${PN}: Performing groupmems with [$opts] and $retries times of retry"
local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'` local groupname=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-g" || $i == "--group") print $(i+1) }'`
local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'` local username=`echo "$opts" | awk '{ for (i = 1; i < NF; i++) if ($i == "-a" || $i == "--add") print $(i+1) }'`
bbnote "${PN}: Running groupmems command with group $groupname and user $username" bbnote "${PN}: Running groupmems command with group $groupname and user $username"
@ -84,25 +63,11 @@ perform_groupmems () {
fi fi
local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`" local mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
if test "x$mem_exists" = "x"; then if test "x$mem_exists" = "x"; then
local count=0 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupmems $opts\' || true
while true; do mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`"
eval $PSEUDO groupmems $opts || true if test "x$mem_exists" = "x"; then
mem_exists="`grep "^$groupname:[^:]*:[^:]*:\([^,]*,\)*$username\(,[^,]*\)*" $rootdir/etc/group || true`" bbfatal "${PN}: groupmems command did not succeed."
if test "x$mem_exists" = "x"; then fi
bbwarn "${PN}: groupmems command did not succeed. Retrying..."
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
if test "x$gshadow" = "xno"; then
rm -f $rootdir${sysconfdir}/gshadow
rm -f $rootdir${sysconfdir}/gshadow-
fi
bbfatal "${PN}: Tried running groupmems command $retries times without success, giving up"
fi
sleep $count
done
else else
bbnote "${PN}: group $groupname already contains $username, not re-adding it" bbnote "${PN}: group $groupname already contains $username, not re-adding it"
fi fi
@ -115,26 +80,15 @@ perform_groupmems () {
perform_groupdel () { perform_groupdel () {
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing groupdel with [$opts]"
bbnote "${PN}: Performing groupdel with [$opts] and $retries times of retry"
local groupname=`echo "$opts" | awk '{ print $NF }'` local groupname=`echo "$opts" | awk '{ print $NF }'`
local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
if test "x$group_exists" != "x"; then if test "x$group_exists" != "x"; then
local count=0 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupdel $opts\' || true
while true; do group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
eval $PSEUDO groupdel $opts || true if test "x$group_exists" != "x"; then
group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" bbfatal "${PN}: groupdel command did not succeed."
if test "x$group_exists" != "x"; then fi
bbwarn "${PN}: groupdel command did not succeed. Retrying..."
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
bbfatal "${PN}: Tried running groupdel command $retries times without success, giving up"
fi
sleep $count
done
else else
bbnote "${PN}: group $groupname doesn't exist, not removing it" bbnote "${PN}: group $groupname doesn't exist, not removing it"
fi fi
@ -143,26 +97,15 @@ perform_groupdel () {
perform_userdel () { perform_userdel () {
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing userdel with [$opts]"
bbnote "${PN}: Performing userdel with [$opts] and $retries times of retry"
local username=`echo "$opts" | awk '{ print $NF }'` local username=`echo "$opts" | awk '{ print $NF }'`
local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
if test "x$user_exists" != "x"; then if test "x$user_exists" != "x"; then
local count=0 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO userdel $opts\' || true
while true; do user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
eval $PSEUDO userdel $opts || true if test "x$user_exists" != "x"; then
user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" bbfatal "${PN}: userdel command did not succeed."
if test "x$user_exists" != "x"; then fi
bbwarn "${PN}: userdel command did not succeed. Retrying..."
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
bbfatal "${PN}: Tried running userdel command $retries times without success, giving up"
fi
sleep $count
done
else else
bbnote "${PN}: user $username doesn't exist, not removing it" bbnote "${PN}: user $username doesn't exist, not removing it"
fi fi
@ -174,25 +117,14 @@ perform_groupmod () {
set +e set +e
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing groupmod with [$opts]"
bbnote "${PN}: Performing groupmod with [$opts] and $retries times of retry"
local groupname=`echo "$opts" | awk '{ print $NF }'` local groupname=`echo "$opts" | awk '{ print $NF }'`
local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`" local group_exists="`grep "^$groupname:" $rootdir/etc/group || true`"
if test "x$group_exists" != "x"; then if test "x$group_exists" != "x"; then
local count=0 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO groupmod $opts\'
while true; do if test $? != 0; then
eval $PSEUDO groupmod $opts bbwarn "${PN}: groupmod command did not succeed."
if test $? != 0; then fi
bbwarn "${PN}: groupmod command did not succeed. Retrying..."
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
bbfatal "${PN}: Tried running groupmod command $retries times without success, giving up"
fi
sleep $count
done
else else
bbwarn "${PN}: group $groupname doesn't exist, unable to modify it" bbwarn "${PN}: group $groupname doesn't exist, unable to modify it"
fi fi
@ -204,25 +136,14 @@ perform_usermod () {
set +e set +e
local rootdir="$1" local rootdir="$1"
local opts="$2" local opts="$2"
local retries="$3" bbnote "${PN}: Performing usermod with [$opts]"
bbnote "${PN}: Performing usermod with [$opts] and $retries times of retry"
local username=`echo "$opts" | awk '{ print $NF }'` local username=`echo "$opts" | awk '{ print $NF }'`
local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`" local user_exists="`grep "^$username:" $rootdir/etc/passwd || true`"
if test "x$user_exists" != "x"; then if test "x$user_exists" != "x"; then
local count=0 eval flock -x -w 100 $rootdir${sysconfdir} -c \'$PSEUDO usermod $opts\'
while true; do if test $? != 0; then
eval $PSEUDO usermod $opts bbfatal "${PN}: usermod command did not succeed."
if test $? != 0; then fi
bbwarn "${PN}: usermod command did not succeed. Retrying..."
else
break
fi
count=`expr $count + 1`
if test $count = $retries; then
bbfatal "${PN}: Tried running usermod command $retries times without success, giving up"
fi
sleep $count
done
else else
bbwarn "${PN}: user $username doesn't exist, unable to modify it" bbwarn "${PN}: user $username doesn't exist, unable to modify it"
fi fi