Interprocess communication with a Daemon
Asked Answered
E

3

20

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.

Ellsworth answered 17/7, 2011 at 0:35 Comment(0)
T
13

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.

Tolan answered 17/7, 2011 at 1:37 Comment(5)
Thanks for the reply, is using unix domain sockets the right way to communicate locally with a daemon? do you know how we communicate (command-line) with mysqld, httpd...?Ellsworth
According to Mysqld's manual page (linuxcommand.org/man_pages/mysqld1.html,) the default behavior is to create a Unix domain socket in /tmp/mysql.sock, but you can specify a different path with the --socket option. As for httpd, which httpd do you mean? There are a few of them.Tolan
Thanks Max, i mean httpd the main daemon, which launch the apache server with the command : httpd start or stop...Ellsworth
It looks like Apache does this by sending signals (en.wikipedia.org/wiki/Signal_%28computing%29) directly to the process, which works if the only thing you want to ask a process to do is exit or hang up. If you want to send more different types of commands, signals won't cut it.Tolan
Thanks Max, i think that the better way in my case is to do it like mysqld with a local unix domain socket. Thanks again.Ellsworth
C
1

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.

Cynthla answered 30/1, 2019 at 11:43 Comment(0)
C
0

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

Case answered 17/7, 2011 at 0:39 Comment(2)
If that's the case, just bind the TCP socket to localhost. Otherwise, you'll need to write your own (simple) tool to communicate with your daemon. Quagga routing suite does that. You can telnet into zebra,ospfd,ospf6d using telnet. In addition to telnet, quagga routing suite provides vtysh (shell) to communicate with the routing daemons.Case
wcang, is it possible to publish a route to a remote host using vtysh with Quagga?Began

© 2022 - 2024 — McMap. All rights reserved.