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