GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 02-May-2008, 04:59
shoaibjameel123 shoaibjameel123 is offline
Junior Member
 
Join Date: May 2004
Location: India
Posts: 36
shoaibjameel123 is on a distinguished road

Program Automatic Restart after failure.


Hi all...
I have a socket program, which runs fine. It just receives values from another socket. What i want with this code is that if it crashes, it should automatically restart itself.
Can anyone throw some ideas on this?

The code is below:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <inttypes.h>
#include <netdb.h>
#include <ctype.h>
#include <netinet/in.h>
#include <errno.h>
#include <netinet/tcp.h>
#define _GNU_SOURCE
#define BDP 625


static void error_report ( const char *on_what )
{
	perror ( on_what );
	exit ( EXIT_FAILURE );
}

int main ( int argc , char **argv )
{
	int32_t tcp_socket = 0;//this will store the return value from the socket()
	char *srvr_addr = "[i]A.B.C.D[/i]";//change it according to the server IP.
	char *srvr_port = "[i]ABCD[/i]";//as you wish....
	struct sockaddr_in adr_srvr;
	struct sockaddr_in adr_clnt;
	int32_t accept_return = 0;
	int32_t listen_return = 0;
	int32_t recv_return = 0;
	int32_t send_return = 0;
	char *mesg_recv = NULL;
	char *substring = NULL;
	int32_t len_clnt = 0;
	int32_t bind_return = 0;
	uint32_t size_recv = 0;
	int32_t i = 0;
	int32_t j = 0;
	uint32_t size_substring = 0;
	uint32_t size_file_name = 0;
	char *file_name = NULL;
	uint32_t flag = 0;
	FILE *output_file;
	int32_t return_setsockopt = 0;
	int32_t sock_buf_size = 0;
	char *mesg = "this is a test mesg from linux";

		
	if ( argc > 2 )
	{
		srvr_addr = argv[1];
	}
	else
	{
		srvr_addr = "144.0.6.96";
	}
	if ( argc >= 3 )
	{
		srvr_port = argv[2];
	}
	
	tcp_socket = socket ( AF_INET , SOCK_STREAM , IPPROTO_TCP);
	if ( tcp_socket == -1 )
	{
		error_report ( "socket() malfunction" );
	}
	flag = 1;
	return_setsockopt = setsockopt ( tcp_socket , IPPROTO_TCP , TCP_NODELAY , ( char * )&flag , sizeof ( flag ) );
	if ( return_setsockopt == -1 )
	{
		printf ( "Couldn't setsockopt(TCP_NODELAY)\n");
		exit(EXIT_FAILURE);
	}
	sock_buf_size = BDP;
	return_setsockopt = setsockopt ( tcp_socket , SOL_SOCKET , SO_SNDBUF , ( char * )&sock_buf_size , sizeof ( sock_buf_size ) );
	if ( return_setsockopt == -1 )
	{
		error_report ( "setsockopt() malfunction" );
	}
	return_setsockopt = setsockopt ( tcp_socket , SOL_SOCKET , SO_RCVBUF , ( char * ) &sock_buf_size , sizeof ( sock_buf_size ) );
	if ( return_setsockopt == -1 )
	{
		error_report ( "setsockopt() error" );
	}

	flag = 0;
	memset ( &adr_srvr , 0 , sizeof ( adr_srvr ) );
	adr_srvr.sin_family = AF_INET;
	adr_srvr.sin_port = htons ( atoi ( srvr_port ) );
	adr_srvr.sin_addr.s_addr = inet_addr ( srvr_addr );
	bind_return = bind ( tcp_socket , ( struct sockaddr * ) &adr_srvr , sizeof ( adr_srvr ) );
	if ( bind_return == -1 )
	{
		error_report ( "bind() malfunction" );
	}
	listen_return = listen ( tcp_socket , 254 );
	if ( listen_return == -1 )
	{
		error_report ( "listen() malfunction " );
	}
	
	for ( ; ; )
	{
		output_file = fopen ( "output.dat" , "w" );
		
		size_recv = 500;
		mesg_recv = ( char * ) calloc ( size_recv , sizeof ( char ) );
	
		size_substring = 500;
		substring = ( char * ) calloc ( size_substring , sizeof ( char ) );
		
		size_file_name = 500;
		file_name = ( char * ) calloc ( size_file_name , sizeof ( char ) );
		len_clnt = sizeof ( adr_clnt );
		accept_return = accept ( tcp_socket , ( struct sockaddr * ) &adr_clnt , &len_clnt );
		if ( accept_return == -1 )
		{
			error_report ( "accept() malfunction" );
		}
		recv_return = recv ( accept_return , mesg_recv , 500 ,  0 );
		if ( recv_return == -1 )
		{
			error_report ( "recv() malfunction" );
		}
		i = 0;
		while ( mesg_recv[i] != '#' && mesg_recv[i] != '%' )
		{
			*(substring + j ) = mesg_recv[i];
			j++;
			i++;
			if ( mesg_recv[i] == '#' )
			{
				i++;
				fprintf ( output_file , "%s\n" , substring );
				memset ( substring , 0 , strlen( substring ) );;
				j = 0;
			}
		}
		for ( i = 0 ; i < j ; i++ )
		{
			fprintf ( output_file , "%c" , substring[i] );
		}
		fprintf ( output_file , "%c" , '\n' );
		j = 0;
		fclose ( output_file );
		i = 0;
		for ( i = 0 ; mesg_recv[i] != '\0' ; i++ )
		{
			if ( mesg_recv[i] == '%' )
			{
				i++;
				while ( mesg_recv[i] != '\0' )
				{
					*(file_name + j ) = mesg_recv[i];
					j++;
					i++;
					if ( mesg_recv[i] == '\0' )
					{
						flag = 0;
					}
				}
			}
		if ( flag == 1 )
		{
			break;
		}
	}
	i = 0;
	j = 0;
	write ( accept_return , mesg , strlen ( mesg  ) );
	free ( file_name );
	free ( substring );
	free ( mesg_recv );
	i = 0;
	j = 0;
	flag = 0;
	close ( accept_return );
	}
	return ( EXIT_SUCCESS );
}
Last edited by LuciWiz : 02-May-2008 at 14:23. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 02-May-2008, 05:51
mamntc02 mamntc02 is offline
Junior Member
 
Join Date: Mar 2008
Location: Barcelona - Catalonia
Posts: 53
mamntc02 has a spectacular aura about

Re: Program Automatic Restart after failure.


Hi,

A program may crash for different reasons, and you can control some of them. You also have to define which errors are recoverable.
I think we have two main reasons:
  • An unrecoverable error in your code: Out of bounds, file descriptor not initialized, unavailable memory, etc.... These are programming errors, so you must take care of them, and I think you do.
  • An OS signal. The OS (and your process, too) can send some signals to abort or to kill a process. You can handle some of them, using <signal.h> library. You can read about that here. For example to handle a termination Signal (SIGTERM) you can do:
    CPP / C++ / C Code:
    void signalHandler(int sig){
    	// signal sig captured
    	// save variables or states needed to reload process
    	// Reload process
    }
    
    int main ( int argc , char **argv ){
    	signal(SIGTERM, signalHandler);
    	...
    	
    	return 0;
    }
Here you can find the signals you can handle, and its default behavior, as well as, useful information about signals and processes.

Regards
__________________
Please, correct me if I'm wrong, and sorry for my english ;)
  #3  
Old 02-May-2008, 22:33
shoaibjameel123 shoaibjameel123 is offline
Junior Member
 
Join Date: May 2004
Location: India
Posts: 36
shoaibjameel123 is on a distinguished road

Re: Program Automatic Restart after failure.


HI,
Thanks for your reply. Lemme try it...if there's a problem, I'll get back...!!
  #4  
Old 05-May-2008, 06:01
shoaibjameel123 shoaibjameel123 is offline
Junior Member
 
Join Date: May 2004
Location: India
Posts: 36
shoaibjameel123 is on a distinguished road

Re: Program Automatic Restart after failure.


Ok...signal is one...
now i have another idea...
If my program crashes, my program should automatically send a mail to 'root' stating that the program has crashed. Is this possible?
I can write a shell script for that and invoke that script through system() command in the above C program.
  #5  
Old 05-May-2008, 08:41
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program Automatic Restart after failure.


Quote:
Originally Posted by shoaibjameel123
Ok...signal is one...
now i have another idea...
If my program crashes, my program should automatically send a mail to 'root' stating that the program has crashed. Is this possible?
I can write a shell script for that and invoke that script through system() command in the above C program.
I don't get it. Suppose there is a bug in your program that causes a crash under certain circumstances (for example a badly-formed message some other bug that allows the program to try to access invalid memory). Then, how the heck could the program know that it was going to crash?

Or, putting it another way, if you put something in the program that checks to make sure the message is properly formed (instead of just assuming that it's OK as your program seems to do), it wouldn't crash at that point, would it? See Footnote.

Assuming that there is a bug (or two) that can cause a crash, you could write a shell script that checks to see whether there is a process with a particular name, and it could do whatever you need in case no such process is running.

Suppose the process is named 'ThisIsASpecialSomething'. Then you could make a bash script to check periodically:

Code:
#!/bin/bash # # An "alarm" script that can verify whether there is a process # running for your favorite program. # # For test purposes set sleep to a small value # Actual script would probably run silently as # long as the process was alive (and not echo # the "all is well" message). # # while [ 1 ] do sleep 1 PIDOFZ=$(pidof ThisIsASpecialSomething) if [ x$PIDOFZ == x"" ] then echo "This is where you would log the event or whatever..." exit else # For debug purposes print the heartbeat echo "Process ID = $PIDOFZ, all is well." fi done


You would probably launch this "alarm" script in the background and keep it active for as long as you expected the other program to be running.

Regards,

Dave

Footnote:
I have been trying for many years to get computer architects to implement a "Branch On Bug" machine instruction, but I haven't been successful. Maybe some day...
  #6  
Old 06-May-2008, 04:10
shoaibjameel123 shoaibjameel123 is offline
Junior Member
 
Join Date: May 2004
Location: India
Posts: 36
shoaibjameel123 is on a distinguished road

Re: Program Automatic Restart after failure.


The shell script thing is a good idea, I must admit.
What I really meant from the above post is this:
Consider an example from my source code itself:

static void error_report ( const char *on_what )
{
perror ( on_what );
system ("mail root :error has occurred" );//I want to use Linux's mail program to mail root that a program has crashed. So, that the root can again restart it.
exit ( EXIT_FAILURE );
}

Is this possible within a C code?

else as you pointed out write a shell script that invokes after a certain intervals of time and checks whether the program is executing or not.
  #7  
Old 06-May-2008, 07:27
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program Automatic Restart after failure.


Quote:
Originally Posted by shoaibjameel123
What I really meant from the above post is this...
I guess it's a matter of terminology. If a program conducts a controlled shutdown, I wouldn't call it a crash.
Quote:
Originally Posted by shoaibjameel123
...my source code itself...
Is this possible within a C code?
Just about anything that you can do from a command line can be done with a system call inside a C program.

Since your code has this in it, I have to ask whether it works.

If it works, then why would you ask whether it is possible?

If it doesn't work then I have to ask what happens when you try it?

Try things like man sendmail, man mail, and, maybe man syslog.

Regards,

Dave
  #8  
Old 06-May-2008, 22:08
shoaibjameel123 shoaibjameel123 is offline
Junior Member
 
Join Date: May 2004
Location: India
Posts: 36
shoaibjameel123 is on a distinguished road

Re: Program Automatic Restart after failure.


OK..lemme try it..I get back ASAP
  #9  
Old 06-May-2008, 22:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,688
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Program Automatic Restart after failure.


Quote:
Originally Posted by shoaibjameel123
OK..lemme try it..I get back ASAP
Here's the way I would approach it:

1. Try from a command line. Make sure it does exactly what you want.

2. Put the command line into the system call.

Regards,

Dave
  #10  
Old 06-May-2008, 22:56
shoaibjameel123 shoaibjameel123 is offline
Junior Member
 
Join Date: May 2004
Location: India
Posts: 36
shoaibjameel123 is on a distinguished road

Re: Program Automatic Restart after failure.


Ya,
thanks this is what I am trying now!
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
BOOKEEPING program, HELP!! yabud C Programming Language 10 17-Nov-2006 03:48
Help with a complex program lordfuoco C++ Forum 5 24-Jun-2006 06:03
How to read particular memory location ? realnapster C Programming Language 10 10-May-2006 09:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:36.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.