blob: 1103df22f1c5cce5505074b1e8f1649376a387c1 [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,
Jin Qian88ad33e2017-01-23 15:31:04 -080064 CAP_SETUID
ynwang62cb3722016-06-17 14:30:48 -070065 };
66 if (cap_set_flag(caps.get(), CAP_PERMITTED,
67 arraysize(cap_value), cap_value,
68 CAP_SET) < 0) return -1;
69 if (cap_set_flag(caps.get(), CAP_EFFECTIVE,
70 arraysize(cap_value), cap_value,
71 CAP_SET) < 0) return -1;
72 if (cap_set_proc(caps.get()) < 0)
73 return -1;
74
ynwang62cb3722016-06-17 14:30:48 -070075 if (setgid(AID_SYSTEM) != 0) return -1;
76
77 if (setuid(AID_SYSTEM) != 0) return -1;
78
79 if (cap_set_flag(caps.get(), CAP_PERMITTED, 2, cap_value, CAP_CLEAR) < 0) return -1;
80 if (cap_set_flag(caps.get(), CAP_EFFECTIVE, 2, cap_value, CAP_CLEAR) < 0) return -1;
81 if (cap_set_proc(caps.get()) < 0)
82 return -1;
83
84 return 0;
85}
86
87// Function of storaged's main thread
ynwang62cb3722016-06-17 14:30:48 -070088void* storaged_main(void* s) {
89 storaged_t* storaged = (storaged_t*)s;
90
Jin Qian5b962c62017-01-30 14:48:38 -080091 storaged->init_battery_service();
92
Jin Qian535ddbe2017-01-11 17:19:21 -080093 LOG_TO(SYSTEM, INFO) << "storaged: Start";
ynwang62cb3722016-06-17 14:30:48 -070094
95 for (;;) {
Jin Qian3790f5b2017-01-23 14:38:47 -080096 storaged->event_checked();
ynwang62cb3722016-06-17 14:30:48 -070097 storaged->pause();
98 }
99 return NULL;
100}
101
102static void help_message(void) {
103 printf("usage: storaged [OPTION]\n");
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800104 printf(" -u --uid Dump uid I/O usage to stdout\n");
ynwang62cb3722016-06-17 14:30:48 -0700105 printf(" -s --start Start storaged (default)\n");
ynwang62cb3722016-06-17 14:30:48 -0700106 fflush(stdout);
107}
108
ynwang62cb3722016-06-17 14:30:48 -0700109int main(int argc, char** argv) {
ynwang62cb3722016-06-17 14:30:48 -0700110 int flag_main_service = 0;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800111 int flag_dump_uid = 0;
ynwang62cb3722016-06-17 14:30:48 -0700112 int fd_emmc = -1;
113 int opt;
114
115 for (;;) {
116 int opt_idx = 0;
117 static struct option long_options[] = {
118 {"start", no_argument, 0, 's'},
119 {"kill", no_argument, 0, 'k'},
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800120 {"uid", no_argument, 0, 'u'},
Jin Qian3790f5b2017-01-23 14:38:47 -0800121 {"help", no_argument, 0, 'h'}
ynwang62cb3722016-06-17 14:30:48 -0700122 };
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800123 opt = getopt_long(argc, argv, ":skdhu0", long_options, &opt_idx);
ynwang62cb3722016-06-17 14:30:48 -0700124 if (opt == -1) {
125 break;
126 }
127
128 switch (opt) {
ynwang62cb3722016-06-17 14:30:48 -0700129 case 's':
130 flag_main_service = 1;
131 break;
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800132 case 'u':
133 flag_dump_uid = 1;
134 break;
ynwang62cb3722016-06-17 14:30:48 -0700135 case 'h':
136 help_message();
137 return 0;
138 case '?':
139 default:
140 fprintf(stderr, "no supported option\n");
141 help_message();
142 return -1;
143 }
144 }
145
146 if (argc == 1) {
147 flag_main_service = 1;
148 }
149
Jin Qian88ad33e2017-01-23 15:31:04 -0800150 if (flag_main_service && flag_dump_uid) {
ynwang62cb3722016-06-17 14:30:48 -0700151 fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
152 help_message();
153 return -1;
154 }
155
ynwang62cb3722016-06-17 14:30:48 -0700156 if (flag_main_service) { // start main thread
ynwang62cb3722016-06-17 14:30:48 -0700157 static const char mmc0_ext_csd[] = "/d/mmc0/mmc0:0001/ext_csd";
158 fd_emmc = android_get_control_file(mmc0_ext_csd);
159 if (fd_emmc < 0)
160 fd_emmc = TEMP_FAILURE_RETRY(open(mmc0_ext_csd, O_RDONLY));
161
162 if (drop_privs() != 0) {
163 return -1;
164 }
165
166 storaged.set_privileged_fds(fd_emmc);
167
ynwang62cb3722016-06-17 14:30:48 -0700168 // Start the main thread of storaged
169 pthread_t storaged_main_thread;
Jin Qian535ddbe2017-01-11 17:19:21 -0800170 errno = pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged);
171 if (errno != 0) {
172 PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
ynwang62cb3722016-06-17 14:30:48 -0700173 return -1;
174 }
175
176 defaultServiceManager()->addService(String16("storaged"), new Storaged());
177 android::ProcessState::self()->startThreadPool();
178 IPCThreadState::self()->joinThreadPool();
179 pthread_join(storaged_main_thread, NULL);
180
ynwang62cb3722016-06-17 14:30:48 -0700181 close(fd_emmc);
182
183 return 0;
184 }
185
Jin Qianbcd6e3b2016-12-28 15:43:51 -0800186 if (flag_dump_uid) {
187 sp<IStoraged> storaged_service = get_storaged_service();
188 if (storaged_service == NULL) {
189 fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
190 return -1;
191 }
192 std::vector<struct uid_info> res = storaged_service->dump_uids(NULL);
193
194 if (res.size() == 0) {
195 fprintf(stderr, "UID I/O is not readable in this version of kernel.\n");
196 return 0;
197 }
198
199 sort_running_uids_info(res);
200 log_console_running_uids_info(res);
201
202 return 0;
203 }
204
ynwang62cb3722016-06-17 14:30:48 -0700205 return 0;
ynwangaf49d972016-06-17 14:30:48 -0700206}