列表5.16.txt

来自「klinux书籍的配套光盘。可以学习学习。」· 文本 代码 · 共 56 行

TXT
56
字号
【列表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;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?