Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interprocess communication with a Daemon

I want to implement a Unix daemon (let's call it myUnixd), and want the user to be able to interact with this daemon via the command line, for example:

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

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

Thanks.

like image 270
funnyCoder Avatar asked Jul 17 '11 00:07

funnyCoder


3 Answers

Use Berkeley sockets. Specifically, you can create a "UNIX domain socket" (otherwise known as a "local domain socket," which will create what looks like a text file. Write to the text file to send text to the daemon, read from 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 sophisticated interface, but which is more complicated to learn.

like image 175
Max E. Avatar answered Oct 10 '22 18:10

Max E.


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

like image 39
wcang Avatar answered Oct 10 '22 18:10

wcang


One could also use Remote Procedure Call (RPC) for such client-server communication. There are different types of messages (protocols) that can be used together with it, one of them is JSON.

The JSON-RPC protocol is very well accepted for such tasks. You can find different tools and libraries to embed in your software. A quick search on google gives this C library. The advantage of such libraries is that from a JSON specification file, where you define all your remote function calls, it creates client and/or server stubs that you can just use in your code out of the box.

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

like image 1
jav Avatar answered Oct 10 '22 20:10

jav