Пример #1
0
/* make this program run detached from the current console */
int proc__daemonize(void)
{
	pid_t process_id = 0;

	process_id = fork();
    if (process_id < 0) {
        LOGE("[ERROR] daemonize: fork failed! exit.\n");
        exit(1);
    }
    if (process_id > 0) {
        LOGD("[DEBUG] child pid: %d. exit.\n", process_id);
        exit(0);
    }

    /* now pid==0, child (daemon) continues */
	LOGD("[DEBUG] child process...\n");

	setsid(); // obtain a new process group

	umask(027); /* restrict file creation mode to 750 */
	chdir("/"); // set the working directory to the root directory
	redirect_fds();

	LOGD("[DEBUG] daemonize ok.\n");
	// ok...
	return 0;
}
Пример #2
0
int proc__run_background(const char* cmdline, int sleep_msec)
{
    pid_t process_id = 0;

    process_id = fork();
    if (process_id < 0) {
        LOGE("[-] ERROR: run_background: fork failed!\n");
        return -1;
    }
    if (process_id > 0) {
        LOGI("[+] run_background: child pid: %d.\n", process_id);
        return 0;
    }

    /* now pid==0, child (daemon) continues */
    LOGI("[+] child process...\n");


    setsid(); // obtain a new process group

    if(sleep_msec>0) {
        usleep(sleep_msec*1000); // give parent-process times to breathe...
    }

    umask(027); /* restrict file creation mode to 750 */
    chdir("/"); // set the working directory to the root directory
    redirect_fds();

    // ok...
    LOGI("[+] child-proc execl...\n");
    int ret = execl(cmdline, cmdline, NULL);
    // this should not return
    LOGE("[-] ERROR: OOPS! execl returned %d, errno: %d/%s\n", ret, errno, strerror(errno));
    return ret;
}
Пример #3
0
int
daemon_init(const char *pidfile) {
	int pid = check_pid(pidfile);

	if (pid) {
		fprintf(stderr, "Skynet is already running, pid = %d.\n", pid);
		return 1;
	}

#ifdef __APPLE__
	fprintf(stderr, "'daemon' is deprecated: first deprecated in OS X 10.5 , use launchd instead.\n");
#else
	if (daemon(1,1)) {
		fprintf(stderr, "Can't daemonize.\n");
		return 1;
	}
#endif

	pid = write_pid(pidfile);
	if (pid == 0) {
		return 1;
	}

	if (redirect_fds()) {
		return 1;
	}

	return 0;
}
Пример #4
0
int daemon (int nochdir, int noclose)
{
    int status = 0;

    printf ("_SC_OPEN_MAX=%d", sysconf (_SC_OPEN_MAX));

    /* Fork once to go into the background. */
    if ( (status = do_fork()) < 0 )
        ;

    /* Create new session */
    else if (setsid() < 0)               /* shouldn't fail */
        status = -1;

    /* Fork again to ensure that daemon never reacquires a control terminal. */
    else if ( (status = do_fork()) < 0 )
        ;

    else
    {
        /* clear any inherited umask(2) value */

        umask (0);

        /* We're there. */

        if (! nochdir)
        {
            /* Go to a neutral corner. */
            chdir("/");
        }

        if (! noclose)
            redirect_fds();
    }

    return status;
}
      int daemon(int nochdir, int noclose, boost::system::error_code &ec)
      {
         int status = 0;

         //openlog("daemonize", LOG_PID, LOG_DAEMON);

         // fork once to go into the background.
         if((status = do_fork()) < 0 )
            ;
         // create new session
         else if(setsid() < 0)              // shouldn't fail 
            status = -1;
         // fork again to ensure that daemon never reacquires a control terminal. 
         else if((status = do_fork()) < 0 )
            ;
         else
         {
            // clear any inherited umask(2) value 

            umask(0);

            // we're there.

            if(!nochdir)
            {
               // go to a neutral corner. 
               chdir("/");
            }

            if(!noclose)
            {
               redirect_fds(ec);
            }
         }

         return status;
      }