X-Git-Url: http://git.pld-linux.org/?a=blobdiff_plain;f=php-session.sh;h=06fac3372bc4b0342a7b23d42aaf3ca5851b2884;hb=5e40448654b711c59282a41693133f01e3f5b495;hp=bf3000127db431175724b922b6f9674df8527ffd;hpb=c29d80f3d28643b09f3d0fe675790dbbfef55924;p=packages%2Fphp-dirs.git diff --git a/php-session.sh b/php-session.sh old mode 100644 new mode 100755 index bf30001..06fac33 --- a/php-session.sh +++ b/php-session.sh @@ -1,25 +1,48 @@ #!/bin/sh -[ -x /usr/sbin/tmpwatch ] || exit +[ -x /usr/sbin/tmpwatch ] || exit 0 -if [ -x /usr/bin/php ]; then - RUNTIME5=$(/usr/bin/php -r '$t = round(ini_get("session.gc_maxlifetime")/3600); if ($t<1) $t=1; echo $t;' 2> /dev/null) -elif [ -r /etc/php/php.ini ]; then - RUNTIME5=$(awk -F"=" '/^session.gc_maxlifetime[ \t]*=/ { t=sprintf("%d", ($2/3600)); if (t<1) { t=1; }; print t; exit;}' /etc/php/php.ini) -fi +session_dirs="/var/run/php /var/run/php-ug" -if [ -x /usr/bin/php4 ]; then - RUNTIME4=$(/usr/bin/php4 -r '$t = round(ini_get("session.gc_maxlifetime")/3600); if ($t<1) $t=1; echo $t;' 2> /dev/null) -elif [ -r /etc/php4/php.ini ]; then - RUNTIME4=$(awk -F"=" '/^session.gc_maxlifetime[ \t]*=/ { t=sprintf("%d", ($2/3600)); if (t<1) { t=1; }; print t; exit;}' /etc/php4/php.ini) -fi +# use tmpwatch with --test to remove only files matching to 'sess_*' pattern +cleanup_dir() { + local session_dir=$1 -[ -z "$RUNTIME5" ] && RUNTIME5="1h" + test -d "$session_dir" || return -if [ -n "$RUNTIME5" ]; then - /usr/sbin/tmpwatch ${RUNTIME5} /var/run/php -fi + /usr/sbin/tmpwatch $gc_time $session_dir --test | while read action type file; do + case "$action $type $file" in + 'removing file '*/sess_*) + rm "$file" + ;; + esac + done +} -if [ -n "$RUNTIME4" -a "$RUNTIME5" != "$RUNTIME4" ]; then - /usr/sbin/tmpwatch ${RUNTIME4} /var/run/php -fi +# find minimum gc time from any of the php engines +find_gc_time() { + gc_time=0 + local gc php + for php in php $(cd /usr/bin && ls -1 php[0-9]* 2> /dev/null); do + gc= + if [ -x /usr/bin/$php ]; then + gc=$($php -r 'echo max(round(ini_get("session.gc_maxlifetime")/3600), 1);' 2> /dev/null) + elif [ -r /etc/$php/php.ini ]; then + gc=$(awk -F"=" '/^session.gc_maxlifetime[ \t]*=/ { t=sprintf("%d", ($2/3600)); if (t<1) { t=1; }; print t; exit;}' /etc/$php/php.ini) + fi + [ -n "$gc" ] || continue + + if [ "$gc" -lt "$gc_time" ] || [ $gc_time -eq 0 ]; then + gc_time=$gc + fi + done +} + +find_gc_time +[ $gc_time -gt 0 ] || exit 0 + +for session_dir in $session_dirs; do + cleanup_dir $session_dir +done + +exit 0