]> git.ipfire.org Git - thirdparty/libvirt.git/commit
util: refactor virFileOpenAs
authorLaine Stump <laine@laine.org>
Fri, 13 Jan 2012 20:26:45 +0000 (15:26 -0500)
committerLaine Stump <laine@laine.org>
Fri, 3 Feb 2012 21:47:39 +0000 (16:47 -0500)
commit90e4d681bc30074466912e9ae733b0bf0b9d5a03
tree755a5815f41549efdcff8679aef92b192dc8cc79
parentd04394288f1c8fc18c47b9c9b1728ccaafb47810
util: refactor virFileOpenAs

virFileOpenAs previously would only try opening a file as the current
user, or as a different user, but wouldn't try both methods in a
single call. This made it cumbersome to use as a replacement for
open(2). Additionally, it had a lot of historical baggage that led to
it being difficult to understand.

This patch refactors virFileOpenAs in the following ways:

* reorganize the code so that everything dealing with both the parent
  and child sides of the "fork+setuid+setgid+open" method are in a
  separate function. This makes the public function easier to understand.

* Allow a single call to virFileOpenAs() to first attempt the open as
  the current user, and if that fails to automatically re-try after
  doing fork+setuid (if deemed appropriate, i.e. errno indicates it
  would now be successful, and the file is on a networkFS). This makes
  it possible (in many, but possibly not all, cases) to drop-in
  virFileOpenAs() as a replacement for open(2).

  (NB: currently qemuOpenFile() calls virFileOpenAs() twice, once
  without forking, then again with forking. That unfortunately can't
  be changed without at least some discussion of the ramifications,
  because the requested file permissions are different in each case,
  which is something that a single call to virFileOpenAs() can't deal
  with.)

* Add a flag so that any fchown() of the file to a different uid:gid
  is explicitly requested when the function is called, rather than it
  being implied by the presence of the O_CREAT flag. This just makes
  for less subtle surprises to consumers. (Commit
  b1643dc15c5de886fefe56ad18608d65f1325a2c added the check for O_CREAT
  before forcing ownership. This patch just makes that restriction
  more explicit.)

* If either the uid or gid is specified as "-1", virFileOpenAs will
  interpret this to mean "the current [gu]id".

All current consumers of virFileOpenAs should retain their present
behavior (after a few minor changes to their setup code and
arguments).
src/libxl/libxl_driver.c
src/qemu/qemu_driver.c
src/storage/storage_backend.c
src/util/util.c
src/util/util.h