blob: b4377e2fa9983025fd123fa7214bd331ceb447b8 [file] [log] [blame]
Christopher Wileya59f7b92013-02-26 15:07:02 -08001// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/file_io.h"
6
7#include <fcntl.h>
8#include <unistd.h>
9
10#include <base/posix/eintr_wrapper.h>
11
12namespace shill {
13
14namespace {
15
Ben Chanbe0849f2014-08-15 23:02:32 -070016base::LazyInstance<FileIO> g_file_io = LAZY_INSTANCE_INITIALIZER;
Christopher Wileya59f7b92013-02-26 15:07:02 -080017
18} // namespace
19
20FileIO::FileIO() {}
21
22FileIO::~FileIO() {}
23
24// static
25FileIO *FileIO::GetInstance() {
26 return g_file_io.Pointer();
27}
28
29ssize_t FileIO::Write(int fd, const void *buf, size_t count) {
30 return HANDLE_EINTR(write(fd, buf, count));
31}
32
33ssize_t FileIO::Read(int fd, void *buf, size_t count) {
34 return HANDLE_EINTR(read(fd, buf, count));
35}
36
37int FileIO::Close(int fd) {
Mike Frysinger2dd351c2014-05-14 16:27:34 -040038 return IGNORE_EINTR(close(fd));
Christopher Wileya59f7b92013-02-26 15:07:02 -080039}
40
41int FileIO::SetFdNonBlocking(int fd) {
42 const int flags = HANDLE_EINTR(fcntl(fd, F_GETFL)) | O_NONBLOCK;
43 return HANDLE_EINTR(fcntl(fd, F_SETFL, flags));
44}
45
46} // namespace shill