Traditionally, this is done using XSMP and libSM
. If you ask a session manager to log out, it sends "SaveYourself" messages to all clients that have registered with it over XSMP.
The program doesn't get killed, though; it exits by itself when Xlib loses the connection to the X11 server. (I'm not sure if gtk has an option to disable that.)