# tmpfiles - create missing directories in /var/{lock,run} according to
#            /etc/tmpfiles.d/

start on stopping rcS

task

script
       [ -d /etc/tmpfiles.d ] || exit 0

       SELINUX_RESTORE=

       # Check SELinux status
       SELINUX_STATE=
       if [ -e "/selinux/enforce" ] && [ "$(cat /proc/self/attr/current)" != "kernel" ]; then
               if [ -r "/selinux/enforce" ] ; then
                       SELINUX_STATE=$(cat "/selinux/enforce")
               else
                       # assume enforcing if you can't read it
                       SELINUX_STATE=1
               fi
       fi

       for i in /etc/tmpfiles.d/*; do
               while read type path mode uid gid age; do
                       # only directories
                       [ -n "$type" ] || continue
                       [ $type = "d" ] || [ $type = "D" ] || continue
                       # only /var/run and /var/lock
                       [ $path != ${path#/var/run/} ] || [ $path != ${path#/var/lock/} ] || continue
                       if [ ! -d $path ]; then
                               mkdir -p $path || continue
                               SELINUX_RESTORE=1
                               chmod $mode $path
                               chown $uid:$gid $path
                       fi
               done < $i;
       done

       if [ -n "$SELINUX_STATE" ] && [ -n "$SELINUX_RESTORE" ]; then
               /sbin/restorecon -R /var/lock /var/run 2> /dev/null
       fi
end script
