Reputation: 669
In the Linux platform, if I write in console ps -p "pid" -o command
I get full line with all argument which passed in terminal when I run the program. Output in console something like this: COMMAND gedit /home/sasha/Work/unloker/main.cpp (Ubuntu)
. Now I'm writing the program which main purpose to get complete input command line of process. My C++ code is:
snprintf(path_cmdline, sizeof(path_cmdline), "/proc/%d/cmdline", pid);
fd_cmdline = open(path_cmdline, O_RDONLY);
if (fd_cmdline < 0) {
} else {
char process_name[PATH_MAX];
if (read(fd_cmdline, process_name, PATH_MAX) < 0) {
} else {
pid_info pid_t;
pid_t.pid=pid;
strcpy(pid_t.command_line,process_name);
strcpy(pid_t.process_name,basename(process_name));
std::cout << pid_t << std::endl;
}
}
and output of my program somthing like this: 10753 gedit gedit
, but how can I get full command line as when the output of the ps -p "pid" -o command
?
Where in the /proc/%d/
kept full command line of the running program? In Solaris system I know exist command pargs
which do what I want, may be how now where I may found sources of this command?
Upvotes: 2
Views: 5361
Reputation: 76266
The arguments in /proc/pid/cmdline is a list of strings, separated with 0 bytes. Therefore treating it as C string, which is terminated by first 0 byte, will only give you the process name. Replace all 0 bytes up to the size returned by read
with spaces and try again.
Here is proof:
$ hexdump -bc < /proc/32096/cmdline
0000000 142 141 163 150 000 055 162 143 146 151 154 145 000 056 142 141
0000000 b a s h \0 - r c f i l e \0 . b a
0000010 163 150 162 143 000
0000010 s h r c \0
0000015
Upvotes: 9
Reputation: 1063
in linux, the running process info is stored in the /proc/ folder -
/proc/{PROCESS_ID}/cmdline to be exact - for example here is chrome-
$cat /proc/3193/cmdline
/opt/google/chrome/chrome --type=renderer --lang=en-US --force-fieldtrials=ConnCountImpact/conn_count_6/ConnnectBackupJobs/ConnectBackupJobsEnabled/DnsImpact/default_enabled_prefetch/GlobalSdch/global_enable_sdch/IdleSktToImpact/idle_timeout_10/OmniboxDisallowInlineHQP/Standard/OmniboxSearchSuggest/6/Prerender/ContentPrefetchPrerender1/ProxyConnectionImpact/proxy_connections_32/SBInterstitial/V2/SpdyImpact/spdy3/UMA-Dynamic-Binary-Uniformity-Trial/default/UMA-Uniformity-Trial-1-Percent/group_31/UMA-Uniformity-Trial-10-Percent/group_05/UMA-Uniformity-Trial-20-Percent/default/UMA-Uniformity-Trial-5-Percent/group_13/UMA-Uniformity-Trial-50-Percent/group_01/WarmSocketImpact/warmest_socket/ --enable-crash-reporter=81A0480CAE65B69A53CE6E791EAA05A5,Ubuntu 10.10 --disable-client-side-phishing-detection --renderer-print-preview --disable-accelerated-2d-canvas --channel=2980.13.513987986
Upvotes: 1