Переглянути джерело

Searches for running process (parent PID) to allow display of message in TTY or PTS

Laurent HUBERT 9 місяців тому
батько
коміт
af41fab4a9
2 змінених файлів з 21 додано та 11 видалено
  1. 9 2
      README.md
  2. 12 9
      scripts/lfirewall

+ 9 - 2
README.md

@@ -69,6 +69,13 @@ Cette page donne un exemple complet: [How to write startup script for systemd](h
 # TODO
 
 - Exemple de portscanner : [GitHub - mfinelli/firewall: Set of bash scripts to generate a firewall in Linux using iptables. Based on: https://wiki.archlinux.org/index.php/Simple_stateful_firewall](https://github.com/mfinelli/firewall)
-- Vérifier si une règle existe avant de l'appliquer:
+- Chargement d'un fichier shell:
+  1. Lister des fonctions d'un fichier shell ([source](https://stackoverflow.com/a/5868245/1011366)) : `typeset -f | awk '/ \(\) $/ && !/^main / {print $1}'`
+  2. utiliser les fonctions commençant par `lfirewall_set`: `lfirewall_set_input_1`, etc
 
-[How can I check if an iptables rule already exists? - Super User](https://superuser.com/questions/360094/how-can-i-check-if-an-iptables-rule-already-exists)
+
+# Références
+
+## Affichage de messages avec systemctl
+
+https://bugs.freedesktop.org/show_bug.cgi?id=43753

+ 12 - 9
scripts/lfirewall

@@ -62,8 +62,11 @@ find_pid_user_of(){
 }
 
 find_systemctl_pids(){
+	local shell_pid
+	local systemctl_pid
 	ps -elf | grep 'systemctl' | grep -v grep | awk '{print $13}' | sort -u | while read term
 	do
+		echo ${shell_pid} ${systemctl_pid} >&2
 		if [ -z "${shell_pid:-}" ]
 		then
 			shell_pid=$(find_pid_user_of /dev/$term '.*sh$')
@@ -75,24 +78,24 @@ find_systemctl_pids(){
 		echo ${shell_pid} ${systemctl_pid}
 	done
 }
-
 process_and_parent=`find_systemctl_pids`
 
-if has_parent_process ${process_and_parent}
+if [ -n "${process_and_parent}" ]
 then
-	shell_process=$(echo ${process_and_parent} | awk '{print $1}')
-	parent_term=`readlink /proc/${shell_process}/fd/2`
+	if has_parent_process ${process_and_parent}
+	then
+		shell_process=`echo ${process_and_parent} | awk '{print $1}'`
+		parent_term=`readlink /proc/${shell_process}/fd/2`
+	fi
 fi
-echo "$$" > $parent_term
-ps -elf | grep 'systemctl' | grep -v grep > $parent_term
 
-set -eu #o pipefail
+set -eu
 
 help_message_lfirewall(){
 cat <<-EOF
   Light Firewall configuration script.
 
-  $(basename ${0}) [OPTIONS...] COMMAND
+  `basename ${0}` [OPTIONS...] COMMAND
 
 	COMMANDS
 
@@ -114,7 +117,7 @@ cat <<-EOF
 		1. Before setting any rule: clear the firewall
 		2. Setup your rules
 		3. run:
-			$(basename ${0}) saveuser
+			`basename ${0}` saveuser
 
 	AUTHOR
 		Author: Laurent Hubert