Add CMake Build System
Resolve pidfile resource leaks

This patch resolves resource leaks caused by not closing the file streams upon
an write error.