Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef SCOPED_FD_H_included |
| 18 | #define SCOPED_FD_H_included |
| 19 | |
| 20 | #include <unistd.h> |
Spencer Low | b7a2528 | 2015-04-22 17:59:56 -0700 | [diff] [blame] | 21 | #include "JNIHelp.h" // for DISALLOW_COPY_AND_ASSIGN. |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 22 | |
| 23 | // A smart pointer that closes the given fd on going out of scope. |
| 24 | // Use this when the fd is incidental to the purpose of your function, |
| 25 | // but needs to be cleaned up on exit. |
| 26 | class ScopedFd { |
| 27 | public: |
Dmitriy Ivanov | 54733ea | 2015-01-20 11:28:52 -0800 | [diff] [blame] | 28 | explicit ScopedFd(int fd) : fd_(fd) { |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | ~ScopedFd() { |
Ian Rogers | a985e9e | 2014-06-21 22:55:27 -0700 | [diff] [blame] | 32 | reset(); |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | int get() const { |
Dmitriy Ivanov | 54733ea | 2015-01-20 11:28:52 -0800 | [diff] [blame] | 36 | return fd_; |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Vladimir Marko | d528212 | 2013-11-07 10:28:59 +0000 | [diff] [blame] | 39 | int release() __attribute__((warn_unused_result)) { |
Dmitriy Ivanov | 54733ea | 2015-01-20 11:28:52 -0800 | [diff] [blame] | 40 | int localFd = fd_; |
| 41 | fd_ = -1; |
Vladimir Marko | d528212 | 2013-11-07 10:28:59 +0000 | [diff] [blame] | 42 | return localFd; |
| 43 | } |
| 44 | |
Ian Rogers | a985e9e | 2014-06-21 22:55:27 -0700 | [diff] [blame] | 45 | void reset(int new_fd = -1) { |
Dmitriy Ivanov | 54733ea | 2015-01-20 11:28:52 -0800 | [diff] [blame] | 46 | if (fd_ != -1) { |
Spencer Low | b7a2528 | 2015-04-22 17:59:56 -0700 | [diff] [blame] | 47 | // Even if close(2) fails with EINTR, the fd will have been closed. |
| 48 | // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd. |
| 49 | // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html |
| 50 | close(fd_); |
Ian Rogers | a985e9e | 2014-06-21 22:55:27 -0700 | [diff] [blame] | 51 | } |
Dmitriy Ivanov | 54733ea | 2015-01-20 11:28:52 -0800 | [diff] [blame] | 52 | fd_ = new_fd; |
Ian Rogers | a985e9e | 2014-06-21 22:55:27 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 55 | private: |
Dmitriy Ivanov | 54733ea | 2015-01-20 11:28:52 -0800 | [diff] [blame] | 56 | int fd_; |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 57 | |
Ian Rogers | 8288dde | 2014-11-04 11:42:02 -0800 | [diff] [blame] | 58 | DISALLOW_COPY_AND_ASSIGN(ScopedFd); |
Brian Carlstrom | a97f907 | 2013-05-14 14:44:26 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | #endif // SCOPED_FD_H_included |