Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

One thing that has been puzzling me for some time is this:

% which halt
/sbin/halt
% file /sbin/halt
/sbin/halt: symbolic link to `reboot'

However, executing sudo halt does, of course, not reboot the system. Why is that?

There are several other programs working that way, for example pdflatex.

share|improve this question

1 Answer 1

up vote 12 down vote accepted

Every program can see the full command line that was used to run it (except for wildcards and variables, which the shell expands).

In a C program, the command line is stored in argv, which is short for argument vector.
The progam's name is the first element of argv, i.e. argv[0].

Clearly in the case of halt and reboot, the program is changing its behavior based on argv[0].

From bash, you can see the full command line used to run a program using ps -p <pid> -o cmd or cat /proc/<pid>/cmdline.

Note that there is another type of link called a hard link that will have the same effect. On my system for example, sudo and sudoedit are the same file with two different names, and different behaviors.

ls -i can help you find those commands, e.g.:

$ ls -il | awk '$3 != 1 { print }'
total 156872
2491111 -rwsr-xr-x 2 root   root     127560 2011-01-20 05:03 sudo
2491111 -rwsr-xr-x 2 root   root     127560 2011-01-20 05:03 sudoedit

See man ln for more details about hard links if you're not familiar with them.

share|improve this answer
    
Interesting, I didn't know symlinks behaved that way. –  Legate Feb 2 '11 at 10:58
5  
@legate, symlinks don't, applications can. the best example of a program that uses it's name to define how it executes is busybox. symlinks are just the number one way of defining the 0th argument to an application. simply renaming a file would also suffice. –  xenoterracide Feb 2 '11 at 12:50
    
I failed to parse this one: Note that there is also called a hard link that will have the same effect. –  Tshepang Feb 4 '11 at 9:01
    
I have re-written that sentence. Hopefully it is clearer now. –  Mikel Feb 4 '11 at 9:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.