2009/12/31 ibrahim <ibrahim....@asgatech.com>:

> 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 !!

kill -9 pid doesn't work?
One thing that I recall is that when a child process hangs on exit,
the parent process doesn't handle the CHILD signal. But since you exit
directly, I would guess that is not related.
Oh, you don't have a 'exit()' in your signal handler. Man page says
though that once the signal occurs, the signal is reset to DFL, so the
second 'kill -TERM pid' should work.

> 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.

Have you tried to swap the first two code lines from your main? If
QCoreApp sets up  a signal handler, then it replaces yours.

Koos
_______________________________________________
maemo-developers mailing list
maemo-developers@maemo.org
https://lists.maemo.org/mailman/listinfo/maemo-developers

Reply via email to