Traditionally, as I remember, setting SGID on a directory makes the files which are created within to inherit the group of the directory.
For whatever reason, on my systems this is always the case, with or without SGID bit.
Nevertheless, the bit can be set, but then recursive cp(1) may fail in unexpected ways (without actually failing).
Now the fancy part:
We can see, cp has correctly copied the tree. According to the documentation, the
I don't find any notice on the net about the directory SGID behaviour happening even without SGID, neither about a tunable knob where this can be switched. But if this was intentionally implemented in his way, the cp behaviour should also be changed to silently ignore these errors, instead of breaking build scripts.
For whatever reason, on my systems this is always the case, with or without SGID bit.
Nevertheless, the bit can be set, but then recursive cp(1) may fail in unexpected ways (without actually failing).
Code:
$ id
uid=1100(pmc) gid=20(staff) groups=20(staff),5(operator)
$ ls -ld /var/tmp
drwxrwxrwt 15 root wheel 254 Apr 13 02:00 /var/tmp
$ mkdir /var/tmp/XXX
$ ls -ld /var/tmp/XXX
drwxr-xr-x 2 pmc wheel 2 Apr 13 02:03 /var/tmp/XXX
Now the fancy part:
Code:
$ chgrp staff /var/tmp/XXX
$ chmod g+s /var/tmp/XXX
$ mkdir /var/tmp/XXX/YYY
$ ls -ld /var/tmp/XXX /var/tmp/XXX/YYY
drwxr-sr-x 3 pmc staff 3 Apr 13 02:05 /var/tmp/XXX
drwxr-sr-x 2 pmc staff 2 Apr 13 02:05 /var/tmp/XXX/YYY
$ cp -R /var/tmp/XXX /var/tmp/ZZZ
cp: chmod: /var/tmp/ZZZ/YYY: Operation not permitted
cp: chmod: /var/tmp/ZZZ: Operation not permitted
$ echo $?
1
$ ls -ld /var/tmp/ZZZ /var/tmp/ZZZ/YYY
drwxr-xr-x 3 pmc wheel 3 Apr 13 02:07 /var/tmp/ZZZ
drwxr-xr-x 2 pmc wheel 2 Apr 13 02:07 /var/tmp/ZZZ/YYY
We can see, cp has correctly copied the tree. According to the documentation, the
-R
flag copies directory permissions also. As the original directories have the SGID bit set, cp tries to set it on the destination also. But we cannot set an SGID bit with a group that we don't belong to, and the group of the new directories is wheel because that gets inherited without an SGID bit set.I don't find any notice on the net about the directory SGID behaviour happening even without SGID, neither about a tunable knob where this can be switched. But if this was intentionally implemented in his way, the cp behaviour should also be changed to silently ignore these errors, instead of breaking build scripts.