Greetings;

I am trying to create a background application (starts with system 
startup, and keeps running in the background ) . the only solution i 
found is to make it as daemon.
searching the internet about how to create a daemon 
<http://www.enderunix.org/docs/eng/daemon.php> , I built a small app 
around my findings.

Qt Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
      

        

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include "globaldefs.h"
 
void log_message(char *filename,char *message)
{
    FILE *logfile;
    logfile=fopen(filename,"a");
    if(!logfile) return;
    fprintf(logfile,"%s\n",message);
    fclose(logfile);
}
 
 
/**
  a signal handler for the Linux signals sent to daemon process,
  for more signals, refer to 
http://www.comptechdoc.org/os/linux/programming/linux_pgsignals.html
  */
void signal_handler(int sig)
{
    switch(sig) {
    case SIGHUP:
        log_message(LOG_FILE,"hangup signal catched");
        break;
    case SIGTERM:
        log_message(LOG_FILE,"terminate signal catched");
        break;
    }
}
 
/**
  create background process out of the application, source code taken from: 
http://www.enderunix.org/docs/eng/daemon.php
  with some minor modifications
  */
void init_daemon()
{
    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 */
}
int main(int argc, char *argv[])
{
    // first, create the daemon
    init_daemon();
    QCoreApplication <http://doc.trolltech.com/latest/QCoreApplication.html> 
a(argc, argv);
 
    return a.exec();
}

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 the phone !!
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. But i can't find any clue . On the other hand, i 
can't init my qt app without creating a QCoreApplication instance -says 
qt documentation.
When I try to debug the application. it fails to fork a new process thus 
exit() is issued and the debugging session terminates.

I am using Qt 4.5 and the  phone's libqt version is 4.5.3
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;
_______________________________________________
maemo-developers mailing list
maemo-developers@maemo.org
https://lists.maemo.org/mailman/listinfo/maemo-developers

Reply via email to