1*ec779b8eSAndroid Build Coastguard Worker /*
2*ec779b8eSAndroid Build Coastguard Worker * Copyright (C) 2020 The Android Open Source Project
3*ec779b8eSAndroid Build Coastguard Worker *
4*ec779b8eSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*ec779b8eSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*ec779b8eSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*ec779b8eSAndroid Build Coastguard Worker *
8*ec779b8eSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*ec779b8eSAndroid Build Coastguard Worker *
10*ec779b8eSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*ec779b8eSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*ec779b8eSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*ec779b8eSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*ec779b8eSAndroid Build Coastguard Worker * limitations under the License.
15*ec779b8eSAndroid Build Coastguard Worker */
16*ec779b8eSAndroid Build Coastguard Worker
17*ec779b8eSAndroid Build Coastguard Worker #define LOG_TAG "Watchdog"
18*ec779b8eSAndroid Build Coastguard Worker
19*ec779b8eSAndroid Build Coastguard Worker #include <watchdog/Watchdog.h>
20*ec779b8eSAndroid Build Coastguard Worker
21*ec779b8eSAndroid Build Coastguard Worker #include <android-base/logging.h>
22*ec779b8eSAndroid Build Coastguard Worker #include <android-base/threads.h>
23*ec779b8eSAndroid Build Coastguard Worker #include <signal.h>
24*ec779b8eSAndroid Build Coastguard Worker #include <time.h>
25*ec779b8eSAndroid Build Coastguard Worker #include <cstring>
26*ec779b8eSAndroid Build Coastguard Worker #include <utils/Log.h>
27*ec779b8eSAndroid Build Coastguard Worker
28*ec779b8eSAndroid Build Coastguard Worker namespace android {
29*ec779b8eSAndroid Build Coastguard Worker
Watchdog(::std::chrono::steady_clock::duration timeout)30*ec779b8eSAndroid Build Coastguard Worker Watchdog::Watchdog(::std::chrono::steady_clock::duration timeout) {
31*ec779b8eSAndroid Build Coastguard Worker // Create the timer.
32*ec779b8eSAndroid Build Coastguard Worker struct sigevent sev;
33*ec779b8eSAndroid Build Coastguard Worker sev.sigev_notify = SIGEV_THREAD_ID;
34*ec779b8eSAndroid Build Coastguard Worker sev.sigev_notify_thread_id = base::GetThreadId();
35*ec779b8eSAndroid Build Coastguard Worker sev.sigev_signo = SIGABRT;
36*ec779b8eSAndroid Build Coastguard Worker sev.sigev_value.sival_ptr = &mTimerId;
37*ec779b8eSAndroid Build Coastguard Worker int err = timer_create(CLOCK_MONOTONIC, &sev, &mTimerId);
38*ec779b8eSAndroid Build Coastguard Worker if (err != 0) {
39*ec779b8eSAndroid Build Coastguard Worker PLOG(FATAL) << "Failed to create timer";
40*ec779b8eSAndroid Build Coastguard Worker }
41*ec779b8eSAndroid Build Coastguard Worker
42*ec779b8eSAndroid Build Coastguard Worker // Start the timer.
43*ec779b8eSAndroid Build Coastguard Worker struct itimerspec spec;
44*ec779b8eSAndroid Build Coastguard Worker memset(&spec, 0, sizeof(spec));
45*ec779b8eSAndroid Build Coastguard Worker auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(timeout);
46*ec779b8eSAndroid Build Coastguard Worker LOG_ALWAYS_FATAL_IF(timeout.count() <= 0, "Duration must be positive");
47*ec779b8eSAndroid Build Coastguard Worker spec.it_value.tv_sec = ns.count() / 1000000000;
48*ec779b8eSAndroid Build Coastguard Worker spec.it_value.tv_nsec = ns.count() % 1000000000;
49*ec779b8eSAndroid Build Coastguard Worker err = timer_settime(mTimerId, 0, &spec, nullptr);
50*ec779b8eSAndroid Build Coastguard Worker if (err != 0) {
51*ec779b8eSAndroid Build Coastguard Worker PLOG(FATAL) << "Failed to start timer";
52*ec779b8eSAndroid Build Coastguard Worker }
53*ec779b8eSAndroid Build Coastguard Worker }
54*ec779b8eSAndroid Build Coastguard Worker
~Watchdog()55*ec779b8eSAndroid Build Coastguard Worker Watchdog::~Watchdog() {
56*ec779b8eSAndroid Build Coastguard Worker // Delete the timer.
57*ec779b8eSAndroid Build Coastguard Worker int err = timer_delete(mTimerId);
58*ec779b8eSAndroid Build Coastguard Worker if (err != 0) {
59*ec779b8eSAndroid Build Coastguard Worker PLOG(FATAL) << "Failed to delete timer";
60*ec779b8eSAndroid Build Coastguard Worker }
61*ec779b8eSAndroid Build Coastguard Worker }
62*ec779b8eSAndroid Build Coastguard Worker
63*ec779b8eSAndroid Build Coastguard Worker } // namespace android
64