|
3 | 3 | * SPDX-License-Identifier: BSD-3-Clause
|
4 | 4 | */
|
5 | 5 |
|
| 6 | +/* |
| 7 | + * Copyright (C) 2018 Intel Corporation |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <errno.h> |
| 23 | +#include <stdio.h> |
| 24 | +#include <fcntl.h> |
| 25 | +#include <unistd.h> |
| 26 | +#include <stdlib.h> |
| 27 | +#include <string.h> |
| 28 | + |
| 29 | +#include "crash_dump.h" |
| 30 | +#include "packet.h" |
| 31 | +#include "log_sys.h" |
| 32 | +#include "protocol.h" |
| 33 | + |
6 | 34 | /**
|
7 | 35 | * Usercrash works as C/S model: usercrash_c works as usercrash client to
|
8 | 36 | * collect crash logs and information once crash event occurs. For each time,
|
|
12 | 40 | * completed.
|
13 | 41 | */
|
14 | 42 |
|
15 |
| -int main(void) |
| 43 | +/** |
| 44 | + * @sockfd: the socket fd. |
| 45 | + * set_timeout is used to set timeout for the sockfd, in case client is blocked |
| 46 | + * when client cannot receive the data from server, or send data to server |
| 47 | + */ |
| 48 | +static int set_timeout(int sockfd) |
| 49 | +{ |
| 50 | + struct timeval timeout = {50, 0}; |
| 51 | + |
| 52 | + if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, |
| 53 | + sizeof(timeout)) != 0) { |
| 54 | + LOGE("failed to set receive timeout\n"); |
| 55 | + return -1; |
| 56 | + } |
| 57 | + if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeout, |
| 58 | + sizeof(timeout)) != 0) { |
| 59 | + LOGE("failed to set send timeout\n"); |
| 60 | + return -1; |
| 61 | + } |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * @pid: crash process pid. |
| 67 | + * @usercrashd_socket: client socket fd pointer, get the value from |
| 68 | + * usercrashd_connect function. |
| 69 | + * @output_fd: file fd, receives from server to store dump file. |
| 70 | + * @name: crash process name |
| 71 | + * usercrashd_connect is used to connect to server, and get the crash file fd |
| 72 | + * from server |
| 73 | + */ |
| 74 | +bool usercrashd_connect(int pid, int *usercrashd_socket, |
| 75 | + int *output_fd, char *name) |
| 76 | +{ |
| 77 | + int sockfd; |
| 78 | + int tmp_output_fd; |
| 79 | + ssize_t rc; |
| 80 | + int flags; |
| 81 | + struct crash_packet packet = {0}; |
| 82 | + |
| 83 | + sockfd = socket_local_client(SOCKET_NAME, SOCK_SEQPACKET); |
| 84 | + if (sockfd == -1) { |
| 85 | + LOGE("failed to connect to usercrashd, error (%s)\n", |
| 86 | + strerror(errno)); |
| 87 | + return false; |
| 88 | + } |
| 89 | + packet.packet_type = kDumpRequest; |
| 90 | + packet.pid = pid; |
| 91 | + strncpy(packet.name, name, COMM_NAME_LEN - 1); |
| 92 | + if (set_timeout(sockfd)) { |
| 93 | + close(sockfd); |
| 94 | + return -1; |
| 95 | + } |
| 96 | + if (write(sockfd, &packet, sizeof(packet)) != |
| 97 | + sizeof(packet)) { |
| 98 | + LOGE("failed to write DumpRequest packet, error (%s)\n", |
| 99 | + strerror(errno)); |
| 100 | + close(sockfd); |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + rc = recv_fd(sockfd, &packet, sizeof(packet), |
| 105 | + &tmp_output_fd); |
| 106 | + if (rc == -1) { |
| 107 | + LOGE("failed to read response to DumpRequest packet, "); |
| 108 | + LOGE("error (%s)\n", strerror(errno)); |
| 109 | + close(sockfd); |
| 110 | + return false; |
| 111 | + } else if (rc != sizeof(packet)) { |
| 112 | + LOGE("received DumpRequest response packet of incorrect "); |
| 113 | + LOGE("length (expected %lu, got %ld)\n", sizeof(packet), rc); |
| 114 | + goto fail; |
| 115 | + } |
| 116 | + if (packet.packet_type != kPerformDump) { |
| 117 | + LOGE("unexpected dump response:%d\n", packet.packet_type); |
| 118 | + goto fail; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Make the fd O_APPEND so that our output is guaranteed to be at the |
| 123 | + * end of a file. (This also makes selinux rules consistent, because |
| 124 | + * selinux distinguishes between writing to a regular fd, and writing |
| 125 | + * to an fd with O_APPEND) |
| 126 | + */ |
| 127 | + flags = fcntl(tmp_output_fd, F_GETFL); |
| 128 | + if (fcntl(tmp_output_fd, F_SETFL, flags | O_APPEND) != 0) { |
| 129 | + LOGE("failed to set output fd flags, error (%s)\n", |
| 130 | + strerror(errno)); |
| 131 | + goto fail; |
| 132 | + } |
| 133 | + |
| 134 | + *usercrashd_socket = sockfd; |
| 135 | + *output_fd = tmp_output_fd; |
| 136 | + |
| 137 | + return true; |
| 138 | +fail: |
| 139 | + close(sockfd); |
| 140 | + close(tmp_output_fd); |
| 141 | + return false; |
| 142 | + |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * @usercrashd_socket: client socket fd, used to communicate with server. |
| 147 | + * usercrashd_notify_completion is used to tell the server it has done the |
| 148 | + * dump, the server will pop another crash from the queue and execute the dump |
| 149 | + * process |
| 150 | + */ |
| 151 | +bool usercrashd_notify_completion(int usercrashd_socket) |
| 152 | +{ |
| 153 | + struct crash_packet packet = {0}; |
| 154 | + |
| 155 | + packet.packet_type = kCompletedDump; |
| 156 | + if (set_timeout(usercrashd_socket)) { |
| 157 | + close(usercrashd_socket); |
| 158 | + return -1; |
| 159 | + } |
| 160 | + if (write(usercrashd_socket, &packet, |
| 161 | + sizeof(packet)) != sizeof(packet)) { |
| 162 | + return false; |
| 163 | + } |
| 164 | + return true; |
| 165 | +} |
| 166 | + |
| 167 | +static void print_usage(void) |
| 168 | +{ |
| 169 | + printf("It's the client role of usercrash.\n"); |
| 170 | + printf("[Usage]\n"); |
| 171 | + printf("\t--coredump, usercrash_c <pid> <comm> <sig> "); |
| 172 | + printf("(root role to run)\n"); |
| 173 | + printf("[Option]\n"); |
| 174 | + printf("\t-h: print this usage message\n"); |
| 175 | + printf("\t-v: print usercrash_c version\n"); |
| 176 | +} |
| 177 | + |
| 178 | +int main(int argc, char *argv[]) |
16 | 179 | {
|
17 |
| - //TO BE DONE |
18 |
| - //This empty function is to satisfy the dependency of Makefile. |
19 |
| - //This is the entry of usercrash_c, the implementation will be |
20 |
| - //filled by following patches. |
| 180 | + int pid; |
| 181 | + int sig; |
| 182 | + int out_fd; |
| 183 | + int sock; |
| 184 | + int ret; |
| 185 | + |
| 186 | + if (argc > 1) { |
| 187 | + if (strcmp(argv[1], "-v") == 0) { |
| 188 | + printf("usercrash_c version is 1.0\n"); |
| 189 | + return 0; |
| 190 | + } |
| 191 | + if (strcmp(argv[1], "-h") == 0) { |
| 192 | + print_usage(); |
| 193 | + return 0; |
| 194 | + } |
| 195 | + } else |
| 196 | + print_usage(); |
| 197 | + |
| 198 | + if (getuid() != 0) { |
| 199 | + LOGE("failed to execute usercrash_c, root is required\n"); |
| 200 | + exit(EXIT_FAILURE); |
| 201 | + } |
| 202 | + |
| 203 | + if (argc == 4) { |
| 204 | + /* it's from coredump */ |
| 205 | + pid = atoi(argv[1]); |
| 206 | + sig = atoi(argv[3]); |
| 207 | + ret = usercrashd_connect(pid, &sock, &out_fd, argv[2]); |
| 208 | + if (!ret) { |
| 209 | + LOGE("usercrashd_connect failed, error (%s)\n", |
| 210 | + strerror(errno)); |
| 211 | + exit(EXIT_FAILURE); |
| 212 | + } |
| 213 | + crash_dump(pid, sig, out_fd); |
| 214 | + close(out_fd); |
| 215 | + if (!usercrashd_notify_completion(sock)) { |
| 216 | + LOGE("failed to notify usercrashd of completion"); |
| 217 | + close(sock); |
| 218 | + exit(EXIT_FAILURE); |
| 219 | + } |
| 220 | + close(sock); |
| 221 | + } else { |
| 222 | + print_usage(); |
| 223 | + return 0; |
| 224 | + } |
| 225 | + |
21 | 226 | return 0;
|
22 | 227 | }
|
0 commit comments