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 | |
Jin Qian | 4882eab | 2017-04-03 18:05:01 -0700 | [diff] [blame] | 45 | sp<storaged_t> storaged; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 46 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 47 | // Function of storaged's main thread |
Jin Qian | 4882eab | 2017-04-03 18:05:01 -0700 | [diff] [blame] | 48 | void* storaged_main(void* /* unused */) { |
| 49 | storaged = new storaged_t(); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 50 | |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 51 | storaged->init_battery_service(); |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 52 | storaged->report_storage_info(); |
Jin Qian | 5b962c6 | 2017-01-30 14:48:38 -0800 | [diff] [blame] | 53 | |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 54 | LOG_TO(SYSTEM, INFO) << "storaged: Start"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 55 | |
| 56 | for (;;) { |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 57 | storaged->event_checked(); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 58 | storaged->pause(); |
| 59 | } |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | static void help_message(void) { |
| 64 | printf("usage: storaged [OPTION]\n"); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 65 | printf(" -u --uid Dump uid I/O usage to stdout\n"); |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 66 | printf(" -t --task Dump task I/O usage to stdout\n"); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 67 | printf(" -s --start Start storaged (default)\n"); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 68 | fflush(stdout); |
| 69 | } |
| 70 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 71 | int main(int argc, char** argv) { |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 72 | bool flag_main_service = false; |
| 73 | bool flag_dump_uid = false; |
| 74 | bool flag_dump_task = false; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 75 | int opt; |
| 76 | |
| 77 | for (;;) { |
| 78 | int opt_idx = 0; |
| 79 | static struct option long_options[] = { |
| 80 | {"start", no_argument, 0, 's'}, |
| 81 | {"kill", no_argument, 0, 'k'}, |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 82 | {"uid", no_argument, 0, 'u'}, |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 83 | {"task", no_argument, 0, 't'}, |
Jin Qian | 3790f5b | 2017-01-23 14:38:47 -0800 | [diff] [blame] | 84 | {"help", no_argument, 0, 'h'} |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 85 | }; |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 86 | opt = getopt_long(argc, argv, ":skdhu0t", long_options, &opt_idx); |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 87 | if (opt == -1) { |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | switch (opt) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 92 | case 's': |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 93 | flag_main_service = true; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 94 | break; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 95 | case 'u': |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 96 | flag_dump_uid = true; |
| 97 | break; |
| 98 | case 't': |
| 99 | flag_dump_task = true; |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 100 | break; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 101 | case 'h': |
| 102 | help_message(); |
| 103 | return 0; |
| 104 | case '?': |
| 105 | default: |
| 106 | fprintf(stderr, "no supported option\n"); |
| 107 | help_message(); |
| 108 | return -1; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (argc == 1) { |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 113 | flag_main_service = true; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 116 | if (flag_main_service && (flag_dump_uid || flag_dump_task)) { |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 117 | fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n"); |
| 118 | help_message(); |
| 119 | return -1; |
| 120 | } |
| 121 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 122 | if (flag_main_service) { // start main thread |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 123 | // Start the main thread of storaged |
| 124 | pthread_t storaged_main_thread; |
Jin Qian | 4882eab | 2017-04-03 18:05:01 -0700 | [diff] [blame] | 125 | errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL); |
Jin Qian | 535ddbe | 2017-01-11 17:19:21 -0800 | [diff] [blame] | 126 | if (errno != 0) { |
| 127 | PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread"; |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | defaultServiceManager()->addService(String16("storaged"), new Storaged()); |
| 132 | android::ProcessState::self()->startThreadPool(); |
| 133 | IPCThreadState::self()->joinThreadPool(); |
| 134 | pthread_join(storaged_main_thread, NULL); |
| 135 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 136 | return 0; |
| 137 | } |
| 138 | |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 139 | if (flag_dump_uid || flag_dump_task) { |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 140 | sp<IStoraged> storaged_service = get_storaged_service(); |
| 141 | if (storaged_service == NULL) { |
| 142 | fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n"); |
| 143 | return -1; |
| 144 | } |
| 145 | std::vector<struct uid_info> res = storaged_service->dump_uids(NULL); |
| 146 | |
| 147 | if (res.size() == 0) { |
| 148 | fprintf(stderr, "UID I/O is not readable in this version of kernel.\n"); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | sort_running_uids_info(res); |
Yang Jin | 3906c89 | 2017-06-22 15:18:21 -0700 | [diff] [blame] | 153 | log_console_running_uids_info(res, flag_dump_task); |
Jin Qian | bcd6e3b | 2016-12-28 15:43:51 -0800 | [diff] [blame] | 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
ynwang | 62cb372 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 158 | return 0; |
ynwang | af49d97 | 2016-06-17 14:30:48 -0700 | [diff] [blame] | 159 | } |