Sponsored Content
Full Discussion: Daemon
Top Forums Programming Daemon Post 302107552 by rv1125 on Monday 19th of February 2007 06:42:42 AM
Old 02-19-2007
you must use 2 parts:
1. tcp server
2. fork() for daemonize your server

if you wish to use more than 1 tcp session from outside, than you must use fork() or pthread_create(). The pthread version is better (less memory and faster). Example code is some big, but you can find it in many places.

For go to daemon, you can use this code (create header for it and use toDaemon call):

//
// close all FDs >= a specified value
//
void
daemon::closeall(int fd) {
#ifndef SOLARIS
int fdlimit = sysconf(_SC_OPEN_MAX);
while(fd < fdlimit) close(fd++);
#endif //SOLARIS
}

// detach process from user and disappear into the background
// returns -1 on failure, but you can't do much except exit in that case
// since we may already have forked. This is based on the BSD version,
// so the caller is responsible for things like the umask, etc.
int
daemon::toDaemon(int nochdir, int noclose) {
switch(fork()) {
case 0:
break;
case -1:
return -1;
default:
_exit(0); // exit the original process
}
if(setsid() < 0) // shoudn't fail
return -1;
// dyke out this switch if you want to acquire a control tty in
// the future -- not normally advisable for daemons
switch(fork()) {
case 0:
break;
case -1:
return -1;
default:
_exit(0);
}
if(!nochdir) chdir("/");
if(!noclose) {
closeall(0);
open("/dev/null",O_RDWR);
dup(0); dup(0);
}
return 0;
}

// fork2() -- like fork, but the new process is immediately orphaned
// (won't leave a zombie when it exits)
// Returns 1 to the parent, not any meaningful pid.
// The parent cannot wait() for the new process (it's unrelated).

// This version assumes that you *haven't* caught or ignored SIGCHLD.
// If you have, then you should just be using fork() instead anyway.

int
daemon::fork2() {
pid_t pid;
int status;

if(!(pid = fork())) {
switch (fork()) {
case 0:
return 0;
case -1:
_exit(errno); // assumes all errnos are <256
default:
_exit(0);
}
}
if(pid < 0 || waitpid(pid,&status,0) < 0) return -1;
if(WIFEXITED(status)) {
if(WEXITSTATUS(status) == 0) return 1;
else errno = WEXITSTATUS(status);
} else {
errno = EINTR; // well, sort of :-)
}
return -1;
}
 

9 More Discussions You Might Find Interesting

1. Programming

daemon

I want to write background running program. How to use daemon function. Please send me source code. Thanks. (1 Reply)
Discussion started by: bat_oyu
1 Replies

2. Shell Programming and Scripting

Creating a Daemon??

How in the world do you create a daemon and get it to start with a startup script? could someone tell me in detail im going nuts...thanks (1 Reply)
Discussion started by: nmajin
1 Replies

3. Programming

Daemon...Zombie?? Please help me

Hello, i am very very puzzled, im doing this project for school, its a deamon logger, but anyways I'm supposed to run the daemon, let it run on the backgroun, and then run a different program (from command like prompt). but when i run my daemon, it never goes back to the nova> prompt. :( i dont... (3 Replies)
Discussion started by: Kacyndra
3 Replies

4. Shell Programming and Scripting

daemon scripts

Hello, id like to know how can i do for making my script a daemon. Im not sure, but once , i might read about a "daemon" command. Thats true? Whats it for? It isnt in my man. (2 Replies)
Discussion started by: Jariya
2 Replies

5. Programming

Creating a Daemon

how to convert a c program into a Daemon. thanks in advance svh (2 Replies)
Discussion started by: svh
2 Replies

6. AIX

rsync daemon

Does anyone out there use rsync on AIX 5.2, I've installed and would like to run in server mode, I've setup in services and inetd.conf and refreshed inetd. The rsync daemon however does not start, I've also tried rsync --daemon, this just returns to the # prompt. The rsync command itself appears to... (1 Reply)
Discussion started by: gefa
1 Replies

7. Programming

How to write daemon?

Hi , I want to know how to write a daemon process. I also want to know the concept behind daemon processes. Any material or sample program will be great :) . Thanks in advance -sg (2 Replies)
Discussion started by: sg6876
2 Replies

8. Programming

Help with task daemon

believe it or not but this is my first c program (i've worked with java, C#, php though) I am trying to make a daemon that checks if mplayer is running(it's for a projection room) and if it is not then to run mplayer with a file.. So far it's not working and I don't know why Help and comments... (5 Replies)
Discussion started by: james2432
5 Replies

9. UNIX for Advanced & Expert Users

stunnel4 daemon

If my web browser does not support SSL.This means that I can't connect like this "https://xxxxx". My question is how can i make a stunnel4 daemon in client mode ? like when i connect to "http://localhost:3000" in the web browser i am navigating througth "https://www.randomweb.com". I need to know... (0 Replies)
Discussion started by: pepeleches
0 Replies
DAEMON(3)						     Linux Programmer's Manual							 DAEMON(3)

NAME
daemon - run in the background SYNOPSIS
#include <unistd.h> int daemon(int nochdir, int noclose); Feature Test Macro Requirements for glibc (see feature_test_macros(7)): daemon(): _BSD_SOURCE || (_XOPEN_SOURCE && _XOPEN_SOURCE < 500) DESCRIPTION
The daemon() function is for programs wishing to detach themselves from the controlling terminal and run in the background as system dae- mons. If nochdir is zero, daemon() changes the calling process's current working directory to the root directory ("/"); otherwise, the current working directory is left unchanged. If noclose is zero, daemon() redirects standard input, standard output and standard error to /dev/null; otherwise, no changes are made to these file descriptors. RETURN VALUE
(This function forks, and if the fork(2) succeeds, the parent calls _exit(2), so that further errors are seen by the child only.) On suc- cess daemon() returns zero. If an error occurs, daemon() returns -1 and sets errno to any of the errors specified for the fork(2) and set- sid(2). CONFORMING TO
Not in POSIX.1-2001. A similar function appears on the BSDs. The daemon() function first appeared in 4.4BSD. NOTES
The glibc implementation can also return -1 when /dev/null exists but is not a character device with the expected major and minor numbers. In this case errno need not be set. SEE ALSO
fork(2), setsid(2) COLOPHON
This page is part of release 3.53 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. GNU
2009-12-05 DAEMON(3)
All times are GMT -4. The time now is 10:37 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy