%bcond_with systemd

%define soversion 1.0.42
%define soversion_major 1

Name: libcgroup
Summary: Tools and libraries to control and monitor control groups
Summary(ja): CGROUPS を管理・監視するためのツールおよびライブラリ
Version: 0.42.2
Release: 2%{?_dist_release}%{?with_systemd:.systemd}
Group: system
Vendor: Project Vine
Distribution: Vine Linux

License: LGPLv2+
URL: http://libcg.sourceforge.net/
Source0: https://github.com/%{name}/%{name}/archive/v%{version}/%{name}-v%{version}.tar.gz
Source1: cgconfig.conf.sysvinit
Source2: cgconfig.sysconfig
Source100: cgconfig.service
Source101: cgconfig.conf.systemd

Patch1: libcgroup-0.37-chmod.patch
Patch2: libcgroup-0.40.rc1-coverity.patch
Patch3: libcgroup-0.40.rc1-fread.patch
Patch4: libcgroup-0.40.rc1-templates-fix.patch

Patch1001: 0002-fix-initscripts.patch

BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
BuildRequires: pam-devel
BuildRequires: byacc
BuildRequires: flex
BuildRequires: coreutils
Requires(pre): shadow-utils
%if %{with systemd}
%{?systemd_requires}
%else
Requires(post): chkconfig, /sbin/service
Requires(preun): /sbin/chkconfig
%endif

%description
Control groups infrastructure. The tools and library help manipulate, control,
administrate and monitor control groups and the associated controllers.


%package pam
Summary: A Pluggable Authentication Module for libcgroup
Summary(ja): Libcgroup の PAM モジュール
Group: system
Requires: libcgroup = %{version}-%{release}

%description pam
Linux-PAM module, which allows administrators to classify the user's login
processes to pre-configured control group.


%package devel
Summary: Development files for libcgroup
Summary(ja): libcgroup の開発用ファイル
Group: programming
Requires: libcgroup = %{version}-%{release}

%description devel
It provides API to create/delete and modify cgroup nodes. It will also in the
future allow creation of persistent configuration for control groups and
provide scripts to manage that configuration.


%debug_package


%prep
%setup -q -n %{name}-%{version}
%autopatch -p1
autoreconf -vif


%build
%configure \
	--bindir=/bin --sbindir=/sbin --libdir=%{_libdir} \
	--enable-pam-module-dir=/%{_lib}/security \
%if !%{with systemd}
	--enable-initscript-install \
%endif
	%{nil}

make %{?_smp_mflags}


%install
make DESTDIR=%{buildroot} install

# install config files
mkdir -p %{buildroot}/%{_sysconfdir}/sysconfig
cp samples/cgred.conf %{buildroot}/%{_sysconfdir}/sysconfig/cgred.conf
cp %{SOURCE2} %{buildroot}/%{_sysconfdir}/sysconfig/cgconfig
cp samples/cgrules.conf %{buildroot}/%{_sysconfdir}/cgrules.conf
cp samples/cgsnapshot_blacklist.conf %{buildroot}/%{_sysconfdir}/cgsnapshot_blacklist.conf

%if %{with systemd}
install -Dpm644 %{SOURCE100} %{buildroot}/%{_unitdir}/cgconfig.service
cp %{SOURCE101} %{buildroot}/%{_sysconfdir}/cgconfig.conf
%else
cp %{SOURCE1} %{buildroot}/%{_sysconfdir}/cgconfig.conf
%endif

# sanitize pam module, we need only pam_cgroup.so
mv -f %{buildroot}/%{_lib}/security/pam_cgroup.so.*.*.* %{buildroot}/%{_lib}/security/pam_cgroup.so
rm -f %{buildroot}/%{_lib}/security/pam_cgroup.la %{buildroot}/%{_lib}/security/pam_cgroup.so.*

# move the libraries  to /
mkdir -p %{buildroot}/%{_lib}
mv -f %{buildroot}/%{_libdir}/libcgroup.so.%{soversion} %{buildroot}/%{_lib}
rm -f %{buildroot}/%{_libdir}/libcgroup.so.%{soversion_major}
ln -sf libcgroup.so.%{soversion} %{buildroot}/%{_lib}/libcgroup.so.%{soversion_major}
ln -sf ../../%{_lib}/libcgroup.so.%{soversion} %{buildroot}/%{_libdir}/libcgroup.so
rm -f %{buildroot}/%{_libdir}/*.{a,la}
rm -f %{buildroot}/%{_libdir}/libcgroupfortesting.*
rm -f %{buildroot}/%{_lib}/security/*.a

# pre-create /cgroup directory
mkdir %{buildroot}/cgroup


%pre
getent group cgred >/dev/null || groupadd -r cgred

%post 
%if %{with systemd}
%systemd_post cgconfig.service
%else
/sbin/chkconfig --add cgred
/sbin/chkconfig --add cgconfig
%endif

%preun
%if %{with systemd}
%systemd_preun cgconfig.service
%else
if [ $1 = 0 -o -x /bin/systemctl ]; then
    /sbin/service cgred stop > /dev/null 2>&1 || :
    /sbin/service cgconfig stop > /dev/null 2>&1 || :
    /sbin/chkconfig --del cgconfig
    /sbin/chkconfig --del cgred
fi
%endif

%if %{with systemd}
%postun
%systemd_postun_with_restart cgconfig.service
%endif


%files 
%defattr(-,root,root,-)
%config(noreplace) %{_sysconfdir}/sysconfig/cgred.conf
%config(noreplace) %{_sysconfdir}/sysconfig/cgconfig
%config(noreplace) %{_sysconfdir}/cgconfig.conf
%config(noreplace) %{_sysconfdir}/cgrules.conf
%config(noreplace) %{_sysconfdir}/cgsnapshot_blacklist.conf
/%{_lib}/libcgroup.so.*
%attr(2755, root, cgred) /bin/cgexec
/bin/cgclassify
/bin/cgcreate
/bin/cgget
/bin/cgset
/bin/cgdelete
/bin/lscgroup
/bin/lssubsys
/sbin/cgconfigparser
/sbin/cgrulesengd
/sbin/cgclear
/bin/cgsnapshot
%attr(0644, root, root) %{_mandir}/man1/*
%attr(0644, root, root) %{_mandir}/man5/*
%attr(0644, root, root) %{_mandir}/man8/*
%if %{with systemd}
%{_unitdir}/cgconfig.service
%else
%attr(0755,root,root) %{_initrddir}/cgconfig
%attr(0755,root,root) %{_initrddir}/cgred
%endif
%license COPYING
%doc README_daemon

%files pam
%defattr(-,root,root,-)
%attr(0755,root,root) /%{_lib}/security/pam_cgroup.so
%license COPYING

%files devel
%defattr(-,root,root,-)
%{_includedir}/libcgroup.h
%{_includedir}/libcgroup/*.h
%{_libdir}/libcgroup.*
/%{_libdir}/pkgconfig/libcgroup.pc
%license COPYING


%changelog
* Mon Mar 22 2021 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.42.2-2
- dropped ldconfig scriptlets.
- updated Patch1001: made LSB free.
- added systemd support (disabled as default).

* Thu Mar 19 2020 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.42.2-1
- new upstream release.
- dropped Patch1-15.
- imported Patch1-4 from rawhide.
- updated Patch1001.

* Thu May 23 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.41-2
- updated Patch1000: add line for systemd-container.

* Sat Jan 12 2019 Tomohiro "Tomo-p" KATO <tomop@teamgedoh.net> 0.41-1
- updated and renumbered Patch0 to Patch1000.
- updated and renumbered Patch1 to Patch1001.
- imported Patch1-15 from rawhide.

* Sun Oct 12 2014 Ryoichi INAGAKI <ryo1@toki.waseda.jp> 0.37.1-2
- moved libcgroup to System Environment/Libraries

* Wed Mar 23 2011 Daisuke SUZUKI <daisuke@linux.or.jp> 0.37.1-1
- initial build for Vine Linux

* Thu Mar  3 2011 Jan Safranek <jsafrane@redhat.com> 0.37.1-1
- Update to 0.37.1

* Mon Feb 07 2011 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.37-3
- Rebuilt for https://fedoraproject.org/wiki/Fedora_15_Mass_Rebuild

* Mon Jan 17 2011 Jan Safranek <jsafrane@redhat.com> 0.37-2
- Create the 'cgred' group as system group, not as user
- Fix cgclassify exit code

* Mon Dec 13 2010 Jan Safranek <jsafrane@redhat.com> 0.37-1
- Update to 0.37
- use /sys/fs/cgroup as default directory to mount control groups (and rely on
  systemd mounting tmpfs there)

* Fri Nov 12 2010 Jan Safranek <jsafrane@redhat.com> 0.36.2-3
- Ignore systemd hierarchy - it's now invisible to libcgroup (#627378)

* Mon Aug  2 2010 Jan Safranek <jsafrane@redhat.com> 0.36.2-2
- Fix initscripts to report stopped cgconfig service as not running
  (#619091)

* Tue Jun 22 2010 Jan Safranek <jsafrane@redhat.com> 0.36.2-1
- Update to 0.36.2, fixing packaging the libraries (#605434)
- Remove the dependency on redhat-lsb (#603578)

* Fri May 21 2010 Jan Safranek <jsafrane@redhat.com> 0.36-1
- Update to 0.36.1

* Tue Mar  9 2010 Jan Safranek <jsafrane@redhat.com> 0.35-1
- Update to 0.35.1
- Separate pam module to its own subpackage

* Mon Jan 18 2010 Jan Safranek <jsafrane@redhat.com> 0.34-4
- Added README.Fedora to describe initscript integration

* Mon Oct 19 2009 Jan Safranek <jsafrane@redhat.com> 0.34-3
- Change the default configuration to mount everything to /cgroup

* Fri Jul 24 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.34-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_12_Mass_Rebuild

* Tue Jul  7 2009 Jan Safranek <jsafrane@redhat.com> 0.34-1
- Update to 0.34
* Mon Mar 09 2009 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.33-3
- Add a workaround for rt cgroup controller.
* Mon Mar 09 2009 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.33-2
- Change the cgconfig script to start earlier
- Move the binaries to /bin and /sbin
* Mon Mar 02 2009 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.33-1
- Update to latest upstream
* Wed Feb 25 2009 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> 0.32.2-4
- Rebuilt for https://fedoraproject.org/wiki/Fedora_11_Mass_Rebuild

* Mon Jan 05 2009 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.32.2-3
- Fix redhat-lsb dependency
* Mon Dec 29 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.32.2-2
- Fix build dependencies
* Mon Dec 29 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.32.2-1
- Update to latest upstream
* Thu Oct 23 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.32.1-1
* Tue Feb 24 2009 Balbir Singh <balbir@linux.vnet.ibm.com> 0.33-1
- Update to 0.33, spec file changes to add Makefiles and pam_cgroup module
* Fri Oct 10 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.32-1
- Update to latest upstream
* Thu Sep 11 2008 Dhaval Giani <dhaval@linux-vnet.ibm.com> 0.31-1
- Update to latest upstream
* Sat Aug 2 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.1c-3
- Change release to fix broken upgrade path
* Wed Jun 11 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.1c-1
- Update to latest upstream version
* Tue Jun 3 2008 Balbir Singh <balbir@linux.vnet.ibm.com> 0.1b-3
- Add post and postun. Also fix Requires for devel to depend on base n-v-r
* Sat May 31 2008 Balbir Singh <balbir@linux.vnet.ibm.com> 0.1b-2
- Fix makeinstall, Source0 and URL (review comments from Tom)
* Mon May 26 2008 Balbir Singh <balbir@linux.vnet.ibm.com> 0.1b-1
- Add a generatable spec file
* Tue May 20 2008 Balbir Singh <balbir@linux.vnet.ibm.com> 0.1-1
- Get the spec file to work
* Tue May 20 2008 Dhaval Giani <dhaval@linux.vnet.ibm.com> 0.01-1
- The first version of libcg