2001-03-17 04:17:14 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Utility routines.
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
2001-03-17 04:17:14 +05:30
|
|
|
*
|
2006-05-20 00:59:19 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-17 04:17:14 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
|
|
|
/* try to open up the specified device */
|
2008-06-27 08:22:20 +05:30
|
|
|
int FAST_FUNC device_open(const char *device, int mode)
|
2001-03-17 04:17:14 +05:30
|
|
|
{
|
2007-11-06 08:35:54 +05:30
|
|
|
int m, f, fd;
|
2001-03-17 04:17:14 +05:30
|
|
|
|
|
|
|
m = mode | O_NONBLOCK;
|
|
|
|
|
|
|
|
/* Retry up to 5 times */
|
2006-09-08 23:01:55 +05:30
|
|
|
/* TODO: explain why it can't be considered insane */
|
2007-11-06 08:35:54 +05:30
|
|
|
for (f = 0; f < 5; f++) {
|
|
|
|
fd = open(device, m, 0600);
|
|
|
|
if (fd >= 0)
|
2001-03-17 04:17:14 +05:30
|
|
|
break;
|
2007-11-06 08:35:54 +05:30
|
|
|
}
|
2001-03-17 04:17:14 +05:30
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
/* Reset original flags. */
|
|
|
|
if (m != mode)
|
|
|
|
fcntl(fd, F_SETFL, mode);
|
|
|
|
return fd;
|
|
|
|
}
|