[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

Controlling child process

I'm writing some kind of dialin server under FreeBSD. After authentification process it starts pppd with system()call.
I'd like to terminate pppd after certain timeout (for example, if client exceeded allowed time). What should I use in this case?
  • 5
  • 4
1 Solution
Since you're using system, you won't get control back until pppd completes, and then you'll get a return value.  Unfortunately, pppd forks itself and returns immediately.  However, you can do a system call on

pppd -k

to have pppd kill itself off.

ddaAuthor Commented:
1. May be I shouln't use system(), just tell me what else can I use to make it working. :)
2. pppd doesn't understand '-k' option:
> uname -srm
FreeBSD 2.2.7-RELEASE i386
> pppd -k
pppd: unrecognized option '-k'
pppd version 2.3 patch level 5
Usage: pppd [ options ], where options are:
        <device>        Communicate over the named device
        <speed>         Set the baud rate to <speed>
        <loc>:<rem>     Set the local and/or remote interface IP
                        addresses.  Either one may be omitted.
        asyncmap <n>    Set the desired async map to hex <n>
        auth            Require authentication from peer
        connect <p>     Invoke shell command <p> to set up the serial line
        crtscts         Use hardware RTS/CTS flow control
        defaultroute    Add default route through interface
        file <f>        Take options from file <f>
        modem           Use modem control lines
        mru <n>         Set MRU value to <n> for negotiation
See pppd(8) for more options.

3. Any other ideas are welcome.
Hmmm, sorry.  often pppd is run from a shell script called ppp which does accept the -k option.  

Alternatively, if you check the man page, I'm pretty sure that pppd creates a lock file that contains it's pid.  If the name of that file is /var/tmp/ppp.lock (it isn't, but I don't remember what it is right now), then you can do:

open(PPPPID, '</var/tmp/ppp.lock') || die "Unable to open /var/tmp/ppp.lock: $!";
$ppppid = <PPPPID>;
kill 1 $ppppid;

You may need to do

kill 9 $ppppid

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

ddaAuthor Commented:
Ok, there are some kind of files at /var/run (FreeBSD) that contain PIDs, but how can I determine the right one (for my child)? Files are named pppn.pid, where n is 0 - ??? in order of appearance of pppd's. I have to know the propoer PID exactly.
Is there any function that return PID?
Last question first:

$$ will return the PID of the current process.

Now, the meat. I know virtuallly nothing about FreeBSD, and I'll assume that it doesn't have the proc system.  On the other hand, /var/run may be the same thing.  Do the /var/run files contain anything else?

Secondly, was I right in assuming that pppd immediately forks itself out?

In any case, rather than calling the daemon via exec, try calling it as

($ppid = open(FIL, "| pppd")) || die "Failed to start daemon: $!";

Now, $ppid is the pid of pppd.  If pppd does not fork itself out, then $ppid is the actual pid that you can kill.  If it does fork itself, then $ppid is the _parent_ pid. In that case, you can grab the output of ps, look for a process where the parent pid matches $ppid, and you should be done
ddaAuthor Commented:
FreeBSD has /proc system, at least it has directory named '/proc' with files named as PIDs. /var/run contains several files named differently, this files belong to various application, I think. Some of them may contain PIDs.
How can I find out that pppd fork itself? When I uissue "system" call the flow of my program is topped until pppd is no terminated (or any signal is catched). In "($ppid = open(FIL, "| pppd")) || die "Failed to start daemon: $!"; ", what is the FIL?
ddaAuthor Commented:
b2pi, I'd like to grade your answer. Sorry for delay.
What did you end up doing?
ddaAuthor Commented:
Finally I used fork() and waitpid(). All this stuff is rather new for me, so you did helped greatly. Thanks.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now