3
0

device_open.c 639 B

1234567891011121314151617181920212223242526272829303132
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. /* try to open up the specified device */
  11. int 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. }