From 20e49c321e8b5a1df19d4625f4d4fdda41cb7e2e Mon Sep 17 00:00:00 2001
From: Marcin Krol <hawk@tld-linux.org>
Date: Sat, 18 Jul 2015 14:45:42 +0000
Subject: [PATCH 1/1] - from PLD

---
 php-dirs.spec     | 64 +++++++++++++++++++++++++++++++++++++++++++++++
 php-dirs.tmpfiles |  1 +
 php-session.sh    | 48 +++++++++++++++++++++++++++++++++++
 3 files changed, 113 insertions(+)
 create mode 100644 php-dirs.spec
 create mode 100644 php-dirs.tmpfiles
 create mode 100755 php-session.sh

diff --git a/php-dirs.spec b/php-dirs.spec
new file mode 100644
index 0000000..cf0b01c
--- /dev/null
+++ b/php-dirs.spec
@@ -0,0 +1,64 @@
+# TODO
+# - move tmpwatch S: to php-session package (as session files file storage no
+#   longer can cleanup itself due dir perms)
+Summary:	Common dirs for different PHP versions
+Summary(pl.UTF-8):	Wspólne katalogi dla różnych wersji PHP
+Name:		php-dirs
+Version:	1.5
+Release:	2
+License:	GPL
+Group:		Base
+Source0:	php-session.sh
+Source1:	%{name}.tmpfiles
+BuildRequires:	rpmbuild(macros) >= 1.644
+Requires(postun):	/usr/sbin/groupdel
+Requires(pre):	/usr/bin/getgid
+Requires(pre):	/usr/sbin/groupadd
+Suggests:	tmpwatch
+Provides:	group(http)
+BuildArch:	noarch
+BuildRoot:	%{tmpdir}/%{name}-%{version}-root-%(id -u -n)
+
+%description
+Common directories for PHP version 4 and version 5.
+
+%description -l pl.UTF-8
+Wspólne katalogi dla PHP w wersji 4 oraz 5.
+
+%prep
+
+%install
+rm -rf $RPM_BUILD_ROOT
+install -d $RPM_BUILD_ROOT{%{php_data_dir}/tests,/etc/cron.hourly,/var/{cache,log,run}/php,/var/log/archive/php} \
+	$RPM_BUILD_ROOT%{_docdir}/phpdoc \
+	$RPM_BUILD_ROOT%{systemdtmpfilesdir}
+
+install -p %{SOURCE0} $RPM_BUILD_ROOT/etc/cron.hourly
+cp -p %{SOURCE1} $RPM_BUILD_ROOT%{systemdtmpfilesdir}/%{name}.conf
+
+%clean
+rm -rf $RPM_BUILD_ROOT
+
+%pre
+%groupadd -g 51 http
+
+%postun
+if [ "$1" = "0" ]; then
+	%groupremove http
+fi
+
+%files
+%defattr(644,root,root,755)
+%{systemdtmpfilesdir}/%{name}.conf
+%attr(755,root,root) %{_sysconfdir}/cron.hourly/php-session.sh
+%dir %{php_data_dir}
+%dir %{php_data_dir}/tests
+%dir %{_docdir}/phpdoc
+%attr(775,root,http) %dir %verify(not group mode) /var/log/php
+%attr(770,root,root) %dir %verify(not group mode) /var/log/archive/php
+# no +r, so only predictable names can be used. currently php-soap wsdl cache is there
+%attr(730,root,http) %dir %verify(not group mode) /var/cache/php
+# http needs only x for directory (otherwise it knows session file
+# names and can read it contents)
+# keep o+x for fcgi.sock (lighttpd)
+%attr(731,root,http) %dir %verify(not group mode) /var/run/php
diff --git a/php-dirs.tmpfiles b/php-dirs.tmpfiles
new file mode 100644
index 0000000..5f8fa50
--- /dev/null
+++ b/php-dirs.tmpfiles
@@ -0,0 +1 @@
+d /var/run/php 0731 root http -
diff --git a/php-session.sh b/php-session.sh
new file mode 100755
index 0000000..d7bc1a7
--- /dev/null
+++ b/php-session.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+[ -x /usr/sbin/tmpwatch ] || exit 0
+
+session_dirs="/var/run/php /var/run/php-ug"
+
+# use tmpwatch with --test to remove only files matching to 'sess_*' pattern
+cleanup_dir() {
+	local session_dir=$1
+
+	test -d "$session_dir" || return
+
+	/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
+}
+
+# find minimum gc time from any of the php engines
+find_gc_time() {
+	gc_time=0
+	local gc php
+	for php in php php4 php52 php53 php54 php55 php56; 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
-- 
2.49.0