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?
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
readwith spaces and try again.Here is proof: