Inter-process communication with a demon

I want to implement a Unix daemon (let me have it myUnixd) and I want the user to be able to interact with this daemon through the command line, for example:

myUnixd --help # will display help information myUnixd --show # will show some data (the deamon should be doing the work) 

So my question is: how can I communicate with the demon? I was thinking about Unix domain sockets. Can someone tell me the correct way to do this?

Thanks.

+11
c command-line unix daemon inter-process-communicat
source share
3 answers

Use Berkeley sockets . In particular, you can create a “UNIX socket” (otherwise known as a “local domain socket”) that creates what looks like a text file. Write to a text file to send text to the daemon, read it to receive text from the daemon. You can implement this with a few function calls.

If you want something more advanced, you can also use DBus , which offers a more complex interface, but which is harder to learn.

+9
source share

use tcp socket if you want to use telnet to communicate with your daemon.

+1
source share

You can also use remote procedure call (RPC) for this client-server communication. There are various types of messages (protocols) that can be used with it, one of which is JSON.

The JSON-RPC protocol is very well suited for such tasks. You can find various tools and libraries for embedding in your software. A quick Google search gives this library C. The advantage of such libraries is that from the JSON specification file, where you define all your remote function calls, it creates client and / or server stubs that you can simply use in your code from the box .

As a listener, you can use sockets as the state of other responses, or simply have an embedded HTTP server such as microhttpd (and libcurl for the client). There are many examples to just reuse. HTTP also allows you to run your client behind a proxy server.

0
source share

All Articles