Commit 28a7a863 authored by zhujiashun's avatar zhujiashun

Support get command name in macos

parent 4793dfa1
......@@ -221,6 +221,7 @@ set(BUTIL_SOURCES
${CMAKE_SOURCE_DIR}/src/butil/memory/weak_ptr.cc
${CMAKE_SOURCE_DIR}/src/butil/posix/file_descriptor_shuffle.cc
${CMAKE_SOURCE_DIR}/src/butil/posix/global_descriptors.cc
${CMAKE_SOURCE_DIR}/src/butil/process_util.cc
${CMAKE_SOURCE_DIR}/src/butil/rand_util.cc
${CMAKE_SOURCE_DIR}/src/butil/rand_util_posix.cc
${CMAKE_SOURCE_DIR}/src/butil/fast_rand.cpp
......
......@@ -16,16 +16,16 @@
#include <iomanip>
#include <sys/time.h>
#include <fcntl.h> // O_RDONLY
#include <fcntl.h> // O_RDONLY
#include <gflags/gflags.h>
#include "butil/logging.h"
#include "butil/fd_guard.h" // fd_guard
#include "butil/file_util.h" // butil::FilePath
#include "butil/third_party/murmurhash3/murmurhash3.h"
#include "butil/process_util.h" // ReadCommandLine
#include "brpc/server.h"
#include "brpc/builtin/common.h"
namespace brpc {
DEFINE_string(rpc_profiling_dir, "./rpc_data/profiling",
......@@ -310,42 +310,6 @@ const char* ProfilingType2String(ProfilingType t) {
return "unknown";
}
ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) {
butil::fd_guard fd(open("/proc/self/cmdline", O_RDONLY));
if (fd < 0) {
LOG(ERROR) << "Fail to open /proc/self/cmdline";
return -1;
}
ssize_t nr = read(fd, buf, len);
if (nr <= 0) {
LOG(ERROR) << "Fail to read /proc/self/cmdline";
return -1;
}
if (with_args) {
if ((size_t)nr == len) {
LOG(ERROR) << "buf is not big enough";
return -1;
}
for (ssize_t i = 0; i < nr; ++i) {
if (buf[i] == '\0') {
buf[i] = '\n';
}
}
return nr;
} else {
for (ssize_t i = 0; i < nr; ++i) {
if (buf[i] == '\0') {
return i;
}
}
if ((size_t)nr == len) {
LOG(ERROR) << "buf is not big enough";
return -1;
}
return nr;
}
}
int FileChecksum(const char* file_path, unsigned char* checksum) {
butil::fd_guard fd(open(file_path, O_RDONLY));
if (fd < 0) {
......@@ -367,7 +331,7 @@ static pthread_once_t create_program_name_once = PTHREAD_ONCE_INIT;
static const char* s_program_name = "unknown";
static char s_cmdline[256];
static void CreateProgramName() {
const ssize_t nr = ReadCommandLine(s_cmdline, sizeof(s_cmdline) - 1, false);
const ssize_t nr = butil::ReadCommandLine(s_cmdline, sizeof(s_cmdline) - 1, false);
if (nr > 0) {
s_cmdline[nr] = '\0';
s_program_name = s_cmdline;
......
......@@ -103,12 +103,6 @@ const char* logo();
// Convert ProfilingType to its description.
const char* ProfilingType2String(ProfilingType t);
// Read command line of this program. If `with_args' is true, args are
// included and separated with spaces.
// Returns length of the command line on sucess, -1 otherwise.
// NOTE: `buf' does not end with zero.
ssize_t ReadCommandLine(char* buf, size_t len, bool with_args);
// Compute 128-bit checksum of the file at `file_path'.
// Return 0 on success.
int FileChecksum(const char* file_path, unsigned char* checksum);
......
......@@ -24,6 +24,7 @@
#include "butil/files/scoped_file.h" // ScopedFILE
#include "butil/time.h"
#include "butil/popen.h" // butil::read_command_output
#include "butil/process_util.h" // butil::ReadCommandLine
#include "brpc/log.h"
#include "brpc/controller.h" // Controller
#include "brpc/closure_guard.h" // ClosureGuard
......@@ -557,9 +558,9 @@ void PProfService::cmdline(::google::protobuf::RpcController* controller_base,
Controller* cntl = static_cast<Controller*>(controller_base);
cntl->http_response().set_content_type("text/plain" /*FIXME*/);
char buf[1024]; // should be enough?
const ssize_t nr = ReadCommandLine(buf, sizeof(buf), true);
const ssize_t nr = butil::ReadCommandLine(buf, sizeof(buf), true);
if (nr < 0) {
cntl->SetFailed(ENOENT, "Fail to read /proc/self/cmdline");
cntl->SetFailed(ENOENT, "Fail to read cmdline");
return;
}
cntl->response_attachment().append(buf, nr);
......
......@@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Zhu,Jiashun (zhujiahun@baidu.com)
// Author: Zhu,Jiashun (zhujiashun@baidu.com)
// Date: Wed Mar 14 17:44:58 CST 2018
#include "bthread/sys_futex.h"
......
// Process-related Info
// Copyright (c) 2018 Baidu, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Zhu,Jiashun (zhujiashun@baidu.com)
// Date: Wed Apr 11 14:35:56 CST 2018
#include "process_util.h"
#include <fcntl.h> // open
#include <stdio.h> // snprintf
#include <butil/fd_guard.h> // butil::fd_guard
#include <butil/logging.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h> // read, gitpid
#include <sstream> // std::ostringstream
#include <butil/popen.h> // read_command_output
namespace butil {
ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) {
#if defined(OS_LINUX)
butil::fd_guard fd(open("/proc/self/cmdline", O_RDONLY));
if (fd < 0) {
LOG(ERROR) << "Fail to open /proc/self/cmdline";
return -1;
}
ssize_t nr = read(fd, buf, len);
if (nr <= 0) {
LOG(ERROR) << "Fail to read /proc/self/cmdline";
return -1;
}
#elif defined(OS_MACOSX)
static pid_t pid = getpid();
std::ostringstream oss;
char cmdbuf[32];
snprintf(cmdbuf, sizeof(cmdbuf), "ps -p %ld -o command=", (long)pid);
if (butil::read_command_output(oss, cmdbuf) != 0) {
LOG(ERROR) << "Fail to read cmdline";
return -1;
}
const std::string& result = oss.str();
ssize_t nr = std::min(result.size(), len);
memcpy(buf, result.data(), nr);
#else
#error Not Implemented
#endif
if (with_args) {
if ((size_t)nr == len) {
LOG(ERROR) << "buf is not big enough";
return -1;
}
for (ssize_t i = 0; i < nr; ++i) {
if (buf[i] == '\0') {
buf[i] = '\n';
}
}
return nr;
} else {
for (ssize_t i = 0; i < nr; ++i) {
if (buf[i] == '\0') {
return i;
}
}
if ((size_t)nr == len) {
LOG(ERROR) << "buf is not big enough";
return -1;
}
return nr;
}
}
} // namespace butil
// Process-related Info
// Copyright (c) 2018 Baidu, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Author: Zhu,Jiashun (zhujiashun@baidu.com)
// Date: Wed Apr 11 14:35:56 CST 2018
#ifndef BUTIL_PROCESS_UTIL_H
#define BUTIL_PROCESS_UTIL_H
#include <sys/types.h>
namespace butil {
// Read command line of this program. If `with_args' is true, args are
// included and separated with spaces.
// Returns length of the command line on sucess, -1 otherwise.
// NOTE: `buf' does not end with zero.
ssize_t ReadCommandLine(char* buf, size_t len, bool with_args);
} // namespace butil
#endif // BUTIL_PROCESS_UTIL_H
......@@ -26,6 +26,7 @@
#include "butil/files/scoped_file.h"
#include "butil/files/dir_reader_posix.h"
#include "butil/file_util.h"
#include "butil/process_util.h" // ReadCommandLine
#include "bvar/passive_status.h"
namespace bvar {
......@@ -480,12 +481,16 @@ static std::string read_first_line(const char* filepath) {
return result;
}
struct ReadProcSelfCmdline {
struct ReadSelfCmdline {
std::string content;
ReadProcSelfCmdline() : content(read_first_line("/proc/self/cmdline")) {}
ReadSelfCmdline() {
char buf[1024];
const ssize_t nr = butil::ReadCommandLine(buf, sizeof(buf), true);
content.append(buf, nr);
}
};
static void get_cmdline(std::ostream& os, void*) {
os << butil::get_leaky_singleton<ReadProcSelfCmdline>()->content;
os << butil::get_leaky_singleton<ReadSelfCmdline>()->content;
}
struct ReadProcVersion {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment