Solved

Controlling child process

Posted on 1998-10-26
9
172 Views
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?
0
Comment
Question by:dda
  • 5
  • 4
9 Comments
 
LVL 5

Expert Comment

by:b2pi
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.

0
 
LVL 4

Author Comment

by:dda
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.
0
 
LVL 5

Expert Comment

by:b2pi
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>;
close(PPPPID);
chomp($ppppid);
kill 1 $ppppid;


You may need to do

kill 9 $ppppid

instead.
0
 
LVL 4

Author Comment

by:dda
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 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?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:b2pi
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
0
 
LVL 4

Author Comment

by:dda
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?
0
 
LVL 4

Author Comment

by:dda
ID: 1205716
b2pi, I'd like to grade your answer. Sorry for delay.
0
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1205717
What did you end up doing?
0
 
LVL 4

Author Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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.…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now