device_open.c 640 B

12345678910111213141516171819202122232425262728293031
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. /* try to open up the specified device */
  11. int FAST_FUNC device_open(const char *device, int mode)
  12. {
  13. int m, f, fd;
  14. m = mode | O_NONBLOCK;
  15. /* Retry up to 5 times */
  16. /* TODO: explain why it can't be considered insane */
  17. for (f = 0; f < 5; f++) {
  18. fd = open(device, m, 0600);
  19. if (fd >= 0)
  20. break;
  21. }
  22. if (fd < 0)
  23. return fd;
  24. /* Reset original flags. */
  25. if (m != mode)
  26. fcntl(fd, F_SETFL, mode);
  27. return fd;
  28. }