summaryrefslogtreecommitdiffstats
blob: 31ada686a4ce88fc7b11f7552eb17f2c0ddb53ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "storaged"
#define KLOG_LEVEL 6

#include <fcntl.h>
#include <getopt.h>
#include <pthread.h>
#include <stdio.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <vector>

#include <android-base/macros.h>
#include <android-base/stringprintf.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <binder/IPCThreadState.h>
#include <cutils/android_get_control_file.h>
#include <cutils/klog.h>
#include <cutils/sched_policy.h>
#include <private/android_filesystem_config.h>

#include <storaged.h>
#include <storaged_service.h>
#include <storaged_utils.h>

storaged_t storaged;

static int drop_privs() {
    // privilege setting
    struct sched_param param;
    memset(&param, 0, sizeof(param));

    if (set_sched_policy(0, SP_BACKGROUND) < 0) return -1;

    if (sched_setscheduler((pid_t) 0, SCHED_BATCH, &param) < 0) return -1;

    if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) return -1;

    if (prctl(PR_SET_KEEPCAPS, 1) < 0) return -1;

    std::unique_ptr<struct _cap_struct, int(*)(void *)> caps(cap_init(), cap_free);
    if (cap_clear(caps.get()) < 0) return -1;
    cap_value_t cap_value[] = {
        CAP_SETGID,
        CAP_SETUID,
        CAP_SYS_PTRACE // allow access to proc/<pid>/io as non-root user
    };
    if (cap_set_flag(caps.get(), CAP_PERMITTED,
                     arraysize(cap_value), cap_value,
                     CAP_SET) < 0) return -1;
    if (cap_set_flag(caps.get(), CAP_EFFECTIVE,
                     arraysize(cap_value), cap_value,
                     CAP_SET) < 0) return -1;
    if (cap_set_proc(caps.get()) < 0)
        return -1;

    gid_t groups[] = { AID_READPROC };

    if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) == -1) return -1;

    if (setgid(AID_SYSTEM) != 0) return -1;

    if (setuid(AID_SYSTEM) != 0) return -1;

    if (cap_set_flag(caps.get(), CAP_PERMITTED, 2, cap_value, CAP_CLEAR) < 0) return -1;
    if (cap_set_flag(caps.get(), CAP_EFFECTIVE, 2, cap_value, CAP_CLEAR) < 0) return -1;
    if (cap_set_proc(caps.get()) < 0)
        return -1;

    return 0;
}

// Function of storaged's main thread
extern int fd_dmesg;
void* storaged_main(void* s) {
    storaged_t* storaged = (storaged_t*)s;

    if (fd_dmesg >= 0) {
        static const char start_message[] = {KMSG_PRIORITY(LOG_INFO),
            's', 't', 'o', 'r', 'a', 'g', 'e', 'd', ':', ' ', 'S', 't', 'a', 'r', 't', '\n'};
        write(fd_dmesg, start_message, sizeof(start_message));
    }

    for (;;) {
        storaged->event();
        storaged->pause();
    }
    return NULL;
}

static void help_message(void) {
    printf("usage: storaged [OPTION]\n");
    printf("  -d    --dump                  Dump task I/O usage to stdout\n");
    printf("  -s    --start                 Start storaged (default)\n");
    printf("        --emmc=INTERVAL         Set publish interval of emmc lifetime information (in days)\n");
    printf("        --diskstats=INTERVAL    Set publish interval of diskstats (in hours)\n");
    printf("        --unit=INTERVAL         Set storaged's refresh interval (in seconds)\n");
    fflush(stdout);
}

#define HOUR_TO_SEC ( 3600 )
#define DAY_TO_SEC ( 3600 * 24 )

int main(int argc, char** argv) {
    klog_set_level(KLOG_LEVEL);
    int flag_main_service = 0;
    int flag_dump_task = 0;
    int flag_config = 0;
    int unit_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_UNIT;
    int diskstats_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_DISK_STATS_PUBLISH;
    int emmc_interval = DEFAULT_PERIODIC_CHORES_INTERVAL_EMMC_INFO_PUBLISH;
    int fd_emmc = -1;
    int opt;

    for (;;) {
        int opt_idx = 0;
        static struct option long_options[] = {
            {"start",       no_argument,        0, 's'},
            {"kill",        no_argument,        0, 'k'},
            {"dump",        no_argument,        0, 'd'},
            {"help",        no_argument,        0, 'h'},
            {"unit",        required_argument,  0,  0 },
            {"diskstats",   required_argument,  0,  0 },
            {"emmc",        required_argument,  0,  0 }
        };
        opt = getopt_long(argc, argv, ":skdh0", long_options, &opt_idx);
        if (opt == -1) {
            break;
        }

        switch (opt) {
        case 0:
            printf("option %s", long_options[opt_idx].name);
            if (optarg) {
                printf(" with arg %s", optarg);
                if (strcmp(long_options[opt_idx].name, "unit") == 0) {
                    unit_interval = atoi(optarg);
                    if (unit_interval == 0) {
                        fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
                                long_options[opt_idx].name);
                        help_message();
                        return -1;
                    }
                } else if (strcmp(long_options[opt_idx].name, "diskstats") == 0) {
                    diskstats_interval = atoi(optarg) * HOUR_TO_SEC;
                    if (diskstats_interval == 0) {
                        fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
                                long_options[opt_idx].name);
                        help_message();
                        return -1;
                    }

                } else if (strcmp(long_options[opt_idx].name, "emmc") == 0) {
                    emmc_interval = atoi(optarg) * DAY_TO_SEC;
                    if (diskstats_interval == 0) {
                        fprintf(stderr, "Invalid argument. Option %s requires an integer argument greater than 0.\n",
                                long_options[opt_idx].name);
                        help_message();
                        return -1;
                    }
                }
                flag_config = 1;
            } else {
                fprintf(stderr, "Invalid argument. Option %s requires an argument.\n",
                        long_options[opt_idx].name);
                help_message();
                return -1;
            }
            printf("\n");
            break;
        case 's':
            flag_main_service = 1;
            break;
        case 'd':
            flag_dump_task = 1;
            break;
        case 'h':
            help_message();
            return 0;
        case '?':
        default:
            fprintf(stderr, "no supported option\n");
            help_message();
            return -1;
        }
    }

    if (argc == 1) {
        flag_main_service = 1;
    }

    if (flag_main_service && flag_dump_task) {
        fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
        help_message();
        return -1;
    }

    if (flag_config && flag_dump_task) {
        fprintf(stderr, "Invalid arguments. Cannot set configs in \'dump\' option.\n");
        help_message();
        return -1;
    }

    if (flag_main_service) { // start main thread
        static const char dev_kmsg[] = "/dev/kmsg";
        fd_dmesg = android_get_control_file(dev_kmsg);
        if (fd_dmesg < 0)
            fd_dmesg = TEMP_FAILURE_RETRY(open(dev_kmsg, O_WRONLY));

        static const char mmc0_ext_csd[] = "/d/mmc0/mmc0:0001/ext_csd";
        fd_emmc = android_get_control_file(mmc0_ext_csd);
        if (fd_emmc < 0)
            fd_emmc = TEMP_FAILURE_RETRY(open(mmc0_ext_csd, O_RDONLY));

        if (drop_privs() != 0) {
            return -1;
        }

        storaged.set_privileged_fds(fd_emmc);

        if (flag_config) {
            storaged.set_unit_interval(unit_interval);
            storaged.set_diskstats_interval(diskstats_interval);
            storaged.set_emmc_interval(emmc_interval);
        }

        // Start the main thread of storaged
        pthread_t storaged_main_thread;
        if (pthread_create(&storaged_main_thread, NULL, storaged_main, &storaged)) {
            if (fd_dmesg >= 0) {
                std::string error_message = android::base::StringPrintf(
                    "%s Failed to create main thread\n", kmsg_error_prefix);
                write(fd_dmesg, error_message.c_str(), error_message.length());
            }
            return -1;
        }

        defaultServiceManager()->addService(String16("storaged"), new Storaged());
        android::ProcessState::self()->startThreadPool();
        IPCThreadState::self()->joinThreadPool();
        pthread_join(storaged_main_thread, NULL);

        close(fd_dmesg);
        close(fd_emmc);

        return 0;
    }

    if (flag_dump_task) {
        sp<IStoraged> storaged_service = get_storaged_service();
        if (storaged_service == NULL) {
            fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
            return -1;
        }
        std::vector<struct task_info> res = storaged_service->dump_tasks(NULL);

        if (res.size() == 0) {
            fprintf(stderr, "Task I/O is not readable in this version of kernel.\n");
            return 0;
        }

        time_t starttime = storaged.get_starttime();

        if (starttime == (time_t)-1) {
            fprintf(stderr, "Unknown start time\n");
        } else {
            char* time_str = ctime(&starttime);
            printf("Application I/O was collected by storaged since %s", time_str);
        }

        sort_running_tasks_info(res);
        log_console_running_tasks_info(res);

        return 0;
    }

    return 0;
}