If circular symlinks are useless, then why are they allowed?
Asked Answered
A

2

4

I was just reading this post here:

What are circular symlinks in Unix-like systems used for?

And the answers were quite interesting. They seem to say conclusively that there is no reason one would ever create such a circular symlink, and therefore it must have been created in error. If this is true, then why on earth are they allowed? Is it because the mechanics of disallowing them are prohibitively complicated or computationally intensive?

I don't see why this would be the case: can't we just compare the address in memory to see if it is the same as the target address, and then if they are the same, throw an error?

EDIT: perhaps in certain languages there will be an error unless you use some sort of forcing option. In those cases, then my question simply becomes: why would you allow a force option?

EDIT: upon some further research with the help of @Wumpus Q Wembley, it appears that this is indeed disallowed in unix and results in the following error:

ln: ‘/usr/bin/apt-config’ and ‘/usr/bin/apt-config’ are the same file

but that this can indeed happen when the files that are being symlinked to themselves are already symlinks from some other file. I'm not sure why that behavior is desirable?

-Paul

Astereognosis answered 24/5, 2016 at 1:30 Comment(2)
It could be just that nobody has ever considered it a problem worth solving. There is (or at least was) a mindset in Unix that assumes the user knows what he is doing and therefore gives him what he asks for -- for example, while in DOS/Windows, a entering del *.* by default leads to an "are you sure?" prompt; under Unix, an "rm *" instantly nukes all the files in your directory, because that's what you asked it to do. So this may be another instance of that mindset -- if the Unix designers can't prove a circular symlink is useless, then they won't go out of their way to prevent it.Uruguay
But isn't it useless almost by definition? And I guess more philosophically, isn't there some sort of cost/benefit we should be considering rather than taking an absolutist point of view, even if you are a staunch unix developer?Astereognosis
A
6

I know of (and use) one case where circular symlink is useful. It's a corner case, but nonetheless.

The symlink in question is located in the /boot directory and (circularly) points to itself as in:

boot -> ./

It was created with the following command:

ln -s . boot

The reason it is necessary is as follows:

When I set up various Linux systems, in some cases I use separate /boot partition and in some - I don't.

I have a cookie-cutter grub.cfg file, which reads something like:

menuentry "Gentoo GNU/Linux" {
    echo "Loading Linux kernel"
    linux /boot/vmlinuz root=...
    echo "Loading initial ramdisk"
    initrd /boot/initramfs
}

If I didn't have the symlink, the above would not work for the systems with separate /boot partition. For those systems I would need to have:

menuentry "Gentoo GNU/Linux" {
    echo "Loading Linux kernel"
    linux /vmlinuz root=...
    echo "Loading initial ramdisk"
    initrd /initramfs
}

Note the absence of /boot in front of /vmlinuz and /initramfs.

The reason for this is that the /boot partition is the root partition for the GRUB and vmlinuz and initramfs are located in the root / directory.

Whereas, for systems without separate /boot partition, the system root / partition is also the root partition for the GRUB, and vmlinuz and the initramfs are located in the /boot directory.

With the symlink in place, I can use the same grub.cfg for both types systems.

Alliteration answered 24/5, 2016 at 1:50 Comment(5)
Interesting. It seems this would also be a good answer to the question I linked above!Astereognosis
@Paul, unfortunately that question is closed :( I will add a comment there linking to my answer here.Alliteration
I have just selected this answer choice because it indeed answers my question, but I still think it seems like the potential costs of allowing them outweigh this very small benefit.Astereognosis
@Astereognosis what would be the cost of not allowing this. Can you think of a way that would always halt.Liam
That is not really a circular symlink, is it? /boot/boot points to /boot. The equivalent of a real circular symlink as mentioned in the other question would be to have /boot/boot point to /boot/boot, i.e., if you'd create the symlink with ln -s boot /boot/boot. Your symlink "goes back up one level", which indeed has some use cases, because you can arbitrarily append /boot/boot/boot/boot/... and still end up in the same path. I don't see a use case for the real circular variant though.Trafalgar
R
3

It wouldn't be especially difficult to detect that a symlink would point to itself when creating it, but that wouldn't prevent them from being created in other ways. Consider:

mkdir foo
cd foo
ln -s ../bar/example example
cd ..
mv foo bar

Preventing that would require that before renaming the directory, the OS would scan through its contents (including subdirectories) checking the target of every link. Much too costly.

Even checking upon creation can be costly, if the symlink's target path contains lots of other symlinks or some slow network filesystems.

Basically, it's just not worth the trouble. Self-referential symlinks aren't useful, but they aren't really harmful either.

Ragout answered 24/5, 2016 at 1:49 Comment(9)
thank you, and Interesting point. I can see now how this would be a hassle. Though I'm not sure its entirely true they are harmless. I may have just broken my computer because of a massive circular symlink, which is what prompted me to get all philosophical :pAstereognosis
Add a third symlink in and the cost of detection becomes even more problematic, e.g. A->B->C->A.Chung
That is true. I guess if you wanted to do an exhaustive search, you would have to look at 2^n different cases (where n is the number of files in both directories)! Since that is how many possible subsets there areAstereognosis
No, you just have to go through the path and resolve each step that's a symlink, repeating until you have an absolute path. It's the same thing the operating system does whenever you open a file.Ragout
ah, ok that makes sense. I think I was looking too hard for a reason it would be a hassle...Astereognosis
also, I just wanted to add that they can in fact be harmful, if you see my post above. for example, if you symlink something to itself which is already linked to, you can break important pre-existing links, which can ultiumately mess up your whole system nearly irreversibly.Astereognosis
@Astereognosis said “if you symlink something to itself which is already linked to”, the harm here was done when you removed the valid file. You then replaced it with an invalid one (a symlink), there are however many other invalid files, only some of which are symlinks.Liam
@richard thanks for responding, but I'm not sure I'm getting your point? The question isn't whether the circular symlink is solely responsible for my noobish mistake, but rather whether allowing it provides any benefit to compensate for the potential costAstereognosis
If you question is “does the benefit of allowing circular symlinks out way the cost”. Then also consider the question “does the cost of dis-allowing circular symlinks, out way the benefits”. I think the cost of disallowing them may be infinite, in the general case (we can detect the most common cases easily, but there are some that we will miss, or that will take forever to compute).Liam

© 2022 - 2024 — McMap. All rights reserved.