/ src / client / linux / crash_generation / crash_generation_client.cc
crash_generation_client.cc
  1  // Copyright 2010 Google LLC
  2  //
  3  // Redistribution and use in source and binary forms, with or without
  4  // modification, are permitted provided that the following conditions are
  5  // met:
  6  //
  7  //     * Redistributions of source code must retain the above copyright
  8  // notice, this list of conditions and the following disclaimer.
  9  //     * Redistributions in binary form must reproduce the above
 10  // copyright notice, this list of conditions and the following disclaimer
 11  // in the documentation and/or other materials provided with the
 12  // distribution.
 13  //     * Neither the name of Google LLC nor the names of its
 14  // contributors may be used to endorse or promote products derived from
 15  // this software without specific prior written permission.
 16  //
 17  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 21  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 22  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 23  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 24  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 25  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 27  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28  
 29  #ifdef HAVE_CONFIG_H
 30  #include <config.h>  // Must come first
 31  #endif
 32  
 33  #include "client/linux/crash_generation/crash_generation_client.h"
 34  
 35  #include <stdio.h>
 36  #include <sys/socket.h>
 37  #include <sys/types.h>
 38  
 39  #include <algorithm>
 40  
 41  #include "common/linux/eintr_wrapper.h"
 42  #include "common/linux/ignore_ret.h"
 43  #include "third_party/lss/linux_syscall_support.h"
 44  
 45  namespace google_breakpad {
 46  
 47  namespace {
 48  
 49  class CrashGenerationClientImpl : public CrashGenerationClient {
 50   public:
 51    explicit CrashGenerationClientImpl(int server_fd) : server_fd_(server_fd) {}
 52    CrashGenerationClientImpl(const CrashGenerationClientImpl&) = delete;
 53    void operator=(const CrashGenerationClientImpl&) = delete;
 54    ~CrashGenerationClientImpl() override = default;
 55  
 56    bool RequestDump(const void* blob, size_t blob_size) override {
 57      int fds[2];
 58      if (sys_pipe(fds) < 0)
 59        return false;
 60      static const unsigned kControlMsgSize = CMSG_SPACE(sizeof(int));
 61  
 62      struct kernel_iovec iov;
 63      iov.iov_base = const_cast<void*>(blob);
 64      iov.iov_len = blob_size;
 65  
 66      struct kernel_msghdr msg = { 0 };
 67      msg.msg_iov = &iov;
 68      msg.msg_iovlen = 1;
 69      char cmsg[kControlMsgSize] = "";
 70      msg.msg_control = cmsg;
 71      msg.msg_controllen = sizeof(cmsg);
 72  
 73      struct cmsghdr* hdr = CMSG_FIRSTHDR(&msg);
 74      hdr->cmsg_level = SOL_SOCKET;
 75      hdr->cmsg_type = SCM_RIGHTS;
 76      hdr->cmsg_len = CMSG_LEN(sizeof(int));
 77      int* p = reinterpret_cast<int*>(CMSG_DATA(hdr));
 78      *p = fds[1];
 79  
 80      ssize_t ret = HANDLE_EINTR(sys_sendmsg(server_fd_, &msg, 0));
 81      sys_close(fds[1]);
 82      if (ret < 0) {
 83        sys_close(fds[0]);
 84        return false;
 85      }
 86  
 87      // Wait for an ACK from the server.
 88      char b;
 89      IGNORE_RET(HANDLE_EINTR(sys_read(fds[0], &b, 1)));
 90      sys_close(fds[0]);
 91  
 92      return true;
 93    }
 94  
 95   private:
 96    int server_fd_;
 97  };
 98  
 99  }  // namespace
100  
101  // static
102  CrashGenerationClient* CrashGenerationClient::TryCreate(int server_fd) {
103    if (server_fd < 0)
104      return NULL;
105    return new CrashGenerationClientImpl(server_fd);
106  }
107  
108  }  // namespace google_breakpad