C++: stop RPC service - c++

from my C++ source, I am starting an RPC service calling svc_run(). Everything looks just fine and I can see my service running if I type rpcinfo -p in my terminal.
Now I am working on a "cleanup" function which should stop this service and remove it from the rpcinfo -p list.
How can I do that? At the moment I am only able to stop it using sudo rpcinfo -d program version in my terminal. How can I do this from my source file?
Thanks.

After some time, I found out how to do this. Actually I faced some unexpected difficulties. The standard way to do this would be to use this:
svc_unregister(PROGID, VERSION)
but somehow, it did not work for me. After lots of trial and some online help (http://www.spinics.net/lists/linux-nfs/msg05619.html) I was able to delete the RPC service calling:
pmap_unset(PROGID, VERSION);
Hope this will help :)

Try to use void svc_exit(void) function. For more detailed description please refer to rpc_svc_calls chapter.

I tried this force stop of svc_run(), however did not find a solution, I however made the svc_run() stop from within the registered function and then it stopped - perhaps this could help you - please look at this : svc_exit Subroutine

The 'nicest' solution is to use both DevCpp's and Danilo's solution combined:
Among the RPC functions of your server, define one function which, when called by the client, executes svc_exit(). This will let your RPC server return from the svc_run() loop. Now you can either extend your RPC client application or create a separate client application to terminate your server.
In your RPC server's main program, right after the call to svc_run(), execute 'pmap_unset(PROGID, VERSION);'. This will let rpcbind unregister your RPC address.
Then do the usual cleanup of your application.
This combination allows your RPC server to run as a demon, i.e. without user interaction, while still offering a clean exit without having to cancel the process.

Related

using python 2.7 and library OpenOPC using read or write function explode

I use python 2.7 & OpenOPC to communicate with OPC servers.
I have 2 different servers.
With one server everything is Ok.
I can:
- connect and get information from the server
- get list of objects
- get properties of objects
- read value of items
- write values. If a value is not autorized for writing, an error is get from the OpenOPC library and I can manage it with the python code
On the other server, it crashes:
- everything is working like the first server but
- when I try to read or write, the python code explode
With this server, if I use the opc.exe in the command line, it also explode when write or read. For example:
- opc -s Als1.s8000.1 -i connect the server and let me see the properties
- opc -s Als1.s8000.1 -l L4A1 list all the sub items under L4A1
- opc -s Als1.s8000.1 -r L4A1.LPSLOOP1C01.RM02 -> generate a popup "opc.exe has stopped working bla bla bla"
Thanks in advance for your help
Well, I found where is the problem.
The Alstom OPCServer is managing the OPC request in his way. I've seen the c++ code from the server and I managed to modify the python OpenOPC library to send correct arguments to the Alstom server.
Thanks for your help and the positive evaluation of my question.
Some progress about this question.
We tried to use some trace to see what is managed by the server. Curiously when I use OPCInspector, an application able to communicate with the OPCservers, we are able to read and write... But the logs show us OPCInspector does not use the same function to do that as OpenOPC.
So next step is to add some traces in the server side to understand what happens in the Alstom server. I mean, it smells this is not an OpenOPC question but the server question.

How to make a C++ Program listen for system commands

I was recently asked how/if possible that one could do this. I am not sure if it is even possible, and I have been searching on it for a while now.
Basically let's take Windows for example there is a system command to shut down the computer. Let's say shutdown -s -t 30 -c "Shutdown"
Is there a way to write a program which will listen for a shutdown command, and then run shutdown -a in response to abort that command?
In short, can you make it listen for certain system commands on the computer and execute system commands in response?
This is indeed possible. Your example, however, is probably not the best one to describe a generic problem. There are session events in Windows that applications can listen to, and shutdown is one of them. And after all, shutdown.exe is not the only application that can ask Windows to shut down.
In general, however, applications “listening” for commands being executed will have to integrate tightly with the operating system. You can imagine that anti-virus software does exactly that and a lot more in order to prevent execution of
“bad” programs. I am not familiar with Windows technology but would imagine hooking the Windows system call that executes binaries is the way to do it. For sure that will require "administrative" permissions and can even require to write a kernel module.
You can go this way:
Check if the program is exiting.
If yes, then cancel the program exit call and execute system("shutdown -a);
Else, do what you want.
Well, probably there many more sophisticated ways to do that. And I agree with #VladLazarenko's answer; programs like antiviruses keep listening to system commands. But I guess that would require in-depth knowledge of the API. This is just a simple way to do that. If you are not a newbie, then you must visit this link, http://www.codeproject.com/Articles/2082/API-hooking-revealed (from #RedX's comment). Good Luck! :)

Ejabberd module with child process

I created a logging module which logs messages to a mysql db, the current code is located here:
https://github.com/amiadogroup/mod_log_chat_mysql5/blob/master/src/mod_log_chat_mysql5.erl
The Problem with the current code is, that sometimes the connection gets closed and as a result, the module doesn't work anymore.
As you see in the code, I store the DBRef in an ets table, which is not really the good way to go.
I asked the erlang mailinglist about this and they suggested me to do the DB Connection as an own child process of the module. This would enable the module to gracefully restart the connection upon closing of the connection.
Now my question is: how can I implement this child process with gen_server and/or gen_mod?
Do I need to create two files or can I do it within the same file?
Is there any example somewhere on how I could achieve that?
Edit: As you can see in the linked github repo, I updated the code and it works now, weeha!
Looking at the mod_Archive code helped me a lot, although I didn't decide to upgrade my ejabberd version.
I ran into another, but related problem now. In the code you see that I do an initial query with "SET NAMES UTF8" to prevent garbling of messages. It seems that this isn't done again if the gen_server does a reconnect. Is there any hook I can call upon reconnect so that the UTF8 query is done everytime?
Edit#2:
Now I switched to Emysql (https://github.com/Eonblast/Emysql) and it works out of the box by specifying the encoding directly on connect.
Code is on github.
Thanks for your help,
Michael
I suggest you look into general Erlang/OTP principles (gen_server, supervisor, etc).
ejabberd is relying on this standard Erlang architecture pattern.
Regarding your comment on database, ejabberd has its own way on managing database and passing queries to MySQL for example. You should as well look into it.
In your source code you are only applying the gen_mod behaviour, if you do wish to have a gen_server you can do it in the same module, if you define the gen_server behaviour has well.
A good example would be the ejabberd module mod_archive, which implements both behaviours.
Edit: I never really worked "directly" with mysql on erlang. But through the ejabberd methods I find it pretty "easy"(you will have to make a few setup, but rather easy). You have the method
ejabberd_odbc:sql_query_t(Query)
And has an example you can find it on the module mod_archive_odbc.
To use that method(and the last module) I haved downloaded the mysql native driver and put the beams created from the driver in ejabberd ebin dir (you can put it anywhere has long is on the erlang path).
A a soft link to the ejabberd ebin is my favorite:
ln -s <diryouhavethedriver>/ebin/*.beam /usr/lib/ejabberd/ebin/
and do a few configurations on you ejabberd.cfg. This process is described on this page on process one. Notice that the full steps are to make mysql the full database of ejabberd. You may not want that, so you must jump a few steps.
Hope this help.

Control multiple program instances - open multiple files problem

This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine.
So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files.
My system (ubuntu 10.10), when I right click a file and I choose "Open With 'Default Application'" the it runs
Code:
default_application path/to/the/selected/file1
So, if you select multiple files and select "Open With 'Default Application'" the system will call
Code:
default_application path/to/the/selected/file1
default_application path/to/the/selected/file2
default_application path/to/the/selected/file3
So, this is a big problem for me, because I handle the concurrent processes from inside the program, so when another instance of the program is running, a warning message is appeared. So, each application's call will recognize the others as currently running applications and so it'll show the message. I'll end up with 3 Messages saying that another process of the program is running --_--'
My application handles multiple URLs this way:
Code:
myapp path/to/the/selected/file1 path/to/the/selected/file2 path/to/the/selected/file3
How can I make my code handle all these multiple instances at the same time? Everything I've tried fails, because everything I've tried requires a check from the first instance called, which is too slow and other instances come app and all together are warning about concurrent processes of the same program
So, how can I fix this? is it system depended, or can I do something with the code?
The way is to make your application recognize that there is already an instance running and make the new instance just forward a request to the first instance before dying :)
EDIT:
The way to do that is to have your first application instance behave as a server. The pseudo algo is something like :
start();
try_to_contact_master_server_instance();
if(no_master())
{
I_am_master();
start_listening_server_that_wait_for_requests();
}
else
{
send_request_to_master("open file path/to/the/selected/file1");
send_request_to_master("open file path/to/the/selected/file2");
send_request_to_master("open file path/to/the/selected/file3");
die();
}
handle_incoming_requests();
I hope it's clearer ? Tell me if you need more precisions ...
For the server part, you can do your own or use some software bus provided by the OS like dbus or whatever, but it makes your application dependent, of course.
my2c

How to know if a process had been started but crashed in Linux

Consider the following situation: -
I am using Linux.
I have doubt that my application has crashed.
I had not enabled core dump.
There is no information in the log.
How can I be sure that, after the system restart my app was started, but now it is not running, because it has crashed.
My app is configured as a service, written in C/C++.
In a way: how can I get all the process/service names that have executed since the system start? Is it even possible?
I know, I can enable logging and start the process again to get the crash.
This feature is included in Linux Kernel. It's called: BSD process accounting.
Standard practice is to have a pid file for your daemon (/var/run/$NAME.pid), in which you can find its process id without having to parse the process tree manually. You can then either check the state of that process, or make your daemon respond to a signal (usually SIGHUP), and report its status. It's a good idea to make sure that this pid still belongs to your process too, and the easiest way is to check /proc/$PID/cmdline.
Addendum:
If you're only using newer fedora or ubuntu, your init system is upstart, which has monitoring and triggering capabilities built in.
As #emg-2 noted, BSD process accounting is available, but I don't think it's the correct approach for this situation.
I would recommend that you write the fact that you started out to some kind of log file, either a private one which get's overwritten on each start up or one via syslogd.
Also, you can log a timestamp heartbeat so that you know exactly when it crashed.
you probably can make a decoy, ie an application or shell script that is just a wrapper around the true application, but adds some logging like "Application started".
Then you change the name of your original app, and give the original name to your decoy.
As JimB mentions, you have the daemon write a PID file. You can tell if it's running or not by sending it a signal 0, via either the kill(2) system call or the kill(1) program. The return status will tell you whether or not the process with that PID exists.
Daemons should always:
1) Write the currently running instance's process to /var/run/$NAME.pid using getpid() (man getpid) or an equivalent command for your language.
2) Write a standard logfile to /var/log/$NAME.log (larger logfiles should be broken up into .0.log for currently running logs along with .X.log.gz for other logs, where X is a number with lower being more recent)
3) /Should/ have an LSB compatible run script accepting at least the start stop status and restart flags. Status could be used to check whether the daemon is running.
I don't know of a standard way of getting all the process names that have executed; there might be a way however to do this with SystemTap.
If you just want to monitor your process, I would recommend using waitid (man 2 wait) after the fork instead of detaching and daemonizing.
If your app has crashed, that's not distinguishable from "your app was never started", unless your app writes in the system log. syslog(3) is your friend.
To find your app you can try a number of ideas:
Look in the /proc filesystem
Run the ps command
Try killall appname -0 and check the return code