#!/bin/sh # ===================================================================== # Projet : DARKCONTAINER # Fichier : lib/service-systemctl # Auteur : Nicolas "DarkHack" Chapuis (c) 2026 # ================== DARKHACK ================== # [ darkweb.fr - system online ] # Powered by Perplexity, your AI assistant # https://www.perplexity.ai # ============================================== # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ===================================================================== if ! type "shInclude" > /dev/null 2>&1; then racineSh="${racineSh:-/etc/darksh}"; . "$racineSh/lib/shTools"; shDebugEnable="1";fi rootDarkContainer="${rootDarkContainer:-/etc/darkcontainer}" [ ! -d "$rootDarkContainer" ] && shDebug 0 "exit cause rootDarkContainer is not dir:$rootDarkContainer" >&2 && exit 0 $(shInclude $rootDarkContainer/lib/chroot) #|-------------------------------------------------------------| #| which "base" tools : cp | rm | echo | head | tail | #|-------------------------------------------------------------| # &&&&& #|-----------------------------------------------------------------------| #| which "directory" tools : mkdir | chmod | chgrp | mount | umount | #|-----------------------------------------------------------------------| # &&&&& #|------------------------------------------------------------------------------------| #| which "user right" tools : chmod | chown | chgrp | groupadd | useradd | chroot | #|------------------------------------------------------------------------------------| $(shInclude $rootDarkContainer/which/lib/base) && $(shInclude $rootDarkContainer/which/lib/systemctl) && $(shInclude $rootDarkContainer/which/lib/userRight) cmdSh="${cmdSh:-$(which sh)}" # pName="$pName" services_systemctl_create services_systemctl_create() { local log="services_systemctl_create:" shErrorOnMissing "pName cmdSystemctl cmdHead cmdTail cmdGrep" shDebug 2 "${log} pName:$pName pStart=$pStart" if [ -n "$($cmdSystemctl status ${pName}* | $cmdHead -n 3 | $cmdTail -n 1 | $cmdGrep running)" ];then shDebug 1 ""$log" service ${pName} is running, we stop them." $cmdSystemctl stop ${pName} $cmdSystemctl stop ${pName}@* fi } #pName="$pName" pServiceInfo="$pServiceInfo" pServiceDoc="$pServiceDoc"\ #pStart="$pStart" pStartPre="$pStartPre" pStop="$pStop" pReload="$pReload"\ #pIdFile="$pIdFile" serviceType="$serviceType"\ #pSystemCtlWants="$pSystemCtlWants" pSystemCtlBefore="$pSystemCtlBefore" pSystemCtlAfter="$pSystemCtlAfter" pSystemCtlRequires="$pSystemCtlRequires"\ #services_systemctl_install services_systemctl_install() { local log="services_systemctl_install:" shErrorOnMissing "pName pServiceInfo pServiceDoc" shErrorOnMissing "pStart pIdFile serviceType" shWarnOnMissing "pStartPre pReload pStop" shWarnOnMissing "pSystemCtlWants pSystemCtlBefore pSystemCtlAfter pSystemCtlRequires" shErrorOnMissing "cmdEcho cmdSystemctl cmdGrep" shWarnOnMissing "cmdStartStopDaemon" shDebug 2 "${log} pName:$pName pStart=$pStart" >&2 if [ -z "$pStop" ];then if [ -n "$cmdStartStopDaemon" ];then local pStop="$cmdStartStopDaemon --pidfile ${pIdFile} --stop || $cmdEcho \"echec stop service\"" else local pStop="$cmdKill -9 \"\$($cmdCat ${pIdFile})\" || $cmdEcho \"echec stop service\"" fi fi local serviceFile="/etc/systemd/system/${pName}.service" if [ -n "$($cmdEcho $pStart | $cmdGrep %i)" ];then local serviceFile="/etc/systemd/system/${pName}@.service";fi shDebug 1 ""$log" service file:$serviceFile" >&2 #pName="$pName" pServiceInfo="$pServiceInfo" pServiceDoc="$pServiceDoc"\ #pSystemCtlWants="$pSystemCtlWants" pSystemCtlBefore="$pSystemCtlBefore" pSystemCtlAfter="$pSystemCtlAfter" pSystemCtlRequires="$pSystemCtlRequires"\ services_systemctl_unit_gen > "$serviceFile" #pName="$pName" pServiceInfo="$pServiceInfo" pServiceDoc="$pServiceDoc"\ #pStart="$pStart" pStartPre="$pStartPre" pReload="$pReload"\ #pIdFile="$pIdFile" serviceType="$serviceType" pStop="$pStop" \ services_systemctl_service_gen >> "$serviceFile" $cmdEcho -e "[Install] \n\ WantedBy=multi-user.target \n\ " >> "$serviceFile" $cmdSystemctl daemon-reload $cmdSystemctl enable ${pName} $cmdSystemctl restart ${pName} $cmdSystemctl status ${pName} } #pName="$pName" pServiceInfo="$pServiceInfo" pServiceDoc="$pServiceDoc"\ #pSystemCtlWants="$pSystemCtlWants" pSystemCtlBefore="$pSystemCtlBefore" pSystemCtlAfter="$pSystemCtlAfter" #services_systemctl_unit_gen services_systemctl_unit_gen() { local log="services_systemctl_unit_gen:" shErrorOnMissing "pName pServiceInfo pServiceDoc" shWarnOnMissing "pSystemCtlWants pSystemCtlBefore pSystemCtlAfter" shErrorOnMissing "cmdEcho" shDebug 2 "${log} pName:$pName pStart=$pStart" >&2 $cmdEcho -e "[Unit] \n\ Description=$pServiceInfo \n\ #Documentation=$pServiceDoc \n\ DefaultDependencies=no \n\ " if [ -n "$pSystemCtlWants" ];then $cmdEcho -e "Wants=$pSystemCtlWants \n" fi if [ -n "$pSystemCtlBefore" ];then $cmdEcho -e "Before=$pSystemCtlBefore \n" fi if [ -n "$pSystemCtlAfter" ];then $cmdEcho -e "After=$pSystemCtlAfter \n" fi if [ -n "$pSystemCtlRequires" ];then $cmdEcho -e "Requires=$pSystemCtlRequires \n" fi # $pSystemCtlRestart $pSystemCtlRestartSec $pSystemCtlStartLimitIntervalSec $pSystemCtlStartLimitBurst if [ -n "$pSystemCtlStartLimitIntervalSec" ];then $cmdEcho -e "StartLimitIntervalSec=$pSystemCtlStartLimitIntervalSec \n" fi if [ -n "$pSystemCtlStartLimitBurst" ];then $cmdEcho -e "StartLimitBurst=$pSystemCtlStartLimitBurst \n" fi } #pName="$pName" pIdFile="$pIdFile" serviceType="$serviceType"\ #pStart="$pStart" pStartPre="$pStartPre" pStop="$pStop" pReload="$pReload"\ #services_systemctl_service_gen services_systemctl_service_gen() { local log="services_systemctl_install:" shErrorOnMissing "pName pIdFile serviceType" #shErrorOnMissing "pStart pStartPre pStop pReload" shErrorOnMissing "cmdEcho" shDebug 2 "${log} pName:$pName pStart=$pStart" >&2 if [ -n "$pChrootDir" ];then shDebug 2 "${log} pChrootDir:$pChrootDir" chroot_gen_conf "$pChrootDir" "$pName" "$pUser" "$pGroup" "$pMountFolders" "$pDirUserGroupRight" fi local ExecStartPre="" if [ -n "$pStartPre" ];then local ExecStartPre="ExecStartPre=$pStartPre";fi #if [ -n "$pStartPre" ];then local ExecStartPre="ExecStartPre=$cmdSh -c '$pStartPre'";fi local ExecStart="" if [ -n "pStart" ];then local ExecStart="ExecStart=$pStart";fi local ExecReload="" if [ -n "$pReload" ];then local ExecReload="ExecReload=$pReload";fi local ExecStop="" if [ -n "$pStop" ];then local ExecStop="ExecStop=$pStop";fi $cmdEcho -e "[Service] \n\ Type=$serviceType \n\ PIDFile=$pIdFile \n\ " if [ -n "$pSystemCtlRestart" ];then $cmdEcho -e "Restart=$pSystemCtlRestart \n" fi if [ -n "$pSystemCtlRestartSec" ];then $cmdEcho -e "RestartSec=$pSystemCtlRestartSec \n" fi if [ -n "$pChrootDir" ];then $cmdEcho -e " \n\ ExecStartPre=$cmdSh -c 'rootDarkContainer=\"$rootDarkContainer\" \"$rootDarkContainer/chroot/bin/container\" start \"\$(echo $pChrootDir | base64 -i)\"' \n\ ";fi $cmdEcho -e "$ExecStartPre \n\ $ExecStart \n\ " if [ -n "$pChrootDir" ];then $cmdEcho -e " \n\ ExecStartPost=$cmdSh -c 'rootDarkContainer=\"$rootDarkContainer\" \"$rootDarkContainer/chroot/bin/container\" register-pid \"\$(echo $pChrootDir | base64 -i)\" \"\$PIDFile\"' \n\ ";fi $cmdEcho -e "$ExecReload \n\ $ExecStop \n\ " if [ -n "$pChrootDir" ];then $cmdEcho -e " \n\ ExecStopPost=$cmdSh -c 'rootDarkContainer=\"$rootDarkContainer\" \"$rootDarkContainer/chroot/bin/container\" stop \"\$(echo $pChrootDir | base64 -i)\" \"\$PIDFile\"' \n\ ";fi $cmdEcho -e " \n\ PrivateTmp=false \n\ " }