MCPcopy Create free account
hub / github.com/apache/brpc / ReadCommandLine

Function ReadCommandLine

src/butil/process_util.cc:38–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

36namespace butil {
37
38ssize_t ReadCommandLine(char* buf, size_t len, bool with_args) {
39#if defined(OS_LINUX)
40 butil::fd_guard fd(open("/proc/self/cmdline", O_RDONLY));
41 if (fd < 0) {
42 LOG(ERROR) << "Fail to open /proc/self/cmdline";
43 return -1;
44 }
45 ssize_t nr = read(fd, buf, len);
46 if (nr <= 0) {
47 LOG(ERROR) << "Fail to read /proc/self/cmdline";
48 return -1;
49 }
50#elif defined(OS_MACOSX)
51 static pid_t pid = getpid();
52 std::ostringstream oss;
53 char cmdbuf[32];
54 snprintf(cmdbuf, sizeof(cmdbuf), "ps -p %ld -o command=", (long)pid);
55 if (butil::read_command_output(oss, cmdbuf) != 0) {
56 LOG(ERROR) << "Fail to read cmdline";
57 return -1;
58 }
59 const std::string& result = oss.str();
60 ssize_t nr = std::min(result.size(), len);
61 memcpy(buf, result.data(), nr);
62#else
63 #error Not Implemented
64#endif
65
66 if (with_args) {
67 if ((size_t)nr == len) {
68 return len;
69 }
70 for (ssize_t i = 0; i < nr; ++i) {
71 if (buf[i] == '\0') {
72 buf[i] = '\n';
73 }
74 }
75 return nr;
76 } else {
77 for (ssize_t i = 0; i < nr; ++i) {
78 // The command in macos is separated with space and ended with '\n'
79 if (buf[i] == '\0' || buf[i] == '\n' || buf[i] == ' ') {
80 return i;
81 }
82 }
83 if ((size_t)nr == len) {
84 LOG(ERROR) << "buf is not big enough";
85 return -1;
86 }
87 return nr;
88 }
89}
90
91ssize_t GetProcessAbsolutePath(char *buf, size_t len) {
92#if defined(OS_LINUX)

Callers 3

cmdlineMethod · 0.85
CreateProgramNameFunction · 0.85
ReadSelfCmdlineMethod · 0.85

Calls 4

snprintfFunction · 0.85
read_command_outputFunction · 0.85
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected