Daemon


 
Thread Tools Search this Thread
Top Forums Programming Daemon
# 1  
Old 02-16-2007
Daemon

i want to write a daemon service which listens the 8080 port and write down all the details in one file. How can i do this ?
# 2  
Old 02-16-2007
Start with this on writing a daemon:
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
# 3  
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;
}
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
Login or Register to Ask a Question