ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #define LOG_TAG "storaged" |
| 18 | #define KLOG_LEVEL 6 |
| 19 | |
| 20 | #include <fcntl.h> |
| 21 | #include <getopt.h> |
| 22 | #include <pthread.h> |
| 23 | #include <stdio.h> |
| 24 | #include <sys/capability.h> |
| 25 | #include <sys/prctl.h> |
| 26 | #include <sys/resource.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <vector> |
| 30 | |
| 31 | #include <android-base/macros.h> |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 33 | #include <android-base/stringprintf.h> |
| 34 | #include <binder/ProcessState.h> |
| 35 | #include <binder/IServiceManager.h> |
| 36 | #include <binder/IPCThreadState.h> |
| 37 | #include <cutils/android_get_control_file.h> |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 38 | #include <cutils/sched_policy.h> |
| 39 | #include <private/android_filesystem_config.h> |
| 40 | |
| 41 | #include <storaged.h> |
| 42 | #include <storaged_service.h> |
| 43 | #include <storaged_utils.h> |
| 44 | |
| 45 | storaged_t storaged; |
| 46 | |
| 47 | static int drop_privs() { |
| 48 | // privilege setting |
| 49 | struct sched_param param; |
| 50 | memset(¶m, 0, sizeof(param)); |
| 51 | |
| 52 | if (set_sched_policy(0, SP_BACKGROUND) < 0) return -1; |
| 53 | |
| 54 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) return -1; |
| 55 | |
| 56 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) return -1; |
| 57 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | // Function of storaged's main thread |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 62 | void* storaged_main(void* s) { |
| 63 | storaged_t* storaged = (storaged_t*)s; |
| 64 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 65 | storaged->init_battery_service(); |
| 66 | |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 67 | LOG_TO(SYSTEM, INFO) << "storaged: Start"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 68 | |
| 69 | for (;;) { |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 70 | storaged->event_checked(); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 71 | storaged->pause(); |
| 72 | } |
| 73 | return NULL; |
| 74 | } |
| 75 | |
| 76 | static void help_message(void) { |
| 77 | printf("usage: storaged [OPTION]\n"); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 78 | printf(" -u --uid Dump uid I/O usage to stdout\n"); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 79 | printf(" -s --start Start storaged (default)\n"); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 80 | fflush(stdout); |
| 81 | } |
| 82 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 83 | int main(int argc, char** argv) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 84 | int flag_main_service = 0; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 85 | int flag_dump_uid = 0; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 86 | int fd_emmc = -1; |
| 87 | int opt; |
| 88 | |
| 89 | for (;;) { |
| 90 | int opt_idx = 0; |
| 91 | static struct option long_options[] = { |
| 92 | {"start", no_argument, 0, 's'}, |
| 93 | {"kill", no_argument, 0, 'k'}, |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 94 | {"uid", no_argument, 0, 'u'}, |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 95 | {"help", no_argument, 0, 'h'} |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 96 | }; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 97 | opt = getopt_long(argc, argv, ":skdhu0", long_options, &opt_idx); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 98 | if (opt == -1) { |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | switch (opt) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 103 | case 's': |
| 104 | flag_main_service = 1; |
| 105 | break; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 106 | case 'u': |
| 107 | flag_dump_uid = 1; |
| 108 | break; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 109 | case 'h': |
| 110 | help_message(); |
| 111 | return 0; |
| 112 | case '?': |
| 113 | default: |
| 114 | fprintf(stderr, "no supported option\n"); |
| 115 | help_message(); |
| 116 | return -1; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (argc == 1) { |
| 121 | flag_main_service = 1; |
| 122 | } |
| 123 | |
Jin Qian | 88ad33e | 2017-01-23 15:31:04 -0800 | [diff] [blame] | 124 | if (flag_main_service && flag_dump_uid) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 125 | fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n"); |
| 126 | help_message(); |
| 127 | return -1; |
| 128 | } |
| 129 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 130 | if (flag_main_service) { // start main thread |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 131 | static const char mmc0_ext_csd[] = "/d/mmc0/mmc0:0001/ext_csd"; |
| 132 | fd_emmc = android_get_control_file(mmc0_ext_csd); |
| 133 | if (fd_emmc < 0) |
| 134 | fd_emmc = TEMP_FAILURE_RETRY(open(mmc0_ext_csd, O_RDONLY)); |
| 135 | |
| 136 | if (drop_privs() != 0) { |
| 137 | return -1; |
| 138 | } |
| 139 | |
| 140 | storaged.set_privileged_fds(fd_emmc); |
| 141 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 142 | // Start the main thread of storaged |
| 143 | pthread_t storaged_main_thread; |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 144 | errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged); |
| 145 | if (errno != 0) { |
| 146 | PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | defaultServiceManager()->addService(String16("storaged"), new Storaged()); |
| 151 | android::ProcessState::self()->startThreadPool(); |
| 152 | IPCThreadState::self()->joinThreadPool(); |
| 153 | pthread_join(storaged_main_thread, NULL); |
| 154 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 155 | close(fd_emmc); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 160 | if (flag_dump_uid) { |
| 161 | sp<IStoraged> storaged_service = get_storaged_service(); |
| 162 | if (storaged_service == NULL) { |
| 163 | fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n"); |
| 164 | return -1; |
| 165 | } |
| 166 | std::vector<struct uid_info> res = storaged_service->dump_uids(NULL); |
| 167 | |
| 168 | if (res.size() == 0) { |
| 169 | fprintf(stderr, "UID I/O is not readable in this version of kernel.\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | sort_running_uids_info(res); |
| 174 | log_console_running_uids_info(res); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 179 | return 0; |
ynwang | af49d97 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 180 | } |