Thread: Retrieving binary file name from a process

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    124

    Retrieving binary file name from a process

    This is a somewhat complicated question.

    Is there anyway for a currently running process to identify the full path to the binary file that launched it?

    Basically, I have this binary file which can be installed anywhere in the UNIX file system, and it creates a running process. I need to be able to find the binary file that created the process. Because it can be anywhere, I can't hard code the path. Because this is a C program, I can't use a shell command like "find". So it has to be a C API call or something like that. Does anyone know of such a function?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you can trust your shell, then look in argv[0].
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    That's a very good idea actually. I totally forgot about that. I'm so used to always using argv[1] and so on just to get the arguments I need that I totally forgot that argv[0] actually has something occasionally useful. Thanks for the help.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    124
    I realized that I misstated my question.

    1) A binary executable file in a random location

    2) A shared library (akin to a DLL in Windows) in a random location

    3) A process that is currently running, launched from the binary executable that invokes routines in the shared library

    I need the routine in the shared library to know where the location of it's shared library is within the UNIX file system. argv[0] will not work because that will give me the EXE and not the DLL.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    /etc/ld.so.conf contains (indirectly) the list of places to search (in addition to /usr/lib)

    If you do
    man ld
    This lists other search strategies as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    8
    The name of the current executable can be found at "/proc/self/exe". Use readlink() to the get path.

    Use dladdr() to get the path to a shared library. Try "man dladdr".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching Binary Files for a Pattern
    By CaptainMorgan in forum C Programming
    Replies: 16
    Last Post: 06-17-2007, 06:04 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM