HTTP connection just before program exits in a DLL using Curl - c

I'm writting a DLL in ANSI C and I'm using Curl to make HTTP connections.
This DLL should be able to connect to my server to send some info just before the application terminates. I would like to know what is the best approach to achieve this. I have tried registering a callback function using atexit and also calling the connectToServer method in a destructor. I also tried to use the DllMain with the DLL_PROCESS_DETACH. On all cases, Curl is unable to connect to server(error code 7) because windows already unloaded required libraries used by Curl.
I have heard people saying the only way to achieve this on Windows is to create a separate process(watchdog) to monitor when the main process terminates and then open the connection in this other process.
The library I'm writting works both on Linux (.so) and Windows (.dll). On Linux, i'm using atexit and everything works fine. I would prefer not to use a watchdog process since I won't use it on the .so.
Is there a way to do this?
Thanks in advance.

You shouldn't do anything in DLL_PROCESS_DETACH for exactly the reasons you've found. The best way to handle this is an explicit call from the application to say "I'm shutting down, do any clean up needed".

If you really want to mess up the semantics of DLL_PROCESS_DETACH, you could manually use LoadLibrary to load the curl library directly from there, regardless of the existing linker setup, and call it through your own late binding.

Related

How does a Client (Written in C) listens to a request on Linux OS?

I am a beginner and I want to understand some basics of Client-Server applications written in C (I understand web based server-client applications written in Java, PHP ,etc).
To be more specific, I am talking about ZABBIX which is a client-server tool (Zabbix_server is a server and zabbix_agentd is a client, both written in C).
Zabbix_server asks zabbix_agentd for data and zabbix_agents responds accordingly.
I have imported zabbix codes on Eclipse and have tried to debug to understand codes. A complete execution of zabbix_agentd on Eclipse just launched the daemon successfully and created a child process. But when I check the log, I find that different functions are being called which had not come in the way while debugging. It means, these functions are called by some process, may be inetd, etc. (correct me if i am wrong). I tried to find inetd on RHEL 6.4 OS, but it was no where found (using "service inetd status", "find").
So, how those functions are being called? Can anyone please give me ideas about that?
Please suggest me how to use those calling requests to further debug my client application.
One thing I have noticed while debugging Eclipse is that, I get below error after calling fork():
No source available for "fork() at 0x36ca0acbc0"
I couldn't understand the impact of the above error as the daemon keeps on working after this error also.
I browsed through similar error different people getting and found that their binaries were not linked to sources. However, my binaries are linked to sources also. I mean, expanding the binary of zabbix_agentd shows several *.h and *.c files.
As this error terminates the debugger immediately but the line of code at this occurrence is the last line of the source file also. So, I cannot say if I need to take care for this error.
Is this error the reason why I am not able to view other functions being called?
Please let me know if more information is required.
Thanks in advance.
Regards,
Rohit

How to handle Win32 Application termination

I have an Win32 application with no window written in C.
My question is: is there any way to handle the termination of my application. Ex. closing it from the task manager or via the console.
It is unclear from the question, but if this is a console mode application then you can call SetConsoleCtrlHandler to install a callback that Windows will call just before it terminates your app. Beware that this callback runs on a separate thread and that you have to complete the callback function quickly.
If it is a native Windows program that just doesn't create a window then you really do need a window to get notifications like this. Which is not a problem, it doesn't have to be visible. Just don't call ShowWindow().
Note that atexit() as mentioned will not work, these are rude aborts you are talking about that don't let the program go through its normal shutdown sequence.
You might like to take a look at the atexit() function (http://msdn.microsoft.com/en-us/library/tze57ck3%28v=vs.100%29.aspx).
Using this function you can install handlers which are called when the program terminates.

HINTERNET InternetOpenUrl() on linux C

I am a native windows programmer and I was just starting to develop some applications on linux. I was wondering if there is a linux function similar to InternetOpenUrl(). I wanted to use this so I could open the Speakeasy speed test to download random images according to size to "speed test" internet connections. If there isn't a similar command does anyone have some C code for initiating a http socket to a server to download a file (or code to do the same on a ftp although i rather not use ftp). I would rather use the InternetOpenUrl() alternative if there is one but i am also open to other methods as well.
You're looking for libcurl, which incidentally also works on Windows.

How do I stop the Console Window from appearing when making system calls?

I am using C and GTK+ for a gui project. For my project I have to make a lot systems calls. e.g system("copy myfile urfile"); or system("mp3player -embed filename") etc. and whenever my program calls the system an annoying console window appears and stays visible until the command is completely carried out. How do I hide that console window? Thanks.
NOTE: For my project I can use GTK+ , the C Standard Library , GLib, WinApi (not recommended) and system calls.
SCREENSHOT:
Using system() to create a new process does this by first starting a new command interpreter, which then in turn executes the command passed to system().
The command interpreter opens the console window.
Thus to avoid opening such a console windows you need to avoid starting the command interpreter. To do the latter try a function out of the spawn family (http://msdn.microsoft.com/en-us/library/20y988d2.aspx) or use the win32 api function CreateProcess() (http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425) to directly start your application.
A side effect on doing so might be, that your application starts faster and also uses less system resourses.
In fact the above mentioned solutions will not work for system calls which explicitly need a command interpreter as in one of your examples (copy src dst).
Use the GLib processes spawning functions instead of platform-specific functions. For file copy you can use g_file_copy from GIO (shipped with GTK) instead of calling the cp or copy command. This makes your code more portable too.

Wait for file to be unlocked - Windows

I'm writing a TFTP server program for university, which needs exclusive access to the files it opens for reading. Thus it can be configured that if a file is locked by another process that it waits for the file to become unlocked.
Is there any way on Win32 to wait for a file become unlocked without creating a handle for it first?
The reason I ask, is that if another process calls CreateFile() with a dwShareMode that is incompatible to the one my process uses, I won't even be able to get a file handle to use for waiting on the lock using LockFileEx().
Thanks for your help in advance!
If you take a look at the Stack Overflow questions What Win32 API can be used to find the process that has a given file open? and SYSTEM_HANDLE_INFORMATION structure, you will find links to code that can be used to enumerate processes and all open handles of each running process. This information can be used to obtain a HANDLE to the process that has the file open as well as its HANDLE for the file. You would then use DuplicateHandle() to create a copy of the file HANDLE, but in the TFTP process' handle table. The duplicated HANDLE could then be used by the TFTP process with LockFileEx().
This solution relies on an internal function, NtQuerySystemInformation(), and an undocumented system information class value that can be used to enumerate open handles. Note that this feature of NtQuerySystemInformation() "may be altered or unavailable in future versions of Windows". You might want to use an SEH handler to guard against access violations were that to happen.
As tools from MS like OH and Process Explorer do it, it is definitely possible to get all the handles opened by a process. From there to wait on what you'd like the road is still long, but it is a beginning :)
If you have no success with the Win32 API, one place to look at is for sure the NT Native API http://en.wikipedia.org/wiki/Native_API
You can start from here http://msdn.microsoft.com/en-us/library/windows/desktop/ms724509%28v=vs.85%29.aspx and see if it works with the SystemProcessInformation flag.
Look also here for a start http://nsylvain.blogspot.com/2007/09/how-list-all-open-handles.html
The native API is poorly documented, but you can find resources online (like here http://www.osronline.com/article.cfm?id=91)
As a disclaimer, I should add that the Native API is somehow "internal", and therefore subject to change on future versions. Some functions, however, are exposed also publicly in the DDK, at kernel level, so the likelihood of these functions to change is low.
Good luck!

Resources