OSDN Git Service

add reboot binary
[android-x86/external-koush-Superuser.git] / Superuser / jni / su / su.h
1 /*
2 ** Copyright 2010, Adam Shanks (@ChainsDD)
3 ** Copyright 2008, Zinx Verituse (@zinxv)
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.
16 */
17
18 #ifndef SU_h 
19 #define SU_h 1
20
21 #ifdef LOG_TAG
22 #undef LOG_TAG
23 #endif
24 #define LOG_TAG "su"
25
26 #ifndef AID_SHELL
27 #define AID_SHELL (get_shell_uid())
28 #endif
29
30 #ifndef AID_ROOT
31 #define AID_ROOT  0
32 #endif
33
34 #ifndef AID_SYSTEM
35 #define AID_SYSTEM (get_system_uid())
36 #endif
37
38 #ifndef AID_RADIO
39 #define AID_RADIO (get_radio_uid())
40 #endif
41
42 // CyanogenMod-specific behavior
43 #define CM_ROOT_ACCESS_DISABLED      0
44 #define CM_ROOT_ACCESS_APPS_ONLY     1
45 #define CM_ROOT_ACCESS_ADB_ONLY      2
46 #define CM_ROOT_ACCESS_APPS_AND_ADB  3
47
48 // DO NOT CHANGE LINE BELOW, java package name will always be the same
49 #define JAVA_PACKAGE_NAME "com.koushikdutta.superuser"
50
51 // If --rename-manifest-package is used in AAPT, this
52 // must be changed to correspond to the new APK package name
53 // See the two Android.mk files for more details.
54 #ifndef REQUESTOR
55 #define REQUESTOR JAVA_PACKAGE_NAME
56 #endif
57 // This is used if wrapping the fragment classes and activities
58 // with classes in another package. CM requirement.
59 #ifndef REQUESTOR_PREFIX
60 #define REQUESTOR_PREFIX JAVA_PACKAGE_NAME
61 #endif
62 #define REQUESTOR_DATA_PATH "/data/data/"
63 #define REQUESTOR_FILES_PATH REQUESTOR_DATA_PATH REQUESTOR "/files"
64 #define REQUESTOR_USER_PATH "/data/user/"
65 #define REQUESTOR_CACHE_PATH "/dev/" REQUESTOR
66
67 // there's no guarantee that the db or files are actually created named as such by
68 // SQLiteOpenHelper, etc. Though that is the behavior as of current.
69 // it is up to the Android application to symlink as appropriate.
70 #define REQUESTOR_DATABASE_PATH REQUESTOR "/databases/su.sqlite"
71 #define REQUESTOR_MULTIUSER_MODE REQUESTOR_FILES_PATH "/multiuser_mode"
72
73 /* intent actions */
74 #define ACTION_REQUEST "start -n " REQUESTOR "/" REQUESTOR_PREFIX ".RequestActivity"
75 #define ACTION_NOTIFY "start -n " REQUESTOR "/" REQUESTOR_PREFIX ".NotifyActivity"
76 #define ACTION_RESULT "broadcast -n " REQUESTOR "/" REQUESTOR_PREFIX ".SuReceiver"
77
78 #define DEFAULT_SHELL "/system/bin/sh"
79
80 #define xstr(a) str(a)
81 #define str(a) #a
82
83 #ifndef VERSION_CODE
84 #define VERSION_CODE 9
85 #endif
86 #define VERSION xstr(VERSION_CODE) " " REQUESTOR
87
88 #define PROTO_VERSION 1
89
90 struct su_initiator {
91     pid_t pid;
92     unsigned uid;
93     unsigned user;
94     char name[64];
95     char bin[PATH_MAX];
96     char args[4096];
97 };
98
99 struct su_request {
100     unsigned uid;
101     char name[64];
102     int login;
103     int keepenv;
104     char *shell;
105     char *command;
106     char **argv;
107     int argc;
108     int optind;
109 };
110
111 struct su_user_info {
112     // the user in android userspace (multiuser)
113     // that invoked this action.
114     unsigned android_user_id;
115     // how su behaves with multiuser. see enum below.
116     int multiuser_mode;
117     // path to superuser directory. this is populated according
118     // to the multiuser mode.
119     // this is used to check uid/gid for protecting socket.
120     // this is used instead of database, as it is more likely
121     // to exist. db will not exist if su has never launched.
122     char base_path[PATH_MAX];
123     // path to su database. this is populated according
124     // to the multiuser mode.
125     char database_path[PATH_MAX];
126 };
127
128 struct su_context {
129     struct su_initiator from;
130     struct su_request to;
131     struct su_user_info user;
132     mode_t umask;
133     char sock_path[PATH_MAX];
134 };
135
136 // multiuser su behavior
137 typedef enum {
138   // only owner can su
139   MULTIUSER_MODE_OWNER_ONLY = 0,
140   // owner gets a su prompt
141   MULTIUSER_MODE_OWNER_MANAGED = 1,
142   // user gets a su prompt
143   MULTIUSER_MODE_USER = 2,
144   MULTIUSER_MODE_NONE = 3,
145 } multiuser_mode_t;
146
147 #define MULTIUSER_VALUE_OWNER_ONLY    "owner"
148 #define MULTIUSER_VALUE_OWNER_MANAGED "managed"
149 #define MULTIUSER_VALUE_USER          "user"
150 #define MULTIUSER_VALUE_NONE          "none"
151
152 typedef enum {
153     INTERACTIVE = 0,
154     DENY = 1,
155     ALLOW = 2,
156 } policy_t;
157
158 extern policy_t database_check(struct su_context *ctx);
159 extern void set_identity(unsigned int uid);
160 extern int send_request(struct su_context *ctx);
161 extern int send_result(struct su_context *ctx, policy_t policy);
162 extern int silent_run(char* command);
163
164 static inline char *get_command(const struct su_request *to)
165 {
166   if (to->command)
167     return to->command;
168   if (to->shell)
169     return to->shell;
170   char* ret = to->argv[to->optind];
171   if (ret)
172     return ret;
173   return DEFAULT_SHELL;
174 }
175
176 void exec_loge(const char* fmt, ...);
177 void exec_logw(const char* fmt, ...);
178 void exec_logd(const char* fmt, ...);
179
180 // fallback to using /system/bin/log.
181 // can't use liblog.so because this is a static binary.
182 #ifndef LOGE
183 #define LOGE exec_loge
184 #endif
185 #ifndef LOGD
186 #define LOGD exec_logd
187 #endif
188 #ifndef LOGW
189 #define LOGW exec_logw
190 #endif
191
192 #if 0
193 #undef LOGE
194 #define LOGE(fmt,args...) fprintf(stderr, fmt, ##args)
195 #undef LOGD
196 #define LOGD(fmt,args...) fprintf(stderr, fmt, ##args)
197 #undef LOGW
198 #define LOGW(fmt,args...) fprintf(stderr, fmt, ##args)
199 #endif
200
201 #include <errno.h>
202 #include <string.h>
203 #define PLOGE(fmt,args...) LOGE(fmt " failed with %d: %s", ##args, errno, strerror(errno))
204 #define PLOGEV(fmt,err,args...) LOGE(fmt " failed with %d: %s", ##args, err, strerror(err))
205
206 #endif