Compare commits

11 Commits
master ... dev

3 changed files with 267 additions and 50 deletions

29
Makefile Normal file
View File

@@ -0,0 +1,29 @@
VERSION=1.0.0
PKG = fakesystemctl
PREFIX ?= /usr/local
DESTDIR ?=
FMODE = -m0644
DMODE = -dm0755
MANF = "fakesystemctl.1"
SCRIPTF = "fakesystemctl"
LICENSEF = "LICENSE"
install_files:
install $(DMODE) $(DESTDIR)$(PREFIX)/bin
install $(DMODE) $(DESTDIR)$(PREFIX)/share/man/man1
install $(DMODE) $(DESTDIR)$(PREFIX)/share/licenses
install $(FMODE) $(SCRIPTF) $(DESTDIR)$(PREFIX)/bin/systemctl
install $(FMODE) $(MANF) $(DESTDIR)$(PREFIX)/share/man/man1/systemctl
gzip $(DESTDIR)$(PREFIX)/share/man/man1/systemctl
install $(FMODE) $(LICENSEF) $(DESTDIR)$(PREFIX)/share/licenses/systemctl
install: install_files
dist:
git archive --format=tar --prefix=$(PKG)-$(VERSION)/ $(VERSION) | gzip -9 > $(PKG)-$(VERSION).tar.gz
gpg --detach-sign --use-agent $(PKG)-$(VERSION).tar.gz
.PHONY: install dist

View File

@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/env bash
############################################################################################
# fakesystemctl a hack helper script for Artix Linux #
#------------------------------------------------------------------------------------------#
@@ -17,29 +17,80 @@
# Code complies with shellcheck https://www.shellcheck.net #
############################################################################################
# Global Variables Definitions
# For 66-suite, if no 66 services verbosity level is given as parameter on input, then set
# to the following level, as default:
release_no="1.0.0" # I dislike the 0.0.x, it is pathetic
if [ -z "$3" ]; then
verbosity66="4"
else
if [[ ! "$3" || "$3" = *[^1-5]* ]]; then
verbosity66="4"
fi
fi
get_runit_folders() {
# Runit set oriented folders per distribution
# Artix Linux
if [ "$distr_name" = "Artix Linux" ]; then
runitsrvdir="/etc/runit/sv"
runitrundir="/run/runit/service"
fi
# Void Linux
if [ "$distr_name" = "Void Linux" ]; then
runitsrvdir="/etc/runit/runsvdir"
runitrundir="/var/service"
fi
}
# functions definitions
get_init() {
openrc="$(pidof init)"
runit="$(pidof runit)"
s6="$(pidof s6-svscan)"
s66="$(pidof 66-shutdownd)"
if [ -z "$s66" ]; then
s66="0"
else
s66="1"
export init_sys="s66"
export s6=""
fi
if [ "$openrc" = "1" ]; then
export init_sys="openrc"
elif [ "$runit" = "1" ]; then
export init_sys="runit"
elif [ "$s6" = "1" ]; then
elif [ "$s6" = "1" ] && [ "$s66" = "0" ]; then
export init_sys="s6"
fi
if [ -z "$init_sys" ]; then
printf "%s\n" "${color_red}ERROR: Uknown init system."
printf "%s\n" "${color_red}Script stopped execution."
printf "\e[0m"
exit 1
fi
}
enable() {
if [ "$init_sys" = "openrc" ]; then
rc-update add "$service default"
elif [ "$init_sys" = "runit" ]; then
ln -s "/etc/runit/sv/$service" "run/runit/service"
ln -s "$runitsrvdir/$service" "$runitrundir"
elif [ "$init_sys" = "s6" ]; then
sh /usr/share/libalpm/scripts/s6-rc-db-update-hook "$service"
elif [ "$init_sys" = "s66" ]; then
66-enable -S -v"${verbosity66}" "$service"
fi
}
@@ -47,30 +98,57 @@ disable() {
if [ "$init_sys" = "openrc" ]; then
rc-update del "$service"
elif [ "$init_sys" = "runit" ]; then
unlink "run/runit/service/$service"
unlink "$runitrundir/$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc-bundle-update delete default "$service"
elif [ "$init_sys" = "s66" ]; then
66-disable -S "$service"
fi
}
start() {
if [ "$init_sys" = "openrc" ]; then
rc-update add "/etc/runit/sv/$service default" && pgrep -a "$service"
rc-update add "/etc/runit/sv/$service default" && pgrep -x "$service"
elif [ "$init_sys" = "runit" ]; then
sv up "$service" && pgrep -a "$service"
sv up "$service" && pgrep -x "$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc -u change "$service" && pgrep -a "$service"
s6-rc -u change "$service" && pgrep -x "$service"
elif [ "$init_sys" = "s66" ]; then
66-start -S -F -v"${verbosity66}" "$service"
fi
}
status() {
if [ "$init_sys" = "openrc" ]; then
rc-update show -v | grep "$service" && pgrep -a "$service"
mapfile -t status_array < <( rc-update show -v | grep -w "$service" && pgrep -x "$service" )
elif [ "$init_sys" = "runit" ]; then
sv status "$service" && pgrep -a "$service"
mapfile -t status_array < <( sv status "$service" && pgrep -x "$service" )
elif [ "$init_sys" = "s6" ]; then
s6-rc -a list "$service" | grep "$service" && pgrep -a "$service"
mapfile -t status_array < <( s6-rc -a list "$service" | grep -w "$service" && pgrep -x "$service" )
elif [ "$init_sys" = "s66" ]; then
mapfile -t status_array < <( 66-inservice "$service" | grep -w "$service" && pgrep -x "$service" )
fi
if [ -n "${status_array[*]}" ]; then
PID_no=$(pgrep -x $service)
if [ -n "$PID_no" ]; then
printf "\n%s\n" "${color_magenta}Service $service has been started:${color_cyan}"
printf "%s\n" "PID number is ${PID_no}"
#pgrep -x "$service"
unset 'status_array[${#status_array[@]}-1]'
for i in "${status_array[@]}"
do
printf "%s\n" "${color_green}${i} exists"
done
printf "\e[0m"
else
printf "\n%s\n" "${color_red}Service $service has not been started, was it enabled?"
printf "\e[0m"
fi
fi
}
stop() {
@@ -80,16 +158,20 @@ stop() {
sv stop "$service"
elif [ "$init_sys" = "s6" ]; then
s6-rc -d change "$service"
elif [ "$init_sys" = "s66" ]; then
66-stop -S -F "$service"
fi
}
is_active() {
service_status=$(pidof $service)
service_status=$(pidof "$service")
if [ -z "$service_status"]; then
printf "\n" "${colorred}Not active"
if [ -z "$service_status" ]; then
printf "%s\n" "${color_red}Not active"
printf "\e[0m"
else
printf "\n" "${colorblue}Active"
printf "%s\n" "${color_blue}Active"
printf "\e[0m"
fi
}
@@ -109,7 +191,7 @@ suspend() {
loginctl susppend
}
setcolors() {
set_colors() {
local opt=$1
local colors
@@ -134,27 +216,28 @@ setcolors() {
case "$opt" in
on|yes|true)
colorred=$(tput setaf 1)
colorgreen=$(tput setaf 2)
#coloryellow=$(tput setaf 3)
colorblue=$(tput setaf 4)
colormagenta=$(tput setaf 5)
colorcyan=$(tput setaf 6)
#colorwhite=$(tput setaf 7)
#colorgray=$(tput setaf 8)
#colorbold=$(tput bold)
#colorreset=$(tput sgr0)
color_red=$(tput setaf 1)
color_green=$(tput setaf 2)
color_yellow=$(tput setaf 3)
color_blue=$(tput setaf 4)
color_magenta=$(tput setaf 5)
color_cyan=$(tput setaf 6)
#color_white=$(tput setaf 7)
#color_gray=$(tput setaf 8)
#color_bold=$(tput bold)
#color_reset=$(tput sgr0)
;;
off|no|false)
colorred=
colorgreen=
#coloryellow=
colormagenta=
colorcyan=
#colorwhite=
#colorgray=
#colorbold=
#colorreset=
color_red=
color_green=
coloryellow=
color_blue=
color_magenta=
color_cyan=
#color_white=
#color_gray=
#color_bold=
#color_reset=
;;
*)
echo "unknown color option: '$opt'" >&2
@@ -163,21 +246,58 @@ setcolors() {
esac
}
get_os() {
distr_line=$(grep -rnw '/etc/os-release' -e 'NAME=')
distr_name="${distr_line#*=}"
distr_str=$(sed -e 's/^"//' -e 's/"$//' <<<"$distr_name")
if [ "$distr_name" = "void" ]; then # beautify that distro name at /etc/os-release men!
distr_name="Void Linux"
fi
if [ -z "$distr_name" ]; then
distr_str="Unknown Operating System"
fi
}
print_release_no() {
print_str="$init_sys"
if [ $init_sys = "s6" ]; then
print_str="s6-rc"
fi
if [ $init_sys = "s66" ]; then
print_str="66-suite"
fi
printf "\n%s\n" "${color_cyan}systemctl (fakesystemctl) script, release ${release_no}, running on ${distr_str} with ${print_str} system"
printf "\e[0m"
}
print_details() {
printf "\n%20s\n" "${colormagenta}----------------------------------------------------------------------------------------"
printf "Artix Linux is a systemd-free distribution, so this is a hack systemctl command,\n"
printf "(c) is held by Linuxer <linuxer@artixlinux.org>, to bypass special cases of systemd deps\n"
printf "Licence: GPL-2.0-only https://spdx.org/licenses/GPL-2.0-only.html \n"
printf "Sources: https://gitea.artixlinux.org/linuxer/fakesystemctl\n"
printf "%s\n" "----------------------------------------------------------------------------------------"
print_release_no
printf "\n%20s\n" "${color_magenta}------------------------------------------------------------------------------------------"
printf "%s\n" "This is a hack systemctl command script for systemd-free distributions,initially for Artix"
printf "%s\n" "Linux. to bypass special cases of systemd binary code dependencies, or to assist lazy"
printf "%s\n" "systemdfree users :D :D :D"
printf "%s\n" "Licence: GPL-2.0-only https://spdx.org/licenses/GPL-2.0-only.html"
printf "%s\n" "Sources: https://gitea.artixlinux.org/linuxer/fakesystemctl"
printf "%s\n" "Copyright (c) is held by Linuxer <linuxer@artixlinux.org> <linuxer@disroot.org>"
printf "%s\n" "------------------------------------------------------------------------------------------"
printf "\e[0m"
}
print_help() {
print_details
printf "\n%s\n" "${colorcyan}Available commands are:"
printf "\n%s\n" "${colorgreen}${tab}systemctl enable <service>"
printf "\n%s\n" "${color_cyan}Available commands are:"
printf "%s\n" "${color_green}${tab}systemctl enable <service>"
printf "%s\n" "${tab}systemctl enable <service> <logging66level>, <logging66level> is optional for 66-suite"
printf "%s\n" "${tab}systemctl start <service>"
printf "%s\n" "${tab}systemctl start <service> <logging66level>, <logging66level> is optional for 66-suite"
printf "%s\n" "${tab}systemctl restart <service>"
printf "%s\n" "${tab}systemctl reload <service>"
printf "%s\n" "${tab}systemctl stop <service>"
printf "%s\n" "${tab}systemctl status <service>"
printf "%s\n" "${tab}systemctl disable <service>"
@@ -186,25 +306,41 @@ print_help() {
printf "%s\n" "${tab}systemctl reboot"
printf "%s\n" "${tab}systemctl poweroff"
printf "%s\n" "${tab}systemctl suspend"
printf "%s\n" "${tab}systemctl -h"
printf "%s\n" "${tab}systemctl --help"
printf "%s\n" "${tab}systemctl -v"
printf "%s\n" "${tab}systemctl --version"
printf "\e[0m"
}
# the main body of the script
if [ "$EUID" -ne 0 ]; then
printf "%s\n" "${color_red}Please run script as root or with doas/sudo"
printf "\e[0m"
exit 1
fi
num_re='^-?[0-9]+$'
tab=' '
setcolors "yes";
set_colors "yes";
get_os
get_runit_folders
get_init
serviceinput="$2"
service_input="$2"
# clean up the .service suffix on user input
forbidden='.service'
if [[ "$serviceinput" =~ .*"$forbidden".* ]]; then
service=${serviceinput%.*}
if [[ "$service_input" =~ .*"$forbidden".* ]]; then
service="${service_input%.*}"
else
service="$serviceinput"
service="$service_input"
fi
# main script flow
@@ -229,6 +365,8 @@ elif [ "$1" = "start" ]; then
start
elif [ "$1" = "restart" ]; then
start
elif [ "$1" = "reload" ]; then
start
elif [ "$1" = "status" ]; then
status
elif [ "$1" = "stop" ]; then
@@ -237,10 +375,15 @@ elif [ "$1" = "-h" ]; then
print_help
elif [ "$1" = "--help" ]; then
print_help
elif [ "$1" = "-v" ]; then
print_release_no
elif [ "$1" = "--version" ]; then
print_release_no
else
printf '%s' "ERROR: wrong command detected:"
printf '%s' "$@"
printf '\n%s\n' "${color_red}ERROR: wrong command arguments detected, as below: "
printf '%s\n' "${color_yellow}$@"
printf "\n"
printf "\e[0m"
exit 1
fi

45
fakesystemctl.1 Normal file
View File

@@ -0,0 +1,45 @@
.\" Manpage for systemctl (fakesystemctl).
.\" Contact <linuxer@artixlinux.org> or <linuxer@disroot.org> to correct errors or typos.
.TH man 8 "10 Oct 2021" "1.0.0" "systemctl (fakesystemctl) man page"
.SH NAME
systemctl (fakesystemctl) \- a hack of systemctl command
.SH SYNOPSIS
systemctl <command> <servicename> <66loggingverbositylevel>
.SH DESCRIPTION
systemctl (fakesystemctl) is a hack script of systemctl command for systemd-free distributions.
.SH INIT SYSTEMS
Designed for OpenRC, Runit, s6-rc, 66-suite init systems
.SH OPTIONS
show help [--help] / [-h]
show release number [--version] [-v]
<66loggingverbositylevel> is a number from 1 to 5 for setting the logging vebosity level, available only on `enable`, `start` & `restart` commands for 66 suite [4 is the default level]
.SH COMMANDS
systemctl enable <servicename>
systemctl start <servicename>
systemctl restart <servicename>
systemctl reload <servicename>
systemctl stop <servicename>
systemctl status <servicename>
systemctl disable <servicename>
systemctl is-active <servicename>
systemctl hibernate
systemctl reboot
systemctl poweroff
systemctl suspend
.SH BUGS
No known bugs.
.SH AUTHOR
Linuxer <linuxer@artixlinux.org> or <linuxer@disroot.org>