blob: f5f1a3dfad1ec60b9d35586d7f5389ecdf5132c1 [file] [log] [blame]
Erik Kline85890042018-05-25 19:19:11 +09001/*
2 * Copyright (C) 2018 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#include "Process.h"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <signal.h>
22#include <sys/resource.h>
23#include <sys/stat.h>
24#include <sys/time.h>
25#include <unistd.h>
26
27#include <chrono>
28#include <sstream>
29
30#include "android-base/stringprintf.h"
31#define LOG_TAG "Netd"
32#include "log/log.h"
33
34#include "netdutils/Misc.h"
35#include "netdutils/Slice.h"
36#include "netdutils/Syscalls.h"
37#include "netdutils/UniqueFd.h"
38
39namespace android {
40
41using base::StringPrintf;
42using netdutils::Fd;
43using netdutils::UniqueFd;
44using netdutils::isOk;
45using netdutils::makeCleanup;
46using netdutils::makeSlice;
47
48namespace net {
49namespace process {
50namespace {
51
52const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
53const mode_t PID_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
54
55// Set up during static initialization.
56const std::chrono::steady_clock::time_point sStartTime = std::chrono::steady_clock::now();
57
58std::chrono::milliseconds totalRunningTime() {
59 return std::chrono::duration_cast<std::chrono::milliseconds>(
60 std::chrono::steady_clock::now() - sStartTime);
61}
62
63std::string formatDuration(std::chrono::milliseconds duration) {
64 auto hrs = std::chrono::duration_cast<std::chrono::hours>(duration);
65 duration -= hrs;
66 const auto mins = std::chrono::duration_cast<std::chrono::minutes>(duration);
67 duration -= mins;
68 const auto secs = std::chrono::duration_cast<std::chrono::seconds>(duration);
69 duration -= secs;
70
71 // No std::chrono::days until C++20.
72 const unsigned days = hrs.count() / 24;
73 hrs -= std::chrono::hours(days * 24);
74
75 return StringPrintf("%ud%luh%lum%llus%llums",
76 days, hrs.count(), mins.count(), secs.count(), duration.count());
77}
78
79} // unnamed namespace
80
81void blockSigPipe() {
82 sigset_t mask;
83
84 sigemptyset(&mask);
85 sigaddset(&mask, SIGPIPE);
86 if (sigprocmask(SIG_BLOCK, &mask, NULL) != 0) {
87 ALOGW("WARNING: SIGPIPE not blocked\n");
88 }
89}
90
91void writePidFile(const std::string& pidFile) {
92 const std::string pid_buf(StringPrintf("%d\n", (int) getpid()));
93
94 Fd pidFd = open(pidFile.c_str(), PID_FILE_FLAGS, PID_FILE_MODE);
95 if (pidFd.get() == -1) {
96 ALOGE("Unable to create pid file (%s)", strerror(errno));
97 return;
98 }
99
100 const UniqueFd autoClosePidFile(pidFd);
101 auto rmFile = makeCleanup([pidFile] { removePidFile(pidFile); });
102
103 // File creation is affected by umask, so make sure the right mode bits are set.
104 if (fchmod(pidFd.get(), PID_FILE_MODE) == -1) {
105 ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, pidFile.c_str(), strerror(errno));
106 return;
107 }
108
109 auto& sys = netdutils::sSyscalls.get();
110 const auto rval = sys.write(pidFd, makeSlice(pid_buf));
111 if (!isOk(rval.status()) || rval.value() != pid_buf.size()) {
112 ALOGE("Unable to write to pid file (%s)", strerror(errno));
113 return;
114 }
115
116 rmFile.release(); // Don't delete the pid file :-)
117}
118
119void removePidFile(const std::string& pidFile) {
120 unlink(pidFile.c_str());
121}
122
123void dump(DumpWriter& dw) {
124 std::stringstream out;
125 out << "ppid:" << getppid() << " -> pid:" << getpid() << " -> tid:" << gettid() << '\n';
126 const auto runningTime = totalRunningTime();
127 out << "total running time: " << formatDuration(runningTime)
128 << " (" << totalRunningTime().count() << "ms)" << '\n';
129
130 struct rusage ru{};
131 if (getrusage(RUSAGE_SELF, &ru) == 0) {
132 out << "user: " << ru.ru_utime.tv_sec << "s" << (ru.ru_utime.tv_usec/1000) << "ms"
133 << " sys: " << ru.ru_stime.tv_sec << "s" << (ru.ru_stime.tv_usec/1000) << "ms"
134 << '\n';
135 out << "maxrss: " << ru.ru_maxrss << "kB" << '\n';
136 }
137
138 dw.println(out.str());
139}
140
141} // namespace process
142} // namespace net
143} // namespace android