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