• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ROSEN_SERVICE_DUMPER_H
17 #define ROSEN_SERVICE_DUMPER_H
18 
19 #include <chrono>
20 #include <cstdio>
21 #include <fcntl.h>
22 #include <string>
23 #include <unistd.h>
24 #include <vector>
25 
26 #include "ipc_types.h"
27 #include "iservice_registry.h"
28 #include "unique_fd.h"
29 
30 namespace OHOS {
31 namespace Detail {
32 template <typename Duration>
33 using SysTime = std::chrono::time_point<std::chrono::system_clock, Duration>;
34 using SysMicroSeconds = SysTime<std::chrono::microseconds>;
35 
MicroSecondsSinceEpoch()36 inline uint64_t MicroSecondsSinceEpoch()
37 {
38     SysMicroSeconds tmp = std::chrono::system_clock::now();
39     return tmp.time_since_epoch().count();
40 }
41 
ToU16String(std::string s)42 inline std::u16string ToU16String(std::string s)
43 {
44     using Converter = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>;
45     static Converter converter;
46     return converter.from_bytes(s);
47 }
48 
49 class ServiceDumper {
50 public:
ServiceDumper(sptr<IRemoteObject> service,std::string serviceName,int timeOutMs)51     ServiceDumper(sptr<IRemoteObject> service, std::string serviceName, int timeOutMs)
52         : service_(std::move(service)), serviceName_(std::move(serviceName)), timeOutMs_(timeOutMs)
53     {
54     }
55 
56     ~ServiceDumper() noexcept = default;
57 
Run(int argc,char * argv[])58     int Run(int argc, char *argv[])
59     {
60         int status = 0;
61         int pipeFd[2];
62         if (pipe2(pipeFd, O_CLOEXEC | O_DIRECT) < 0 || pipeFd[0] < 0 || pipeFd[1] < 0) {
63             (void)fprintf(stderr, "Failed to create pipe: %s.\n", strerror(errno));
64             status = -errno;
65             return status;
66         }
67 
68         readFd_ = UniqueFd(pipeFd[0]);
69 
70         {
71             UniqueFd remoteFd(pipeFd[1]);
72             std::vector<std::u16string> args;
73             if (argc > 1) {
74                 for (int i = 1; i < argc; ++i) {
75                     args.push_back(ToU16String(argv[i]));
76                 }
77             }
78 
79             if (service_->Dump(remoteFd, args) != NO_ERROR) {
80                 (void)fprintf(stderr, "Dump failed for %s.\n", serviceName_.c_str());
81             }
82         }
83 
84         return ReadAndWriteDumpInfo();
85     }
86 
87 private:
88     static constexpr int MICRO_SECS_PER_MILLI = 1000;
ReadAndWriteDumpInfo()89     int ReadAndWriteDumpInfo()
90     {
91         int status = 0;
92         auto start = MicroSecondsSinceEpoch() / MICRO_SECS_PER_MILLI;
93         auto end = start + timeOutMs_;
94 
95         char buf[PIPE_BUF] = {0};
96         while (true) {
97             auto now = MicroSecondsSinceEpoch() / MICRO_SECS_PER_MILLI;
98             if (now >= end) {
99                 break;
100             }
101 
102             auto ret = TEMP_FAILURE_RETRY(read(readFd_, buf, sizeof(buf)-1));
103             buf[ret] = '\0';
104             if (ret == 0) {
105                 // Reach to the EOF
106                 break;
107             } else if (ret < 0) {
108                 (void)fprintf(stderr, "Failed to read service's dump info: %s.\n", strerror(errno));
109                 status = -errno;
110                 break;
111             }
112 
113             (void)fprintf(stdout, "%s", buf);
114             bzero(buf, sizeof(buf));
115         }
116 
117         return status;
118     }
119 
120     sptr<IRemoteObject> service_;
121     std::string serviceName_;
122     int timeOutMs_ = -1;
123     UniqueFd readFd_;
124 };
125 } // namespace Detail
126 } // namespace OHOS
127 
128 #endif // ROSEN_SERVICE_DUMPER_H
129