Line data Source code
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 "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 0 : explicit CrashGenerationClientImpl(int server_fd) : server_fd_(server_fd) {}
49 0 : virtual ~CrashGenerationClientImpl() {}
50 :
51 0 : virtual bool RequestDump(const void* blob, size_t blob_size) {
52 : int fds[2];
53 0 : if (sys_pipe(fds) < 0)
54 0 : return false;
55 : static const unsigned kControlMsgSize = CMSG_SPACE(sizeof(int));
56 :
57 : struct kernel_iovec iov;
58 0 : iov.iov_base = const_cast<void*>(blob);
59 0 : iov.iov_len = blob_size;
60 :
61 0 : struct kernel_msghdr msg = { 0 };
62 0 : msg.msg_iov = &iov;
63 0 : msg.msg_iovlen = 1;
64 0 : char cmsg[kControlMsgSize] = "";
65 0 : msg.msg_control = cmsg;
66 0 : msg.msg_controllen = sizeof(cmsg);
67 :
68 0 : struct cmsghdr* hdr = CMSG_FIRSTHDR(&msg);
69 0 : hdr->cmsg_level = SOL_SOCKET;
70 0 : hdr->cmsg_type = SCM_RIGHTS;
71 0 : hdr->cmsg_len = CMSG_LEN(sizeof(int));
72 0 : int* p = reinterpret_cast<int*>(CMSG_DATA(hdr));
73 0 : *p = fds[1];
74 :
75 0 : ssize_t ret = HANDLE_EINTR(sys_sendmsg(server_fd_, &msg, 0));
76 0 : sys_close(fds[1]);
77 0 : if (ret < 0) {
78 0 : sys_close(fds[0]);
79 0 : return false;
80 : }
81 :
82 : // Wait for an ACK from the server.
83 : char b;
84 0 : IGNORE_RET(HANDLE_EINTR(sys_read(fds[0], &b, 1)));
85 0 : sys_close(fds[0]);
86 :
87 0 : 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 0 : CrashGenerationClient* CrashGenerationClient::TryCreate(int server_fd) {
100 0 : if (server_fd < 0)
101 0 : return NULL;
102 0 : return new CrashGenerationClientImpl(server_fd);
103 : }
104 :
105 : } // namespace google_breakpad
|