This is a little off-topic, but does it strike anybody else as weird that we're still writing this kind of code in our servers instead of using an external utility? Personally, I would probably just write something like:
sudo chpst -u gwan:gwan -o 100000 ./gwan
This uses the chpst utility from runit to do pretty much exactly what you described: set uid/gid, change the file descriptor limit, drop privileges, and execute ./gwan. For similar reasons, it feels crazy that people still manually write code to handle daemonization and pidfile handling, when there are easy tools that will handle that for you, and generally do a really good job of it. Isn't that the Unix Way?