Re: how to make linux program to demon

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 12/21/05, ppunnam@xxxxxxxxxxx <ppunnam@xxxxxxxxxxx> wrote:
>
>   Hi guys..
>
>       I got a linux user level  program, i want to make it as demon. i
> googled it but i didnt found any good tutorial ...
> if any one has some links or info please forward me.
>

Not really a kernel question, but anyway :


#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifdef OPEN_MAX
static int max_descriptors_open = OPEN_MAX;
#else
static int max_descriptors_open = 0;
#endif

#define MAX_TO_CLOSE 256 /* guess fallback if OPEN_MAX can't be determined */

int max_descriptors(void)
{
    if (!max_descriptors_open) {
        errno = 0;
        max_descriptors_open = sysconf(_SC_OPEN_MAX);
        if (max_descriptors_open < 0) {
            if (errno == 0) {
                max_descriptors_open = MAX_TO_CLOSE;
            } else {
                fprintf(stderr, "sysconf error _SC_OPEN_MAX: %s\n",
strerror(errno));
                exit(EXIT_FAILURE);
            }
        }
    }
    return max_descriptors_open;
}

int make_me_a_daemon(void)
{
    pid_t    pid;
    int    i, nr_to_close;

    if ((pid = fork()) < 0) {
        /* fork failed, we can't become a daemon, errno is set */
        fprintf(stderr, "fork failed while trying to daemonize: %s\n",
strerror(errno));
        return -1;
    } else if (pid != 0) {
        exit(EXIT_SUCCESS);    /* this is the parent exiting */
    }
    /* here we continue executing in the child */

    setsid();    /* here we become session leader */
    chdir("/");    /* ensure we don't keep filesystems from unmounting */
    umask(0);    /* make sure we can create files with any mask we like */

    /* now we need to close all open filedescriptors we may have inherited.
       Which ones to close depends on your daemon, so here I'll just try and
       close all.
     */
    nr_to_close = max_descriptors();
    for (i = 0; i < nr_to_close; i++)
        close(i);

    /* we are now a proer daemon */

    return 0;
}

int main(void)
{
    printf("Trying to become a daemon\n");
    if (make_me_a_daemon() != 0) {
        fprintf(stderr, "Unable to become a daemon\n");
        exit(EXIT_FAILURE);
    }
    /* Successfully became a daemon, but we can't print that fact since
       we closed all file descriptors including stdout/stderr, so just
       sleep for 60 seconds so we can be seen for a minute with "ps"
     */
    sleep(60);
    exit(EXIT_SUCCESS);
}


--
Jesper Juhl <jesper.juhl@xxxxxxxxx>
Don't top-post  http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please      http://www.expita.com/nomime.html

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux