procd: introduce procd_lock for init script protection
authorRoman Yeryomin <roman@advem.lv>
Thu, 14 Dec 2017 21:54:26 +0000 (23:54 +0200)
committerJohn Crispin <john@phrozen.org>
Tue, 2 Jan 2018 06:52:27 +0000 (07:52 +0100)
Use flock to protect init script from concurrent execution
(of the same script).
Important for services which generate native config files.

Signed-off-by: Roman Yeryomin <roman@advem.lv>
Signed-off-by: Andrejs Hanins <ahanins@gmail.com>
package/system/procd/files/procd.sh

index 91e2371d6231c9928886d820f7c3603d5f68b22e..b8d3513b1e8d9eb2bfcfb3fb67d6bd7280e23b88 100644 (file)
 PROCD_RELOAD_DELAY=1000
 _PROCD_SERVICE=
 
+procd_lock() {
+       local basescript=$(readlink "$initscript")
+       local service_name="$(basename ${basescript:-$initscript})"
+
+       flock -n 1000 &> /dev/null
+       if [ "$?" != "0" ]; then
+               exec 1000>/var/lock/procd_${service_name}.lock
+               flock 1000
+               if [ "$?" != "0" ]; then
+                       logger "warning: procd flock for $service_name failed"
+               fi
+       fi
+}
+
 _procd_call() {
        local old_cb
 
@@ -47,6 +61,7 @@ _procd_call() {
 }
 
 _procd_wrapper() {
+       procd_lock
        while [ -n "$1" ]; do
                eval "$1() { _procd_call _$1 \"\$@\"; }"
                shift