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