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 | |
| 58 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) return -1; |
| 59 | |
| 60 | std::unique_ptr<struct _cap_struct, int(*)(void *)> caps(cap_init(), cap_free); |
| 61 | if (cap_clear(caps.get()) < 0) return -1; |
| 62 | cap_value_t cap_value[] = { |
| 63 | CAP_SETGID, |
| 64 | CAP_SETUID, |
| 65 | CAP_SYS_PTRACE // allow access to proc/<pid>/io as non-root user |
| 66 | }; |
| 67 | if (cap_set_flag(caps.get(), CAP_PERMITTED, |
| 68 | arraysize(cap_value), cap_value, |
| 69 | CAP_SET) < 0) return -1; |
| 70 | if (cap_set_flag(caps.get(), CAP_EFFECTIVE, |
| 71 | arraysize(cap_value), cap_value, |
| 72 | CAP_SET) < 0) return -1; |
| 73 | if (cap_set_proc(caps.get()) < 0) |
| 74 | return -1; |
| 75 | |
| 76 | gid_t groups[] = { AID_READPROC }; |
| 77 | |
| 78 | if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) == -1) return -1; |
| 79 | |
| 80 | if (setgid(AID_SYSTEM) != 0) return -1; |
| 81 | |
| 82 | if (setuid(AID_SYSTEM) != 0) return -1; |
| 83 | |
| 84 | if (cap_set_flag(caps.get(), CAP_PERMITTED, 2, cap_value, CAP_CLEAR) < 0) return -1; |
| 85 | if (cap_set_flag(caps.get(), CAP_EFFECTIVE, 2, cap_value, CAP_CLEAR) < 0) return -1; |
| 86 | if (cap_set_proc(caps.get()) < 0) |
| 87 | return -1; |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | // Function of storaged's main thread |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 93 | void* storaged_main(void* s) { |
| 94 | storaged_t* storaged = (storaged_t*)s; |
| 95 | |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 96 | LOG_TO(SYSTEM, INFO) << "storaged: Start"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 97 | |
| 98 | for (;;) { |
| 99 | storaged->event(); |
| 100 | storaged->pause(); |
| 101 | } |
| 102 | return NULL; |
| 103 | } |
| 104 | |
| 105 | static void help_message(void) { |
| 106 | printf("usage: storaged [OPTION]\n"); |
| 107 | printf(" -d --dump Dump task I/O usage to stdout\n"); |
| 108 | printf(" -s --start Start storaged (default)\n"); |
| 109 | printf(" --emmc=INTERVAL Set publish interval of emmc lifetime information (in days)\n"); |
| 110 | printf(" --diskstats=INTERVAL Set publish interval of diskstats (in hours)\n"); |
| 111 | printf(" --unit=INTERVAL Set storaged's refresh interval (in seconds)\n"); |
| 112 | fflush(stdout); |
| 113 | } |
| 114 | |
| 115 | #define HOUR_TO_SEC ( 3600 ) |
| 116 | #define DAY_TO_SEC ( 3600 * 24 ) |
| 117 | |
| 118 | int main(int argc, char** argv) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 119 | int flag_main_service = 0; |
| 120 | int flag_dump_task = 0; |
| 121 | int flag_config = 0; |
| 122 | int unit_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT; |
| 123 | int diskstats_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH; |
| 124 | int emmc_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_EMMC_INFO_PUBLISH; |
| 125 | int fd_emmc = -1; |
| 126 | int opt; |
| 127 | |
| 128 | for (;;) { |
| 129 | int opt_idx = 0; |
| 130 | static struct option long_options[] = { |
| 131 | {"start", no_argument, 0, 's'}, |
| 132 | {"kill", no_argument, 0, 'k'}, |
| 133 | {"dump", no_argument, 0, 'd'}, |
| 134 | {"help", no_argument, 0, 'h'}, |
| 135 | {"unit", required_argument, 0, 0 }, |
| 136 | {"diskstats", required_argument, 0, 0 }, |
| 137 | {"emmc", required_argument, 0, 0 } |
| 138 | }; |
| 139 | opt = getopt_long(argc, argv, ":skdh0", long_options, &opt_idx); |
| 140 | if (opt == -1) { |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | switch (opt) { |
| 145 | case 0: |
| 146 | printf("option %s", long_options[opt_idx].name); |
| 147 | if (optarg) { |
| 148 | printf(" with arg %s", optarg); |
| 149 | if (strcmp(long_options[opt_idx].name, "unit") == 0) { |
| 150 | unit_interval = atoi(optarg); |
| 151 | if (unit_interval == 0) { |
| 152 | fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n", |
| 153 | long_options[opt_idx].name); |
| 154 | help_message(); |
| 155 | return -1; |
| 156 | } |
| 157 | } else if (strcmp(long_options[opt_idx].name, "diskstats") == 0) { |
| 158 | diskstats_interval = atoi(optarg) * HOUR_TO_SEC; |
| 159 | if (diskstats_interval == 0) { |
| 160 | fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n", |
| 161 | long_options[opt_idx].name); |
| 162 | help_message(); |
| 163 | return -1; |
| 164 | } |
| 165 | |
| 166 | } else if (strcmp(long_options[opt_idx].name, "emmc") == 0) { |
| 167 | emmc_interval = atoi(optarg) * DAY_TO_SEC; |
| 168 | if (diskstats_interval == 0) { |
| 169 | fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n", |
| 170 | long_options[opt_idx].name); |
| 171 | help_message(); |
| 172 | return -1; |
| 173 | } |
| 174 | } |
| 175 | flag_config = 1; |
| 176 | } else { |
| 177 | fprintf(stderr, "Invalid argument. Option %s requires an argument.\n", |
| 178 | long_options[opt_idx].name); |
| 179 | help_message(); |
| 180 | return -1; |
| 181 | } |
| 182 | printf("\n"); |
| 183 | break; |
| 184 | case 's': |
| 185 | flag_main_service = 1; |
| 186 | break; |
| 187 | case 'd': |
| 188 | flag_dump_task = 1; |
| 189 | break; |
| 190 | case 'h': |
| 191 | help_message(); |
| 192 | return 0; |
| 193 | case '?': |
| 194 | default: |
| 195 | fprintf(stderr, "no supported option\n"); |
| 196 | help_message(); |
| 197 | return -1; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (argc == 1) { |
| 202 | flag_main_service = 1; |
| 203 | } |
| 204 | |
| 205 | if (flag_main_service && flag_dump_task) { |
| 206 | fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n"); |
| 207 | help_message(); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if (flag_config && flag_dump_task) { |
| 212 | fprintf(stderr, "Invalid arguments. Cannot set configs in \'dump\' option.\n"); |
| 213 | help_message(); |
| 214 | return -1; |
| 215 | } |
| 216 | |
| 217 | if (flag_main_service) { // start main thread |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 218 | static const char mmc0_ext_csd[] = "/d/mmc0/mmc0:0001/ext_csd"; |
| 219 | fd_emmc = android_get_control_file(mmc0_ext_csd); |
| 220 | if (fd_emmc < 0) |
| 221 | fd_emmc = TEMP_FAILURE_RETRY(open(mmc0_ext_csd, O_RDONLY)); |
| 222 | |
| 223 | if (drop_privs() != 0) { |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | storaged.set_privileged_fds(fd_emmc); |
| 228 | |
| 229 | if (flag_config) { |
| 230 | storaged.set_unit_interval(unit_interval); |
| 231 | storaged.set_diskstats_interval(diskstats_interval); |
| 232 | storaged.set_emmc_interval(emmc_interval); |
| 233 | } |
| 234 | |
| 235 | // Start the main thread of storaged |
| 236 | pthread_t storaged_main_thread; |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 237 | errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged); |
| 238 | if (errno != 0) { |
| 239 | PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | defaultServiceManager()->addService(String16("storaged"), new Storaged()); |
| 244 | android::ProcessState::self()->startThreadPool(); |
| 245 | IPCThreadState::self()->joinThreadPool(); |
| 246 | pthread_join(storaged_main_thread, NULL); |
| 247 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 248 | close(fd_emmc); |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | if (flag_dump_task) { |
| 254 | sp<IStoraged> storaged_service = get_storaged_service(); |
| 255 | if (storaged_service == NULL) { |
| 256 | fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n"); |
| 257 | return -1; |
| 258 | } |
| 259 | std::vector<struct task_info> res = storaged_service->dump_tasks(NULL); |
| 260 | |
| 261 | if (res.size() == 0) { |
| 262 | fprintf(stderr, "Task I/O is not readable in this version of kernel.\n"); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | time_t starttime = storaged.get_starttime(); |
| 267 | |
| 268 | if (starttime == (time_t)-1) { |
| 269 | fprintf(stderr, "Unknown start time\n"); |
| 270 | } else { |
| 271 | char* time_str = ctime(&starttime); |
| 272 | printf("Application I/O was collected by storaged since %s", time_str); |
| 273 | } |
| 274 | |
| 275 | sort_running_tasks_info(res); |
| 276 | log_console_running_tasks_info(res); |
| 277 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | return 0; |
ynwang | af49d97 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 282 | } |