LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-06-2005, 03:07 AM   #1
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Rep: Reputation: 15
Daemon programming.... Help


Hi all,
I tryed to write a daemon program and executaed it. with this no problem till now. But when i created two copies of this program and tryed to execute the second executable while first one is running i fail to execute the second copy.... Can any body tell me why this is happening to me... what is the possible cause of not running the seconde copy of the created daemon...? Is there any solution to run two copies of the daemon programe....

If any body knows plz help me...


With regard
Rajesh.
 
Old 04-06-2005, 03:36 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
can you please post some code. How can we guess what went wrong?

What kind of deamon did you create. If you created a sever binding a port, then you can not use the same port. Are you checking the return values in your functions?

Also, you already started a thread about this 2 days ago. You could simply reply to your exesting thread. No need to create a new thread.
 
Old 04-06-2005, 07:31 AM   #3
rajsun
Member
 
Registered: Mar 2005
Posts: 61

Original Poster
Rep: Reputation: 15
I m raelly sorry that .... i forgot to send the source code.
Ok any way i'm sending the source code.....
I got the fault .... why it was failing to run.....
But still there is some problem....
That have mentioned after the program....
==========================================

#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>

#define RUNNING_DIR "/tmp"
--------->>>>> Here in the previous line "/tmp" was the problem that i got because i was running in the same directory both the program therfore it was raising conflict while creating "exampled.lock" and i was not checking any error messages.
--------->>>>>>

#define LOCK_FILE "exampled.lock"
#define LOG_FILE "exampled.log"

void log_message(filename,message)
char *filename;
char *message;
{
FILE *logfile;
logfile=fopen(filename,"a");
if(!logfile) return;
fprintf(logfile,"%s\n",message);
fclose(logfile);
}

void signal_handler(sig)
int sig;
{
switch(sig) {
case SIGHUP:
log_message(LOG_FILE,"hangup signal catched");
break;
case SIGTERM:
log_message(LOG_FILE,"terminate signal catched");
exit(0);
break;
}
}

void daemonize()
{
int i,lfp;
char str[10];
if(getppid()==1) return; /* already a daemon */
i=fork();
if (i<0) exit(1); /* fork error */
if (i>0) exit(0); /* parent exits */
/* child (daemon) continues */
setsid(); /* obtain a new process group */
for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */
i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */
umask(027); /* set newly created file permissions */
chdir(RUNNING_DIR); /* change running directory */
lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
if (lfp<0) exit(1); /* can not open */
if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
/* first instance continues */
sprintf(str,"%d\n",getpid());
write(lfp,str,strlen(str)); /* record pid to lockfile */
signal(SIGCHLD,SIG_IGN); /* ignore child */
signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
signal(SIGTTOU,SIG_IGN);
signal(SIGTTIN,SIG_IGN);
signal(SIGHUP,signal_handler); /* catch hangup signal */
signal(SIGTERM,signal_handler); /* catch kill signal */
}

main()
{
daemonize();
while(1) sleep(1); /* run */
}

/* EOF */
==========================================
Problem is here...
---------------------------------------------------------------------------
Consider the following code....

for (i=getdtablesize();i>=0;--i) close(i);
/* close all descriptors */
i=open("/dev/null",O_RDWR);
dup(i);
dup(i);
/* handle standart I/O */
---------------------------------------------------------------------------
if i'm incorporating thi above mentioned code to my daemon program. Then i'm failing to open any other file like "exampled.lock" and "exampled.log". I dont understand whay this is happening ...... becasue after running daemon program i used "ps -ax" command here i got the daemon as running.... that means programe is not terminating abnormally... and at the same time the signal handler is also failing......

Plz help me how to come out of this proble.....

With regard
Rajesh.
 
Old 01-30-2006, 10:27 PM   #4
swatic
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Rep: Reputation: 0
hello.. plz can u tell me steps of how to write a daemon program and how to run it.
 
Old 01-31-2006, 12:45 AM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
Code:
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>

#define RUNNING_DIR "/tmp"
--------->>>>> Here in the previous line "/tmp" was the problem that i got because i was running in the same directory both the program therfore it was raising conflict while creating "exampled.lock" and i was not checking any error messages.
--------->>>>>>

#define LOCK_FILE "exampled.lock"
#define LOG_FILE "exampled.log"

void log_message(filename,message)
char *filename;
char *message;
{
   FILE *logfile;
   logfile=fopen(filename,"a");
   if(!logfile) return;
   fprintf(logfile,"%s\n",message);
   fclose(logfile);
}

void signal_handler(sig)
int sig;
{
   switch(sig) {
      case SIGHUP:
         log_message(LOG_FILE,"hangup signal catched");
      break;

      case SIGTERM:
         log_message(LOG_FILE,"terminate signal catched");
         exit(0);
      break;
   }
}

void daemonize()
{
   int i,lfp;
   char str[10];
   if(getppid()==1)
      return; /* already a daemon */
   i=fork();
   if (i<0)
      exit(1); /* fork error */
   if (i>0)
      exit(0); /* parent exits */

   /* child (daemon) continues */
   setsid(); /* obtain a new process group */
   for (i=getdtablesize();i>=0;--i)
      close(i); /* close all descriptors */
   i=open("/dev/null",O_RDWR); 
   dup(i); 
   dup(i); /* handle standart I/O */
   umask(027); /* set newly created file permissions */
   chdir(RUNNING_DIR); /* change running directory */
   lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
   if (lfp<0)
      exit(1); /* can not open */
   if (lockf(lfp,F_TLOCK,0)<0)
      exit(0); /* can not lock */
   /* first instance continues */
   sprintf(str,"%d\n",getpid());
   write(lfp,str,strlen(str)); /* record pid to lockfile */
   signal(SIGCHLD,SIG_IGN); /* ignore child */
   signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
   signal(SIGTTOU,SIG_IGN);
   signal(SIGTTIN,SIG_IGN);
   signal(SIGHUP,signal_handler); /* catch hangup signal */
   signal(SIGTERM,signal_handler); /* catch kill signal */
}

main()
{
   daemonize();
   while(1)
      sleep(1); /* run */
}

/* EOF */
==========================================
Problem is here...
---------------------------------------------------------------------------
Consider the following code....

   for (i=getdtablesize();i>=0;--i)
      close(i);
   /* close all descriptors */
   i=open("/dev/null",O_RDWR);
   dup(i);
   dup(i);
   /* handle standart I/O */
You will not get any answers, unless you code is readable. Use the code tags

Last edited by perfect_circle; 01-31-2006 at 12:49 AM.
 
Old 01-31-2006, 03:21 AM   #6
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 52
from a really quick look I don't see something wrong with the above code.

Quote:
Originally Posted by swatic
hello.. plz can u tell me steps of how to write a daemon program and how to run it.
What steps?
just add
Code:
if(fork())
   exit();
and the program will be a daemon.

Last edited by perfect_circle; 01-31-2006 at 03:22 AM.
 
Old 01-31-2006, 06:01 PM   #7
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
Use daemon(3)

Example implementation:
http://www.gobsd.com/code/freebsd/lib/libc/gen/daemon.c

There's a comment there about parent sending SIGHUP to child.

I suggest you check return values after *every* system/function call and add perror() to help you with debugging.
 
Old 02-01-2006, 11:23 PM   #8
swatic
LQ Newbie
 
Registered: Jan 2006
Posts: 2

Rep: Reputation: 0
thankx

thankx a lot i will try it!!!1
 
  


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
which programming language is used to do tcp/ip programming?? gajaykrishnan Linux - Networking 9 12-21-2012 05:16 AM
Daemon programming..... rajsun Programming 11 07-01-2005 06:12 AM
Difference between Top Down programming & Bottom up programming minil Programming 1 06-17-2005 02:42 AM
Daemon Server Programming.... HELP rajsun Programming 2 04-05-2005 03:28 AM
What is a daemon? nutshell Linux - General 3 02-26-2002 06:25 AM


All times are GMT -5. The time now is 07:14 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