support_can_chroot.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* Return true if the process can perform a chroot operation.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <support/check.h>
  18. #include <support/namespace.h>
  19. #include <support/support.h>
  20. #include <support/xunistd.h>
  21. #include <sys/stat.h>
  22. #include <unistd.h>
  23. static void
  24. callback (void *closure)
  25. {
  26. int *result = closure;
  27. struct stat64 before;
  28. xstat ("/dev", &before);
  29. if (chroot ("/dev") != 0)
  30. {
  31. *result = errno;
  32. return;
  33. }
  34. struct stat64 after;
  35. xstat ("/", &after);
  36. TEST_VERIFY (before.st_dev == after.st_dev);
  37. TEST_VERIFY (before.st_ino == after.st_ino);
  38. *result = 0;
  39. }
  40. bool
  41. support_can_chroot (void)
  42. {
  43. int *result = support_shared_allocate (sizeof (*result));
  44. *result = 0;
  45. support_isolate_in_subprocess (callback, result);
  46. bool ok = *result == 0;
  47. if (!ok)
  48. {
  49. static bool already_warned;
  50. if (!already_warned)
  51. {
  52. already_warned = true;
  53. errno = *result;
  54. printf ("warning: this process does not support chroot: %m\n");
  55. }
  56. }
  57. support_shared_free (result);
  58. return ok;
  59. }