blob: 91515749323e8e322262dd4cbd943e429e1a9ed1 [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;
46
47static int drop_privs() {
48 // privilege setting
49 struct sched_param param;
50 memset(&param, 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, &param) < 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
ynwang62cb3722016-06-17 14:30:48 -070093void* storaged_main(void* s) {
94 storaged_t* storaged = (storaged_t*)s;
95
Jin Qian535ddbe2017-01-11 17:19:21 -080096 LOG_TO(SYSTEM, INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070097
98 for (;;) {
99 storaged->event();
100 storaged->pause();
101 }
102 return NULL;
103}
104
105static void help_message(void) {
106 printf("usage: storaged [OPTION]\n");
107 printf(" -d --dump Dump task I/O usage to stdout\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800108 printf(" -u --uid Dump uid I/O usage to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -0700109 printf(" -s --start Start storaged (default)\n");
110 printf(" --emmc=INTERVAL Set publish interval of emmc lifetime information (in days)\n");
111 printf(" --diskstats=INTERVAL Set publish interval of diskstats (in hours)\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800112 printf(" --uidio=INTERVAL Set publish interval of uid io (in hours)\n");
ynwang62cb3722016-06-17 14:30:48 -0700113 printf(" --unit=INTERVAL Set storaged's refresh interval (in seconds)\n");
114 fflush(stdout);
115}
116
117#define HOUR_TO_SEC ( 3600 )
118#define DAY_TO_SEC ( 3600 * 24 )
119
120int main(int argc, char** argv) {
ynwang62cb3722016-06-17 14:30:48 -0700121 int flag_main_service = 0;
122 int flag_dump_task = 0;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800123 int flag_dump_uid = 0;
ynwang62cb3722016-06-17 14:30:48 -0700124 int flag_config = 0;
125 int unit_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT;
126 int diskstats_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH;
127 int emmc_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_EMMC_INFO_PUBLISH;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800128 int uid_io_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_UID_IO_ALERT;
ynwang62cb3722016-06-17 14:30:48 -0700129 int fd_emmc = -1;
130 int opt;
131
132 for (;;) {
133 int opt_idx = 0;
134 static struct option long_options[] = {
135 {"start", no_argument, 0, 's'},
136 {"kill", no_argument, 0, 'k'},
137 {"dump", no_argument, 0, 'd'},
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800138 {"uid", no_argument, 0, 'u'},
ynwang62cb3722016-06-17 14:30:48 -0700139 {"help", no_argument, 0, 'h'},
140 {"unit", required_argument, 0, 0 },
141 {"diskstats", required_argument, 0, 0 },
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800142 {"emmc", required_argument, 0, 0 },
143 {"uidio", required_argument, 0, 0 }
ynwang62cb3722016-06-17 14:30:48 -0700144 };
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800145 opt = getopt_long(argc, argv, ":skdhu0", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -0700146 if (opt == -1) {
147 break;
148 }
149
150 switch (opt) {
151 case 0:
152 printf("option %s", long_options[opt_idx].name);
153 if (optarg) {
154 printf(" with arg %s", optarg);
155 if (strcmp(long_options[opt_idx].name, "unit") == 0) {
156 unit_interval = atoi(optarg);
157 if (unit_interval == 0) {
158 fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
159 long_options[opt_idx].name);
160 help_message();
161 return -1;
162 }
163 } else if (strcmp(long_options[opt_idx].name, "diskstats") == 0) {
164 diskstats_interval = atoi(optarg) * HOUR_TO_SEC;
165 if (diskstats_interval == 0) {
166 fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
167 long_options[opt_idx].name);
168 help_message();
169 return -1;
170 }
171
172 } else if (strcmp(long_options[opt_idx].name, "emmc") == 0) {
173 emmc_interval = atoi(optarg) * DAY_TO_SEC;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800174 if (emmc_interval == 0) {
175 fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
176 long_options[opt_idx].name);
177 help_message();
178 return -1;
179 }
180 } else if (strcmp(long_options[opt_idx].name, "uidio") == 0) {
181 uid_io_interval = atoi(optarg) * HOUR_TO_SEC;
182 if (uid_io_interval == 0) {
ynwang62cb3722016-06-17 14:30:48 -0700183 fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
184 long_options[opt_idx].name);
185 help_message();
186 return -1;
187 }
188 }
189 flag_config = 1;
190 } else {
191 fprintf(stderr, "Invalid argument. Option %s requires an argument.\n",
192 long_options[opt_idx].name);
193 help_message();
194 return -1;
195 }
196 printf("\n");
197 break;
198 case 's':
199 flag_main_service = 1;
200 break;
201 case 'd':
202 flag_dump_task = 1;
203 break;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800204 case 'u':
205 flag_dump_uid = 1;
206 break;
ynwang62cb3722016-06-17 14:30:48 -0700207 case 'h':
208 help_message();
209 return 0;
210 case '?':
211 default:
212 fprintf(stderr, "no supported option\n");
213 help_message();
214 return -1;
215 }
216 }
217
218 if (argc == 1) {
219 flag_main_service = 1;
220 }
221
222 if (flag_main_service && flag_dump_task) {
223 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
224 help_message();
225 return -1;
226 }
227
228 if (flag_config && flag_dump_task) {
229 fprintf(stderr, "Invalid arguments. Cannot set configs in \'dump\' option.\n");
230 help_message();
231 return -1;
232 }
233
234 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700235 static const char mmc0_ext_csd[] = "/d/mmc0/mmc0:0001/ext_csd";
236 fd_emmc = android_get_control_file(mmc0_ext_csd);
237 if (fd_emmc < 0)
238 fd_emmc = TEMP_FAILURE_RETRY(open(mmc0_ext_csd, O_RDONLY));
239
240 if (drop_privs() != 0) {
241 return -1;
242 }
243
244 storaged.set_privileged_fds(fd_emmc);
245
246 if (flag_config) {
247 storaged.set_unit_interval(unit_interval);
248 storaged.set_diskstats_interval(diskstats_interval);
249 storaged.set_emmc_interval(emmc_interval);
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800250 storaged.set_uid_io_interval(uid_io_interval);
ynwang62cb3722016-06-17 14:30:48 -0700251 }
252
253 // Start the main thread of storaged
254 pthread_t storaged_main_thread;
Jin Qian535ddbe2017-01-11 17:19:21 -0800255 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged);
256 if (errno != 0) {
257 PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700258 return -1;
259 }
260
261 defaultServiceManager()->addService(String16("storaged"), new Storaged());
262 android::ProcessState::self()->startThreadPool();
263 IPCThreadState::self()->joinThreadPool();
264 pthread_join(storaged_main_thread, NULL);
265
ynwang62cb3722016-06-17 14:30:48 -0700266 close(fd_emmc);
267
268 return 0;
269 }
270
271 if (flag_dump_task) {
272 sp<IStoraged> storaged_service = get_storaged_service();
273 if (storaged_service == NULL) {
274 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
275 return -1;
276 }
277 std::vector<struct task_info> res = storaged_service->dump_tasks(NULL);
278
279 if (res.size() == 0) {
280 fprintf(stderr, "Task I/O is not readable in this version of kernel.\n");
281 return 0;
282 }
283
284 time_t starttime = storaged.get_starttime();
285
286 if (starttime == (time_t)-1) {
287 fprintf(stderr, "Unknown start time\n");
288 } else {
289 char* time_str = ctime(&starttime);
290 printf("Application I/O was collected by storaged since %s", time_str);
291 }
292
293 sort_running_tasks_info(res);
294 log_console_running_tasks_info(res);
295
ynwang62cb3722016-06-17 14:30:48 -0700296 return 0;
297 }
298
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800299 if (flag_dump_uid) {
300 sp<IStoraged> storaged_service = get_storaged_service();
301 if (storaged_service == NULL) {
302 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
303 return -1;
304 }
305 std::vector<struct uid_info> res = storaged_service->dump_uids(NULL);
306
307 if (res.size() == 0) {
308 fprintf(stderr, "UID I/O is not readable in this version of kernel.\n");
309 return 0;
310 }
311
312 sort_running_uids_info(res);
313 log_console_running_uids_info(res);
314
315 return 0;
316 }
317
ynwang62cb3722016-06-17 14:30:48 -0700318 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700319}