Home | History | Annotate | Download | only in storaged
      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>
     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>
     32 #include <android-base/logging.h>
     33 #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>
     38 #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 
     45 sp<storaged_t> storaged;
     46 
     47 // Function of storaged's main thread
     48 void* storaged_main(void* /* unused */) {
     49     storaged = new storaged_t();
     50 
     51     storaged->init_battery_service();
     52 
     53     LOG_TO(SYSTEM, INFO) << "storaged: Start";
     54 
     55     for (;;) {
     56         storaged->event_checked();
     57         storaged->pause();
     58     }
     59     return NULL;
     60 }
     61 
     62 static void help_message(void) {
     63     printf("usage: storaged [OPTION]\n");
     64     printf("  -u    --uid                   Dump uid I/O usage to stdout\n");
     65     printf("  -s    --start                 Start storaged (default)\n");
     66     fflush(stdout);
     67 }
     68 
     69 int main(int argc, char** argv) {
     70     int flag_main_service = 0;
     71     int flag_dump_uid = 0;
     72     int opt;
     73 
     74     for (;;) {
     75         int opt_idx = 0;
     76         static struct option long_options[] = {
     77             {"start",       no_argument,        0, 's'},
     78             {"kill",        no_argument,        0, 'k'},
     79             {"uid",         no_argument,        0, 'u'},
     80             {"help",        no_argument,        0, 'h'}
     81         };
     82         opt = getopt_long(argc, argv, ":skdhu0", long_options, &opt_idx);
     83         if (opt == -1) {
     84             break;
     85         }
     86 
     87         switch (opt) {
     88         case 's':
     89             flag_main_service = 1;
     90             break;
     91         case 'u':
     92             flag_dump_uid = 1;
     93             break;
     94         case 'h':
     95             help_message();
     96             return 0;
     97         case '?':
     98         default:
     99             fprintf(stderr, "no supported option\n");
    100             help_message();
    101             return -1;
    102         }
    103     }
    104 
    105     if (argc == 1) {
    106         flag_main_service = 1;
    107     }
    108 
    109     if (flag_main_service && flag_dump_uid) {
    110         fprintf(stderr, "Invalid arguments. Option \"start\" and \"dump\" cannot be used together.\n");
    111         help_message();
    112         return -1;
    113     }
    114 
    115     if (flag_main_service) { // start main thread
    116         report_storage_health();
    117         // Start the main thread of storaged
    118         pthread_t storaged_main_thread;
    119         errno = pthread_create(&storaged_main_thread, NULL, storaged_main, NULL);
    120         if (errno != 0) {
    121             PLOG_TO(SYSTEM, ERROR) << "Failed to create main thread";
    122             return -1;
    123         }
    124 
    125         defaultServiceManager()->addService(String16("storaged"), new Storaged());
    126         android::ProcessState::self()->startThreadPool();
    127         IPCThreadState::self()->joinThreadPool();
    128         pthread_join(storaged_main_thread, NULL);
    129 
    130         return 0;
    131     }
    132 
    133     if (flag_dump_uid) {
    134         sp<IStoraged> storaged_service = get_storaged_service();
    135         if (storaged_service == NULL) {
    136             fprintf(stderr, "Cannot find storaged service.\nMaybe run storaged --start first?\n");
    137             return -1;
    138         }
    139         std::vector<struct uid_info> res = storaged_service->dump_uids(NULL);
    140 
    141         if (res.size() == 0) {
    142             fprintf(stderr, "UID I/O is not readable in this version of kernel.\n");
    143             return 0;
    144         }
    145 
    146         sort_running_uids_info(res);
    147         log_console_running_uids_info(res);
    148 
    149         return 0;
    150     }
    151 
    152     return 0;
    153 }
    154