Getting the executable name from the code - Linux

This is a discussion on Getting the executable name from the code - Linux ; Hi, Is there a way to find the binary name from any point of execution of the code executed by the corresponding binary (say where I dont have access to argv local variable of main). Can we have a solution ...

+ Reply to Thread
Results 1 to 8 of 8

Thread: Getting the executable name from the code

  1. Getting the executable name from the code

    Hi,

    Is there a way to find the binary name from any point of execution of
    the code executed by the corresponding binary (say where I dont have
    access to argv local variable of main).

    Can we have a solution independent of the shell used to invoke it and
    also if not, a solution dependent on shell, when the shell I use to
    invoke the binary is bash.


    Thanks
    Reji

  2. Re: Getting the executable name from the code

    Reji wrote:
    > Is there a way to find the binary name from any point of execution of
    > the code executed by the corresponding binary (say where I dont have
    > access to argv local variable of main).


    No, not even reliably if you now argv[]. Already what's stored in
    argv[0] is not necessary the name of the program (e.g. if it was
    started from another process that process can set argv[0] to some-
    thing else than the name of the program. And even if it's the name
    of the program it may not contain the full path. And even if con-
    tains the full path the program file could already be deleted (and
    it also could be just a symbolic link).

    > Can we have a solution independent of the shell used to invoke it and
    > also if not, a solution dependent on shell, when the shell I use to
    > invoke the binary is bash.


    A process need to have been started by a shell. And even if it
    was the shell would not tell you.

    What do you want to achieve? Why do you want to know the name of
    the program? The typical reply is that it's for storing some con-
    figuration file at the same location as the programs executable.
    But that's the wrong way under UNIX. First of all, the program
    typically does not have the permissions to write to that place.
    And then different users may want to store different configura-
    tions. So the way to go is to create a file in the users home
    directory (the name is often the programs name with a dot in
    from of it).

    If you really need to know a (Linux-specific) solution is to
    check what the symbolic link '/proc/self/exe' is pointing to.

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  3. Re: Getting the executable name from the code

    > If you really need to know a (Linux-specific) solution is to
    > check what the symbolic link '/proc/self/exe' is pointing to.


    This works in most cases. However, if all the pages from the original
    execve() have been munmap(), then the Linux kernel removes the link
    /proc/self/exe. Some program compression schemes run into this
    (for example, http://upx.sourceforge.net/ ) because the decompressor
    removes all evidence that compression was used.

    For over a decade the Linux kernel has stored the pathname that was
    specified to execve() in the address space of the new process, at
    (1+ strlen(envp[-1+ n_env]) + envp[-1+ n_env]). If you have access
    to the original environment array, and no putenv[] has been done,
    and there has been no overwriting, then that expression works.

    I got tired of all the objections, so I submitted a kernel patch
    which adds a new element AT_EXECFN to the array of Elf32_auxv_t.
    Look in your favorite archive of LKML for my message of 2008-05-01,
    "execve filename: document and export via auxiliary vector".
    This patch is now in the -mm tree of pending kernel patches.
    [Now you just have to get access to the auxiliary vector;
    ask the maintainer of glibc. An excellent mechanism would be
    "main(argc, argv, envp, auxv)".]

    One very legitimate use of the pathname is to be the source
    for expanding the dynamic string token "$ORIGIN" in DT_RUNPATH,
    so that an executable and its shared libraries can be relocated easily
    in a filesystem. [A Solaris hack copied by glibc; never documented.]
    The AT_EXECFN token also is the perfect place for a virtualizer
    program to adjust the environment.

    --

  4. Re: Getting the executable name from the code

    Reji writes:

    > Is there a way to find the binary name from any point of execution of
    > the code executed by the corresponding binary (say where I dont have
    > access to argv local variable of main).
    >
    > Can we have a solution independent of the shell used to invoke it and
    > also if not, a solution dependent on shell, when the shell I use to
    > invoke the binary is bash.


    In addition to what other people have replied, there's also another
    GNU-specific alternative:

    program_invocation_name
    program_invocation_short_name

    These are basically argv[0] and basename(argv[0]), respectively, with
    all the caveats that implies.

    info libc 'Error Messages' for the manual.


    Regards,
    Roger

    --
    .''`. Roger Leigh
    : :' : Debian GNU/Linux http://people.debian.org/~rleigh/
    `. `' Printing on GNU/Linux? http://gutenprint.sourceforge.net/
    `- GPG Public Key: 0x25BFB848 Please GPG sign your mail.

  5. Re: Getting the executable name from the code

    John Reiser writes:

    > [Now you just have to get access to the auxiliary vector;


    On recent kernels, one can read /proc/self/auxv to gain that access.

    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  6. Re: Getting the executable name from the code

    On Jul 8, 4:48*am, Reji wrote:

    > Is there a way to find the binary name from any point of execution of
    > the code executed by the corresponding binary (say where I dont have
    > access to argv local variable of main).


    You have to be way more specific. For example, if the binary has been
    renamed since execution began, do you want the old name or the new
    name?

    >*Can we have a solution independent of the shell used to invoke it and
    > also if not, a solution dependent on shell, when the shell I use to
    > invoke the binary is bash.


    There are all kinds of ways to get this information, but the problem
    is incompletely specified.

    DS

  7. Re: Getting the executable name from the code

    Thanks for all the replies. I feel proc entry should solve my issue.

    In my scenario the binary name is not going to be modified since its
    an embedded system.
    I wanted to get the name of the executable in a library function in a
    shared library and I dont have access to executable code to set any
    env variables to be used by the library.


  8. Re: Getting the executable name from the code

    On 2008-07-08, Reji wrote:

    > Can we have a solution independent of the shell used to invoke it and
    > also if not, a solution dependent on shell, when the shell I use to
    > invoke the binary is bash.


    getenv("_"); will work with bash.

    Bye.
    Jasen

+ Reply to Thread