OSDN Git Service

jni: correct the database path
[android-x86/external-koush-Superuser.git] / Superuser / jni / reboot / reboot.c
1 /*
2 ** Copyright 2013, Kevin Cernekee <cernekee@gmail.com>
3 **
4 ** This was reverse engineered from an HTC "reboot" binary and is an attempt
5 ** to remain bug-compatible with the original.
6 **
7 ** Licensed under the Apache License, Version 2.0 (the "License");
8 ** you may not use this file except in compliance with the License.
9 ** You may obtain a copy of the License at
10 **
11 **     http://www.apache.org/licenses/LICENSE-2.0
12 **
13 ** Unless required by applicable law or agreed to in writing, software
14 ** distributed under the License is distributed on an "AS IS" BASIS,
15 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ** See the License for the specific language governing permissions and
17 ** limitations under the License.
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include <sqlite3.h>
25 #include <sys/reboot.h>
26 #include <android/log.h>
27
28 #define SETTINGS_DB             "/data/data/com.android.providers.settings/databases/settings.db"
29 #define SCREEN_LOCK_STATUS      "/data/misc/screen_lock_status"
30
31 int pattern_lock;
32
33 int sqlcallback(void *private, int n_columns, char **col_values, char **col_names)
34 {
35         pattern_lock = 0;
36
37         if (n_columns == 0 || !col_values[0])
38                 return 0;
39         
40         if (!strcmp(col_values[0], "1"))
41                 pattern_lock = 1;
42
43         __android_log_print(ANDROID_LOG_INFO, NULL,
44                             "sqlcallback %s = %s, pattern_locks= %d\n",
45                             col_names[0], col_values[0], pattern_lock);
46         return 0;
47 }
48
49 int checkPatternLock(void)
50 {
51         sqlite3 *pDb = NULL;
52         char *errmsg = NULL;
53
54         if (sqlite3_open(SETTINGS_DB, &pDb) != 0) {
55                 __android_log_print(ANDROID_LOG_ERROR, NULL,
56                                     "sqlite3_open error");
57                 /* BUG? probably shouldn't call sqlite3_close() if open failed */
58                 goto out;
59         }
60
61         if (sqlite3_exec(pDb,
62                          "select value from system where name= \"lock_pattern_autolock\"",
63                          sqlcallback, "checkPatternLock", &errmsg) != 0) {
64                 __android_log_print(ANDROID_LOG_ERROR, NULL,
65                                     "SQL error: %s\n", errmsg);
66                 sqlite3_free(errmsg);
67                 goto out;
68         }
69
70 out:
71         sqlite3_close(pDb);
72         return 0;
73 }
74
75 int main(int argc, char **argv)
76 {
77         int no_sync = 0, power_off = 0;
78         int ret;
79
80         opterr = 0;
81         while ((ret = getopt(argc, argv, "np")) != -1) {
82                 switch (ret) {
83                 case 'n':
84                         no_sync = 1;
85                         break;
86                 case 'p':
87                         power_off = 1;
88                         break;
89                 case '?':
90                         fprintf(stderr, "usage: %s [-n] [-p] [rebootcommand]\n",
91                                 argv[0]);
92                         exit(1);
93                         break;
94                 }
95         }
96
97         if (argc > (optind + 1)) {
98                 fprintf(stderr, "%s: too many arguments\n", argv[0]);
99                 exit(1);
100         }
101
102         /* BUG: this should use optind */
103         if (argc > 1 && !strcmp(argv[1], "oem-78")) {
104                 /* HTC RUU mode: "reboot oem-78" */
105
106                 FILE *f;
107                 char buf[5];
108
109                 checkPatternLock();
110                 f = fopen(SCREEN_LOCK_STATUS, "r");
111                 if (!f) {
112                         fputs("5\n", stderr);
113                         exit(0);
114                 }
115                 fgets(buf, 5, f);
116                 if (atoi(buf) == 1) {
117                         if (pattern_lock != 0) {
118                                 fputs("1\n", stderr);
119                                 exit(0);
120                         }
121                 }
122                 fputs("0\n", stderr);
123         }
124
125         if (!no_sync)
126                 sync();
127
128         if (power_off) {
129                 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
130                                LINUX_REBOOT_CMD_POWER_OFF, 0);
131         } else if (optind >= argc) {
132                 ret = reboot(LINUX_REBOOT_CMD_RESTART);
133         } else {
134                 ret = __reboot(LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2,
135                                LINUX_REBOOT_CMD_RESTART2, argv[optind]);
136         }
137
138         if (ret < 0) {
139                 perror("reboot");
140                 exit(1);
141         } else {
142                 fputs("reboot returned\n", stderr);
143                 exit(0);
144         }
145 }