2009-04-08 11:40:39     daemon on blackfin

Document created by Aaronwu Employee on Aug 14, 2013
Version 1Show Document
  • View in full screen mode

2009-04-08 11:40:39     daemon on blackfin

Tihomir Culjaga (CROATIA)

Message: 72423   

 

Hello,

 

 

 

i've got an IP04 and i've build some applications. The application consists of two binaries. The 1st binary (cshop) calls the 2dn (cshoptimer) using vfork and execl() within. I can start it the shell logged in as root but not using a startup script during boot time.

 

 

 

The usage is: ./cshop <argument>

 

 

 

Here is a sample code:

 

 

int main(int argc, char* argv[])

{

        printf("\n\ncshop ver. 0.1\n");

 

 

        if (argc == 2)

        {

                pid_t pid = vfork();

                if (pid == 0)

                {

                        execl ("./cshop", "./cshop", "daemon", argv[1], 0);

                        _exit(0);

                }

                else

                {

                        //_exit(0);

                        return 0;

                }

        }

 

 

        signal(SIGTERM,         KillHandler);

        signal(SIGINT,          KillHandler);

 

        dbPath = malloc(128 * sizeof(char));

        memset(dbPath, 0, 128);

        strcpy(dbPath, argv[2]);

 

 

        g_pid = vfork();

        if (g_pid == 0)

        {

                execl ("./cshoptimer", "./cshoptimer", dbPath, 0);

                _exit(0);

        }

 

        EVENT_DATA* pEventData;

        int retcode;

 

        retcode = Initialize();

        if (retcode == CSHOP_ERROR)

                return 0;

 

 

        while (1)

        {

                Ping(g_sockfd);

                printf("\nreceiving event ...");

                fflush(stdout);

                pEventData = ReceiveEvent();

 

                if (pEventData == NULL)

                        continue;

 

                printf("\nhandlinging event ...");

                fflush(stdout);

                HandleEvent(pEventData);

 

                printf("\nfrreing event ...");

                fflush(stdout);

                FreeEvent(pEventData);

        }

 

        return 0;

}

 

 

 

 

When the box reboots, all that remains is the child process runing (cshoptimer) the parent (cshop) dies.

 

 

 

here is my startup script:

 

 

 

BusyBox v1.4.1 (2009-01-06 18:44:00 CET) Built-in shell (msh)

Enter 'help' for a list of built-in commands.

 

root:~>

root:~>

root:~>

root:~> cst /etc/init.d/cshop

cst: not found

root:~> cat /etc/init.d/cshop

#!/bin/sh

# Start up file for cshop

# Tihomit Culjaga 16.03.2009

 

case $1 in

        start)

                cd /persistent/bin;

                cshop /persistent/var/www/cshop/db/cshop.db 2>&1 &

                #cshop --console /persistent/var/www/cshop/db/cshop.db &

 

        ;;

 

        stop)

                #killall -9 php

                kill -9 `ps aux | grep cshop | grep -v grep | cut -d  r -f 1`

        ;;

 

        enable)

                rm -f /etc/rc.d/S95cshop;

                ln -s /etc/init.d/cshop /etc/rc.d/S95cshop

        ;;

 

        disable)

                rm -f /etc/rc.d/S95cshop

        ;;

 

        *)

                cat <<EOF

        ;;

      

      

      

Syntax: /etc/init.d/cshop [command]

 

Available commands:

        start   Start the service

        stop    Stop the service

        enable  Enable service autostart

        disable Disable service autostart

EOF

esac

 

      

root:~> ls -l /etc/rc.d/

lrwxrwxrwx    1 0        0              17 S95cshop -> /etc/init.d/cshop

lrwxrwxrwx    1 0        0              29 S91inadyn -> /persistent/etc/init.d/inadyn

lrwxrwxrwx    1 0        0              31 S90lighttpd -> /persistent/etc/init.d/lighttpd

lrwxrwxrwx    1 0        0              34 S09env_startup -> /persistent/etc/init.d/env_startup

lrwxrwxrwx    1 1000     1000           14 S35cron -> ../init.d/cron

lrwxrwxrwx    1 1000     1000           17 S10network -> ../init.d/network

lrwxrwxrwx    1 1000     1000           16 S40zaptel -> ../init.d/zaptel

lrwxrwxrwx    1 1000     1000           13 S30ntp -> ../init.d/ntp

lrwxrwxrwx    1 1000     1000           18 S50asterisk -> ../init.d/asterisk

lrwxrwxrwx    1 0        0              28 S12pppoe -> /persistent/etc/init.d/pppoe

root:~>

 

 

 

 

 

does anyone have a clue ?

 

 

 

Tihomir.

QuoteReplyEditDelete

 

 

2009-04-08 18:13:23     Re: daemon on blackfin

Mike Frysinger (UNITED STATES)

Message: 72438   

 

you've got a lot of exit paths there with no error messages.  why not sprinkle printf()'s at every exit location to see where it leaves.

 

iirc, stdin is closed when processing init stuff ...

QuoteReplyEditDelete

 

 

2009-04-10 13:46:48     Re: daemon on blackfin

Tihomir Culjaga (CROATIA)

Message: 72514   

 

Hello Mike,

 

 

 

thanks for your help.

 

 

 

i did a different approach and it works now

 

here is a sample code if someone need a how to:

 

 

 

void MakeDaemon()

{

 

        pid_t pid;

        if ((pid = vfork()) < 0)

        {

                return;

        }

        else if (pid != 0)

        {

                _exit(0);

        }

 

        setsid();

 

        if ((pid = vfork()) < 0)

        {

                return;

        }

        else if (pid != 0)

        {

                _exit(0);

        }

 

        setsid();

        umask(0);

        close(0);

        int fd;

 

 

        fd = open("/dev/null", O_RDONLY);

 

        if (fd == -1)

        {

                //return -1;

                return;

        }

 

        close(1);

        {

                fd = open("/dev/null", O_WRONLY);

        }

        if (fd == -1)

        {

                return;

        }

 

        close(2);

        {

                fd = open("/dev/null", O_WRONLY);

        }

 

        if (fd == -1)

        {

                return;

        }

 

}

 

 

int main(int argc, char* argv[])

{

        printf("\n\ncshop ver. 0.1\n");

        if (argc == 2)

        {

                MakeDaemon();

                execl ("./cshop", "./cshop", "daemon", argv[1], 0);

                _exit(0);            

        }

 

        signal(SIGTERM,         KillHandler);

        signal(SIGINT,          KillHandler);

 

        dbPath = malloc(128 * sizeof(char));

        memset(dbPath, 0, 128);

        strcpy(dbPath, argv[2]);

 

        g_pid = vfork();

        if (g_pid == 0)

        {

                execl ("./cshoptimer", "./cshoptimer", dbPath, 0);

                _exit(0);

        }

 

        EVENT_DATA* pEventData;

        int retcode;

 

        retcode = Initialize();

        if (retcode == CSHOP_ERROR)

                return 0;

 

        while (1)

        {

                Ping(g_sockfd);

                printf("\nreceiving event ...");

                fflush(stdout);

                pEventData = ReceiveEvent();

 

                if (pEventData == NULL)

                        continue;

 

                printf("\nhandlinging event ...");

                fflush(stdout);

                HandleEvent(pEventData);

 

                printf("\nfrreing event ...");

                fflush(stdout);

                FreeEvent(pEventData);

        }

 

        return 0;

}

 

 

 

Now the application (both parent and child) is running properly.

                                                                                                                                                                                                                         

QuoteReplyEditDelete

 

 

2009-04-10 13:49:25     Re: daemon on blackfin

Mike Frysinger (UNITED STATES)

Message: 72515   

 

i did implement the proper daemon() symbol in uClibc for our next release (2009R1).  in which case you dont need any of the vfork and exec tricks.  just call daemon() and continue on your way.

QuoteReplyEditDelete

 

 

2009-04-10 16:12:38     Re: daemon on blackfin

Tihomir Culjaga (CROATIA)

Message: 72517   

 

this will be perfect

 

 

 

thanks!

Attachments

    Outcomes