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