Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 39 | namespace android { |
| 40 | |
| 41 | using base::StringPrintf; |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame^] | 42 | using netdutils::DumpWriter; |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 43 | using netdutils::Fd; |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 44 | using netdutils::isOk; |
| 45 | using netdutils::makeCleanup; |
| 46 | using netdutils::makeSlice; |
Luke Huang | b257d61 | 2019-03-14 21:19:13 +0800 | [diff] [blame^] | 47 | using netdutils::UniqueFd; |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 48 | |
| 49 | namespace net { |
| 50 | namespace process { |
| 51 | namespace { |
| 52 | |
| 53 | const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC; |
| 54 | const 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. |
| 57 | const std::chrono::steady_clock::time_point sStartTime = std::chrono::steady_clock::now(); |
| 58 | |
| 59 | std::chrono::milliseconds totalRunningTime() { |
| 60 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
| 61 | std::chrono::steady_clock::now() - sStartTime); |
| 62 | } |
| 63 | |
| 64 | std::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 | |
| 82 | void blockSigPipe() { |
| 83 | sigset_t mask; |
| 84 | |
| 85 | sigemptyset(&mask); |
| 86 | sigaddset(&mask, SIGPIPE); |
Yi Kong | bdfd57e | 2018-07-25 13:26:10 -0700 | [diff] [blame] | 87 | if (sigprocmask(SIG_BLOCK, &mask, nullptr) != 0) { |
Erik Kline | 8589004 | 2018-05-25 19:19:11 +0900 | [diff] [blame] | 88 | ALOGW("WARNING: SIGPIPE not blocked\n"); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void 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 | |
| 120 | void removePidFile(const std::string& pidFile) { |
| 121 | unlink(pidFile.c_str()); |
| 122 | } |
| 123 | |
| 124 | void 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 |