Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 1 | #!/system/bin/sh |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 2 | |
Steve Fung | 6c34c25 | 2015-08-20 00:27:30 -0700 | [diff] [blame] | 3 | # Copyright (C) 2010 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 16 | |
| 17 | set -e |
| 18 | |
| 19 | # Default product ID in crash report (used if GOOGLE_CRASH_* is undefined). |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 20 | BRILLO_PRODUCT=Brillo |
| 21 | |
| 22 | # Base directory that contains any crash reporter state files. |
| 23 | CRASH_STATE_DIR="/data/misc/crash_reporter" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 24 | |
Steve Fung | 12e61ca | 2015-09-15 16:36:33 -0700 | [diff] [blame] | 25 | # File containing crash_reporter's anonymized guid. |
| 26 | GUID_FILE="${CRASH_STATE_DIR}/guid" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 27 | |
| 28 | # Crash sender lock in case the sender is already running. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 29 | CRASH_SENDER_LOCK="${CRASH_STATE_DIR}/lock/crash_sender" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 30 | |
| 31 | # Path to file that indicates a crash test is currently running. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 32 | CRASH_TEST_IN_PROGRESS_FILE="${CRASH_STATE_DIR}/tmp/crash-test-in-progress" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 33 | |
| 34 | # Set this to 1 in the environment to allow uploading crash reports |
| 35 | # for unofficial versions. |
| 36 | FORCE_OFFICIAL=${FORCE_OFFICIAL:-0} |
| 37 | |
| 38 | # Path to hardware class description. |
| 39 | HWCLASS_PATH="/sys/devices/platform/chromeos_acpi/HWID" |
| 40 | |
| 41 | # Path to file that indicates this is a developer image. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 42 | LEAVE_CORE_FILE="${CRASH_STATE_DIR}/.leave_core" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 43 | |
| 44 | # Path to list_proxies. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 45 | LIST_PROXIES="list_proxies" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 46 | |
| 47 | # Maximum crashes to send per day. |
| 48 | MAX_CRASH_RATE=${MAX_CRASH_RATE:-32} |
| 49 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 50 | # File whose existence mocks crash sending. If empty we pretend the |
| 51 | # crash sending was successful, otherwise unsuccessful. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 52 | MOCK_CRASH_SENDING="${CRASH_STATE_DIR}/tmp/mock-crash-sending" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 53 | |
| 54 | # Set this to 1 in the environment to pretend to have booted in developer |
| 55 | # mode. This is used by autotests. |
| 56 | MOCK_DEVELOPER_MODE=${MOCK_DEVELOPER_MODE:-0} |
| 57 | |
| 58 | # Ignore PAUSE_CRASH_SENDING file if set. |
| 59 | OVERRIDE_PAUSE_SENDING=${OVERRIDE_PAUSE_SENDING:-0} |
| 60 | |
| 61 | # File whose existence causes crash sending to be delayed (for testing). |
| 62 | # Must be stateful to enable testing kernel crashes. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 63 | PAUSE_CRASH_SENDING="${CRASH_STATE_DIR}/lock/crash_sender_paused" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 64 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 65 | # Path to a directory of restricted certificates which includes |
Steve Fung | 44aec5f | 2015-09-12 02:52:06 -0700 | [diff] [blame] | 66 | # a certificate for the crash server. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 67 | RESTRICTED_CERTIFICATES_PATH="/system/etc/security/cacerts" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 68 | |
| 69 | # File whose existence implies we're running and not to start again. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 70 | RUN_FILE="${CRASH_STATE_DIR}/run/crash_sender.pid" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 71 | |
| 72 | # Maximum time to sleep between sends. |
| 73 | SECONDS_SEND_SPREAD=${SECONDS_SEND_SPREAD:-600} |
| 74 | |
Peter Qiu | 6aa551e | 2015-03-06 11:42:04 -0800 | [diff] [blame] | 75 | # Set this to 1 to allow uploading of device coredumps. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 76 | DEVCOREDUMP_UPLOAD_FLAG_FILE="${CRASH_STATE_DIR}/device_coredump_upload_allowed" |
Peter Qiu | 6aa551e | 2015-03-06 11:42:04 -0800 | [diff] [blame] | 77 | |
Steve Fung | ed78930 | 2015-09-14 16:14:23 -0700 | [diff] [blame] | 78 | # The weave configuration file. |
| 79 | WEAVE_CONF_FILE="/etc/weaved/weaved.conf" |
| 80 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 81 | # The syslog tag for all logging we emit. |
| 82 | TAG="$(basename $0)[$$]" |
| 83 | |
| 84 | # Directory to store timestamp files indicating the uploads in the past 24 |
| 85 | # hours. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 86 | TIMESTAMPS_DIR="${CRASH_STATE_DIR}/crash_sender" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 87 | |
| 88 | # Temp directory for this process. |
| 89 | TMP_DIR="" |
| 90 | |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 91 | # Crash report log file. |
| 92 | CRASH_LOG="${CRASH_STATE_DIR}/log/uploads.log" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 93 | |
| 94 | lecho() { |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 95 | log -t "${TAG}" "$@" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | # Returns true if mock is enabled. |
| 99 | is_mock() { |
| 100 | [ -f "${MOCK_CRASH_SENDING}" ] && return 0 |
| 101 | return 1 |
| 102 | } |
| 103 | |
| 104 | is_mock_successful() { |
| 105 | local mock_in=$(cat "${MOCK_CRASH_SENDING}") |
| 106 | [ "${mock_in}" = "" ] && return 0 # empty file means success |
| 107 | return 1 |
| 108 | } |
| 109 | |
| 110 | cleanup() { |
| 111 | if [ -n "${TMP_DIR}" ]; then |
| 112 | rm -rf "${TMP_DIR}" |
| 113 | fi |
| 114 | rm -f "${RUN_FILE}" |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 115 | if [ -n "${CRASH_SENDER_LOCK}" ]; then |
| 116 | rm -rf "${CRASH_SENDER_LOCK}" |
| 117 | fi |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 118 | crash_done |
| 119 | } |
| 120 | |
| 121 | crash_done() { |
| 122 | if is_mock; then |
| 123 | # For testing purposes, emit a message to log so that we |
| 124 | # know when the test has received all the messages from this run. |
| 125 | lecho "crash_sender done." |
| 126 | fi |
| 127 | } |
| 128 | |
| 129 | is_official_image() { |
| 130 | [ ${FORCE_OFFICIAL} -ne 0 ] && return 0 |
Steve Fung | 1619214 | 2015-09-29 01:01:38 -0700 | [diff] [blame] | 131 | if [ "$(getprop ro.secure)" = "1" ]; then |
| 132 | return 0 |
| 133 | else |
| 134 | return 1 |
| 135 | fi |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | # Returns 0 if the a crash test is currently running. NOTE: Mirrors |
| 139 | # crash_collector.cc:CrashCollector::IsCrashTestInProgress(). |
| 140 | is_crash_test_in_progress() { |
| 141 | [ -f "${CRASH_TEST_IN_PROGRESS_FILE}" ] && return 0 |
| 142 | return 1 |
| 143 | } |
| 144 | |
| 145 | # Returns 0 if we should consider ourselves to be running on a developer |
| 146 | # image. NOTE: Mirrors crash_collector.cc:CrashCollector::IsDeveloperImage(). |
| 147 | is_developer_image() { |
| 148 | # If we're testing crash reporter itself, we don't want to special-case |
| 149 | # for developer images. |
| 150 | is_crash_test_in_progress && return 1 |
| 151 | [ -f "${LEAVE_CORE_FILE}" ] && return 0 |
| 152 | return 1 |
| 153 | } |
| 154 | |
| 155 | # Returns 0 if we should consider ourselves to be running on a test image. |
| 156 | is_test_image() { |
| 157 | # If we're testing crash reporter itself, we don't want to special-case |
| 158 | # for test images. |
| 159 | is_crash_test_in_progress && return 1 |
| 160 | case $(get_channel) in |
| 161 | test*) return 0;; |
| 162 | esac |
| 163 | return 1 |
| 164 | } |
| 165 | |
| 166 | # Returns 0 if the machine booted up in developer mode. |
| 167 | is_developer_mode() { |
| 168 | [ ${MOCK_DEVELOPER_MODE} -ne 0 ] && return 0 |
| 169 | # If we're testing crash reporter itself, we don't want to special-case |
| 170 | # for developer mode. |
| 171 | is_crash_test_in_progress && return 1 |
Steve Fung | 57c7486 | 2015-09-28 18:00:16 -0700 | [diff] [blame] | 172 | if [ "$(getprop ro.debuggable)" = "1" ]; then |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 173 | return 0 |
| 174 | else |
| 175 | return 1 |
| 176 | fi |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Peter Qiu | 6aa551e | 2015-03-06 11:42:04 -0800 | [diff] [blame] | 179 | # Return 0 if the uploading of device coredumps is allowed. |
| 180 | is_device_coredump_upload_allowed() { |
| 181 | [ -f "${DEVCOREDUMP_UPLOAD_FLAG_FILE}" ] && return 0 |
| 182 | return 1 |
| 183 | } |
| 184 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 185 | # Generate a uniform random number in 0..max-1. |
Steve Fung | 18ca9b3 | 2015-09-12 03:21:59 -0700 | [diff] [blame] | 186 | # POSIX arithmetic expansion requires support of at least signed long integers. |
| 187 | # On 32-bit systems, that may mean 32-bit signed integers, in which case the |
| 188 | # 32-bit random number read from /dev/urandom may be interpreted as negative |
| 189 | # when used inside an arithmetic expansion (since the high bit might be set). |
| 190 | # mksh at least is known to behave this way. |
| 191 | # For this case, simply take the absolute value, which will still give a |
| 192 | # roughly uniform random distribution for the modulo (as we are merely ignoring |
| 193 | # the high/sign bit). |
| 194 | # See corresponding Arithmetic Expansion and Arithmetic Expression sections: |
| 195 | # POSIX: http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_04 |
| 196 | # mksh: http://linux.die.net/man/1/mksh |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 197 | generate_uniform_random() { |
| 198 | local max=$1 |
| 199 | local random="$(od -An -N4 -tu /dev/urandom)" |
Steve Fung | 18ca9b3 | 2015-09-12 03:21:59 -0700 | [diff] [blame] | 200 | echo $(((random < 0 ? -random : random) % max)) |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | # Check if sending a crash now does not exceed the maximum 24hr rate and |
| 204 | # commit to doing so, if not. |
| 205 | check_rate() { |
| 206 | mkdir -p ${TIMESTAMPS_DIR} |
| 207 | # Only consider minidumps written in the past 24 hours by removing all older. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 208 | find "${TIMESTAMPS_DIR}" -mindepth 1 -mtime +1 \ |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 209 | -exec rm -- '{}' ';' |
| 210 | local sends_in_24hrs=$(echo "${TIMESTAMPS_DIR}"/* | wc -w) |
| 211 | lecho "Current send rate: ${sends_in_24hrs}sends/24hrs" |
| 212 | if [ ${sends_in_24hrs} -ge ${MAX_CRASH_RATE} ]; then |
| 213 | lecho "Cannot send more crashes:" |
| 214 | lecho " current ${sends_in_24hrs}send/24hrs >= " \ |
| 215 | "max ${MAX_CRASH_RATE}send/24hrs" |
| 216 | return 1 |
| 217 | fi |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 218 | mktemp "${TIMESTAMPS_DIR}"/XXXXXX > /dev/null |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 219 | return 0 |
| 220 | } |
| 221 | |
| 222 | # Gets the base part of a crash report file, such as name.01234.5678.9012 from |
| 223 | # name.01234.5678.9012.meta or name.01234.5678.9012.log.tar.xz. We make sure |
| 224 | # "name" is sanitized in CrashCollector::Sanitize to not include any periods. |
| 225 | get_base() { |
| 226 | echo "$1" | cut -d. -f-4 |
| 227 | } |
| 228 | |
| 229 | get_extension() { |
| 230 | local extension="${1##*.}" |
| 231 | local filename="${1%.*}" |
| 232 | # For gzipped file, we ignore .gz and get the real extension |
| 233 | if [ "${extension}" = "gz" ]; then |
| 234 | echo "${filename##*.}" |
| 235 | else |
| 236 | echo "${extension}" |
| 237 | fi |
| 238 | } |
| 239 | |
| 240 | # Return which kind of report the given metadata file relates to |
| 241 | get_kind() { |
| 242 | local payload="$(get_key_value "$1" "payload")" |
| 243 | if [ ! -r "${payload}" ]; then |
| 244 | lecho "Missing payload: ${payload}" |
| 245 | echo "undefined" |
| 246 | return |
| 247 | fi |
| 248 | local kind="$(get_extension "${payload}")" |
| 249 | if [ "${kind}" = "dmp" ]; then |
| 250 | echo "minidump" |
| 251 | return |
| 252 | fi |
| 253 | echo "${kind}" |
| 254 | } |
| 255 | |
| 256 | get_key_value() { |
| 257 | local file="$1" key="$2" value |
| 258 | |
| 259 | if [ -f "${file}" ]; then |
| 260 | # Return the first entry. There shouldn't be more than one anyways. |
| 261 | # Substr at length($1) + 2 skips past the key and following = sign (awk |
| 262 | # uses 1-based indexes), but preserves embedded = characters. |
| 263 | value=$(sed -n "/^${key}[[:space:]]*=/{s:^[^=]*=::p;q}" "${file}") |
| 264 | fi |
| 265 | |
| 266 | echo "${value:-undefined}" |
| 267 | } |
| 268 | |
| 269 | get_keys() { |
| 270 | local file="$1" regex="$2" |
| 271 | |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 272 | cut -d '=' -f1 "${file}" | grep --color=never "${regex}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | # Return the channel name (sans "-channel" suffix). |
| 276 | get_channel() { |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 277 | getprop ro.product.channel | sed 's:-channel$::' |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | # Return the hardware class or "undefined". |
| 281 | get_hardware_class() { |
| 282 | if [ -r "${HWCLASS_PATH}" ]; then |
| 283 | cat "${HWCLASS_PATH}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 284 | else |
| 285 | echo "undefined" |
| 286 | fi |
| 287 | } |
| 288 | |
| 289 | send_crash() { |
| 290 | local meta_path="$1" |
| 291 | local report_payload="$(get_key_value "${meta_path}" "payload")" |
| 292 | local kind="$(get_kind "${meta_path}")" |
| 293 | local exec_name="$(get_key_value "${meta_path}" "exec_name")" |
Steve Fung | 44aec5f | 2015-09-12 02:52:06 -0700 | [diff] [blame] | 294 | local url="$(getprop crash_reporter.server)" |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 295 | local brillo_version="$(get_key_value "${meta_path}" "ver")" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 296 | local hwclass="$(get_hardware_class)" |
| 297 | local write_payload_size="$(get_key_value "${meta_path}" "payload_size")" |
| 298 | local log="$(get_key_value "${meta_path}" "log")" |
| 299 | local sig="$(get_key_value "${meta_path}" "sig")" |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 300 | local send_payload_size="$(stat -c "%s" "${report_payload}" 2>/dev/null)" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 301 | local product="$(get_key_value "${meta_path}" "upload_var_prod")" |
| 302 | local version="$(get_key_value "${meta_path}" "upload_var_ver")" |
| 303 | local upload_prefix="$(get_key_value "${meta_path}" "upload_prefix")" |
| 304 | local guid |
Steve Fung | ed78930 | 2015-09-14 16:14:23 -0700 | [diff] [blame] | 305 | local model_manifest_id="$(get_key_value "${WEAVE_CONF_FILE}" "model_id")" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 306 | |
Steve Fung | 44aec5f | 2015-09-12 02:52:06 -0700 | [diff] [blame] | 307 | # If crash_reporter.server is not set return with an error. |
| 308 | if [ -z "${url}" ]; then |
| 309 | lecho "Configuration error: crash_reporter.server not set." |
| 310 | return 1 |
| 311 | fi |
| 312 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 313 | set -- \ |
| 314 | -F "write_payload_size=${write_payload_size}" \ |
| 315 | -F "send_payload_size=${send_payload_size}" |
| 316 | if [ "${sig}" != "undefined" ]; then |
| 317 | set -- "$@" \ |
| 318 | -F "sig=${sig}" \ |
| 319 | -F "sig2=${sig}" |
| 320 | fi |
| 321 | if [ -r "${report_payload}" ]; then |
| 322 | set -- "$@" \ |
| 323 | -F "upload_file_${kind}=@${report_payload}" |
| 324 | fi |
| 325 | if [ "${log}" != "undefined" -a -r "${log}" ]; then |
| 326 | set -- "$@" \ |
| 327 | -F "log=@${log}" |
| 328 | fi |
| 329 | |
| 330 | if [ "${upload_prefix}" = "undefined" ]; then |
| 331 | upload_prefix="" |
| 332 | fi |
| 333 | |
| 334 | # Grab any variable that begins with upload_. |
| 335 | local v |
| 336 | for k in $(get_keys "${meta_path}" "^upload_"); do |
| 337 | v="$(get_key_value "${meta_path}" "${k}")" |
| 338 | case ${k} in |
| 339 | # Product & version are handled separately. |
| 340 | upload_var_prod) ;; |
| 341 | upload_var_ver) ;; |
| 342 | upload_var_*) |
| 343 | set -- "$@" -F "${upload_prefix}${k#upload_var_}=${v}" |
| 344 | ;; |
| 345 | upload_file_*) |
| 346 | if [ -r "${v}" ]; then |
| 347 | set -- "$@" -F "${upload_prefix}${k#upload_file_}=@${v}" |
| 348 | fi |
| 349 | ;; |
| 350 | esac |
| 351 | done |
| 352 | |
| 353 | # When uploading Chrome reports we need to report the right product and |
| 354 | # version. If the meta file does not specify it, use GOOGLE_CRASH_ID |
| 355 | # as the product and GOOGLE_CRASH_VERSION_ID as the version. |
| 356 | if [ "${product}" = "undefined" ]; then |
| 357 | product="$(get_key_value /etc/os-release 'GOOGLE_CRASH_ID')" |
| 358 | fi |
| 359 | if [ "${version}" = "undefined" ]; then |
| 360 | version="$(get_key_value /etc/os-release 'GOOGLE_CRASH_VERSION_ID')" |
| 361 | fi |
| 362 | |
| 363 | # If GOOGLE_CRASH_* is undefined, we look for ID and VERSION_ID in |
| 364 | # /etc/os-release. |
| 365 | if [ "${product}" = "undefined" ]; then |
| 366 | product="$(get_key_value /etc/os-release 'ID')" |
| 367 | fi |
| 368 | if [ "${version}" = "undefined" ]; then |
| 369 | version="$(get_key_value /etc/os-release 'VERSION_ID')" |
| 370 | fi |
| 371 | |
| 372 | # If ID or VERSION_ID is undefined, we use the default product name |
| 373 | # and CHROMEOS_RELEASE_VERSION from /etc/lsb-release. |
| 374 | if [ "${product}" = "undefined" ]; then |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 375 | product="${BRILLO_PRODUCT}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 376 | fi |
| 377 | if [ "${version}" = "undefined" ]; then |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 378 | version="${brillo_version}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 379 | fi |
| 380 | |
| 381 | local image_type |
| 382 | if is_test_image; then |
| 383 | image_type="test" |
| 384 | elif is_developer_image; then |
| 385 | image_type="dev" |
| 386 | elif [ ${FORCE_OFFICIAL} -ne 0 ]; then |
| 387 | image_type="force-official" |
| 388 | elif is_mock && ! is_mock_successful; then |
| 389 | image_type="mock-fail" |
| 390 | fi |
| 391 | |
| 392 | local boot_mode |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 393 | if is_developer_mode; then |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 394 | boot_mode="dev" |
| 395 | fi |
| 396 | |
| 397 | # Need to strip dashes ourselves as Chrome preserves it in the file |
| 398 | # nowadays. This is also what the Chrome breakpad client does. |
Steve Fung | 12e61ca | 2015-09-15 16:36:33 -0700 | [diff] [blame] | 399 | guid=$(tr -d '-' < "${GUID_FILE}") |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 400 | |
| 401 | local error_type="$(get_key_value "${meta_path}" "error_type")" |
| 402 | [ "${error_type}" = "undefined" ] && error_type= |
| 403 | |
| 404 | lecho "Sending crash:" |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 405 | if [ "${product}" != "${BRILLO_PRODUCT}" ]; then |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 406 | lecho " Sending crash report on behalf of ${product}" |
| 407 | fi |
| 408 | lecho " Metadata: ${meta_path} (${kind})" |
| 409 | lecho " Payload: ${report_payload}" |
| 410 | lecho " Version: ${version}" |
| 411 | [ -n "${image_type}" ] && lecho " Image type: ${image_type}" |
| 412 | [ -n "${boot_mode}" ] && lecho " Boot mode: ${boot_mode}" |
| 413 | if is_mock; then |
| 414 | lecho " Product: ${product}" |
| 415 | lecho " URL: ${url}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 416 | lecho " HWClass: ${hwclass}" |
| 417 | lecho " write_payload_size: ${write_payload_size}" |
| 418 | lecho " send_payload_size: ${send_payload_size}" |
| 419 | if [ "${log}" != "undefined" ]; then |
| 420 | lecho " log: @${log}" |
| 421 | fi |
| 422 | if [ "${sig}" != "undefined" ]; then |
| 423 | lecho " sig: ${sig}" |
| 424 | fi |
| 425 | fi |
| 426 | lecho " Exec name: ${exec_name}" |
| 427 | [ -n "${error_type}" ] && lecho " Error type: ${error_type}" |
| 428 | if is_mock; then |
| 429 | if ! is_mock_successful; then |
| 430 | lecho "Mocking unsuccessful send" |
| 431 | return 1 |
| 432 | fi |
| 433 | lecho "Mocking successful send" |
| 434 | return 0 |
| 435 | fi |
| 436 | |
| 437 | # Read in the first proxy, if any, for a given URL. NOTE: The |
| 438 | # double-quotes are necessary due to a bug in dash with the "local" |
| 439 | # builtin command and values that have spaces in them (see |
| 440 | # "https://bugs.launchpad.net/ubuntu/+source/dash/+bug/139097"). |
Dan Colish | 8a59e11 | 2015-06-03 15:11:21 -0700 | [diff] [blame] | 441 | if [ -f "${LIST_PROXIES}" ]; then |
| 442 | local proxy ret |
| 443 | proxy=$("${LIST_PROXIES}" --quiet "${url}") |
| 444 | ret=$? |
| 445 | if [ ${ret} -ne 0 ]; then |
| 446 | proxy='' |
| 447 | lecho -psyslog.warn \ |
| 448 | "Listing proxies failed with exit code ${ret}" |
| 449 | else |
| 450 | proxy=$(echo "${proxy}" | head -1) |
| 451 | fi |
| 452 | fi |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 453 | # if a direct connection should be used, unset the proxy variable. |
| 454 | [ "${proxy}" = "direct://" ] && proxy= |
| 455 | local report_id="${TMP_DIR}/report_id" |
| 456 | local curl_stderr="${TMP_DIR}/curl_stderr" |
| 457 | |
| 458 | set +e |
| 459 | curl "${url}" -v ${proxy:+--proxy "$proxy"} \ |
| 460 | --capath "${RESTRICTED_CERTIFICATES_PATH}" --ciphers HIGH \ |
| 461 | -F "prod=${product}" \ |
| 462 | -F "ver=${version}" \ |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 463 | -F "hwclass=${hwclass}" \ |
| 464 | -F "exec_name=${exec_name}" \ |
Steve Fung | ed78930 | 2015-09-14 16:14:23 -0700 | [diff] [blame] | 465 | -F "model_manifest_id=${model_manifest_id}" \ |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 466 | ${image_type:+-F "image_type=${image_type}"} \ |
| 467 | ${boot_mode:+-F "boot_mode=${boot_mode}"} \ |
| 468 | ${error_type:+-F "error_type=${error_type}"} \ |
| 469 | -F "guid=${guid}" \ |
| 470 | -o "${report_id}" \ |
| 471 | "$@" \ |
| 472 | 2>"${curl_stderr}" |
| 473 | curl_result=$? |
| 474 | set -e |
| 475 | |
| 476 | if [ ${curl_result} -eq 0 ]; then |
| 477 | local id="$(cat "${report_id}")" |
| 478 | local product_name |
| 479 | local timestamp="$(date +%s)" |
| 480 | case ${product} in |
| 481 | Chrome_ChromeOS) |
| 482 | if is_official_image; then |
| 483 | product_name="Chrome" |
| 484 | else |
| 485 | product_name="Chromium" |
| 486 | fi |
| 487 | ;; |
| 488 | *) |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 489 | product_name="Brillo" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 490 | ;; |
| 491 | esac |
| 492 | printf '%s,%s,%s\n' \ |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 493 | "${timestamp}" "${id}" "${product_name}" >> "${CRASH_LOG}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 494 | lecho "Crash report receipt ID ${id}" |
| 495 | else |
| 496 | lecho "Crash sending failed with exit code ${curl_result}: " \ |
| 497 | "$(cat "${curl_stderr}")" |
| 498 | fi |
| 499 | |
| 500 | rm -f "${report_id}" |
| 501 | |
| 502 | return ${curl_result} |
| 503 | } |
| 504 | |
| 505 | # *.meta files always end with done=1 so we can tell if they are complete. |
| 506 | is_complete_metadata() { |
| 507 | grep -q "done=1" "$1" |
| 508 | } |
| 509 | |
| 510 | # Remove the given report path. |
| 511 | remove_report() { |
| 512 | local base="${1%.*}" |
| 513 | rm -f -- "${base}".* |
| 514 | } |
| 515 | |
| 516 | # Send all crashes from the given directory. This applies even when we're on a |
| 517 | # 3G connection (see crosbug.com/3304 for discussion). |
| 518 | send_crashes() { |
| 519 | local dir="$1" |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 520 | lecho "Sending crashes for ${dir}" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 521 | |
| 522 | if [ ! -d "${dir}" ]; then |
| 523 | return |
| 524 | fi |
| 525 | |
| 526 | # Consider any old files which still have no corresponding meta file |
| 527 | # as orphaned, and remove them. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 528 | for old_file in $(find "${dir}" -mindepth 1 \ |
| 529 | -mtime +1 -type f); do |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 530 | if [ ! -e "$(get_base "${old_file}").meta" ]; then |
| 531 | lecho "Removing old orphaned file: ${old_file}." |
| 532 | rm -f -- "${old_file}" |
| 533 | fi |
| 534 | done |
| 535 | |
| 536 | # Look through all metadata (*.meta) files, oldest first. That way, the rate |
| 537 | # limit does not stall old crashes if there's a high amount of new crashes |
| 538 | # coming in. |
| 539 | # For each crash report, first evaluate conditions that might lead to its |
| 540 | # removal to honor user choice and to free disk space as soon as possible, |
| 541 | # then decide whether it should be sent right now or kept for later sending. |
| 542 | for meta_path in $(ls -1tr "${dir}"/*.meta 2>/dev/null); do |
| 543 | lecho "Considering metadata ${meta_path}." |
| 544 | |
| 545 | local kind=$(get_kind "${meta_path}") |
| 546 | if [ "${kind}" != "minidump" ] && \ |
| 547 | [ "${kind}" != "kcrash" ] && \ |
Peter Qiu | 6aa551e | 2015-03-06 11:42:04 -0800 | [diff] [blame] | 548 | [ "${kind}" != "log" ] && |
| 549 | [ "${kind}" != "devcore" ]; then |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 550 | lecho "Unknown report kind ${kind}. Removing report." |
| 551 | remove_report "${meta_path}" |
| 552 | continue |
| 553 | fi |
| 554 | |
| 555 | if ! is_complete_metadata "${meta_path}"; then |
| 556 | # This report is incomplete, so if it's old, just remove it. |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 557 | local old_meta=$(find "${dir}" -mindepth 1 -name \ |
| 558 | $(basename "${meta_path}") -mtime +1 -type f) |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 559 | if [ -n "${old_meta}" ]; then |
| 560 | lecho "Removing old incomplete metadata." |
| 561 | remove_report "${meta_path}" |
| 562 | else |
| 563 | lecho "Ignoring recent incomplete metadata." |
| 564 | fi |
| 565 | continue |
| 566 | fi |
| 567 | |
Peter Qiu | 6aa551e | 2015-03-06 11:42:04 -0800 | [diff] [blame] | 568 | # Ignore device coredump if device coredump uploading is not allowed. |
| 569 | if [ "${kind}" = "devcore" ] && ! is_device_coredump_upload_allowed; then |
| 570 | lecho "Ignoring device coredump. Device coredump upload not allowed." |
| 571 | continue |
| 572 | fi |
| 573 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 574 | if ! is_mock && ! is_official_image; then |
| 575 | lecho "Not an official OS version. Removing crash." |
| 576 | remove_report "${meta_path}" |
| 577 | continue |
| 578 | fi |
| 579 | |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 580 | # Remove existing crashes in case user consent has not (yet) been given or |
| 581 | # has been revoked. This must come after the guest mode check because |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 582 | # metrics_client always returns "not consented" in guest mode. |
| 583 | if ! metrics_client -c; then |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 584 | lecho "Crash reporting is disabled. Removing crash." |
| 585 | remove_report "${meta_path}" |
| 586 | continue |
| 587 | fi |
| 588 | |
| 589 | # Skip report if the upload rate is exceeded. (Don't exit right now because |
| 590 | # subsequent reports may be candidates for deletion.) |
| 591 | if ! check_rate; then |
| 592 | lecho "Sending ${meta_path} would exceed rate. Leaving for later." |
| 593 | continue |
| 594 | fi |
| 595 | |
| 596 | # The .meta file should be written *after* all to-be-uploaded files that it |
| 597 | # references. Nevertheless, as a safeguard, a hold-off time of thirty |
| 598 | # seconds after writing the .meta file is ensured. Also, sending of crash |
| 599 | # reports is spread out randomly by up to SECONDS_SEND_SPREAD. Thus, for |
| 600 | # the sleep call the greater of the two delays is used. |
| 601 | local now=$(date +%s) |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 602 | local holdoff_time=$(($(stat -c "%Y" "${meta_path}") + 30 - ${now})) |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 603 | local spread_time=$(generate_uniform_random "${SECONDS_SEND_SPREAD}") |
| 604 | local sleep_time |
| 605 | if [ ${spread_time} -gt ${holdoff_time} ]; then |
| 606 | sleep_time="${spread_time}" |
| 607 | else |
| 608 | sleep_time="${holdoff_time}" |
| 609 | fi |
| 610 | lecho "Scheduled to send in ${sleep_time}s." |
| 611 | if ! is_mock; then |
| 612 | if ! sleep "${sleep_time}"; then |
| 613 | lecho "Sleep failed" |
| 614 | return 1 |
| 615 | fi |
| 616 | fi |
| 617 | |
| 618 | # Try to upload. |
| 619 | if ! send_crash "${meta_path}"; then |
| 620 | lecho "Problem sending ${meta_path}, not removing." |
| 621 | continue |
| 622 | fi |
| 623 | |
| 624 | # Send was successful, now remove. |
| 625 | lecho "Successfully sent crash ${meta_path} and removing." |
| 626 | remove_report "${meta_path}" |
| 627 | done |
| 628 | } |
| 629 | |
| 630 | usage() { |
| 631 | cat <<EOF |
| 632 | Usage: crash_sender [options] |
| 633 | |
| 634 | Options: |
| 635 | -e <var>=<val> Set env |var| to |val| (only some vars) |
| 636 | EOF |
| 637 | exit ${1:-1} |
| 638 | } |
| 639 | |
| 640 | parseargs() { |
| 641 | # Parse the command line arguments. |
| 642 | while [ $# -gt 0 ]; do |
| 643 | case $1 in |
| 644 | -e) |
| 645 | shift |
| 646 | case $1 in |
| 647 | FORCE_OFFICIAL=*|\ |
| 648 | MAX_CRASH_RATE=*|\ |
| 649 | MOCK_DEVELOPER_MODE=*|\ |
| 650 | OVERRIDE_PAUSE_SENDING=*|\ |
| 651 | SECONDS_SEND_SPREAD=*) |
| 652 | export "$1" |
| 653 | ;; |
| 654 | *) |
| 655 | lecho "Unknown var passed to -e: $1" |
| 656 | exit 1 |
| 657 | ;; |
| 658 | esac |
| 659 | ;; |
| 660 | -h) |
| 661 | usage 0 |
| 662 | ;; |
| 663 | *) |
| 664 | lecho "Unknown options: $*" |
| 665 | exit 1 |
| 666 | ;; |
| 667 | esac |
| 668 | shift |
| 669 | done |
| 670 | } |
| 671 | |
| 672 | main() { |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 673 | parseargs "$@" |
| 674 | |
| 675 | if [ -e "${PAUSE_CRASH_SENDING}" ] && \ |
| 676 | [ ${OVERRIDE_PAUSE_SENDING} -eq 0 ]; then |
| 677 | lecho "Exiting early due to ${PAUSE_CRASH_SENDING}." |
| 678 | exit 1 |
| 679 | fi |
| 680 | |
| 681 | if is_test_image; then |
| 682 | lecho "Exiting early due to test image." |
| 683 | exit 1 |
| 684 | fi |
| 685 | |
| 686 | # We don't perform checks on this because we have a master lock with the |
| 687 | # CRASH_SENDER_LOCK file. This pid file is for the system to keep track |
| 688 | # (like with autotests) that we're still running. |
| 689 | echo $$ > "${RUN_FILE}" |
| 690 | |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 691 | for dependency in "${RESTRICTED_CERTIFICATES_PATH}"; do |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 692 | if [ ! -x "${dependency}" ]; then |
| 693 | lecho "Fatal: Crash sending disabled: ${dependency} not found." |
| 694 | exit 1 |
| 695 | fi |
| 696 | done |
| 697 | |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 698 | TMP_DIR="$(mktemp -d "${CRASH_STATE_DIR}/tmp/crash_sender.XXXXXX")" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 699 | |
| 700 | # Send system-wide crashes |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 701 | send_crashes "${CRASH_STATE_DIR}/crash" |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Steve Fung | 0e8746d | 2015-08-20 17:07:50 -0700 | [diff] [blame] | 704 | trap cleanup EXIT INT TERM |
| 705 | |
| 706 | #TODO(http://b/23937249): Change the locking logic back to using flock. |
| 707 | if ! mkdir "${CRASH_SENDER_LOCK}" 2>/dev/null; then |
Benjamin Lerman | c8cb4ac | 2014-09-11 12:43:41 +0000 | [diff] [blame] | 708 | lecho "Already running; quitting." |
| 709 | crash_done |
| 710 | exit 1 |
| 711 | fi |
| 712 | main "$@" |