2
0

cutils.c 670 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  3. * See the COPYING file in the top-level directory.
  4. */
  5. #include "qemu/osdep.h"
  6. #include "cutils.h"
  7. #include "qapi/error.h"
  8. /**
  9. * qga_open_cloexec:
  10. * @name: the pathname to open
  11. * @flags: as in open()
  12. * @mode: as in open()
  13. *
  14. * A wrapper for open() function which sets O_CLOEXEC.
  15. *
  16. * On error, -1 is returned.
  17. */
  18. int qga_open_cloexec(const char *name, int flags, mode_t mode)
  19. {
  20. int ret;
  21. #ifdef O_CLOEXEC
  22. ret = open(name, flags | O_CLOEXEC, mode);
  23. #else
  24. ret = open(name, flags, mode);
  25. if (ret >= 0) {
  26. qemu_set_cloexec(ret);
  27. }
  28. #endif
  29. return ret;
  30. }