diff -Nur e-smith-base-5.8.0.old/root/etc/e-smith/events/actions/systemd-default e-smith-base-5.8.0/root/etc/e-smith/events/actions/systemd-default --- e-smith-base-5.8.0.old/root/etc/e-smith/events/actions/systemd-default 2020-12-05 22:56:11.568000000 -0500 +++ e-smith-base-5.8.0/root/etc/e-smith/events/actions/systemd-default 2020-12-05 23:22:36.254000000 -0500 @@ -1,5 +1,208 @@ -#!/usr/bin/bash -/usr/bin/systemctl enable sme-server.target -ln -fs sme-server.target /lib/systemd/system/default.target -/usr/bin/systemctl preset-all -/usr/bin/systemctl set-default sme-server.target +#!/usr/bin/perl -w + +use strict; +use Errno; +use esmith::ConfigDB; +use File::Temp; +use esmith::templates; +use File::Basename; +use Cwd 'abs_path'; + +my $conf = esmith::ConfigDB->open_ro; + + +my $event = $ARGV [0]; +my $second = $ARGV [1]; + +my @servicedirpaths = ("/usr/lib/systemd/system/","/etc/systemd/system/"); +my @presetdirpaths = ("/usr/lib/systemd/system-preset/","/etc/systemd/system-preset/"); +my $filename = "/etc/systemd/system-preset/49-koozali.preset"; +my %services; +my %files; +my @WantedBy; + +# make sure our target is enabled +system("/usr/bin/systemctl enable sme-server.target"); +# force the main default target in /usr/lib +#ln -fs sme-server.target /lib/systemd/system/default.target +my $old_qfn = "sme-server.target"; +my $new_qfn = "/lib/systemd/system/default.target"; +if (!symlink($old_qfn, $new_qfn)) { + if ($!{EEXIST}) { + unlink($new_qfn) + or die("Can't remove \"$new_qfn\": $!\n"); + symlink($old_qfn, $new_qfn) + or die("Can't create symlink \"$new_qfn\": $!\n"); + } else { + die("Can't create symlink \"$new_qfn\": $!\n"); + } +} +# we let the dedicated systemd command tryin to do what we will do later in this script +# as up to systemd 236 it is bugged see: +# https://github.com/systemd/systemd/pull/7158 and https://github.com/systemd/systemd/pull/7289 +system("/usr/bin/systemctl preset-all"); +# in case preset-all messed up with our default target +system("/usr/bin/systemctl set-default sme-server.target"); + + +#warn "expanding $filename\n"; +esmith::templates::processTemplate({ + MORE_DATA => { }, + TEMPLATE_PATH => $filename, + OUTPUT_FILENAME => $filename, + }); + +# list both preset directories +# seek files to be removed from usr/lib if same basename exist +foreach my $d (@presetdirpaths) { + opendir my $dir, "$d" or die "Cannot open directory: $!"; + my @dirfiles = readdir $dir; + closedir $dir; + foreach my $fi (@dirfiles) { + next unless ($fi =~ /.preset$/); + $files{$fi}="$d$fi" + } +} + +# list wanted services in the sme-server.target +#Wants=acpid.service atd.service auditd.service avahi-daemon.service brandbot.path crond.service irqbalance.service nfs-client.target remote-fs.target rhel-configure.service rsyslog.service smartd.service yum-cron.service +my $smewants = `grep -P '^Wants=' /usr/lib/systemd/system/sme-server.target -rs`; +chomp $smewants; +my @smematches = ( $smewants =~ /([a-zA-Z0-9\-_]+\.service)/g ); + + +# parse all files on reverse order : lower number take precedence +# we ignore joker lines * +# we ignore @ lines +# we ignore multiple in one line +# our default at the end is to disable if not listed +foreach my $filen (reverse sort keys %files) { +#print "==============> $filen : ".$files{$filen} ."\n"; + # parsing $filename content + # should end with hash with 2 possible value : enable and disable + # ignore lines starting with # or empty character + open(FILE, '<', $files{$filen}) or die $!; + while () { + chomp; # remove newlines + next if (/^\s+$/); + next if (/^#/); + s/^\s+//; # remove leading whitespace + s/\s+$//; # remove trailing whitespace + next unless length; # next rec unless anything left +# print $_ ."\n"; + next unless (/^(enable|disable)\s+([a-zA-Z0-9\-_.@]+\.service)/); +# print $_ ."\n"; + #ignore service that does not exists ! + next unless ( -e "/usr/lib/systemd/system/$2" or -e "/etc/lib/systemd/system/$2" ); + # eliminate duplicates, this way we keep only the last entry of the lowest file as we do it in reverse order of file, + # but from top to bottom of file. + $services{$2}=$1; + + # list all Services explicitely listed in preset that are also in Wants= or with WantedBy= sme-server.target + my $service=$2; + next if (/^$service$/ ~~ @WantedBy); + if ( /^$service$/ ~~ @smematches ) { + push(@WantedBy, $service); + #print "want $service \n"; + } + else { + my $wanted = `grep -P '^WantedBy=.*sme-server.target' /usr/lib/systemd/system/$service* /etc/systemd/system/$service* -rsh` ; + chomp $wanted; + push(@WantedBy , $service) unless ( $wanted eq "") ; + #print "want $service \n" unless ( $wanted eq "") ; + } + + } +} + +# then check content of /etc/systemd/system/sme-server.target.wants/ +# remove what is not in enable +my $d = "/etc/systemd/system/sme-server.target.wants/"; +opendir my $dir, "$d" or die "Cannot open directory: $!"; +my @dirfiles = readdir $dir; +closedir $dir; +foreach my $fi (@dirfiles) { + # we ignore . and .. + next if $fi =~ /\.+$/; + # for the moment we only consider service files and ignore target, mount, device, socket... + next unless ($fi =~ /.service$/); + # remove if file but not a link + print "remove $d$fi : not a link\n" unless ( -l "$d$fi"); + unlink "$d$fi" unless ( -l "$d$fi"); + # remove if link is not to an existing file + my $absFilePath = abs_path("$d$fi") ; + print "remove $d$fi target '$absFilePath' does not exist or is not regular file\n" unless ( -f "$absFilePath"); + unlink "$d$fi" unless ( -f "$absFilePath"); + # is not enable in preset : remove + #print "==$fi \n"; + if ( ! defined $services{$fi} or $services{$fi} ne "enable") { + print "remove $d$fi as not enabled in preset\n"; + unlink "$d$fi"; + } + # if not wanted remove + unless ( /^$fi$/ ~~ @WantedBy) { + print "remove $d$fi as not declared as WantedBy or in Wants for sme-server.target\n"; + unlink "$d$fi"; + } +} + + +# and we add wanted enabled services +# we only do it for sme-server.target, ignoring the remaining of WantedBy +foreach my $service (sort keys %services) { + my $wanted= "not"; + $wanted = "want" if ( /^$service$/ ~~ @WantedBy ); + my $status = $services{$service}; + my $linkedU = ( -e "/usr/lib/systemd/system/sme-server.target.wants/$service" ) ? "linked" : "not"; + my $linkedE = ( -e "/etc/systemd/system/sme-server.target.wants/$service" ) ? "linked" : "not"; + my $linkedD = ( -e "/etc/systemd/system/default.target.wants/$service" or -e "/usr/lib/systemd/system/default.target.wants/$service" ) ? "linked" : "not"; + my $add = ($linkedU eq "not" and $linkedE eq "not" and $linkedD eq "not" and $status eq "enable" and $wanted eq "want") ? "ADDME!!" : ""; + ## adding link if needed in /etc/systemd/system/sme-server.target.wants + if ( $status eq "enable" and $linkedU eq "not" and $linkedE eq "not" and $linkedD eq "not" and $wanted eq "want"){ + print "systemctl add-wants sme-server.target $service\n"; + `systemctl add-wants sme-server.target $service `; + } +} + + + +# do something about /usr/lib/systemd/system/sme-server.target.wants/ +# we check for rpm owned and not rpm owned +# we only inform there, we do not do anything else +$d = "/usr/lib/systemd/system/sme-server.target.wants/"; +opendir $dir, "$d" or die "Cannot open directory: $!"; +@dirfiles = readdir $dir; +closedir $dir; +foreach my $fi (@dirfiles) { + # we ignore . and .. + next if $fi =~ /\.+$/; + # for the moment we only consider service files and ignore target, mount, device, socket... + next unless ($fi =~ /.service$/); + # remove if file but not a link + print "$d$fi is not a link\n" unless ( -l "$d$fi"); + # remove if link is not to an existing file + my $absFilePath = abs_path("$d$fi") ; + print "$d$fi target '$absFilePath' does not exist or is not regular file\n" unless ( -f "$absFilePath"); + # check if owned by rpm + my $rpmowned = `rpm -qf $d$fi`; + chomp $rpmowned; + if ($rpmowned ne "" ) { + #print "$d$fi is owned by $rpmowned\n"; + #next; + } else { + print "$d$fi has been manually added\n"; + } + if ( ! defined $services{$fi} or $services{$fi} ne "enable") { + print "$d$fi is not enabled in preset\n"; + } + # if not wanted remove + # need to check its own files also here + my $service = $fi; + my $wanted = `grep -P '^WantedBy=.*sme-server.target' /usr/lib/systemd/system/$service* /etc/systemd/system/$service* -rsh` ; + chomp $wanted; + #unless ( /^$fi$/ ~~ @WantedBy ) { + unless (grep(/^$fi$/, @WantedBy ) ) { + print "$d$fi is not declared as WantedBy or in Wants for sme-server.target\n"; + } +} + diff -Nur e-smith-base-5.8.0.old/root/etc/e-smith/templates/etc/systemd/system-preset/49-koozali.preset/20services e-smith-base-5.8.0/root/etc/e-smith/templates/etc/systemd/system-preset/49-koozali.preset/20services --- e-smith-base-5.8.0.old/root/etc/e-smith/templates/etc/systemd/system-preset/49-koozali.preset/20services 2020-12-05 22:56:11.611000000 -0500 +++ e-smith-base-5.8.0/root/etc/e-smith/templates/etc/systemd/system-preset/49-koozali.preset/20services 2020-12-05 23:27:36.869000000 -0500 @@ -12,7 +12,7 @@ $status = "disable" if -e "/etc/rc.d/init.d/".$service->key || -e "/etc/rc.d/init.d/supervise/".$service->key; } $OUT .= "# Systemd service file does not exist : " unless -e "/usr/lib/systemd/system/$servicename" || -e "/etc/lib/systemd/system/$servicename"; - $OUT .= "$status $servicename\n"; + $OUT .= "$status $servicename\n"; } diff -Nur e-smith-base-5.8.0.old/root/usr/lib/systemd/system/bootstrap-console.service e-smith-base-5.8.0/root/usr/lib/systemd/system/bootstrap-console.service --- e-smith-base-5.8.0.old/root/usr/lib/systemd/system/bootstrap-console.service 2020-12-05 22:56:11.447000000 -0500 +++ e-smith-base-5.8.0/root/usr/lib/systemd/system/bootstrap-console.service 2020-12-05 22:56:54.442000000 -0500 @@ -24,4 +24,4 @@ [Install] WantedBy=multi-user.target - +WantedBy=sme-server.target diff -Nur e-smith-base-5.8.0.old/root/usr/lib/systemd/system/sme-server.target e-smith-base-5.8.0/root/usr/lib/systemd/system/sme-server.target --- e-smith-base-5.8.0.old/root/usr/lib/systemd/system/sme-server.target 2020-12-05 22:56:11.555000000 -0500 +++ e-smith-base-5.8.0/root/usr/lib/systemd/system/sme-server.target 2020-12-05 23:09:13.423000000 -0500 @@ -1,4 +1,4 @@ -# This file is part of Koozali SME Server. +# This file is part of Koozali SME Server. # [Unit] @@ -9,3 +9,8 @@ After=basic.target rescue.service rescue.target AllowIsolate=yes Wants=acpid.service atd.service auditd.service avahi-daemon.service brandbot.path crond.service irqbalance.service nfs-client.target remote-fs.target rhel-configure.service rsyslog.service smartd.service yum-cron.service +Wants=dbus.service plymouth-quit-wait.service plymouth-quit.service systemd-logind.service systemd-update-utmp-runlevel.service systemd-user-sessions.service +Wants=php-fpm.service php55-php-fpm.service php56-php-fpm.service php70-php-fpm.service php71-php-fpm.service php72-php-fpm.service php73-php-fpm.service php74-php-fpm.service +Wants=runit.service bootstrap-console.service wan.service networking.service masq.service tinydns.service qpsmtpd.service sqpsmtpd.service qmail.service ftp.service dnscache.service dnscache.forwarder.service lpd.service dhcpd.service dovecot.service mariadb.service ntpd.service nut.service clamd.service freshclam.service httpd-admin.service httpd-e-smith.service ldap.service ldap.init.service local.service mysql.init.service spamd.service + + diff -Nur e-smith-base-5.8.0.old/root/usr/lib/systemd/system-preset/50-koozali.preset e-smith-base-5.8.0/root/usr/lib/systemd/system-preset/50-koozali.preset --- e-smith-base-5.8.0.old/root/usr/lib/systemd/system-preset/50-koozali.preset 1969-12-31 19:00:00.000000000 -0500 +++ e-smith-base-5.8.0/root/usr/lib/systemd/system-preset/50-koozali.preset 2020-12-05 23:25:48.139000000 -0500 @@ -0,0 +1,72 @@ +enable sme-server.target +disable multi-user.target +disable graphical.target + +enable dbus.service +enable plymouth-quit-wait.service +enable plymouth-quit.service +enable systemd-logind.service +enable systemd-update-utmp-runlevel.service +enable systemd-user-sessions.service + +enable runit.service +enable bootstrap-console.service +enable networking.service +enable wan.service +enable masq.service +enable php-fpm.service +enable php55-php-fpm.service +enable php56-php-fpm.service +enable php70-php-fpm.service +enable php71-php-fpm.service +enable php72-php-fpm.service +enable php73-php-fpm.service +enable php74-php-fpm.service +enable httpd-e-smith.service +enable httpd-admin.service +enable crond.service +disable dhcpd.service +enable dnscache.service +enable dnscache.forwarder.service +enable dovecot.service +enable irqbalance.service +disable isdn.service +enable lpd.service +enable ldap.service +enable ldap.init.service +enable local.service + +#need to change after integration +#enable mariadb.service + +# need change after deciding service name +# enable mdmonitor.service +# enable raidmonitor.service + +enable ntpd.service +disable nut.service +disable oidentd.service +disable pptpd.service +enable qmail.service +enable qpsmtpd.service +disable radiusd.service +enable raidmonitor.service +enable rsyslog.service +enable smartd.service + +#need to change after integration +#enable smb.service +#enable nmdb.service +#enable smbd.service + +disable smtp-auth-proxy.service +disable spamd.service +disable sqpsmtpd.service +disable squid.service +disable sshd.service +enable tinydns.service +enable wan.service +disable nut-server.service +disable nut-monitor.service +disable ntpdate.service + diff -Nur e-smith-base-5.8.0.old/root/usr/lib/systemd/system-preset/50koozali.preset e-smith-base-5.8.0/root/usr/lib/systemd/system-preset/50koozali.preset --- e-smith-base-5.8.0.old/root/usr/lib/systemd/system-preset/50koozali.preset 2020-12-05 22:56:11.568000000 -0500 +++ e-smith-base-5.8.0/root/usr/lib/systemd/system-preset/50koozali.preset 1969-12-31 19:00:00.000000000 -0500 @@ -1,18 +0,0 @@ -enable sme-server.target -disable multi-user.target -disable graphical.target - -enable runit.service -enable bootstrap-console.service -enable networking.service -enable wan.service -enable masq.service -enable php-fpm.service -enable php55-php-fpm.service -enable php56-php-fpm.service -enable php70-php-fpm.service -enable php71-php-fpm.service -enable php72-php-fpm.service -enable php73-php-fpm.service -enable php74-php-fpm.service - diff -Nur e-smith-base-5.8.0.old/root/etc/e-smith/events/actions/systemd-default e-smith-base-5.8.0/root/etc/e-smith/events/actions/systemd-default --- e-smith-base-5.8.0.old/root/etc/e-smith/events/actions/systemd-default 2020-12-06 16:08:47.304000000 -0500 +++ e-smith-base-5.8.0/root/etc/e-smith/events/actions/systemd-default 2020-12-06 16:08:55.969000000 -0500 @@ -109,7 +109,7 @@ my $wanted = `grep -P '^WantedBy=.*sme-server.target' /usr/lib/systemd/system/$service* /etc/systemd/system/$service* -rsh` ; chomp $wanted; push(@WantedBy , $service) unless ( $wanted eq "") ; - #print "want $service \n" unless ( $wanted eq "") ; + #print "want $service \n" unless ( $wanted eq "") ; } } @@ -127,17 +127,30 @@ # for the moment we only consider service files and ignore target, mount, device, socket... next unless ($fi =~ /.service$/); # remove if file but not a link - print "remove $d$fi : not a link\n" unless ( -l "$d$fi"); - unlink "$d$fi" unless ( -l "$d$fi"); - # remove if link is not to an existing file + unless ( -l "$d$fi") { + print "remove $d$fi : not a link\n"; + unlink "$d$fi"; + next; + } + # remove if also un /usr/lib .. not as preset-all does not care + #if ( -l "/usr/lib/systemd/system/sme-server.target.wants/$fi") { + # print "remove $d$fi : also in /usr/lib/systemd/system/sme-server.target.wants/\n"; + # unlink "$d$fi"; + # next; + #} + # remove if link is not to an existing file # we should also check if poiting to an authorized path! my $absFilePath = abs_path("$d$fi") ; - print "remove $d$fi target '$absFilePath' does not exist or is not regular file\n" unless ( -f "$absFilePath"); - unlink "$d$fi" unless ( -f "$absFilePath"); + if ( ! -f "$absFilePath" or ( ! -f "/etc/systemd/system/$fi" and ! -f "/usr/lib/systemd/system/$fi") ) { + print "remove $d$fi target '$absFilePath' does not exist or is not regular file in expeted path\n"; + unlink "$d$fi"; + next; + } # is not enable in preset : remove #print "==$fi \n"; if ( ! defined $services{$fi} or $services{$fi} ne "enable") { print "remove $d$fi as not enabled in preset\n"; unlink "$d$fi"; + next; } # if not wanted remove unless ( /^$fi$/ ~~ @WantedBy) { @@ -156,10 +169,10 @@ my $linkedU = ( -e "/usr/lib/systemd/system/sme-server.target.wants/$service" ) ? "linked" : "not"; my $linkedE = ( -e "/etc/systemd/system/sme-server.target.wants/$service" ) ? "linked" : "not"; my $linkedD = ( -e "/etc/systemd/system/default.target.wants/$service" or -e "/usr/lib/systemd/system/default.target.wants/$service" ) ? "linked" : "not"; - my $add = ($linkedU eq "not" and $linkedE eq "not" and $linkedD eq "not" and $status eq "enable" and $wanted eq "want") ? "ADDME!!" : ""; ## adding link if needed in /etc/systemd/system/sme-server.target.wants - if ( $status eq "enable" and $linkedU eq "not" and $linkedE eq "not" and $linkedD eq "not" and $wanted eq "want"){ - print "systemctl add-wants sme-server.target $service\n"; + ## readd event if present in usr/lib as preste-all does not care about that. + if ( $status eq "enable" and $linkedE eq "not" and $linkedD eq "not" and $wanted eq "want"){ + #print "systemctl add-wants sme-server.target $service\n"; `systemctl add-wants sme-server.target $service `; } }