fix(scripts): chcon error on CentOS 7

This commit is contained in:
Haruue 2024-04-26 14:52:34 +08:00
parent 3409904294
commit e7d7dbbf8f
No known key found for this signature in database
GPG key ID: F6083B28CBCBC148

View file

@ -187,6 +187,29 @@ chcon() {
command chcon "$@"
}
get_selinux_context() {
local _file="$1"
local _lsres="$(ls -dZ "$_file" | head -1)"
local _sectx=''
case "$(echo "$_lsres" | wc -w)" in
2)
_sectx="$(echo "$_lsres" | cut -d ' ' -f 1)"
;;
5)
_sectx="$(echo "$_lsres" | cut -d ' ' -f 4)"
;;
*)
;;
esac
if [[ "x$_sectx" == "x?" ]]; then
_sectx=""
fi
echo "$_sectx"
}
show_argument_error_and_exit() {
local _error_msg="$1"
@ -433,8 +456,8 @@ check_environment_selinux() {
if [[ -z "$SECONTEXT_SYSTEMD_UNIT" ]]; then
if [[ -z "$FORCE_NO_SYSTEMD" ]] && [[ -e "$SYSTEMD_SERVICES_DIR" ]]; then
local _sectx="$(ls -ldZ "$SYSTEMD_SERVICES_DIR" | cut -d ' ' -f 5)"
if [[ "x$_sectx" == "x?" ]]; then
local _sectx="$(get_selinux_context "$SYSTEMD_SERVICES_DIR")"
if [[ -z "$_sectx" ]]; then
warning "Failed to obtain SEContext of $SYSTEMD_SERVICES_DIR"
else
SECONTEXT_SYSTEMD_UNIT="$_sectx"