blob: e5639c712204ee919e45a9663765fdcf03635910 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 */
Eric Andersenaad1a882001-03-16 22:47:14 +00009#include "libbb.h"
10
Eric Andersenaad1a882001-03-16 22:47:14 +000011/* try to open up the specified device */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000012int FAST_FUNC device_open(const char *device, int mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000013{
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000014 int m, f, fd;
Eric Andersenaad1a882001-03-16 22:47:14 +000015
16 m = mode | O_NONBLOCK;
17
18 /* Retry up to 5 times */
Denis Vlasenkobd8f43d2006-09-08 17:31:55 +000019 /* TODO: explain why it can't be considered insane */
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000020 for (f = 0; f < 5; f++) {
21 fd = open(device, m, 0600);
22 if (fd >= 0)
Eric Andersenaad1a882001-03-16 22:47:14 +000023 break;
Denis Vlasenko6bef3d12007-11-06 03:05:54 +000024 }
Eric Andersenaad1a882001-03-16 22:47:14 +000025 if (fd < 0)
26 return fd;
27 /* Reset original flags. */
28 if (m != mode)
29 fcntl(fd, F_SETFL, mode);
30 return fd;
31}