LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-09-2005, 04:13 PM   #1
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Rep: Reputation: 30
Help with fork, wait, kill in C


Hi all,

Edit for brevity:
My current code:
Code:
for( x = 0; x < 5; x++ ) {

    libnet_write( context ); // sends ARP who-has
    // The following line blocks until a packet is captured, then passes it to
    // pcap_callback_fct, which dissembles and puts the source MAC in
    // temp_mac, a global char*

    pcap_dispatch( pkt_descriptor, 0, ( void * )pcap_callback_fct, NULL );

    memcpy( target_mac[ x ], temp_mac, MAX_SIZE_ETHADDR );
}
This will not work, as Linux does not implement the read timeout in pcap_open_live(), so if I am sending packets to a non-replying address, pcap_dispatch blocks indefinitely. I need to:

1. Fork a child that will execute pcap_dispatch and return when a packet is captured
2. Have the parent wait for pehaps a second for the child to return and if it does not, then kill it
3. Implement temp_mac as shared memory so that when the child's pcap_dispatch call in turn calls pcap_callback_fct, that last will be able to write to memory space that the parent can read.

There, I think I have removed excess varbiage. TIA!

Last edited by zaichik; 09-09-2005 at 05:59 PM.
 
Old 09-09-2005, 08:34 PM   #2
zaichik
Member
 
Registered: May 2004
Location: Iowa USA
Distribution: CentOS
Posts: 419

Original Poster
Rep: Reputation: 30
I'm trying this:
Code:
pid_t child_pid;
char *shared_mem; // This will be shared for getting the MAC into target_mac[]
int shmseg_id;
shmseg_id = shmget( IPC_PRIVATE, MAX_SIZE_ETHADDR, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR );
for( x = 0; x < 5; x++ ) {
     libnet_write( context );
     child_pid = fork();
     if( child_pid == 0 ) {
          // attach to shared memory
          shared_mem = ( char * ) shmat( shmseg_id, 0, 0 );
          pcap_dispatch( pkt_descriptor, 0, ( void * )pcap_callback_fct, NULL );
          memcpy( shared_mem, temp_mac, MAX_SIZE_ETHADDR );
          exit( 0 );
     }
     else {
          sleep( 2 );
          shared_mem = ( char * ) shmat( shmseg_id, 0, 0 );
          if( strlen( shared_mem ) == 0 ) {
               // the child has blocked, kill it
               kill( child_pid, SIGTERM );
          }
     }
     memcpy( target_mac[ x ], shared_mem, MAX_SIZE_ETHADDR );
}
// detach shared memory and deallocate
shmdt( shared_mem );
shmctl( shmseg_id, IPC_RMID, 0 );
It compiles, and seems to run, but I am getting a new error now that pcap_dispatch is not blocking indefinitely. New thread on the way...

Last edited by zaichik; 09-09-2005 at 08:39 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to kill a Child and all its subsequent child process in C shayer009 Programming 3 12-04-2007 12:40 AM
Alternative way of subscribing to a thread DoubleOTeC LQ Suggestions & Feedback 3 03-04-2005 12:03 AM
Killing a child process from another child marri Programming 6 10-01-2004 07:08 PM
Main thread sending notification to child thread rajesh_b Programming 1 09-22-2004 09:15 AM
configure qt thread issue (just compiled qt w/ -thread option) cleff Linux - Software 8 05-07-2004 11:11 PM


All times are GMT -5. The time now is 05:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration