| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2008 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #include <sys/types.h> | 
 | 18 | #include <sys/stat.h> | 
 | 19 | #include <fcntl.h> | 
 | 20 | #include <unistd.h> | 
 | 21 | #include <string.h> | 
 | 22 | #include <stdio.h> | 
 | 23 | #include <linux/kd.h> | 
 | 24 | #include <errno.h> | 
 | 25 | #include <sys/socket.h> | 
 | 26 | #include <netinet/in.h> | 
 | 27 | #include <linux/if.h> | 
 | 28 | #include <arpa/inet.h> | 
 | 29 | #include <stdlib.h> | 
 | 30 | #include <sys/mount.h> | 
 | 31 | #include <sys/resource.h> | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 32 | #include <sys/wait.h> | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 33 | #include <linux/loop.h> | 
| Ken Sumrall | 7bc6e9e | 2011-05-26 20:01:39 -0700 | [diff] [blame] | 34 | #include <cutils/partition_utils.h> | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 35 | #include <cutils/android_reboot.h> | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 36 | #include <fs_mgr.h> | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 37 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 38 | #include <selinux/selinux.h> | 
 | 39 | #include <selinux/label.h> | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 40 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 41 | #include "init.h" | 
 | 42 | #include "keywords.h" | 
 | 43 | #include "property_service.h" | 
 | 44 | #include "devices.h" | 
| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 45 | #include "init_parser.h" | 
| Colin Cross | 3899e9f | 2010-04-13 20:35:46 -0700 | [diff] [blame] | 46 | #include "util.h" | 
| Colin Cross | ed8a7d8 | 2010-04-19 17:05:34 -0700 | [diff] [blame] | 47 | #include "log.h" | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 48 |  | 
 | 49 | #include <private/android_filesystem_config.h> | 
 | 50 |  | 
 | 51 | void add_environment(const char *name, const char *value); | 
 | 52 |  | 
 | 53 | extern int init_module(void *, unsigned long, const char *); | 
 | 54 |  | 
 | 55 | static int write_file(const char *path, const char *value) | 
 | 56 | { | 
 | 57 |     int fd, ret, len; | 
 | 58 |  | 
| Nick Kralevich | 5535b05 | 2013-09-17 14:43:12 -0700 | [diff] [blame] | 59 |     fd = open(path, O_WRONLY|O_CREAT|O_NOFOLLOW, 0600); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 60 |  | 
 | 61 |     if (fd < 0) | 
| Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 62 |         return -errno; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 63 |  | 
 | 64 |     len = strlen(value); | 
 | 65 |  | 
 | 66 |     do { | 
 | 67 |         ret = write(fd, value, len); | 
 | 68 |     } while (ret < 0 && errno == EINTR); | 
 | 69 |  | 
 | 70 |     close(fd); | 
 | 71 |     if (ret < 0) { | 
| Mike Chan | 008abac | 2009-06-29 20:30:55 -0700 | [diff] [blame] | 72 |         return -errno; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 73 |     } else { | 
 | 74 |         return 0; | 
 | 75 |     } | 
 | 76 | } | 
 | 77 |  | 
| Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 78 | static int _open(const char *path) | 
 | 79 | { | 
 | 80 |     int fd; | 
 | 81 |  | 
 | 82 |     fd = open(path, O_RDONLY | O_NOFOLLOW); | 
 | 83 |     if (fd < 0) | 
 | 84 |         fd = open(path, O_WRONLY | O_NOFOLLOW); | 
 | 85 |  | 
 | 86 |     return fd; | 
 | 87 | } | 
 | 88 |  | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 89 | static int _chown(const char *path, unsigned int uid, unsigned int gid) | 
 | 90 | { | 
 | 91 |     int fd; | 
 | 92 |     int ret; | 
 | 93 |  | 
| Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 94 |     fd = _open(path); | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 95 |     if (fd < 0) { | 
 | 96 |         return -1; | 
 | 97 |     } | 
 | 98 |  | 
 | 99 |     ret = fchown(fd, uid, gid); | 
 | 100 |     if (ret < 0) { | 
 | 101 |         int errno_copy = errno; | 
 | 102 |         close(fd); | 
 | 103 |         errno = errno_copy; | 
 | 104 |         return -1; | 
 | 105 |     } | 
 | 106 |  | 
 | 107 |     close(fd); | 
 | 108 |  | 
 | 109 |     return 0; | 
 | 110 | } | 
 | 111 |  | 
 | 112 | static int _chmod(const char *path, mode_t mode) | 
 | 113 | { | 
 | 114 |     int fd; | 
 | 115 |     int ret; | 
 | 116 |  | 
| Benoit Goby | 5890301 | 2012-03-28 18:15:56 -0700 | [diff] [blame] | 117 |     fd = _open(path); | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 118 |     if (fd < 0) { | 
 | 119 |         return -1; | 
 | 120 |     } | 
 | 121 |  | 
 | 122 |     ret = fchmod(fd, mode); | 
 | 123 |     if (ret < 0) { | 
 | 124 |         int errno_copy = errno; | 
 | 125 |         close(fd); | 
 | 126 |         errno = errno_copy; | 
 | 127 |         return -1; | 
 | 128 |     } | 
 | 129 |  | 
 | 130 |     close(fd); | 
 | 131 |  | 
 | 132 |     return 0; | 
 | 133 | } | 
 | 134 |  | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 135 | static int insmod(const char *filename, char *options) | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 136 | { | 
 | 137 |     void *module; | 
 | 138 |     unsigned size; | 
 | 139 |     int ret; | 
 | 140 |  | 
 | 141 |     module = read_file(filename, &size); | 
 | 142 |     if (!module) | 
 | 143 |         return -1; | 
 | 144 |  | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 145 |     ret = init_module(module, size, options); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 146 |  | 
 | 147 |     free(module); | 
 | 148 |  | 
 | 149 |     return ret; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | static int setkey(struct kbentry *kbe) | 
 | 153 | { | 
 | 154 |     int fd, ret; | 
 | 155 |  | 
 | 156 |     fd = open("/dev/tty0", O_RDWR | O_SYNC); | 
 | 157 |     if (fd < 0) | 
 | 158 |         return -1; | 
 | 159 |  | 
 | 160 |     ret = ioctl(fd, KDSKBENT, kbe); | 
 | 161 |  | 
 | 162 |     close(fd); | 
 | 163 |     return ret; | 
 | 164 | } | 
 | 165 |  | 
 | 166 | static int __ifupdown(const char *interface, int up) | 
 | 167 | { | 
 | 168 |     struct ifreq ifr; | 
 | 169 |     int s, ret; | 
 | 170 |  | 
 | 171 |     strlcpy(ifr.ifr_name, interface, IFNAMSIZ); | 
 | 172 |  | 
 | 173 |     s = socket(AF_INET, SOCK_DGRAM, 0); | 
 | 174 |     if (s < 0) | 
 | 175 |         return -1; | 
 | 176 |  | 
 | 177 |     ret = ioctl(s, SIOCGIFFLAGS, &ifr); | 
 | 178 |     if (ret < 0) { | 
 | 179 |         goto done; | 
 | 180 |     } | 
 | 181 |  | 
 | 182 |     if (up) | 
 | 183 |         ifr.ifr_flags |= IFF_UP; | 
 | 184 |     else | 
 | 185 |         ifr.ifr_flags &= ~IFF_UP; | 
 | 186 |  | 
 | 187 |     ret = ioctl(s, SIOCSIFFLAGS, &ifr); | 
 | 188 |      | 
 | 189 | done: | 
 | 190 |     close(s); | 
 | 191 |     return ret; | 
 | 192 | } | 
 | 193 |  | 
 | 194 | static void service_start_if_not_disabled(struct service *svc) | 
 | 195 | { | 
 | 196 |     if (!(svc->flags & SVC_DISABLED)) { | 
| San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 197 |         service_start(svc, NULL); | 
| JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 198 |     } else { | 
 | 199 |         svc->flags |= SVC_DISABLED_START; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 200 |     } | 
 | 201 | } | 
 | 202 |  | 
| Jay Freeman (saurik) | e7cb137 | 2008-11-17 06:41:10 +0000 | [diff] [blame] | 203 | int do_chdir(int nargs, char **args) | 
 | 204 | { | 
 | 205 |     chdir(args[1]); | 
 | 206 |     return 0; | 
 | 207 | } | 
 | 208 |  | 
 | 209 | int do_chroot(int nargs, char **args) | 
 | 210 | { | 
 | 211 |     chroot(args[1]); | 
 | 212 |     return 0; | 
 | 213 | } | 
 | 214 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 215 | int do_class_start(int nargs, char **args) | 
 | 216 | { | 
 | 217 |         /* Starting a class does not start services | 
 | 218 |          * which are explicitly disabled.  They must | 
 | 219 |          * be started individually. | 
 | 220 |          */ | 
 | 221 |     service_for_each_class(args[1], service_start_if_not_disabled); | 
 | 222 |     return 0; | 
 | 223 | } | 
 | 224 |  | 
 | 225 | int do_class_stop(int nargs, char **args) | 
 | 226 | { | 
 | 227 |     service_for_each_class(args[1], service_stop); | 
 | 228 |     return 0; | 
 | 229 | } | 
 | 230 |  | 
| Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 231 | int do_class_reset(int nargs, char **args) | 
 | 232 | { | 
 | 233 |     service_for_each_class(args[1], service_reset); | 
 | 234 |     return 0; | 
 | 235 | } | 
 | 236 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 237 | int do_domainname(int nargs, char **args) | 
 | 238 | { | 
 | 239 |     return write_file("/proc/sys/kernel/domainname", args[1]); | 
 | 240 | } | 
 | 241 |  | 
| JP Abgrall | 3beec7e | 2014-05-02 21:14:29 -0700 | [diff] [blame] | 242 | int do_enable(int nargs, char **args) | 
 | 243 | { | 
 | 244 |     struct service *svc; | 
 | 245 |     svc = service_find_by_name(args[1]); | 
 | 246 |     if (svc) { | 
 | 247 |         svc->flags &= ~(SVC_DISABLED | SVC_RC_DISABLED); | 
 | 248 |         if (svc->flags & SVC_DISABLED_START) { | 
 | 249 |             service_start(svc, NULL); | 
 | 250 |         } | 
 | 251 |     } else { | 
 | 252 |         return -1; | 
 | 253 |     } | 
 | 254 |     return 0; | 
 | 255 | } | 
 | 256 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 257 | int do_exec(int nargs, char **args) | 
 | 258 | { | 
 | 259 |     return -1; | 
 | 260 | } | 
 | 261 |  | 
 | 262 | int do_export(int nargs, char **args) | 
 | 263 | { | 
 | 264 |     add_environment(args[1], args[2]); | 
 | 265 |     return 0; | 
 | 266 | } | 
 | 267 |  | 
 | 268 | int do_hostname(int nargs, char **args) | 
 | 269 | { | 
 | 270 |     return write_file("/proc/sys/kernel/hostname", args[1]); | 
 | 271 | } | 
 | 272 |  | 
 | 273 | int do_ifup(int nargs, char **args) | 
 | 274 | { | 
 | 275 |     return __ifupdown(args[1], 1); | 
 | 276 | } | 
 | 277 |  | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 278 |  | 
 | 279 | static int do_insmod_inner(int nargs, char **args, int opt_len) | 
 | 280 | { | 
 | 281 |     char options[opt_len + 1]; | 
 | 282 |     int i; | 
 | 283 |  | 
 | 284 |     options[0] = '\0'; | 
 | 285 |     if (nargs > 2) { | 
 | 286 |         strcpy(options, args[2]); | 
 | 287 |         for (i = 3; i < nargs; ++i) { | 
 | 288 |             strcat(options, " "); | 
 | 289 |             strcat(options, args[i]); | 
 | 290 |         } | 
 | 291 |     } | 
 | 292 |  | 
 | 293 |     return insmod(args[1], options); | 
 | 294 | } | 
 | 295 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 296 | int do_insmod(int nargs, char **args) | 
 | 297 | { | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 298 |     int i; | 
 | 299 |     int size = 0; | 
 | 300 |  | 
 | 301 |     if (nargs > 2) { | 
 | 302 |         for (i = 2; i < nargs; ++i) | 
 | 303 |             size += strlen(args[i]) + 1; | 
 | 304 |     } | 
 | 305 |  | 
 | 306 |     return do_insmod_inner(nargs, args, size); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 307 | } | 
 | 308 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 309 | int do_mkdir(int nargs, char **args) | 
 | 310 | { | 
 | 311 |     mode_t mode = 0755; | 
| Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 312 |     int ret; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 313 |  | 
 | 314 |     /* mkdir <path> [mode] [owner] [group] */ | 
 | 315 |  | 
 | 316 |     if (nargs >= 3) { | 
 | 317 |         mode = strtoul(args[2], 0, 8); | 
 | 318 |     } | 
 | 319 |  | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 320 |     ret = make_dir(args[1], mode); | 
| Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 321 |     /* chmod in case the directory already exists */ | 
 | 322 |     if (ret == -1 && errno == EEXIST) { | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 323 |         ret = _chmod(args[1], mode); | 
| Chia-chi Yeh | 27164dc | 2011-07-08 12:57:36 -0700 | [diff] [blame] | 324 |     } | 
 | 325 |     if (ret == -1) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 326 |         return -errno; | 
 | 327 |     } | 
 | 328 |  | 
 | 329 |     if (nargs >= 4) { | 
 | 330 |         uid_t uid = decode_uid(args[3]); | 
 | 331 |         gid_t gid = -1; | 
 | 332 |  | 
 | 333 |         if (nargs == 5) { | 
 | 334 |             gid = decode_uid(args[4]); | 
 | 335 |         } | 
 | 336 |  | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 337 |         if (_chown(args[1], uid, gid) < 0) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 338 |             return -errno; | 
 | 339 |         } | 
| Benoit Goby | 5c8574b | 2012-08-14 15:43:46 -0700 | [diff] [blame] | 340 |  | 
 | 341 |         /* chown may have cleared S_ISUID and S_ISGID, chmod again */ | 
 | 342 |         if (mode & (S_ISUID | S_ISGID)) { | 
 | 343 |             ret = _chmod(args[1], mode); | 
 | 344 |             if (ret == -1) { | 
 | 345 |                 return -errno; | 
 | 346 |             } | 
 | 347 |         } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 348 |     } | 
 | 349 |  | 
 | 350 |     return 0; | 
 | 351 | } | 
 | 352 |  | 
 | 353 | static struct { | 
 | 354 |     const char *name; | 
 | 355 |     unsigned flag; | 
 | 356 | } mount_flags[] = { | 
 | 357 |     { "noatime",    MS_NOATIME }, | 
| Lars Svensson | b6ee25e | 2011-07-14 13:39:09 +0200 | [diff] [blame] | 358 |     { "noexec",     MS_NOEXEC }, | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 359 |     { "nosuid",     MS_NOSUID }, | 
 | 360 |     { "nodev",      MS_NODEV }, | 
 | 361 |     { "nodiratime", MS_NODIRATIME }, | 
 | 362 |     { "ro",         MS_RDONLY }, | 
 | 363 |     { "rw",         0 }, | 
 | 364 |     { "remount",    MS_REMOUNT }, | 
| Jeff Sharkey | e50ac5f | 2012-08-14 11:34:34 -0700 | [diff] [blame] | 365 |     { "bind",       MS_BIND }, | 
 | 366 |     { "rec",        MS_REC }, | 
 | 367 |     { "unbindable", MS_UNBINDABLE }, | 
 | 368 |     { "private",    MS_PRIVATE }, | 
 | 369 |     { "slave",      MS_SLAVE }, | 
 | 370 |     { "shared",     MS_SHARED }, | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 371 |     { "defaults",   0 }, | 
 | 372 |     { 0,            0 }, | 
 | 373 | }; | 
 | 374 |  | 
| Ken Sumrall | 752923c | 2010-12-03 16:33:31 -0800 | [diff] [blame] | 375 | #define DATA_MNT_POINT "/data" | 
 | 376 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 377 | /* mount <type> <device> <path> <flags ...> <options> */ | 
 | 378 | int do_mount(int nargs, char **args) | 
 | 379 | { | 
 | 380 |     char tmp[64]; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 381 |     char *source, *target, *system; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 382 |     char *options = NULL; | 
 | 383 |     unsigned flags = 0; | 
 | 384 |     int n, i; | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 385 |     int wait = 0; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 386 |  | 
 | 387 |     for (n = 4; n < nargs; n++) { | 
 | 388 |         for (i = 0; mount_flags[i].name; i++) { | 
 | 389 |             if (!strcmp(args[n], mount_flags[i].name)) { | 
 | 390 |                 flags |= mount_flags[i].flag; | 
 | 391 |                 break; | 
 | 392 |             } | 
 | 393 |         } | 
 | 394 |  | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 395 |         if (!mount_flags[i].name) { | 
 | 396 |             if (!strcmp(args[n], "wait")) | 
 | 397 |                 wait = 1; | 
 | 398 |             /* if our last argument isn't a flag, wolf it up as an option string */ | 
 | 399 |             else if (n + 1 == nargs) | 
 | 400 |                 options = args[n]; | 
 | 401 |         } | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 402 |     } | 
 | 403 |  | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 404 |     system = args[1]; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 405 |     source = args[2]; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 406 |     target = args[3]; | 
 | 407 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 408 |     if (!strncmp(source, "mtd@", 4)) { | 
 | 409 |         n = mtd_name_to_number(source + 4); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 410 |         if (n < 0) { | 
 | 411 |             return -1; | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 412 |         } | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 413 |  | 
 | 414 |         sprintf(tmp, "/dev/block/mtdblock%d", n); | 
 | 415 |  | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 416 |         if (wait) | 
 | 417 |             wait_for_file(tmp, COMMAND_RETRY_TIMEOUT); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 418 |         if (mount(tmp, target, system, flags, options) < 0) { | 
 | 419 |             return -1; | 
 | 420 |         } | 
 | 421 |  | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 422 |         goto exit_success; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 423 |     } else if (!strncmp(source, "loop@", 5)) { | 
 | 424 |         int mode, loop, fd; | 
 | 425 |         struct loop_info info; | 
 | 426 |  | 
 | 427 |         mode = (flags & MS_RDONLY) ? O_RDONLY : O_RDWR; | 
 | 428 |         fd = open(source + 5, mode); | 
 | 429 |         if (fd < 0) { | 
 | 430 |             return -1; | 
 | 431 |         } | 
 | 432 |  | 
 | 433 |         for (n = 0; ; n++) { | 
 | 434 |             sprintf(tmp, "/dev/block/loop%d", n); | 
 | 435 |             loop = open(tmp, mode); | 
 | 436 |             if (loop < 0) { | 
 | 437 |                 return -1; | 
 | 438 |             } | 
 | 439 |  | 
 | 440 |             /* if it is a blank loop device */ | 
 | 441 |             if (ioctl(loop, LOOP_GET_STATUS, &info) < 0 && errno == ENXIO) { | 
 | 442 |                 /* if it becomes our loop device */ | 
 | 443 |                 if (ioctl(loop, LOOP_SET_FD, fd) >= 0) { | 
 | 444 |                     close(fd); | 
 | 445 |  | 
 | 446 |                     if (mount(tmp, target, system, flags, options) < 0) { | 
 | 447 |                         ioctl(loop, LOOP_CLR_FD, 0); | 
 | 448 |                         close(loop); | 
 | 449 |                         return -1; | 
 | 450 |                     } | 
 | 451 |  | 
 | 452 |                     close(loop); | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 453 |                     goto exit_success; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 454 |                 } | 
 | 455 |             } | 
 | 456 |  | 
 | 457 |             close(loop); | 
 | 458 |         } | 
 | 459 |  | 
 | 460 |         close(fd); | 
 | 461 |         ERROR("out of loopback devices"); | 
 | 462 |         return -1; | 
 | 463 |     } else { | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 464 |         if (wait) | 
 | 465 |             wait_for_file(source, COMMAND_RETRY_TIMEOUT); | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 466 |         if (mount(source, target, system, flags, options) < 0) { | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 467 |             return -1; | 
| Jay Freeman (saurik) | e520d03 | 2008-11-20 03:37:30 +0000 | [diff] [blame] | 468 |         } | 
 | 469 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 470 |     } | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 471 |  | 
 | 472 | exit_success: | 
| Ken Sumrall | dd4d786 | 2011-02-17 18:09:47 -0800 | [diff] [blame] | 473 |     return 0; | 
 | 474 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 475 | } | 
 | 476 |  | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 477 | int do_mount_all(int nargs, char **args) | 
 | 478 | { | 
 | 479 |     pid_t pid; | 
 | 480 |     int ret = -1; | 
 | 481 |     int child_ret = -1; | 
 | 482 |     int status; | 
 | 483 |     const char *prop; | 
| Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 484 |     struct fstab *fstab; | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 485 |  | 
 | 486 |     if (nargs != 2) { | 
 | 487 |         return -1; | 
 | 488 |     } | 
 | 489 |  | 
 | 490 |     /* | 
 | 491 |      * Call fs_mgr_mount_all() to mount all filesystems.  We fork(2) and | 
 | 492 |      * do the call in the child to provide protection to the main init | 
 | 493 |      * process if anything goes wrong (crash or memory leak), and wait for | 
 | 494 |      * the child to finish in the parent. | 
 | 495 |      */ | 
 | 496 |     pid = fork(); | 
 | 497 |     if (pid > 0) { | 
 | 498 |         /* Parent.  Wait for the child to return */ | 
 | 499 |         waitpid(pid, &status, 0); | 
 | 500 |         if (WIFEXITED(status)) { | 
 | 501 |             ret = WEXITSTATUS(status); | 
 | 502 |         } else { | 
 | 503 |             ret = -1; | 
 | 504 |         } | 
 | 505 |     } else if (pid == 0) { | 
 | 506 |         /* child, call fs_mgr_mount_all() */ | 
 | 507 |         klog_set_level(6);  /* So we can see what fs_mgr_mount_all() does */ | 
| Ken Sumrall | ab6b852 | 2013-02-13 12:58:40 -0800 | [diff] [blame] | 508 |         fstab = fs_mgr_read_fstab(args[1]); | 
 | 509 |         child_ret = fs_mgr_mount_all(fstab); | 
 | 510 |         fs_mgr_free_fstab(fstab); | 
| Ken Sumrall | 0e9dd90 | 2012-04-17 17:20:16 -0700 | [diff] [blame] | 511 |         if (child_ret == -1) { | 
 | 512 |             ERROR("fs_mgr_mount_all returned an error\n"); | 
 | 513 |         } | 
 | 514 |         exit(child_ret); | 
 | 515 |     } else { | 
 | 516 |         /* fork failed, return an error */ | 
 | 517 |         return -1; | 
 | 518 |     } | 
 | 519 |  | 
 | 520 |     /* ret is 1 if the device is encrypted, 0 if not, and -1 on error */ | 
 | 521 |     if (ret == 1) { | 
 | 522 |         property_set("ro.crypto.state", "encrypted"); | 
 | 523 |         property_set("vold.decrypt", "1"); | 
 | 524 |     } else if (ret == 0) { | 
 | 525 |         property_set("ro.crypto.state", "unencrypted"); | 
 | 526 |         /* If fs_mgr determined this is an unencrypted device, then trigger | 
 | 527 |          * that action. | 
 | 528 |          */ | 
 | 529 |         action_for_each_trigger("nonencrypted", action_add_queue_tail); | 
 | 530 |     } | 
 | 531 |  | 
 | 532 |     return ret; | 
 | 533 | } | 
 | 534 |  | 
| Ken Sumrall | a76baaa | 2013-07-09 18:42:09 -0700 | [diff] [blame] | 535 | int do_swapon_all(int nargs, char **args) | 
 | 536 | { | 
 | 537 |     struct fstab *fstab; | 
 | 538 |     int ret; | 
 | 539 |  | 
 | 540 |     fstab = fs_mgr_read_fstab(args[1]); | 
 | 541 |     ret = fs_mgr_swapon_all(fstab); | 
 | 542 |     fs_mgr_free_fstab(fstab); | 
 | 543 |  | 
 | 544 |     return ret; | 
 | 545 | } | 
 | 546 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 547 | int do_setcon(int nargs, char **args) { | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 548 |     if (is_selinux_enabled() <= 0) | 
 | 549 |         return 0; | 
 | 550 |     if (setcon(args[1]) < 0) { | 
 | 551 |         return -errno; | 
 | 552 |     } | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 553 |     return 0; | 
 | 554 | } | 
 | 555 |  | 
 | 556 | int do_setenforce(int nargs, char **args) { | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 557 |     if (is_selinux_enabled() <= 0) | 
 | 558 |         return 0; | 
 | 559 |     if (security_setenforce(atoi(args[1])) < 0) { | 
 | 560 |         return -errno; | 
 | 561 |     } | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 562 |     return 0; | 
 | 563 | } | 
 | 564 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 565 | int do_setkey(int nargs, char **args) | 
 | 566 | { | 
 | 567 |     struct kbentry kbe; | 
 | 568 |     kbe.kb_table = strtoul(args[1], 0, 0); | 
 | 569 |     kbe.kb_index = strtoul(args[2], 0, 0); | 
 | 570 |     kbe.kb_value = strtoul(args[3], 0, 0); | 
 | 571 |     return setkey(&kbe); | 
 | 572 | } | 
 | 573 |  | 
 | 574 | int do_setprop(int nargs, char **args) | 
 | 575 | { | 
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 576 |     const char *name = args[1]; | 
 | 577 |     const char *value = args[2]; | 
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 578 |     char prop_val[PROP_VALUE_MAX]; | 
 | 579 |     int ret; | 
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 580 |  | 
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 581 |     ret = expand_props(prop_val, value, sizeof(prop_val)); | 
 | 582 |     if (ret) { | 
 | 583 |         ERROR("cannot expand '%s' while assigning to '%s'\n", value, name); | 
 | 584 |         return -EINVAL; | 
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 585 |     } | 
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 586 |     property_set(name, prop_val); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 587 |     return 0; | 
 | 588 | } | 
 | 589 |  | 
 | 590 | int do_setrlimit(int nargs, char **args) | 
 | 591 | { | 
 | 592 |     struct rlimit limit; | 
 | 593 |     int resource; | 
 | 594 |     resource = atoi(args[1]); | 
 | 595 |     limit.rlim_cur = atoi(args[2]); | 
 | 596 |     limit.rlim_max = atoi(args[3]); | 
 | 597 |     return setrlimit(resource, &limit); | 
 | 598 | } | 
 | 599 |  | 
 | 600 | int do_start(int nargs, char **args) | 
 | 601 | { | 
 | 602 |     struct service *svc; | 
 | 603 |     svc = service_find_by_name(args[1]); | 
 | 604 |     if (svc) { | 
| San Mehat | f24e252 | 2009-05-19 13:30:46 -0700 | [diff] [blame] | 605 |         service_start(svc, NULL); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 606 |     } | 
 | 607 |     return 0; | 
 | 608 | } | 
 | 609 |  | 
 | 610 | int do_stop(int nargs, char **args) | 
 | 611 | { | 
 | 612 |     struct service *svc; | 
 | 613 |     svc = service_find_by_name(args[1]); | 
 | 614 |     if (svc) { | 
 | 615 |         service_stop(svc); | 
 | 616 |     } | 
 | 617 |     return 0; | 
 | 618 | } | 
 | 619 |  | 
 | 620 | int do_restart(int nargs, char **args) | 
 | 621 | { | 
 | 622 |     struct service *svc; | 
 | 623 |     svc = service_find_by_name(args[1]); | 
 | 624 |     if (svc) { | 
| Mike Kasick | b54f39f | 2012-01-25 23:48:46 -0500 | [diff] [blame] | 625 |         service_restart(svc); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 626 |     } | 
 | 627 |     return 0; | 
 | 628 | } | 
 | 629 |  | 
| Nick Kralevich | ca8e66a | 2013-04-18 12:20:02 -0700 | [diff] [blame] | 630 | int do_powerctl(int nargs, char **args) | 
 | 631 | { | 
 | 632 |     char command[PROP_VALUE_MAX]; | 
 | 633 |     int res; | 
 | 634 |     int len = 0; | 
 | 635 |     int cmd = 0; | 
 | 636 |     char *reboot_target; | 
 | 637 |  | 
 | 638 |     res = expand_props(command, args[1], sizeof(command)); | 
 | 639 |     if (res) { | 
 | 640 |         ERROR("powerctl: cannot expand '%s'\n", args[1]); | 
 | 641 |         return -EINVAL; | 
 | 642 |     } | 
 | 643 |  | 
 | 644 |     if (strncmp(command, "shutdown", 8) == 0) { | 
 | 645 |         cmd = ANDROID_RB_POWEROFF; | 
 | 646 |         len = 8; | 
 | 647 |     } else if (strncmp(command, "reboot", 6) == 0) { | 
 | 648 |         cmd = ANDROID_RB_RESTART2; | 
 | 649 |         len = 6; | 
 | 650 |     } else { | 
 | 651 |         ERROR("powerctl: unrecognized command '%s'\n", command); | 
 | 652 |         return -EINVAL; | 
 | 653 |     } | 
 | 654 |  | 
 | 655 |     if (command[len] == ',') { | 
 | 656 |         reboot_target = &command[len + 1]; | 
 | 657 |     } else if (command[len] == '\0') { | 
 | 658 |         reboot_target = ""; | 
 | 659 |     } else { | 
 | 660 |         ERROR("powerctl: unrecognized reboot target '%s'\n", &command[len]); | 
 | 661 |         return -EINVAL; | 
 | 662 |     } | 
 | 663 |  | 
 | 664 |     return android_reboot(cmd, 0, reboot_target); | 
 | 665 | } | 
 | 666 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 667 | int do_trigger(int nargs, char **args) | 
 | 668 | { | 
| Jay Freeman (saurik) | 11e1c42 | 2008-11-17 06:35:08 +0000 | [diff] [blame] | 669 |     action_for_each_trigger(args[1], action_add_queue_tail); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 670 |     return 0; | 
 | 671 | } | 
 | 672 |  | 
 | 673 | int do_symlink(int nargs, char **args) | 
 | 674 | { | 
 | 675 |     return symlink(args[1], args[2]); | 
 | 676 | } | 
 | 677 |  | 
| Ken Sumrall | 203bad5 | 2011-01-18 17:37:41 -0800 | [diff] [blame] | 678 | int do_rm(int nargs, char **args) | 
 | 679 | { | 
 | 680 |     return unlink(args[1]); | 
 | 681 | } | 
 | 682 |  | 
 | 683 | int do_rmdir(int nargs, char **args) | 
 | 684 | { | 
 | 685 |     return rmdir(args[1]); | 
 | 686 | } | 
 | 687 |  | 
| The Android Open Source Project | 35237d1 | 2008-12-17 18:08:08 -0800 | [diff] [blame] | 688 | int do_sysclktz(int nargs, char **args) | 
 | 689 | { | 
 | 690 |     struct timezone tz; | 
 | 691 |  | 
 | 692 |     if (nargs != 2) | 
 | 693 |         return -1; | 
 | 694 |  | 
 | 695 |     memset(&tz, 0, sizeof(tz)); | 
 | 696 |     tz.tz_minuteswest = atoi(args[1]);    | 
 | 697 |     if (settimeofday(NULL, &tz)) | 
 | 698 |         return -1; | 
 | 699 |     return 0; | 
 | 700 | } | 
 | 701 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 702 | int do_write(int nargs, char **args) | 
 | 703 | { | 
| Mike Lockwood | 1f0bd32 | 2011-06-08 17:31:27 -0700 | [diff] [blame] | 704 |     const char *path = args[1]; | 
 | 705 |     const char *value = args[2]; | 
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 706 |     char prop_val[PROP_VALUE_MAX]; | 
 | 707 |     int ret; | 
| Mike Lockwood | 2c4d5dc | 2011-06-07 17:30:11 -0700 | [diff] [blame] | 708 |  | 
| Dima Zavin | 84bf9af | 2011-12-20 13:44:41 -0800 | [diff] [blame] | 709 |     ret = expand_props(prop_val, value, sizeof(prop_val)); | 
 | 710 |     if (ret) { | 
 | 711 |         ERROR("cannot expand '%s' while writing to '%s'\n", value, path); | 
 | 712 |         return -EINVAL; | 
 | 713 |     } | 
 | 714 |     return write_file(path, prop_val); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 715 | } | 
 | 716 |  | 
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 717 | int do_copy(int nargs, char **args) | 
 | 718 | { | 
 | 719 |     char *buffer = NULL; | 
 | 720 |     int rc = 0; | 
 | 721 |     int fd1 = -1, fd2 = -1; | 
 | 722 |     struct stat info; | 
 | 723 |     int brtw, brtr; | 
 | 724 |     char *p; | 
 | 725 |  | 
 | 726 |     if (nargs != 3) | 
 | 727 |         return -1; | 
 | 728 |  | 
 | 729 |     if (stat(args[1], &info) < 0)  | 
 | 730 |         return -1; | 
 | 731 |  | 
 | 732 |     if ((fd1 = open(args[1], O_RDONLY)) < 0)  | 
 | 733 |         goto out_err; | 
 | 734 |  | 
| Tom Zhu | 4833d9f | 2009-09-28 19:53:12 -0500 | [diff] [blame] | 735 |     if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC, 0660)) < 0) | 
| San Mehat | 7c44fe5 | 2009-08-26 16:39:25 -0700 | [diff] [blame] | 736 |         goto out_err; | 
 | 737 |  | 
 | 738 |     if (!(buffer = malloc(info.st_size))) | 
 | 739 |         goto out_err; | 
 | 740 |  | 
 | 741 |     p = buffer; | 
 | 742 |     brtr = info.st_size; | 
 | 743 |     while(brtr) { | 
 | 744 |         rc = read(fd1, p, brtr); | 
 | 745 |         if (rc < 0) | 
 | 746 |             goto out_err; | 
 | 747 |         if (rc == 0) | 
 | 748 |             break; | 
 | 749 |         p += rc; | 
 | 750 |         brtr -= rc; | 
 | 751 |     } | 
 | 752 |  | 
 | 753 |     p = buffer; | 
 | 754 |     brtw = info.st_size; | 
 | 755 |     while(brtw) { | 
 | 756 |         rc = write(fd2, p, brtw); | 
 | 757 |         if (rc < 0) | 
 | 758 |             goto out_err; | 
 | 759 |         if (rc == 0) | 
 | 760 |             break; | 
 | 761 |         p += rc; | 
 | 762 |         brtw -= rc; | 
 | 763 |     } | 
 | 764 |  | 
 | 765 |     rc = 0; | 
 | 766 |     goto out; | 
 | 767 | out_err: | 
 | 768 |     rc = -1; | 
 | 769 | out: | 
 | 770 |     if (buffer) | 
 | 771 |         free(buffer); | 
 | 772 |     if (fd1 >= 0) | 
 | 773 |         close(fd1); | 
 | 774 |     if (fd2 >= 0) | 
 | 775 |         close(fd2); | 
 | 776 |     return rc; | 
 | 777 | } | 
 | 778 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 779 | int do_chown(int nargs, char **args) { | 
 | 780 |     /* GID is optional. */ | 
 | 781 |     if (nargs == 3) { | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 782 |         if (_chown(args[2], decode_uid(args[1]), -1) < 0) | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 783 |             return -errno; | 
 | 784 |     } else if (nargs == 4) { | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 785 |         if (_chown(args[3], decode_uid(args[1]), decode_uid(args[2])) < 0) | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 786 |             return -errno; | 
 | 787 |     } else { | 
 | 788 |         return -1; | 
 | 789 |     } | 
 | 790 |     return 0; | 
 | 791 | } | 
 | 792 |  | 
 | 793 | static mode_t get_mode(const char *s) { | 
 | 794 |     mode_t mode = 0; | 
 | 795 |     while (*s) { | 
 | 796 |         if (*s >= '0' && *s <= '7') { | 
 | 797 |             mode = (mode<<3) | (*s-'0'); | 
 | 798 |         } else { | 
 | 799 |             return -1; | 
 | 800 |         } | 
 | 801 |         s++; | 
 | 802 |     } | 
 | 803 |     return mode; | 
 | 804 | } | 
 | 805 |  | 
 | 806 | int do_chmod(int nargs, char **args) { | 
 | 807 |     mode_t mode = get_mode(args[1]); | 
| Geremy Condra | 9ed1fe7 | 2012-03-20 12:49:55 -0700 | [diff] [blame] | 808 |     if (_chmod(args[2], mode) < 0) { | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 809 |         return -errno; | 
 | 810 |     } | 
 | 811 |     return 0; | 
 | 812 | } | 
 | 813 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 814 | int do_restorecon(int nargs, char **args) { | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 815 |     int i; | 
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 816 |     int ret = 0; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 817 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 818 |     for (i = 1; i < nargs; i++) { | 
| Stephen Smalley | e096e36 | 2012-06-11 13:37:39 -0400 | [diff] [blame] | 819 |         if (restorecon(args[i]) < 0) | 
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 820 |             ret = -errno; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 821 |     } | 
| Stephen Smalley | 726e8f7 | 2013-10-09 16:02:09 -0400 | [diff] [blame] | 822 |     return ret; | 
 | 823 | } | 
 | 824 |  | 
 | 825 | int do_restorecon_recursive(int nargs, char **args) { | 
 | 826 |     int i; | 
 | 827 |     int ret = 0; | 
 | 828 |  | 
 | 829 |     for (i = 1; i < nargs; i++) { | 
 | 830 |         if (restorecon_recursive(args[i]) < 0) | 
 | 831 |             ret = -errno; | 
 | 832 |     } | 
 | 833 |     return ret; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 834 | } | 
 | 835 |  | 
 | 836 | int do_setsebool(int nargs, char **args) { | 
| Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 837 |     const char *name = args[1]; | 
 | 838 |     const char *value = args[2]; | 
 | 839 |     SELboolean b; | 
 | 840 |     int ret; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 841 |  | 
 | 842 |     if (is_selinux_enabled() <= 0) | 
 | 843 |         return 0; | 
 | 844 |  | 
| Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 845 |     b.name = name; | 
 | 846 |     if (!strcmp(value, "1") || !strcasecmp(value, "true") || !strcasecmp(value, "on")) | 
 | 847 |         b.value = 1; | 
 | 848 |     else if (!strcmp(value, "0") || !strcasecmp(value, "false") || !strcasecmp(value, "off")) | 
 | 849 |         b.value = 0; | 
 | 850 |     else { | 
 | 851 |         ERROR("setsebool: invalid value %s\n", value); | 
 | 852 |         return -EINVAL; | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 853 |     } | 
 | 854 |  | 
| Stephen Smalley | 0e23fee | 2012-11-28 13:52:12 -0500 | [diff] [blame] | 855 |     if (security_set_boolean_list(1, &b, 0) < 0) { | 
 | 856 |         ret = -errno; | 
 | 857 |         ERROR("setsebool: could not set %s to %s\n", name, value); | 
 | 858 |         return ret; | 
 | 859 |     } | 
| Kenny Root | b5982bf | 2012-10-16 23:07:05 -0700 | [diff] [blame] | 860 |  | 
| Stephen Smalley | e46f9d5 | 2012-01-13 08:48:47 -0500 | [diff] [blame] | 861 |     return 0; | 
 | 862 | } | 
 | 863 |  | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 864 | int do_loglevel(int nargs, char **args) { | 
 | 865 |     if (nargs == 2) { | 
| Dima Zavin | 8f91282 | 2011-08-31 18:26:17 -0700 | [diff] [blame] | 866 |         klog_set_level(atoi(args[1])); | 
| The Android Open Source Project | 4f6e8d7 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 867 |         return 0; | 
 | 868 |     } | 
 | 869 |     return -1; | 
 | 870 | } | 
 | 871 |  | 
| Ken Sumrall | c5c5103 | 2011-03-08 17:01:29 -0800 | [diff] [blame] | 872 | int do_load_persist_props(int nargs, char **args) { | 
 | 873 |     if (nargs == 1) { | 
 | 874 |         load_persist_props(); | 
 | 875 |         return 0; | 
 | 876 |     } | 
 | 877 |     return -1; | 
 | 878 | } | 
 | 879 |  | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 880 | int do_wait(int nargs, char **args) | 
 | 881 | { | 
 | 882 |     if (nargs == 2) { | 
 | 883 |         return wait_for_file(args[1], COMMAND_RETRY_TIMEOUT); | 
| Patrick McCormick | 96d0a4d | 2011-02-04 10:51:39 -0800 | [diff] [blame] | 884 |     } else if (nargs == 3) { | 
 | 885 |         return wait_for_file(args[1], atoi(args[2])); | 
 | 886 |     } else | 
 | 887 |         return -1; | 
| Colin Cross | cd0f173 | 2010-04-19 17:10:24 -0700 | [diff] [blame] | 888 | } |