Solved

Controlling child process

Posted on 1998-10-26
9
169 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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 (http://dilbert.com/strips/comic/2007-08…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

11 Experts available now in Live!

Get 1:1 Help Now