I have a generic C++ program that normally constantly runs. However, I want the program to stop when the current user logs out.

This program is running on OS X, which is very close to BSD and is similar to Linux. Is there some event or signal I can listen for to know when the current user is logging out so my program can exit?