Greetings;

I am trying to create a background application (starts with system startup, and keeps running in the background ) under linux. the only solution i found is to make it as daemon.
searching the internet about how to create a daemon , I built a small app around my findings.

Qt Code:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include "globaldefs.h"
  7.  
  8. void log_message(char *filename,char *message)
  9. {
  10. FILE *logfile;
  11. logfile=fopen(filename,"a");
  12. if(!logfile) return;
  13. fprintf(logfile,"%s\n",message);
  14. fclose(logfile);
  15. }
  16.  
  17.  
  18. /**
  19.   a signal handler for the Linux signals sent to daemon process,
  20.   for more signals, refer to http://www.comptechdoc.org/os/linux/programming/linux_pgsignals.html
  21.   */
  22. void signal_handler(int sig)
  23. {
  24. switch(sig) {
  25. case SIGHUP:
  26. log_message(LOG_FILE,"hangup signal catched");
  27. break;
  28. case SIGTERM:
  29. log_message(LOG_FILE,"terminate signal catched");
  30. break;
  31. }
  32. }
  33.  
  34. /**
  35.   create background process out of the application, source code taken from: http://www.enderunix.org/docs/eng/daemon.php
  36.   with some minor modifications
  37.   */
  38. void init_daemon()
  39. {
  40. int i,lfp;
  41. char str[10];
  42. if(getppid()==1)
  43. return; /* already a daemon */
  44. i=fork();
  45. if (i<0)
  46. exit(1); /* fork error */
  47. if (i>0)
  48. exit(0); /* parent exits */
  49.  
  50. /* child (daemon) continues */
  51. setsid(); /* obtain a new process group */
  52.  
  53. for (i=getdtablesize();i>=0;--i)
  54. close(i); /* close all descriptors */
  55. i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */
  56.  
  57. umask(027); /* set newly created file permissions */
  58.  
  59. chdir(RUNNING_DIR); /* change running directory */
  60. lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
  61. if (lfp<0)
  62. exit(1); /* can not open */
  63. if (lockf(lfp,F_TLOCK,0)<0)
  64. exit(0); /* can not lock */
  65. /* first instance continues */
  66. sprintf(str,"%d\n",getpid());
  67. write(lfp,str,strlen(str)); /* record pid to lockfile */
  68. signal(SIGCHLD,SIG_IGN); /* ignore child */
  69. signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
  70. signal(SIGTTOU,SIG_IGN);
  71. signal(SIGTTIN,SIG_IGN);
  72. signal(SIGHUP,signal_handler); /* catch hangup signal */
  73. signal(SIGTERM,signal_handler); /* catch kill signal */
  74. }
  75. int main(int argc, char *argv[])
  76. {
  77. // first, create the daemon
  78. init_daemon();
  79. QCoreApplication a(argc, argv);
  80.  
  81. return a.exec();
  82. }
To copy to clipboard, switch view to plain text mode 
the code doesn't handle signals (for example, a termination signal). So, when I run it from the command line, and try to kill it, it refuses to get killed!!! the only way to kill the process is to restart my machine!!
I really suspect that creating a QCoreApplication instance in the main method forks a new process other than the one i forked in the init_daemom() method.
When I try to debug the application. it fails tp fork a new process thus exit() is issued and the debugging session terminates.

I am using Qt 4.5 under ubuntu 9.04
What am I missing? can there be anything done to make my Qt application run as a daemon process ?? Am i using a wrong way to do so?

thanks in advance;