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