Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Controlling child process

Posted on 1998-10-26
9
Medium Priority
?
191 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
[X]
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
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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 400 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

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!

Question has a verified solution.

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

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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

730 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