summaryrefslogtreecommitdiffstats
path: root/tools/android/adb_reboot/adb_reboot.c
blob: 48c5cbe58f251d054aec0ecc087de290c35df829 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

int main(int argc, char ** argv) {
  int i = fork();
  struct stat ft;
  time_t ct;

  if (i < 0) {
    printf("fork error");
    return 1;
  }
  if (i > 0)
    return 0;

  /* child (daemon) continues */
  int j;
  for (j = 0; j < getdtablesize(); j++)
    close(j);

  setsid(); /* obtain a new process group */

  while (1) {
    sleep(120);

    stat("/sdcard/host_heartbeat", &ft);
    time(&ct);
    if (ct - ft.st_mtime  > 120) {
      /* File was not touched for some time. */
      system("su -c reboot");
    }
  }

  return 0;
}