c++ - Replacing getpid with my own implementation -
I have an application where I have to write a new getpid function to replace the original one of the OS. The implementation will be the same as:
pid_t getpid (zero) {if (gi_PID! = -1) {return gi_PID; } Else {// OS level getpid () function}} How can I call the original getpid () implementation of OS via this function?
Edit: I tried:
pid_t getpid (zero) {if (gi_PID! = -1) {return gi_PID; } And {return _getpid (); }} As Jonathan suggested. G ++:
pid_t getpid () in function: 'SerendibPlugin.cpp: 882: Error: _getpid' undeclared (previously used this function) SerendibPlugin.cpp: 882: Error: (Each unauthorized identifier is reported only once for each function). Edit 2: I have managed to do this work by using the function pointer and using the "GETPID" ID using the DSCIM (RTLD_NEXT, "getpid"), the next second Setting in Symbol. Here is my sample code:
vi xx.c "xx.c" 23 lines, 425 characters #include & lt; Sys / types.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Iostream & gt; # Include & lt; Dlfcn.h & gt; using namespace std; Pid_t (* ___ getpid) (); Pid_t getpid (zero) {cout & lt; & Lt; "Mary Gatepid" & lt; & Lt; Endl; Cout & lt; & Lt; "PID:" & lt; & Lt; (* ___ GATEPID) () & lt; & Lt; Endl; Return (* ___ GATEPID) (); } Ent Main (zero) {___getpid = (pid_t (*) ()) dlsym (RTLD_NEXT, "getpid"); Pid_t p1 = getpid (); Printf ("% d \ n", (int) p1); Return (0); } G ++ xx.c -o xout
My GatePid PID: 7802 7802 _getpid () getpid () one getpid () . can be called in return
The first version of the answer __getpid () ;
This code works for me on Solaris 10 (SPARC) - with a C ++ compiler:
#include & lt; Sys / & gt types.h; # Include & lt; Unistd.h & gt; # Include & lt; Stdio.h & gt; Extern "C" pid_t _getpid (); Pid_t getpid (zero) {return (-1); } Int main (zero) {pid_t p1 = getpid (); Pid_t p2 = _getpid (); Printf ("% d vs% d \ n", (int) p1, (int) p2); Return (0); } This code works for me on Solaris 10 (SPARC) - with a C compiler:
BlackJL: cat xx.c # Include & lt; Sys / types.h & gt; # Include & lt; Unistd.h & gt; # Include & lt; Stdio.h & gt; Pid_t getpid (zero) {return (-1); } Int main (zero) {pid_t p1 = getpid (); Pid_t p2 = _getpid (); Printf ("% d vs% d \ n", (int) p1, (int) p2); Return (0); } Black JL: XX & amp; ./x cc xx.c -o xx "xx.c", line 13: warning: built-in function declaration: _getpid -1 vs. 29808 black jl:
Comments
Post a Comment