Hijack main function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ravit
    New Member
    • Sep 2007
    • 11

    Hijack main function

    Hello everybody,

    Recently I got to do some initialization work just before first executable statement of every user C application.

    For that,
    Can we hijack the main function and write a wrapper around main? using LD_PRELOAD ??

    The solution should be a portable one.

    thanks in advance.


    --
    Ravi.T
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Hijack the main function? The main function is already being called by the CRT. Which in itself is implementation specific. So unless you want to disable the CRT and reimplement everything yourself...I think you can see where I'm going with this.

    Instead of trying to come up with a solution yourself, why don't you describe what you want in the end, what constraints you have to work with, and perhaps we can suggest solutions.

    Comment

    • ravit
      New Member
      • Sep 2007
      • 11

      #3
      Ok.

      I have a function foo() which I have to execute before any statement in the user application. then give the control to the user application.

      I can give user a library which he can link to his application while preparing executable. With that facility I have to make the application to run my function foo() before any executable statement.

      Should be portable to all Unix-like platforms.


      I think the problem definition is clear. If not I please ask details.


      --
      Ravi.T

      Comment

      • oler1s
        Recognized Expert Contributor
        • Aug 2007
        • 671

        #4
        Actually, your explanation isn't clear, so I'm making a few educated guesses based on what you have said.

        As I understand it, you have a library and API you want others to use. One of the conditions for using that API is that there is a function foo() that must with certainty be called before any other API calls.

        --- STOP HERE if I made a wrong guess. If you are trying to do something more fundamental like replace something in the CRT, then it's a whole lot more complicated ---

        In any case, the solution is to inform the user about what he has to do. This should be in your API instructions.

        You can't force the foo function to be called first. It's, as you might imagine, someone else's code. And with a bit of thought, you can see how you can't randomly hijack other people's code.

        Comment

        • ashitpro
          Recognized Expert Contributor
          • Aug 2007
          • 542

          #5
          Have a look at "Linux Module Programming"

          Every module has two sections
          Say you module is test.o

          initialize()
          {
          //Do your pre-formating here
          //Call the application or Your_main_funct ion
          }
          cleanup()
          {

          }
          Your_main_funct ion()
          {

          }
          module_init(ini tialize); //registering functions
          module_cleanup( cleanup);


          Once you load this module, It will start executing initialize section
          Here solution resides.
          You can load this module as: insmod test.o and remove by: rmmod test.o

          Comment

          • ravit
            New Member
            • Sep 2007
            • 11

            #6
            Not specific to Linux.
            May be ported on AIX,SOlaris or anything of like Unix.

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              I'm with oler1s on this.

              have a clear defined API and make sure the people using your code/library are aware that they need to call the initialisation routine as the first thing in their main.

              Comment

              • ashitpro
                Recognized Expert Contributor
                • Aug 2007
                • 542

                #8
                Originally posted by ravit
                Not specific to Linux.
                May be ported on AIX,SOlaris or anything of like Unix.
                Loadable module support is applicable for all POSIX complaint environments.
                All you need is to make your module versatile to handle different platforms.

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  You have to write a shell with your main() and its call to foo(). Then you start the C application.

                  Everyone has to use your shell.

                  That's what Bill Gates did. Initially his main() was inside Windows. It was called by DOS. Bill's main() called foo() and a bunch of ther stuff and then called WinMain to start the application.

                  The user wrote WinMain().

                  So there is a precedent.

                  Too bad you aren't using C++. Since global variables are required to be created before main() starts you could call foo() from inside the constrcutor of a global object. That code would run before main(). All you would have to do is declare this global object somewhere in the program.

                  Comment

                  Working...