.:: Bots United ::.  
filebase forums discord server github wiki web
cubebot epodbot fritzbot gravebot grogbot hpbbot ivpbot jkbotti joebot
meanmod podbotmm racc rcbot realbot sandbot shrikebot soulfathermaps yapb

Go Back   .:: Bots United ::. > Developer's Farm > General Programming
General Programming Help others and get yourself helped here!

Reply
 
Thread Tools
How to detect dynamic library load/unload?
Old
  (#1)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default How to detect dynamic library load/unload? - 18-10-2004

I want to get my linux loadable library to run a function whenever it is loaded and unloaded.

I know how to to this in windows using the entry/exit function

BOOL WINAPI DllMain
( HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)

Under linux g++ I read that I should use these function macros

__init and __exit

I created two functions for my library based on examples I saw,

static void __init StartUp(void)
static void __exit ShutDown(void)

But these functions do not get called when dlopen is called or when the libary is unloaded.

Can anyone help me with this?


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#2)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: How to detect dynamic library load/unload? - 19-10-2004

Where did you read that ? I thought it was

Code:
static void init (void)
and

Code:
static void fini (void)
??

BTW. all of you can use the code tags now, they work



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#3)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: How to detect dynamic library load/unload? - 19-10-2004

I read about the __init and __exit macros from some website. You have to include <linux/init.h> for it to work, or so goes the story.

I tried init and fini as suggested but no luck.

Is there a compiler/linker option for this?


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#4)
botman
Super Moderator
 
Status: Offline
Posts: 280
Join Date: Jan 2004
Location: Plano, TX
Default Re: How to detect dynamic library load/unload? - 19-10-2004

Did you try "man dlopen"???

botman
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#5)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: How to detect dynamic library load/unload? - 19-10-2004

No, but :RTFM:was the obvious thing to do thanks for the suggestion.

FYI I found this, looks like init and fini are now obsolete and the last para is what I'll have to dig into:

The obsolete symbols _init and _fini
The linker recognizes special symbols _init and _fini. If a dynamic
library exports a routine named _init, then that code is executed after
the loading, before dlopen() returns. If the dynamic library exports a
routine named _fini, then that routine is called just before the
library is unloaded. In case you need to avoid linking against the
system startup files, this can be done by giving gcc the "-nostart-
files" parameter on the command line.

Using these routines, or the gcc -nostartupfiles or -nostdlib options,
is not recommended. Their use may result in undesired behavior, since
the constructor/destructor routines will not be executed (unless spe-
cial measures are taken).

Instead, libraries should export routines using the __attribute__((con-
structor)) and __attribute__((destructor)) function attributes. See
the gcc info pages for information on these. Constructor routines are
executed before dlopen returns, and destructor routines are executed
before dlclose returns.


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#6)
Pierre-Marie Baty
Roi de France
 
Pierre-Marie Baty's Avatar
 
Status: Offline
Posts: 5,049
Join Date: Nov 2003
Location: 46°43'60N 0°43'0W 0.187A
Default Re: How to detect dynamic library load/unload? - 19-10-2004

Google told me that

The C prototypes for these functions are:
Code:
  void __attribute__ ((constructor)) my_init (void);

  void __attribute__ ((destructor)) my_fini (void);
put the function names you want, but type the attributes verbatim. Tell me if it works because I'm interested in that issue too



RACC home - Bots-United: beer, babies & bots (especially the latter)
"Learn to think by yourself, else others will do it for you."
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#7)
Whistler
Summoner
 
Whistler's Avatar
 
Status: Offline
Posts: 1,499
Join Date: Feb 2004
Location: Mist Village
Default Re: How to detect dynamic library load/unload? - 23-10-2004

why not use the constructor/destructor in a global class ?
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#8)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: How to detect dynamic library load/unload? - 24-10-2004

why not use the constructor/destructor in a global class ?

I suppose that would work in some cases. It is the order of events that can be important.


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Re: How to detect dynamic library load/unload?
Old
  (#9)
botmeister
Ex-Council Member
 
botmeister's Avatar
 
Status: Offline
Posts: 1,090
Join Date: Nov 2003
Location: Canada
Default Re: How to detect dynamic library load/unload? - 24-10-2004

Tell me if it works because I'm interested in that issue too

It looks like the functions are being called, but my code is crashing. It's late, so I don't have time to check what's going wrong, but it looks like I'm calling things that are not yet initialized which is causing the crash.

The order in which things happen are very important, and I'll have to figure this all out when I get the time.


Maker of the (mEAn) Bot.Admin Manager

"In theory, there is no difference between theory and practice. But, in practice, there is." - Jan L.A. van de Snepscheut
  
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com