getting main's argv[0] - Unix

This is a discussion on getting main's argv[0] - Unix ; Hi all Is there a usual way of accessing main's argv[0] from other functions, or some string containing the same thing, but without passing an additional parameter or defining my own global? Gnu provides program_invocation_name, but what about *BSD or ...

+ Reply to Thread
Results 1 to 14 of 14

Thread: getting main's argv[0]

  1. getting main's argv[0]

    Hi all

    Is there a usual way of accessing main's argv[0] from other functions, or
    some string containing the same thing, but without passing an additional
    parameter or defining my own global?

    Gnu provides program_invocation_name, but what about *BSD or others?

    If there is no general way, what are the system specific methods?

    Thanks

    viza


  2. Re: getting main's argv[0]

    On October 9, 2008 11:48, in comp.unix.programmer, viza
    (tom.viza@gm-il.com.obviouschange.invalid) wrote:

    > Hi all
    >
    > Is there a usual way of accessing main's argv[0] from other functions, or
    > some string containing the same thing, but without passing an additional
    > parameter or defining my own global?


    There is no general way to do this.

    > Gnu provides program_invocation_name, but what about *BSD or others?
    >
    > If there is no general way, what are the system specific methods?


    In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
    contains a list of argv strings

    FWIW, argv[0] is not guaranteed to be the name of the program invoked.
    It /is/ the first argument value passed to the exec() family of functions,
    and /by convention/ is the name of the program. However, there is
    no /requirement/ that argv[0] name a program (witness the argv[0] of your
    login shell, which is typically named '-', no matter /which/ binary is
    used).

    HTH
    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------



  3. Re: getting main's argv[0]

    Hi

    On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    > On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:


    >> Is there a usual way of accessing main's argv[0] from other functions,
    >> or some string containing the same thing, but without passing an
    >> additional parameter or defining my own global?
    >>
    >> Gnu provides program_invocation_name, but what about *BSD or others?
    >>
    >> If there is no general way, what are the system specific methods?

    >
    > In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
    > contains a list of argv strings


    I'm hoping for a symbol (variable or function) that the gives the pointer
    just after the start of the stack where this might be found. This varies
    depending on architecture so perhaps the various libc's define something.

    Any ideas?


    > FWIW, argv[0] is not guaranteed to be the name of the program invoked.


    Yes I realise that, all I want is a name by which the program should
    identify itself in logs.

    Thanks
    viza

  4. Re: getting main's argv[0]

    viza wrote:
    > Yes I realise that, all I want is a name by which the program should
    > identify itself in logs.


    Do we know that that which launches the executable will "normalize"
    the path used to launch it? If it is launched as "foo" and then later
    launched as "/usr/local/bin/foo" will argv[0] match in both cases? If
    not will your application "parse" argv[0] to do the normalization
    itself?

    Will your program append other information to uniquely identify two
    concurrent intsances of "foo" running?

    It sounds like the most portable thing to do is to compile-in a
    default id and allow an overrid via the command line or config file.

    rick jones
    --
    The computing industry isn't as much a game of "Follow The Leader" as
    it is one of "Ring Around the Rosy" or perhaps "Duck Duck Goose."
    - Rick Jones
    these opinions are mine, all mine; HP might not want them anyway...
    feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...

  5. Re: getting main's argv[0]

    viza writes:

    > Hi
    >
    > On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    >> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:

    >
    >>> Is there a usual way of accessing main's argv[0] from other functions,
    >>> or some string containing the same thing, but without passing an
    >>> additional parameter or defining my own global?
    >>>
    >>> Gnu provides program_invocation_name, but what about *BSD or others?


    It looks like some flavors of BSD have a function '_getprogname()' and a
    variable '__progname'. It seems to be undocumented and could change.

    >>> If there is no general way, what are the system specific methods?

    >>
    >> In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
    >> contains a list of argv strings

    >
    > I'm hoping for a symbol (variable or function) that the gives the pointer
    > just after the start of the stack where this might be found. This varies
    > depending on architecture so perhaps the various libc's define something.


    I don't think so. You might be able to unwind the stack by chasing the
    frame pointers, but some compiler options might make this impossible.

    > Any ideas?
    >
    >
    >> FWIW, argv[0] is not guaranteed to be the name of the program invoked.

    >
    > Yes I realise that, all I want is a name by which the program should
    > identify itself in logs.


    I presume you're writing a library, so that main() is not your function.
    I think your best bet is to make the program tell you: provide a
    set_program_name() function or something that the user is expected to
    call in main(). It's awkward, yes, but I think anything else is going
    to be very painful and unreliable.

  6. Re: getting main's argv[0]

    On Thu, 09 Oct 2008 15:48:54 GMT, viza wrote:
    > Hi all
    >
    > Is there a usual way of accessing main's argv[0] from other functions,
    > or some string containing the same thing, but without passing an
    > additional parameter or defining my own global?
    >
    > Gnu provides program_invocation_name, but what about *BSD or others?


    The BSDs include a getprogname() and setprogname() pair of functions.


  7. Re: getting main's argv[0]

    On Thu, 09 Oct 2008 11:51:06 -0700, Nate Eldredge wrote:
    >viza writes:
    >>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    >>>On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
    >>>> Is there a usual way of accessing main's argv[0] from other functions,
    >>>> or some string containing the same thing, but without passing an
    >>>> additional parameter or defining my own global?
    >>>>
    >>>> Gnu provides program_invocation_name, but what about *BSD or others?

    >
    > It looks like some flavors of BSD have a function '_getprogname()' and a
    > variable '__progname'. It seems to be undocumented and could change.


    Please don't use the `internal' _getprogname() and __progname symbols,
    but their `public' version: getprogname(). They are defined by stdlib.h
    in the BSDs and their manpage states:

    HISTORY
    These functions first appeared in NetBSD 1.6, and made their way
    into FreeBSD 4.4.

    For older versions of FreeBSD you can always define a program-specific
    global, but if you are still using such an old release, the lack of
    getprogname() is likely to be a very minor source of trouble. The
    releases that are older than 4.4 have been superseded by more modern
    FreeBSD releases, and they are very likely to have security bugs that
    are unfixed.


  8. Re: getting main's argv[0]

    Giorgos Keramidas wrote:

    >> Is there a usual way of accessing main's argv[0] from other functions,
    >> or some string containing the same thing, but without passing an
    >> additional parameter or defining my own global?
    >>
    >> Gnu provides program_invocation_name, but what about *BSD or others?

    >
    > The BSDs include a getprogname() and setprogname() pair of functions.


    And Solaris has getexecname. Now googling for the 3 variants leads to a
    piece of code in mesa (the graphics library) that defines a macro
    GET_PROGRAM_NAME as a wrapper to these.

  9. Re: getting main's argv[0]

    Giorgos Keramidas writes:

    > On Thu, 09 Oct 2008 11:51:06 -0700, Nate Eldredge wrote:
    >>viza writes:
    >>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    >>>>On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
    >>>>> Is there a usual way of accessing main's argv[0] from other functions,
    >>>>> or some string containing the same thing, but without passing an
    >>>>> additional parameter or defining my own global?
    >>>>>
    >>>>> Gnu provides program_invocation_name, but what about *BSD or others?

    >>
    >> It looks like some flavors of BSD have a function '_getprogname()' and a
    >> variable '__progname'. It seems to be undocumented and could change.

    >
    > Please don't use the `internal' _getprogname() and __progname symbols,
    > but their `public' version: getprogname(). They are defined by stdlib.h
    > in the BSDs and their manpage states:


    Thanks. I was looking at the source for syslog and crt0 which use the
    internal ones, so I didn't see the public version. Obviously that is
    preferable.

  10. Re: getting main's argv[0]

    On Oct 9, 5:19*pm, Nate Eldredge wrote:
    > Giorgos Keramidas writes:
    > > On Thu, 09 Oct 2008 11:51:06 -0700, Nate Eldredge wrote:
    > >>viza writes:
    > >>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    > >>>>On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
    > >>>>> Is there a usual way of accessing main's argv[0] from other functions,
    > >>>>> or some string containing the same thing, but without passing an
    > >>>>> additional parameter or defining my own global?

    >
    > >>>>> Gnu provides program_invocation_name, but what about *BSD or others?

    >
    > >> It looks like some flavors of BSD have a function '_getprogname()' anda
    > >> variable '__progname'. *It seems to be undocumented and could change..

    >
    > > Please don't use the `internal' _getprogname() and __progname symbols,
    > > but their `public' version: getprogname(). *They are defined by stdlib.h
    > > in the BSDs and their manpage states:

    >
    > Thanks. *I was looking at the source for syslog and crt0 which use the
    > internal ones, so I didn't see the public version. *Obviously that is
    > preferable.




    It's not going to change while a program is running. You would get
    exactly what you want by storing a copy of argv[0] in a *gasp* global
    variable when the program starts, and referring to it elsewhere.

    If you are using it for logging and it makes you feel more
    comfortable, simply have some concept of "initializing" logging on
    program startup and store the value of argv[0] there.

    Jason

  11. Re: getting main's argv[0]

    On Sat, 11 Oct 2008 14:38:30 -0700, Jason C wrote:
    > On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:


    >> Is there a usual way of accessing main's argv[0] from other
    >> functions, or some string containing the same thing, but without
    >> passing an additional parameter or defining my own global?


    > It's not going to change while a program is running. You would get
    > exactly what you want by storing a copy of argv[0] in a *gasp* global
    > variable when the program starts, and referring to it elsewhere.


    As Nate correctly identified my library cannot modify every application's
    main().

  12. Re: getting main's argv[0]

    In article ,
    viza wrote:
    >Hi
    >
    >On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    >> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:

    >
    >>> Is there a usual way of accessing main's argv[0] from other functions,
    >>> or some string containing the same thing, but without passing an
    >>> additional parameter or defining my own global?
    >>>
    >>> Gnu provides program_invocation_name, but what about *BSD or others?
    >>>
    >>> If there is no general way, what are the system specific methods?

    >>
    >> In a Linux 2.6 system, you could open and read /proc/self/cmdline, which
    >> contains a list of argv strings

    >
    >I'm hoping for a symbol (variable or function) that the gives the pointer
    >just after the start of the stack where this might be found. This varies
    >depending on architecture so perhaps the various libc's define something.
    >
    >Any ideas?
    >
    >
    >> FWIW, argv[0] is not guaranteed to be the name of the program invoked.

    >
    >Yes I realise that, all I want is a name by which the program should
    >identify itself in logs.
    >
    >Thanks
    >viza


    It sounds like you've got your answer, which is: There's no (POSIX)
    portable way to do it, but there are (differing) ways to do it in many
    of the major Unix flavors in common use today. The fact that you are
    writing a library, not an application, means that you can't get "inside
    main()" to do any dirty work.

    So, your best bet is to "ifdef" for the major Unix flavors in common use
    today.

    BTW, in Linux, I find /proc/self/exe to be very useful.


  13. Re: getting main's argv[0]

    Hi

    On Tue, 14 Oct 2008 14:08:13 +0000, Kenny McCormack wrote:
    > wrote:
    >>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    >>> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:

    >>
    >>>> Is there a usual way of accessing main's argv[0] from other
    >>>> functions, or some string containing the same thing, but without
    >>>> passing an additional parameter or defining my own global?


    > It sounds like you've got your answer, which is: There's no (POSIX)
    > portable way to do it, but there are (differing) ways to do it in many
    > of the major Unix flavors in common use today. The fact that you are
    > writing a library, not an application, means that you can't get "inside
    > main()" to do any dirty work.
    >
    > So, your best bet is to "ifdef" for the major Unix flavors in common use
    > today.
    >
    > BTW, in Linux, I find /proc/self/exe to be very useful.


    Since I want to use OpenBSD which doesn't seem to have any of the methods
    mentioned I guess I'll have to have a function that copies the name, and
    require the user to call it from main. I do so hate forcing clutter on
    applications' main. :-(.

    Thanks for all the answers.

    viza

  14. Re: getting main's argv[0]

    viza writes:

    > Hi
    >
    > On Tue, 14 Oct 2008 14:08:13 +0000, Kenny McCormack wrote:
    >> wrote:
    >>>On Thu, 09 Oct 2008 12:16:55 -0400, Lew Pitcher wrote:
    >>>> On October 9, 2008 11:48, in comp.unix.programmer, viza wrote:
    >>>
    >>>>> Is there a usual way of accessing main's argv[0] from other
    >>>>> functions, or some string containing the same thing, but without
    >>>>> passing an additional parameter or defining my own global?

    >
    >> It sounds like you've got your answer, which is: There's no (POSIX)
    >> portable way to do it, but there are (differing) ways to do it in many
    >> of the major Unix flavors in common use today. The fact that you are
    >> writing a library, not an application, means that you can't get "inside
    >> main()" to do any dirty work.
    >>
    >> So, your best bet is to "ifdef" for the major Unix flavors in common use
    >> today.
    >>
    >> BTW, in Linux, I find /proc/self/exe to be very useful.

    >
    > Since I want to use OpenBSD which doesn't seem to have any of the methods
    > mentioned I guess I'll have to have a function that copies the name, and
    > require the user to call it from main. I do so hate forcing clutter on
    > applications' main. :-(.


    OpenBSD does have an undocumented variable __progname, from looking at
    the source. It's not in any of the header files so you could declare it
    yourself:

    extern char *__progname;

    Of course, it could go away, etc, but at least you'd know at compile
    time. It's possible but unlikely that they'd keep the variable but
    change the meaning.

    You might consider filing a feature request for OpenBSD to implement and
    document the getprogname() interface to match other BSDs.



+ Reply to Thread