blob: e25298b129a53f30bb4f8d925695cf4f31cb6623 [file] [log] [blame]
ynwang62cb3722016-06-17 14:30:48 -07001/*
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 Qian535ddbe2017-01-11 17:19:21 -080032#include <android-base/logging.h>
ynwang62cb3722016-06-17 14:30:48 -070033#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>
ynwang62cb3722016-06-17 14:30:48 -070038#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
45storaged_t storaged;
Jin Qian4fc338e2017-03-15 19:03:06 -070046emmc_info_t emmc_info;
ynwang62cb3722016-06-17 14:30:48 -070047
ynwang62cb3722016-06-17 14:30:48 -070048// Function of storaged's main thread
ynwang62cb3722016-06-17 14:30:48 -070049void* storaged_main(void* s) {
50 storaged_t* storaged = (storaged_t*)s;
51
Jin Qian5b962c62017-01-30 14:48:38 -080052 storaged->init_battery_service();
53
Jin Qian535ddbe2017-01-11 17:19:21 -080054 LOG_TO(SYSTEM, INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070055
56 for (;;) {
Jin Qian3790f5b2017-01-23 14:38:47 -080057 storaged->event_checked();
ynwang62cb3722016-06-17 14:30:48 -070058 storaged->pause();
59 }
60 return NULL;
61}
62
63static void help_message(void) {
64 printf("usage: storaged [OPTION]\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -080065 printf(" -u --uid Dump uid I/O usage to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -070066 printf(" -s --start Start storaged (default)\n");
ynwang62cb3722016-06-17 14:30:48 -070067 fflush(stdout);
68}
69
ynwang62cb3722016-06-17 14:30:48 -070070int main(int argc, char** argv) {
ynwang62cb3722016-06-17 14:30:48 -070071 int flag_main_service = 0;
Jin Qianbcd6e3b2016-12-28 15:43:51 -080072 int flag_dump_uid = 0;
ynwang62cb3722016-06-17 14:30:48 -070073 int opt;
74
75 for (;;) {
76 int opt_idx = 0;
77 static struct option long_options[] = {
78 {"start", no_argument, 0, 's'},
79 {"kill", no_argument, 0, 'k'},
Jin Qianbcd6e3b2016-12-28 15:43:51 -080080 {"uid", no_argument, 0, 'u'},
Jin Qian3790f5b2017-01-23 14:38:47 -080081 {"help", no_argument, 0, 'h'}
ynwang62cb3722016-06-17 14:30:48 -070082 };
Jin Qianbcd6e3b2016-12-28 15:43:51 -080083 opt = getopt_long(argc, argv, ":skdhu0", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -070084 if (opt == -1) {
85 break;
86 }
87
88 switch (opt) {
ynwang62cb3722016-06-17 14:30:48 -070089 case 's':
90 flag_main_service = 1;
91 break;
Jin Qianbcd6e3b2016-12-28 15:43:51 -080092 case 'u':
93 flag_dump_uid = 1;
94 break;
ynwang62cb3722016-06-17 14:30:48 -070095 case 'h':
96 help_message();
97 return 0;
98 case '?':
99 default:
100 fprintf(stderr, "no supported option\n");
101 help_message();
102 return -1;
103 }
104 }
105
106 if (argc == 1) {
107 flag_main_service = 1;
108 }
109
Jin Qian88ad33e2017-01-23 15:31:04 -0800110 if (flag_main_service && flag_dump_uid) {
ynwang62cb3722016-06-17 14:30:48 -0700111 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
112 help_message();
113 return -1;
114 }
115
ynwang62cb3722016-06-17 14:30:48 -0700116 if (flag_main_service) { // start main thread
Jin Qian4fc338e2017-03-15 19:03:06 -0700117 if (emmc_info.init()) {
118 storaged.set_storage_info(&emmc_info);
119 }
ynwang62cb3722016-06-17 14:30:48 -0700120
ynwang62cb3722016-06-17 14:30:48 -0700121 // Start the main thread of storaged
122 pthread_t storaged_main_thread;
Jin Qian535ddbe2017-01-11 17:19:21 -0800123 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged);
124 if (errno != 0) {
125 PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700126 return -1;
127 }
128
129 defaultServiceManager()->addService(String16("storaged"), new Storaged());
130 android::ProcessState::self()->startThreadPool();
131 IPCThreadState::self()->joinThreadPool();
132 pthread_join(storaged_main_thread, NULL);
133
ynwang62cb3722016-06-17 14:30:48 -0700134 return 0;
135 }
136
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800137 if (flag_dump_uid) {
138 sp<IStoraged> storaged_service = get_storaged_service();
139 if (storaged_service == NULL) {
140 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
141 return -1;
142 }
143 std::vector<struct uid_info> res = storaged_service->dump_uids(NULL);
144
145 if (res.size() == 0) {
146 fprintf(stderr, "UID I/O is not readable in this version of kernel.\n");
147 return 0;
148 }
149
150 sort_running_uids_info(res);
151 log_console_running_uids_info(res);
152
153 return 0;
154 }
155
ynwang62cb3722016-06-17 14:30:48 -0700156 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700157}