【列表5.16】读一个进程的cmdline 文件。
{
ProcReadFile reads the contents of the specified file
for the passed process id, and returns the contents in the
supplied buffer. On entry, length contains the size of
the buffer. On exit, length contains the number of bytes
read. The function returns 0 on success, -1 on error.
}
function ProcReadFile (pid: __pid_t; const path: String;
var buffer; var length: Integer): integer;
var
sFileName : String;
fd : Integer;
begin
Result := -1;
try
sFileName := Format ('/proc/%d/%s,, [pid, path]);
fd := Libc.open (PChar (sFilename), 0_RDONLY);
if fd -1 then
begin
try
Result := Libc. __read (fd, Buffer, length);
finally
Libc. __close(fd);
end;
end;
except
// swallow exception...
end;
end;
{
ProcReadFileString returns the entire contents of the specified
file in a string.
}
function ProcReadFileString (pid: _pid_t;
const path: String): String;
var
buffer : array [0..16000] of char;
length : Integer;
begin
length := sizeof (buffer)-1;
length := ProcReadFile (pid, path, buffer, length);
if length = -1 then
Result := ',
else
begin
buffer[length] := #0;
Result := buffer;
end;
end;
// ProcGetCmdline - return the command line that. launched the process.
function ProcGetCmdline (pid: __pid_t) : String;
begin
Result := ProcReadFileString (pid, 'cmdline');
end;