[fpc-pascal] check if process ID is running under BSD
Mattias Gaertner
nc-gaertnma at netcologne.de
Thu Jun 9 09:03:23 CEST 2011
On Wed, 8 Jun 2011 12:58:55 +0200 (CEST)
marcov at stack.nl (Marco van de Voort) wrote:
> In our previous episode, Mattias Gaertner said:
> > > 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}'".
> >
> > 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?
>
> 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
More information about the fpc-pascal
mailing list