adb: add "adb reboot" command.

This will allow rebooting the device via adb on any build, including user builds.
An optional argument can be provided
(for example, "adb reboot bootloader" or adb reboot recovery")

Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/services.c b/services.c
index 78d092b..517da55 100644
--- a/services.c
+++ b/services.c
@@ -33,6 +33,7 @@
 #  endif
 #else
 #include <sys/poll.h>
+#include <sys/reboot.h>
 #endif
 
 typedef struct stinfo stinfo;
@@ -133,6 +134,20 @@
     }
 }
 
+void reboot_service(int fd, char *arg)
+{
+    char buf[100];
+    int ret;
+
+    sync();
+    ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, LINUX_REBOOT_CMD_RESTART2, arg);
+    if (ret < 0) {
+        snprintf(buf, sizeof(buf), "reboot failed: %s\n", strerror(errno));
+        writex(fd, buf, strlen(buf));
+    }
+    adb_close(fd);
+}
+
 #endif
 
 #if 0
@@ -399,6 +414,11 @@
         ret = create_service_thread(file_sync_service, NULL);
     } else if(!strncmp(name, "remount:", 8)) {
         ret = create_service_thread(remount_service, NULL);
+    } else if(!strncmp(name, "reboot:", 7)) {
+        char* arg = name + 7;
+        if (*name == 0)
+            arg = NULL;
+        ret = create_service_thread(reboot_service, arg);
     } else if(!strncmp(name, "root:", 5)) {
         ret = create_service_thread(restart_root_service, NULL);
 #endif