init
This commit is contained in:
148
daemon.c
Normal file
148
daemon.c
Normal file
@@ -0,0 +1,148 @@
|
||||
|
||||
#undef OSF1
|
||||
|
||||
/*
|
||||
** This code written by danny@wildstar.net <-- wildThang -->
|
||||
** You are free to use this code under the stipulation that it
|
||||
** is not used to design any sort of clone/flood, or otherwise harassing
|
||||
** type of application. I wrote this quickly and tried to keep it as
|
||||
** basic as i could think up but enough to keep it interesting.
|
||||
** Use it to LEARN! let me know if you have questions about it.
|
||||
** -- this notice must remain in all code based on this code.
|
||||
*/
|
||||
|
||||
|
||||
/* DAEMON.C
|
||||
This file handles moving program into the background.
|
||||
and it also contains the routine needed to establish a port
|
||||
in case of wanting to have it listne to a port as well
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/wait.h>
|
||||
#include <netdb.h>
|
||||
#include <time.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/*#ifdef SIGTSTP */
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/file.h>
|
||||
/*#endif */
|
||||
#include <sys/param.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <netinet/in.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include "config.h"
|
||||
|
||||
handle_kids()
|
||||
{int pid;
|
||||
|
||||
#ifdef OSF1
|
||||
union wait *status;
|
||||
while( (pid = wait3(status,WNOHANG, (struct rusage *)0)) > 0) { }
|
||||
|
||||
#else
|
||||
|
||||
/* union wait status;
|
||||
*/
|
||||
int status;
|
||||
while( (pid = wait3(&status,WNOHANG, (struct rusage *)0)) > 0) { }
|
||||
|
||||
#endif /* OSF1 */
|
||||
|
||||
}
|
||||
|
||||
/* this function releases tty conrol and moves program into background */
|
||||
goto_bg()
|
||||
{
|
||||
register int child,fd;
|
||||
close(0); close(1); close(2);
|
||||
|
||||
if(getppid() == 1) goto out;
|
||||
|
||||
#ifdef SIGTTOU
|
||||
signal(SIGTTOU,SIG_IGN);
|
||||
#endif
|
||||
#ifdef SIGTTIN
|
||||
signal(SIGTTIN,SIG_IGN);
|
||||
#endif
|
||||
#ifdef SIGTSTP
|
||||
signal(SIGTSTP,SIG_IGN);
|
||||
#endif
|
||||
|
||||
if( (child =fork()) < 0)
|
||||
perror("fork");
|
||||
else if (child > 0)
|
||||
exit(0);
|
||||
|
||||
#ifdef SIGTSTP
|
||||
if (setpgrp(0,getpid()) == -1)
|
||||
perror("SETGROUP");
|
||||
|
||||
if( (fd = open("/dev/tty",O_RDWR)) >= 0) {
|
||||
ioctl(fd, TIOCNOTTY, (char *)NULL);
|
||||
close(fd);
|
||||
}
|
||||
#else
|
||||
if(setpgrp() == -1)
|
||||
perror("set group");
|
||||
|
||||
signal(SIGHUP,SIG_IGN);
|
||||
|
||||
if( (child = fork()) < 0)
|
||||
perror("cant fork");
|
||||
else if (child > 0)
|
||||
exit(0);
|
||||
#endif
|
||||
|
||||
out:
|
||||
for(fd=0;fd<NOFILE;fd++)
|
||||
close(fd);
|
||||
errno = 0;
|
||||
|
||||
|
||||
#ifdef SIGTSTP
|
||||
/*
|
||||
signal(SIGCLD, handle_kids);
|
||||
*/
|
||||
#else
|
||||
signal(SIGCLD, SIG_IGN);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* this routine connects, binds, and listens to a port number */
|
||||
int get_port(portnum)
|
||||
int portnum;
|
||||
{ int sock,l;
|
||||
int opt = 1;
|
||||
static struct sockaddr_in server;
|
||||
|
||||
sock = socket(AF_INET,SOCK_STREAM, 0);
|
||||
if(sock < 0) exit(-1);
|
||||
|
||||
#ifdef SO_REUSEADDR
|
||||
if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt)) < 0)
|
||||
exit(-1);
|
||||
#endif
|
||||
|
||||
server.sin_family = AF_INET;
|
||||
server.sin_addr.s_addr = INADDR_ANY;
|
||||
server.sin_port = htons(portnum);
|
||||
|
||||
for(l=0;l<10;l++)
|
||||
{ if(bind(sock, (struct sockaddr *) &server, sizeof(server)))
|
||||
{ if(l > 9) exit(-1);
|
||||
;
|
||||
}
|
||||
else break;
|
||||
}
|
||||
|
||||
listen(sock,3);
|
||||
return(sock);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user