API to control Linux daemon - c++

What I need is the possibility to control a Linux daemon though some sort of API, for example check if a certain daemon is running, start/stop/restart it, etc.
Is there any Linux library that provides this functionality?

You could also use D-Bus or SNMP. However, most daemons just write their PID to some file under /var/run/ and accept the SIGTERM signal to stop, and the SIGHUP signal to reload their configuration files (usually under /etc/).
Notice that if you adopt the usual convention that your daemon program mydprog is writing its pid in /var/run/mydprog.pid some other program could read that pid there and check, using kill(2) with a 0 signal, that the daemon process is running. You might also access to some pseudo-files under /proc/1234/ (where 1234 is the daemon's pid), notably /proc/1234/status, see proc(5) for more.
You can also design your daemon so that it answers, e.g. using some JSONRPC protocol on some unix(7) or tcp(7) socket, to some queries by giving status information. You might consider using some HTTP protocol thru some HTTP server library like libonion, or any other message passing or remote procedure call protocol.

short answer is no.
Some daemons might have an api but that will be specific for that daemon.
You can run /etc/init.d/<daemon_name> start|stop|status to start stop or get the status most daemons

Related

Share socket (listen) between unrelated processes

Running in Linux 3.9 kernel and later, I have an application X, which listens on a particular socket for connections. I want to write an unrelated application, Y, which tracks the number of attempts to connect to this socket, the source IP, etc.
Is it possible in c++ (ideally through Qt library) to share / monitor a socket already in use by an unrelated process? I found several StackOverflow questions which suggest forking to share the socket, but that's not possible in this case.
It is possible to transfer a file descriptor to another process, which behaves like a cross process dup(2). See Can I open a socket and pass it to another process in Linux for details. But this needs to be explicitly done, i.e. one process sends the file descriptor and another receives it. Thus the "unrelated" process must cooperate.
But a listen socket cannot be used for monitoring. The socket can only accept a connection but it is not possible to see if another process accepted a connection on the same socket, no matter if the sockets are shared with fork,threading or by file descriptor passing.
Given the right permissions and OS you can monitor the behavior of an application at the syscall level using the ptrace(2) or similar interface. There you could see if the application uses accept and what it returns. Or like suggested in a comment you can use packet capturing (tcpdump, raw sockets) to simply watch the traffic and deduct from a successful TCP handshake that some (unknown) process must have accepted the connection.

Django + mod_wsgi + apache2 - child process XXX still did not exit, sending a SIGTERM

I am getting intermittent errors -
child process XXX still did not exit, sending a SIGTERM.. and then a SIGKILL. It occurs intermittently and the web page hangs.
I was not using Daemon process..but now I am, still the problem exists..
Also I have some Error opening file for reading: Permission Denied.
Please can someone help?
I am new to this forum, so sorry if that has been answered before.
If you were not using daemon mode of mod_wsgi, that would imply that Apache must have been restarted at the time that initial message was displayed.
What is occurring is that in trying to do a restart, Apache sends a SIGTERM to its child processes. If they do not exit by their own accord it will send SIGTERM again at 1 second intervals and finally send it a SIGKILL after 3 seconds. The message is warning you of the latter and that it force killed the process.
The issue now is what is causing the process to not shutdown promptly. There could be various reasons for this.
Using an extension module for Python which doesn't work in sub interpreters properly which is deadlocking and hanging the process, preventing it from shutting down. http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
Use of background threads in the Python web application which have not been set as being daemon threads properly with the result they are then blocking process shutdown.
Your web application is simply trying to do too much on process shutdown somehow and not completing within the time limit.
Even if using daemon mode you will likely see this behaviour as it implements a similar shutdown timeout, albeit that the timeout is configurable for daemon mode.
Anyway, force use of the main Python interpreter as explained in the documentation link above
As to the permissions issue, read:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory
In short, ensure access permissions are correct of files/directories you need to access and ensure you are always using absolute path names when accessing the file system.

Executing daemon process on server

I have one deamon process which listen to the request from user and respond back.
While working on local system I execute it on terminal ./daemon. When user make request ./client from php page(executed by shell_exex() command) daemon process respond which some results. This is ok.
now I want to place this on ftp server. Php page whichc execute daemon process on button click event.
How could I make daemon process to keep listening on server continously? see daemon is c++ executable file.
One this is everytime I first execute shell_exec(daemon) but then purpose is lost. I want some way daemon process continously keep listening for the request!
Use daemon(), it does exactly what you want.
If this function is not available on your system, take a look at this tutorial which explains you how to rewrite the function.

Is it possible to get the thread Id of a process that is listening to a Port in Windows?

In windows we can list the ports that are currently used and the process that are listening to it using the netstat command. Just wondering if it is possible to find the exact thread id that opened the port inside the process?
I am looking for programmatic solutions like open process, IPC, etx. In windows if I inject a dll to the process I can get all the windows messages, however it does not include port related events.
hook socket, bind, listen, accept (and WSAXxx equivalents) and call GetCurrentThreadId() in the hook handlers. you can leverage e.g. MS Detours, EasyHook, or MHook etc to implement your handlers.

Instance of running embedded jetty server at certain port

An embedded jetty server is running at a particular port number. I want to get that running instance to stop from
Take a look at either the STOP.PORT mechanism or the shutdown handler.
http://wiki.eclipse.org/Jetty/Howto/Secure_Termination
or
http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/handler/ShutdownHandler.html
there is example embedded usage in the shutdown handler javadoc linked.
cheers