use of softlink and argv[0]

This is a discussion on use of softlink and argv[0] within the C Programming forums, part of the General Programming Boards category; On linux, if I add a softlink to an executable: Code: ln -s a.out myprog When myprog is executed (really ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    use of softlink and argv[0]

    On linux, if I add a softlink to an executable:
    Code:
    ln -s a.out myprog
    When myprog is executed (really a softlink to a.out), argv[0] is the name of the softlink. This means a single, pre-compiled binary can respond to the name of the softlink via which it is invoked (which could be other than the actual name of the executable).

    I am almost positive this will be true on all other unix style filesystems, but I just wanted to check with anyone who might know for sure before I incorporate this potential into something that will be used cross-platform.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I believe (but can't quite seem to verify) that this behavior is based on the runtime library, so anybody running GNU libc would see the same behavior. (Certainly this machine running Mac OSX exhibits the same behavior.)

    This is a very common thing (so, for instance, vi/vim/ex are linked to the same executable, and modes are set based on the command line; same with tex/latex/pdf[la]tex/etc.) in the unix world, so I would feel comfortable going ahead.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yay, thanks. I am kind of thrilled I was asked to do this, so I am being a little paranoid about all the angles.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    Yay, thanks. I am kind of thrilled I was asked to do this, so I am being a little paranoid about all the angles.
    And to add to the list of "executable is the same file with soft links":
    g++ -> gcc
    If it's good enough for gcc, it should be good enoung for you.

    I'm not sure, but I think umount and mount are the same way too.

    Obviously, it won't work that way in Windows, because ln -s doesn't exist (although there is a way to make links in NTFS).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I was mostly asking because The Application In Question will end up in some BSD distro (or so I'm told), which I've never used. I notice that FreeBSD is GNU C. If tabstop says MacOS respects this too then it must have Pretty Old Roots.

    I suppose I should check with my peeps about this, but you guys have reassured me enough that I'm willing to go ahead now. Thanks!!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    This is always true, and in fact the "rsh" program uses this to do a goofy trick -- if you symlink rsh to "hostname," then invoking it through the link will automatically connect to the named host.

    The runtime library COULD resolve the name to a canonical name, but it has no business doing so, and any runtime which does so is broken.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    methinks it's the command intrepreter ie the shell that does the trick for you so it's transparent to the user process

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    methinks it's the command intrepreter ie the shell that does the trick for you so it's transparent to the user process
    Indeed, I agree with that. But like brewbuck says, should the command interpreter (shell) decide to resolve the link and find the actual file, and replace argv[0] with that, it would break MANY applications that rely on this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    So this should be pretty much foolproof, is what you are saying.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MK27 View Post
    So this should be pretty much foolproof, is what you are saying.
    Yes, on any Unix-based OS it should be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by matsp View Post
    Indeed, I agree with that. But like brewbuck says, should the command interpreter (shell) decide to resolve the link and find the actual file, and replace argv[0] with that, it would break MANY applications that rely on this.

    --
    Mats
    IMO the shell fork()'s a copy and calls exec()'s thereby overlaying the shell copy with a copy of the program and it's inside the exec() call that the shell follows the symlink to the program it actually points too.
    Last edited by itCbitC; 02-24-2009 at 02:28 PM.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by itCbitC View Post
    IMO the shell fork()'s a copy and calls exec()'s thereby overlaying the shell copy with a copy of the program and it's inside the exec() call that the shell follows the symlink to the program it actually points too.
    The shell doesn't follow the symlink. It doesn't care if it's a link or not. The OS is what resolves the link.

    Applications should never know, nor care, whether they are working with links or real files. That defeats the purpose of links.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by brewbuck View Post
    The shell doesn't follow the symlink. It doesn't care if it's a link or not. The OS is what resolves the link.
    I may have this wrong but I thought it was the shell that issued the open() for exec'ing on the symlink making it to follow that to the actual program.
    Quote Originally Posted by brewbuck View Post
    Applications should never know, nor care, whether they are working with links or real files. That defeats the purpose of links.
    Yes that's the purpose of symlinks ie to be transparent to the application.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by itCbitC View Post
    I may have this wrong but I thought it was the shell that issued the open() for exec'ing on the symlink making it to follow that to the actual program.

    Yes that's the purpose of symlinks ie to be transparent to the application.
    The shell just calls exec(), it does not open anything. The translation of the symlink is done by the kernel -- the shell has no idea it happens.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by brewbuck View Post
    The shell just calls exec(), it does not open anything. The translation of the symlink is done by the kernel -- the shell has no idea it happens.
    thanks for clarifying!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-10-2009, 01:14 PM
  2. argv[0] contains command name -- why?
    By Billy123 in forum Linux Programming
    Replies: 3
    Last Post: 04-12-2008, 11:53 AM
  3. main() ; argv[1] = string at argv[0]???
    By UCnLA in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 AM
  4. argv[0] with WinMain()
    By dwks in forum Windows Programming
    Replies: 4
    Last Post: 03-05-2006, 11:41 AM
  5. change argv[0] after fork
    By garion in forum C Programming
    Replies: 2
    Last Post: 02-05-2006, 09:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21