How to restart C daemon program in Linux after receiving SIGHUP signal

前端 未结 4 1905
故里飘歌
故里飘歌 2021-01-12 10:57

Can anybody please post some example code on how I can reread a configuration file and restart my daemon after the daemon receives a SIGHUP signal. The daemon is a user spac

4条回答
  •  日久生厌
    2021-01-12 11:33

    Depends how you structure it; if you handle multiple connections in a single thread / process, then you should probably somehow notify them to quit (if you can; depends on the protocol) before reloading the config (or exec itself).

    If the protocol allows you to say "go away and come back later", then clearly doing that is a good win. If the clients need to remain connected, you could apply the config changes to already-connected clients, if it's a single-process-single-thread daemon, if that makes sense.

    If it's multi process, things get more complex. You'd have to notify the processes about the new config, or ensure that they continue with the old config, or that they can pick the new config up when their client quits.

    If it's multi thread, the threads would need to safely be able to read the new config in the middle of whatever they happened to be doing, which might need locking, or you could allocate memory for a new config structure and do a switch-over somehow,

提交回复
热议问题