Hi all,
I need to check under BSD if a PID is running and what name it has. Is there already a function for that?
Mattias

Mailing lists / fpc-pascal / 2011-06
7 messages · 3 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
Hi all,
I need to check under BSD if a PID is running and what name it has. Is there already a function for that?
Mattias
Hi Mattias,
I don't know if it exist a FPC function for that.
i'll do an external commad like " ps -ef | grep bash | grep -v grep | awk '{print $8}'".
I 'm assuming the output is:
user1 2793 2789 0 22:52 pts/0 00:00:00 bash
and encapsulate the command is one of the following system call.
1) TProcess (http://wiki.freepascal.org/Executing_External_Programs)
2) SysUtils.ExecuteProcess
(http://wiki.freepascal.org/Executing_External_Programs)
3) fpsystem
(http://wiki.freepascal.org/Console_Mode_Pascal#Execute_shell_command)
4) POpen (http://www.freepascal.org/docs-html/rtl/unix/popen.html)
5) Shell (http://www.freepascal.org/docs-html/rtl/unix/shell.html)
6) FpExecve (http://www.freepascal.org/docs-html/rtl/baseunix/fpexecve.html)
Choice 5 is quick to implement and you can move to others if you want later.
Hope my update help you, if the way i'll do it. I don't think there are some major difference between FPC implementation on Linux and BSD.
Regards,
Phoebus
Thanks, I knew that.
I prefer a more direct way - for example via a sysctl.
I need to check under BSD if a PID is running and what name it has. Is there already a function for that?
Mattias
I need to check under BSD if a PID is running and what name it has. Is there already a…
In our previous episode, Mattias Gaertner said:
I need to check under BSD if a PID is running and what name it has. Is there already a function for that?
No. Note that /proc can be turned off on BSD, and often is so on servers, so relying on is not that safe anyway.
On Mon, 6 Jun 2011 22:02:39 +0100 (BST) Thanks, I knew that. I prefer a more direct way -…
In our previous episode, Mattias Gaertner said:
> '{print $8}'". Thanks, I knew that. I prefer a more direct way - for example via a sysctl.
Not yet, but I'll have a look.
On Mon, 6 Jun 2011 22:02:39 +0100 (BST) Thanks, I knew that. I prefer a more direct way -…
In our previous episode, Mattias Gaertner said:
2.5.1 since r17692 try this:
uses freebsd,baseunix,sysutils;
var pid : pid_t;
s: ansistring;
begin
pid:=strtointdef(paramstr(1),-1);
writeln('pid (-1 for current):',pid);
// loads name of process + args with #0's inbetween.
if kernproc_getargs(pid,s)-1 then // does not work with -1
writeln('arguments:',s)
else
writeln('error fetching arguments:',fpgeterrno);
// loads full path of running binary
if kernproc_getpath(pid,s)-1 then // -1 means own process
writeln('arguments:',s)
else
writeln('error fetching path:',fpgeterrno);
end.See the implementation of the above procedures in freebsd.pas for more info, note that the kern_proc_* values were also updated in sysctl.
Thanks a lot.
This solution is only for FreeBSD.
What about the other BSD flavors?
What about darwin?
Mattias