1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef BASE_FILE_DESCRIPTOR_POSIX_H_ 6*635a8641SAndroid Build Coastguard Worker #define BASE_FILE_DESCRIPTOR_POSIX_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include "base/files/file.h" 9*635a8641SAndroid Build Coastguard Worker #include "base/files/scoped_file.h" 10*635a8641SAndroid Build Coastguard Worker 11*635a8641SAndroid Build Coastguard Worker namespace base { 12*635a8641SAndroid Build Coastguard Worker 13*635a8641SAndroid Build Coastguard Worker // ----------------------------------------------------------------------------- 14*635a8641SAndroid Build Coastguard Worker // We introduct a special structure for file descriptors in order that we are 15*635a8641SAndroid Build Coastguard Worker // able to use template specialisation to special-case their handling. 16*635a8641SAndroid Build Coastguard Worker // 17*635a8641SAndroid Build Coastguard Worker // IMPORTANT: This is primarily intended for use when sending file descriptors 18*635a8641SAndroid Build Coastguard Worker // over IPC. Even if |auto_close| is true, base::FileDescriptor does NOT close() 19*635a8641SAndroid Build Coastguard Worker // |fd| when going out of scope. Instead, a consumer of a base::FileDescriptor 20*635a8641SAndroid Build Coastguard Worker // must invoke close() on |fd| if |auto_close| is true. 21*635a8641SAndroid Build Coastguard Worker // 22*635a8641SAndroid Build Coastguard Worker // In the case of IPC, the the IPC subsystem knows to close() |fd| after sending 23*635a8641SAndroid Build Coastguard Worker // a message that contains a base::FileDescriptor if auto_close == true. On the 24*635a8641SAndroid Build Coastguard Worker // other end, the receiver must make sure to close() |fd| after it has finished 25*635a8641SAndroid Build Coastguard Worker // processing the IPC message. See the IPC::ParamTraits<> specialization in 26*635a8641SAndroid Build Coastguard Worker // ipc/ipc_message_utils.h for all the details. 27*635a8641SAndroid Build Coastguard Worker // ----------------------------------------------------------------------------- 28*635a8641SAndroid Build Coastguard Worker struct FileDescriptor { FileDescriptorFileDescriptor29*635a8641SAndroid Build Coastguard Worker FileDescriptor() : fd(-1), auto_close(false) {} 30*635a8641SAndroid Build Coastguard Worker FileDescriptorFileDescriptor31*635a8641SAndroid Build Coastguard Worker FileDescriptor(int ifd, bool iauto_close) : fd(ifd), auto_close(iauto_close) { 32*635a8641SAndroid Build Coastguard Worker } 33*635a8641SAndroid Build Coastguard Worker FileDescriptorFileDescriptor34*635a8641SAndroid Build Coastguard Worker FileDescriptor(File file) : fd(file.TakePlatformFile()), auto_close(true) {} FileDescriptorFileDescriptor35*635a8641SAndroid Build Coastguard Worker explicit FileDescriptor(ScopedFD fd) : fd(fd.release()), auto_close(true) {} 36*635a8641SAndroid Build Coastguard Worker 37*635a8641SAndroid Build Coastguard Worker bool operator==(const FileDescriptor& other) const { 38*635a8641SAndroid Build Coastguard Worker return (fd == other.fd && auto_close == other.auto_close); 39*635a8641SAndroid Build Coastguard Worker } 40*635a8641SAndroid Build Coastguard Worker 41*635a8641SAndroid Build Coastguard Worker bool operator!=(const FileDescriptor& other) const { 42*635a8641SAndroid Build Coastguard Worker return !operator==(other); 43*635a8641SAndroid Build Coastguard Worker } 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard Worker // A comparison operator so that we can use these as keys in a std::map. 46*635a8641SAndroid Build Coastguard Worker bool operator<(const FileDescriptor& other) const { 47*635a8641SAndroid Build Coastguard Worker return other.fd < fd; 48*635a8641SAndroid Build Coastguard Worker } 49*635a8641SAndroid Build Coastguard Worker 50*635a8641SAndroid Build Coastguard Worker int fd; 51*635a8641SAndroid Build Coastguard Worker // If true, this file descriptor should be closed after it has been used. For 52*635a8641SAndroid Build Coastguard Worker // example an IPC system might interpret this flag as indicating that the 53*635a8641SAndroid Build Coastguard Worker // file descriptor it has been given should be closed after use. 54*635a8641SAndroid Build Coastguard Worker bool auto_close; 55*635a8641SAndroid Build Coastguard Worker }; 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker } // namespace base 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard Worker #endif // BASE_FILE_DESCRIPTOR_POSIX_H_ 60