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