Skip to content

Commit

Permalink
replace getent with grep #477
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauscher authored and Lauscher committed Jan 15, 2023
1 parent 484555a commit a5f6cea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Project website: https://github.com/mviereck/x11docker
[(464)](https://github.com/mviereck/x11docker/issues/464)
- `--xonly`: run socat unprivileged to make the unix socket accessible.
[(466)](https://github.com/mviereck/x11docker/issues/466)
- `--share`: Softlink fix for folders in `HOME`.
- `--share`: Softlink fix for folders in `HOME`.
[(475)](https://github.com/mviereck/x11docker/issues/475)
- `jq` support to lessen python dependency.
- `jq` support to lessen python dependency.
[(478)](https://github.com/mviereck/x11docker/issues/478)
- Replace `getent` with custom `grep` command.
[(477)](https://github.com/mviereck/x11docker/issues/477)
### Deprecated
- `--backend=proot`: Deprecated because likely no one uses it,
but would need further checks and maintenance.
Expand Down
37 changes: 20 additions & 17 deletions x11docker
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Run 'x11docker --help' or scroll down to read usage information.
# More documentation at: https://github.com/mviereck/x11docker

Version="7.5.0-beta-4"
Version="7.5.0-beta-5"
Packagedversion="no" # Set to "yes" if you want to package x11docker. This disables installation options.

# --enforce-i: Enforce running in interactive mode to allow commands tty and weston-launch in special setups. (deprecated)
Expand Down Expand Up @@ -1015,6 +1015,9 @@ isnum() { # check if $1 is a number
makecookie() { # bake a cookie
mcookie 2>/dev/null || echo $RANDOM$RANDOM$RANDOM$RANDOM$RANDOM$RANDOM | cut -b1-32
}
mygetent() {
grep "^${2:-NOENTRY}:" < "/etc/${1:-NOFILESPECIFIED}"
}
mysleep() { # catch cases where sleep only supports integer
sleep "${1:-1}" 2>/dev/null || sleep 1
}
Expand Down Expand Up @@ -4360,8 +4363,8 @@ create_xcontainercommand() { # option --xc: create docker command for X in co
echo "$Containeruser:x:${Containeruseruid:-$Hostuseruid}:${Containerusergid:-$Hostusergid}:$Containeruser,,,:/tmp:/bin/bash" >> "$Cachefolder/etcpasswd.xcontainer"

mkfile "$Cachefolder/etcgroup.xcontainer"
echo "video:x:$(getent group video | cut -d: -f3):$Containeruser" >> "$Cachefolder/etcgroup.xcontainer"
echo "render:x:$(getent group render | cut -d: -f3):$Containeruser" >> "$Cachefolder/etcgroup.xcontainer"
echo "video:x:$(mygetent group video | cut -d: -f3):$Containeruser" >> "$Cachefolder/etcgroup.xcontainer"
echo "render:x:$(mygetent group render | cut -d: -f3):$Containeruser" >> "$Cachefolder/etcgroup.xcontainer"
echo "weston-launch:x:104:$Containeruser" >> "$Cachefolder/etcgroup.xcontainer"

## code snippets:
Expand Down Expand Up @@ -4457,10 +4460,10 @@ create_xcontainercommand() { # option --xc: create docker command for X in co
}
case "$Xcontainerbackend" in
docker|podman)
getent group video >/dev/null && Xc_gpu="$Xc_gpu \\
--group-add $(getent group video | cut -d: -f3)"
getent group render >/dev/null && Xc_gpu="$Xc_gpu \\
--group-add $(getent group render | cut -d: -f3)"
mygetent group video >/dev/null && Xc_gpu="$Xc_gpu \\
--group-add $(mygetent group video | cut -d: -f3)"
mygetent group render >/dev/null && Xc_gpu="$Xc_gpu \\
--group-add $(mygetent group render | cut -d: -f3)"
;;
esac

Expand Down Expand Up @@ -5960,12 +5963,12 @@ check_containeruser() { # check container user (also option --user)
Containeruser="$(echo "$Containeruser" | cut -d: -f1)"
}
[ "$Containeruser" = "root" ] && Containeruser="0"
[ -n "$(getent passwd "$Containeruser")" ] && { # user exists on host
Containeruser=$(getent passwd "$Containeruser" | cut -d: -f1) # can be name or uid -> now name
Containeruseruid=$(getent passwd "$Containeruser" | cut -d: -f3)
[ -z "$Containerusergid" ] && Containerusergid="$(getent passwd "$Containeruser" | cut -d: -f4)"
[ -n "$(mygetent passwd "$Containeruser")" ] && { # user exists on host
Containeruser=$(mygetent passwd "$Containeruser" | cut -d: -f1) # can be name or uid -> now name
Containeruseruid=$(mygetent passwd "$Containeruser" | cut -d: -f3)
[ -z "$Containerusergid" ] && Containerusergid="$(mygetent passwd "$Containeruser" | cut -d: -f4)"
[ "$Containeruser" = "$Hostuser" ] && Containeruserhosthome="$Hostuserhome"
[ -z "$Containeruserhosthome" ] && Containeruserhosthome="$(getent passwd "$Containeruser" | cut -d: -f6)"
[ -z "$Containeruserhosthome" ] && Containeruserhosthome="$(mygetent passwd "$Containeruser" | cut -d: -f6)"
:
} || { # user does not exist on host
[[ "$Containeruser" =~ ^[0-9]+$ ]] || error "Option --user: Unknown host user or invalid user number '$Containeruser'.
Expand All @@ -5976,7 +5979,7 @@ check_containeruser() { # check container user (also option --user)
Containeruserhosthome=""
}

Containerusergroup="$(getent group "$Containerusergid" | cut -d: -f1 || echo group_"$Containeruser")"
Containerusergroup="$(mygetent group "$Containerusergid" | cut -d: -f1 || echo group_"$Containeruser")"
[ "$Containeruseruid" = "0" ] && {
Containeruser="root"
Containerusergid="0"
Expand Down Expand Up @@ -6162,8 +6165,8 @@ create_backendcommand() { ### create command to run docker|podman|nerdctl
case "$Backend" in
docker|podman)
for Line in $Containerusergroups; do
getent group "${Line:-nonsense}" >/dev/null && Backendcommand="$Backendcommand \\
--group-add $(getent group "$Line" | cut -d: -f3)"
mygetent group "${Line:-nonsense}" >/dev/null && Backendcommand="$Backendcommand \\
--group-add $(mygetent group "$Line" | cut -d: -f3)"
done
;;
esac
Expand Down Expand Up @@ -8794,7 +8797,7 @@ check_hostuser() { # check for unprivileged host user
[ "$Startuser" = "root" ] || error "Option --hostuser: x11docker must run as root
to choose a host user different from user '$Startuser'."
}
getent passwd "$Hostuser" >/dev/null 2>&1 || {
mygetent passwd "$Hostuser" >/dev/null 2>&1 || {
[ -f /etc/passwd ] || warning "Your system misses /etc/passwd"
error "Could not find user '$Hostuser' in /etc/passwd."
}
Expand All @@ -8810,7 +8813,7 @@ check_hostuser() { # check for unprivileged host user
*) Unpriv="su $Hostuser -c" ;;
esac

[ -z "$Hostuserhome" ] && Hostuserhome=$(getent passwd "$Hostuser" 2>/dev/null | cut -d: -f6)
[ -z "$Hostuserhome" ] && Hostuserhome=$(mygetent passwd "$Hostuser" 2>/dev/null | cut -d: -f6)
[ -z "$Hostuserhome" ] && {
Hostuserhome="/tmp/home/$Hostuser"
mkfolder "$Hostuserhome"
Expand Down

0 comments on commit a5f6cea

Please sign in to comment.