Controlling child process

Posted on 1998-10-26
Last Modified: 2010-03-04
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?
Question by:dda
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4

Expert Comment

ID: 1205710
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.


Author Comment

ID: 1205711
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.

Expert Comment

ID: 1205712
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1205713
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, 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?

Expert Comment

ID: 1205714
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

Author Comment

ID: 1205715
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?

Author Comment

ID: 1205716
b2pi, I'd like to grade your answer. Sorry for delay.

Accepted Solution

b2pi earned 100 total points
ID: 1205717
What did you end up doing?

Author Comment

ID: 1205718
Finally I used fork() and waitpid(). All this stuff is rather new for me, so you did helped greatly. Thanks.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl - Mawk 2 108
Filtering a file to table 9 104
Need to install DBD::MySQL on a MAC without a local MySQL server for Perl 4 77
read an xml file in perl 2 84
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question