blob: 8a422ddc4bade964c9e6e4f417615393ea2d82a7 [file] [log] [blame]
Steve Fung0e8746d2015-08-20 17:07:50 -07001#!/system/bin/sh
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +00002
Steve Fung6c34c252015-08-20 00:27:30 -07003# 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 Lermanc8cb4ac2014-09-11 12:43:41 +000016
17set -e
18
19# Default product ID in crash report (used if GOOGLE_CRASH_* is undefined).
Steve Fung0e8746d2015-08-20 17:07:50 -070020BRILLO_PRODUCT=Brillo
21
22# Base directory that contains any crash reporter state files.
23CRASH_STATE_DIR="/data/misc/crash_reporter"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000024
Steve Fung12e61ca2015-09-15 16:36:33 -070025# File containing crash_reporter's anonymized guid.
26GUID_FILE="${CRASH_STATE_DIR}/guid"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000027
28# Crash sender lock in case the sender is already running.
Steve Fung0e8746d2015-08-20 17:07:50 -070029CRASH_SENDER_LOCK="${CRASH_STATE_DIR}/lock/crash_sender"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000030
31# Path to file that indicates a crash test is currently running.
Steve Fung0e8746d2015-08-20 17:07:50 -070032CRASH_TEST_IN_PROGRESS_FILE="${CRASH_STATE_DIR}/tmp/crash-test-in-progress"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000033
34# Set this to 1 in the environment to allow uploading crash reports
35# for unofficial versions.
36FORCE_OFFICIAL=${FORCE_OFFICIAL:-0}
37
38# Path to hardware class description.
39HWCLASS_PATH="/sys/devices/platform/chromeos_acpi/HWID"
40
41# Path to file that indicates this is a developer image.
Steve Fung0e8746d2015-08-20 17:07:50 -070042LEAVE_CORE_FILE="${CRASH_STATE_DIR}/.leave_core"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000043
44# Path to list_proxies.
Steve Fung0e8746d2015-08-20 17:07:50 -070045LIST_PROXIES="list_proxies"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000046
47# Maximum crashes to send per day.
48MAX_CRASH_RATE=${MAX_CRASH_RATE:-32}
49
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000050# File whose existence mocks crash sending. If empty we pretend the
51# crash sending was successful, otherwise unsuccessful.
Steve Fung0e8746d2015-08-20 17:07:50 -070052MOCK_CRASH_SENDING="${CRASH_STATE_DIR}/tmp/mock-crash-sending"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000053
54# Set this to 1 in the environment to pretend to have booted in developer
55# mode. This is used by autotests.
56MOCK_DEVELOPER_MODE=${MOCK_DEVELOPER_MODE:-0}
57
58# Ignore PAUSE_CRASH_SENDING file if set.
59OVERRIDE_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 Fung0e8746d2015-08-20 17:07:50 -070063PAUSE_CRASH_SENDING="${CRASH_STATE_DIR}/lock/crash_sender_paused"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000064
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000065# Path to a directory of restricted certificates which includes
Steve Fung44aec5f2015-09-12 02:52:06 -070066# a certificate for the crash server.
Steve Fung0e8746d2015-08-20 17:07:50 -070067RESTRICTED_CERTIFICATES_PATH="/system/etc/security/cacerts"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000068
69# File whose existence implies we're running and not to start again.
Steve Fung0e8746d2015-08-20 17:07:50 -070070RUN_FILE="${CRASH_STATE_DIR}/run/crash_sender.pid"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000071
72# Maximum time to sleep between sends.
73SECONDS_SEND_SPREAD=${SECONDS_SEND_SPREAD:-600}
74
Peter Qiu6aa551e2015-03-06 11:42:04 -080075# Set this to 1 to allow uploading of device coredumps.
Steve Fung0e8746d2015-08-20 17:07:50 -070076DEVCOREDUMP_UPLOAD_FLAG_FILE="${CRASH_STATE_DIR}/device_coredump_upload_allowed"
Peter Qiu6aa551e2015-03-06 11:42:04 -080077
Steve Funged789302015-09-14 16:14:23 -070078# The weave configuration file.
79WEAVE_CONF_FILE="/etc/weaved/weaved.conf"
80
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000081# The syslog tag for all logging we emit.
82TAG="$(basename $0)[$$]"
83
84# Directory to store timestamp files indicating the uploads in the past 24
85# hours.
Steve Fung0e8746d2015-08-20 17:07:50 -070086TIMESTAMPS_DIR="${CRASH_STATE_DIR}/crash_sender"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000087
88# Temp directory for this process.
89TMP_DIR=""
90
Steve Fung0e8746d2015-08-20 17:07:50 -070091# Crash report log file.
92CRASH_LOG="${CRASH_STATE_DIR}/log/uploads.log"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000093
94lecho() {
Steve Fung0e8746d2015-08-20 17:07:50 -070095 log -t "${TAG}" "$@"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +000096}
97
98# Returns true if mock is enabled.
99is_mock() {
100 [ -f "${MOCK_CRASH_SENDING}" ] && return 0
101 return 1
102}
103
104is_mock_successful() {
105 local mock_in=$(cat "${MOCK_CRASH_SENDING}")
106 [ "${mock_in}" = "" ] && return 0 # empty file means success
107 return 1
108}
109
110cleanup() {
111 if [ -n "${TMP_DIR}" ]; then
112 rm -rf "${TMP_DIR}"
113 fi
114 rm -f "${RUN_FILE}"
Steve Fung0e8746d2015-08-20 17:07:50 -0700115 if [ -n "${CRASH_SENDER_LOCK}" ]; then
116 rm -rf "${CRASH_SENDER_LOCK}"
117 fi
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000118 crash_done
119}
120
121crash_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
129is_official_image() {
130 [ ${FORCE_OFFICIAL} -ne 0 ] && return 0
Steve Fung16192142015-09-29 01:01:38 -0700131 if [ "$(getprop ro.secure)" = "1" ]; then
132 return 0
133 else
134 return 1
135 fi
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000136}
137
138# Returns 0 if the a crash test is currently running. NOTE: Mirrors
139# crash_collector.cc:CrashCollector::IsCrashTestInProgress().
140is_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().
147is_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.
156is_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.
167is_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 Fung57c74862015-09-28 18:00:16 -0700172 if [ "$(getprop ro.debuggable)" = "1" ]; then
Steve Fung0e8746d2015-08-20 17:07:50 -0700173 return 0
174 else
175 return 1
176 fi
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000177}
178
Peter Qiu6aa551e2015-03-06 11:42:04 -0800179# Return 0 if the uploading of device coredumps is allowed.
180is_device_coredump_upload_allowed() {
181 [ -f "${DEVCOREDUMP_UPLOAD_FLAG_FILE}" ] && return 0
182 return 1
183}
184
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000185# Generate a uniform random number in 0..max-1.
Steve Fung18ca9b32015-09-12 03:21:59 -0700186# 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000197generate_uniform_random() {
198 local max=$1
199 local random="$(od -An -N4 -tu /dev/urandom)"
Steve Fung18ca9b32015-09-12 03:21:59 -0700200 echo $(((random < 0 ? -random : random) % max))
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000201}
202
203# Check if sending a crash now does not exceed the maximum 24hr rate and
204# commit to doing so, if not.
205check_rate() {
206 mkdir -p ${TIMESTAMPS_DIR}
207 # Only consider minidumps written in the past 24 hours by removing all older.
Steve Fung0e8746d2015-08-20 17:07:50 -0700208 find "${TIMESTAMPS_DIR}" -mindepth 1 -mtime +1 \
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000209 -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 Fung0e8746d2015-08-20 17:07:50 -0700218 mktemp "${TIMESTAMPS_DIR}"/XXXXXX > /dev/null
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000219 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.
225get_base() {
226 echo "$1" | cut -d. -f-4
227}
228
229get_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
241get_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
256get_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
269get_keys() {
270 local file="$1" regex="$2"
271
Steve Fung0e8746d2015-08-20 17:07:50 -0700272 cut -d '=' -f1 "${file}" | grep --color=never "${regex}"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000273}
274
275# Return the channel name (sans "-channel" suffix).
276get_channel() {
Steve Fung0e8746d2015-08-20 17:07:50 -0700277 getprop ro.product.channel | sed 's:-channel$::'
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000278}
279
280# Return the hardware class or "undefined".
281get_hardware_class() {
282 if [ -r "${HWCLASS_PATH}" ]; then
283 cat "${HWCLASS_PATH}"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000284 else
285 echo "undefined"
286 fi
287}
288
289send_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 Fung44aec5f2015-09-12 02:52:06 -0700294 local url="$(getprop crash_reporter.server)"
Steve Fung0e8746d2015-08-20 17:07:50 -0700295 local brillo_version="$(get_key_value "${meta_path}" "ver")"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000296 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 Fung0e8746d2015-08-20 17:07:50 -0700300 local send_payload_size="$(stat -c "%s" "${report_payload}" 2>/dev/null)"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000301 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 Funged789302015-09-14 16:14:23 -0700305 local model_manifest_id="$(get_key_value "${WEAVE_CONF_FILE}" "model_id")"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000306
Steve Fung44aec5f2015-09-12 02:52:06 -0700307 # 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000313 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 Fung0e8746d2015-08-20 17:07:50 -0700375 product="${BRILLO_PRODUCT}"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000376 fi
377 if [ "${version}" = "undefined" ]; then
Steve Fung0e8746d2015-08-20 17:07:50 -0700378 version="${brillo_version}"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000379 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 Fung0e8746d2015-08-20 17:07:50 -0700393 if is_developer_mode; then
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000394 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 Fung12e61ca2015-09-15 16:36:33 -0700399 guid=$(tr -d '-' < "${GUID_FILE}")
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000400
401 local error_type="$(get_key_value "${meta_path}" "error_type")"
402 [ "${error_type}" = "undefined" ] && error_type=
403
404 lecho "Sending crash:"
Steve Fung0e8746d2015-08-20 17:07:50 -0700405 if [ "${product}" != "${BRILLO_PRODUCT}" ]; then
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000406 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000416 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 Colish8a59e112015-06-03 15:11:21 -0700441 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000453 # 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000463 -F "hwclass=${hwclass}" \
464 -F "exec_name=${exec_name}" \
Steve Funged789302015-09-14 16:14:23 -0700465 -F "model_manifest_id=${model_manifest_id}" \
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000466 ${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 Fung0e8746d2015-08-20 17:07:50 -0700489 product_name="Brillo"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000490 ;;
491 esac
492 printf '%s,%s,%s\n' \
Steve Fung0e8746d2015-08-20 17:07:50 -0700493 "${timestamp}" "${id}" "${product_name}" >> "${CRASH_LOG}"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000494 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.
506is_complete_metadata() {
507 grep -q "done=1" "$1"
508}
509
510# Remove the given report path.
511remove_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).
518send_crashes() {
519 local dir="$1"
Steve Fung0e8746d2015-08-20 17:07:50 -0700520 lecho "Sending crashes for ${dir}"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000521
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 Fung0e8746d2015-08-20 17:07:50 -0700528 for old_file in $(find "${dir}" -mindepth 1 \
529 -mtime +1 -type f); do
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000530 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 Qiu6aa551e2015-03-06 11:42:04 -0800548 [ "${kind}" != "log" ] &&
549 [ "${kind}" != "devcore" ]; then
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000550 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 Fung0e8746d2015-08-20 17:07:50 -0700557 local old_meta=$(find "${dir}" -mindepth 1 -name \
558 $(basename "${meta_path}") -mtime +1 -type f)
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000559 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 Qiu6aa551e2015-03-06 11:42:04 -0800568 # 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000574 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 Lermanc8cb4ac2014-09-11 12:43:41 +0000580 # 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 Fung0e8746d2015-08-20 17:07:50 -0700582 # metrics_client always returns "not consented" in guest mode.
583 if ! metrics_client -c; then
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000584 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 Fung0e8746d2015-08-20 17:07:50 -0700602 local holdoff_time=$(($(stat -c "%Y" "${meta_path}") + 30 - ${now}))
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000603 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
630usage() {
631 cat <<EOF
632Usage: crash_sender [options]
633
634Options:
635 -e <var>=<val> Set env |var| to |val| (only some vars)
636EOF
637 exit ${1:-1}
638}
639
640parseargs() {
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
672main() {
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000673 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 Fung0e8746d2015-08-20 17:07:50 -0700691 for dependency in "${RESTRICTED_CERTIFICATES_PATH}"; do
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000692 if [ ! -x "${dependency}" ]; then
693 lecho "Fatal: Crash sending disabled: ${dependency} not found."
694 exit 1
695 fi
696 done
697
Steve Fung0e8746d2015-08-20 17:07:50 -0700698 TMP_DIR="$(mktemp -d "${CRASH_STATE_DIR}/tmp/crash_sender.XXXXXX")"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000699
700 # Send system-wide crashes
Steve Fung0e8746d2015-08-20 17:07:50 -0700701 send_crashes "${CRASH_STATE_DIR}/crash"
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000702}
703
Steve Fung0e8746d2015-08-20 17:07:50 -0700704trap cleanup EXIT INT TERM
705
706#TODO(http://b/23937249): Change the locking logic back to using flock.
707if ! mkdir "${CRASH_SENDER_LOCK}" 2>/dev/null; then
Benjamin Lermanc8cb4ac2014-09-11 12:43:41 +0000708 lecho "Already running; quitting."
709 crash_done
710 exit 1
711fi
712main "$@"