Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Running install target with root privilages breaks subsequent invocations #1302

Open
Psirus opened this issue Jul 17, 2017 · 18 comments
Open

Comments

@Psirus
Copy link

Psirus commented Jul 17, 2017

A "common pattern" for a library is to run make and then sudo make install. This works nicely with CMake. However, when using Ninja as the underlying build system, sudo ninja install changes the owner of .ninja_log to root. Subsequent invocations of ninja than fail with

 ninja: error: opening build log: Permission denied

Just deleting the file afterwards seems fine, but this is not the way to go, right? When I raised this issue on CMake, one suggestion was for Ninja to try and write the log file with the original owner if it is run as root. What do you think?

@evmar
Copy link
Collaborator

evmar commented Jul 19, 2017

If you have a ninja target that builds anything, "sudo ninja ..." will build those intermediate outputs as root, which is also undesirable (the files will have the wrong owner and running the compiler as root).

I think better would be if the install commands themselves used sudo. That is, the build file would be configured such that ninja install would run a command that looks like sudo cp ....

@mathstuf
Copy link
Contributor

Putting it into the install rules is nasty: not all installs should happen as root and not all machines use sudo.

@evmar
Copy link
Collaborator

evmar commented Jul 19, 2017 via email

@Psirus
Copy link
Author

Psirus commented Aug 10, 2017

So the CMake dev thinks this should be solved by ninja, and you think it should be solved by CMake. Understandable, and I'm not knowledgable enough to give an opinion, but unlikely to resolve the issue.

@evmar
Copy link
Collaborator

evmar commented Aug 10, 2017

Can you link to the relevant cmake thread?

@mathstuf
Copy link
Contributor

It's linked to in the description: https://gitlab.kitware.com/cmake/cmake/issues/17073.

marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Nov 24, 2017
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Nov 24, 2017
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Nov 24, 2017
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
@marcelhollerbach
Copy link

marcelhollerbach commented Nov 24, 2017

Hello,

How about checking if there is already a log file, if there is, ensure the permissions stay the same, if not, just rename it (and take the uid and gid of the running user)?

I created PR #1362 to see how it looks in code.

marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Dec 1, 2017
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Dec 1, 2017
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
@ajaybd
Copy link

ajaybd commented Mar 30, 2018

I faced the same issue while building the sources.
The issue is some folders are built with root permission and some other folder with other username permission.
To get rid of this, change the complete permission of the source code to a single user.
sudo chown -R username:username <android_source_code>

marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 31, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 31, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 31, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 31, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Jun 3, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Jun 3, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Jun 3, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Jun 4, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Jun 13, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue Jun 18, 2019
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
@jgrocha
Copy link

jgrocha commented Dec 6, 2019

Same problem here. Always have to do a sudo chown $USER:$USER .ninja_* after sudo ninja install. Thank you all for looking into this issue. You are great!

marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 14, 2020
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 14, 2020
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
marcelhollerbach added a commit to marcelhollerbach/ninja that referenced this issue May 14, 2020
see ninja-build#1302 as a normal workflow you do things like "ninja all && sudo
ninja install" for the case that the install target executes a build
target then ninja_log will be writte ntwo which will result in its
permissions to be root / root, which is a problem since you cannot
execute the workflow from above again.
@jerome-pouiller
Copy link

It seems we can consider that as a major issue and the problem seems well understood.

So, I am surprised it is not yet fixed after 5 years. can I help on that issue? What is the problem with the proposed workaround? (a bit hacky maybe?)

@evmar
Copy link
Collaborator

evmar commented Aug 24, 2022

Fundamentally when you run sudo ninja <anything>, ninja may write any of a bunch of files, which means those files will now be owned by root. Not just the files that are patched in the above workaround, but also any other build intermediates.

Ninja's policy is that if you need some specific behavior for some specific task it executes, it's up to you to generate a ninja file that contains the specific task you need executed. For that reason I think it does belong in the generator. (Alternatively you could run your entire build as root, which seems like a bad idea in general, but that's up to you...)

@jonesmz

This comment was marked as abuse.

@jerome-pouiller
Copy link

For the record, the user can run ninja -t recompact just after sudo ninja install. Since the user has write access to the directory containing .ninja_deps, it is possible to remove .ninja_deps and rewrite it (exactly what ninja -t recompact does).

It is probably a bit less hacky than playing with chown.

@jerome-pouiller
Copy link

@jonesmz your suggestion looks like the patch submitted by @marcelhollerbach. Why it has not been accepted?

@jonesmz

This comment was marked as abuse.

@jerome-pouiller
Copy link

Not knowing which patch you're referring to (can you link the PR?)

marcelhollerbach@1365832

@jonesmz

This comment was marked as abuse.

@MorelloCherry
Copy link

Hi all involved,

it has now been another year but unfortunately I can still produce this another year on. In my build I have gone to some extend using the chown-hack but that is not as reliable as I have hoped.

Could I implore you to find a good solution that would work in conjunction with cmake?

It surely helps nobody if

  1. The issue is being pushed to cmake and they might just do the same - we are getting nowhere with this
  2. Ignoring the issue as it is - let's not pretend please the issue is not real
  3. Pretending nobody cares - there are several users out there including me facing this issue

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants