** Copyright (c) 1983 Eric P. Allman
** Copyright (c) 1983 Regents of the University of California.
** All rights reserved. The Berkeley software License Agreement
** specifies the terms and conditions for redistribution.
static char SccsId
[] = "@(#)daemon.c 5.7 (Berkeley) %G% (w/o daemon mode)";
# include <sys/resource.h>
static char SccsId
[] = "@(#)daemon.c 5.7 (Berkeley) %G% (with daemon mode)";
** DAEMON.C -- routines to use when running as a daemon.
** This entire file is highly dependent on the 4.2 BSD
** interprocess communication primitives. No attempt has
** been made to make this file portable to Version 7,
** Version 6, MPX files, etc. If you should try such a
** thing yourself, I recommend chucking the entire file
** and starting from scratch. Basic semantics are:
** Opens a port and initiates a connection.
** Returns in a child. Must set InChannel and
** OutChannel appropriately.
** Close any open files associated with getting
** the connection; this is used when running the queue,
** etc., to avoid having extra file descriptors during
** the queue run and to avoid confusing the network
** makeconnection(host, port, outfile, infile)
** Make a connection to the named host on the given
** port. Set *outfile and *infile to the files
** appropriate for communication. Returns zero on
** success, else an exit status describing the
** The semantics of both of these should be clean.
static FILE *MailPort
; /* port that mail comes in on */
** GETREQUESTS -- open mail IPC port and get requests.
** Waits until some interesting activity occurs. When
** it does, a child is created to process it, and the
** parent waits for completion. Return from this
** routine is always in the child. The file pointers
** "InChannel" and "OutChannel" should be set to point
** to the communication channel.
struct sockaddr_in SendmailAddress
;/* internet address of sendmail */
int DaemonSocket
= -1; /* fd describing socket */
char *NetName
; /* name of home (local?) network */
register struct servent
*sp
;
** Set up the address for the mailer.
sp
= getservbyname("smtp", "tcp");
syserr("server \"smtp\" unknown");
SendmailAddress
.sin_family
= AF_INET
;
SendmailAddress
.sin_addr
.s_addr
= INADDR_ANY
;
SendmailAddress
.sin_port
= sp
->s_port
;
** Try to actually open the connection.
printf("getrequests: port 0x%x\n", SendmailAddress
.sin_port
);
/* get a socket for the SMTP connection */
DaemonSocket
= socket(AF_INET
, SOCK_STREAM
, 0);
/* probably another daemon already */
syserr("getrequests: can't create socket");
syslog(LOG_ALERT
, "cannot get connection");
/* turn on network debugging? */
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof on
);
if (bind(DaemonSocket
, &SendmailAddress
, sizeof SendmailAddress
) < 0)
syserr("getrequests: cannot bind");
(void) close(DaemonSocket
);
if (listen(DaemonSocket
, 10) < 0)
syserr("getrequests: cannot listen");
(void) close(DaemonSocket
);
printf("getrequests: %d\n", DaemonSocket
);
write(MailPort
, &wbuf
, sizeof wbuf
);
** MAKECONNECTION -- make a connection to an SMTP socket on another machine.
** host -- the name of the host.
** port -- the port number to connect to.
** outfile -- a pointer to a place to put the outfile
** infile -- ditto for infile.
** An exit code telling whether the connection could be
** made and if not why not.
makeconnection(host
, port
, outfile
, infile
)
** Set up the address for the mailer.
** Accept "[a.b.c.d]" syntax for host name.
register char *p
= index(host
, ']');
hid
= inet_addr(&host
[1]);
if (p
== NULL
|| hid
== -1)
usrerr("Invalid numeric domain spec \"%s\"", host
);
SendmailAddress
.sin_addr
.s_addr
= hid
;
register struct hostent
*hp
= gethostbyname(host
);
CurEnv
->e_flags
&= ~EF_FATALERRS
;
bcopy(hp
->h_addr
, (char *) &SendmailAddress
.sin_addr
, hp
->h_length
);
** Determine the port number.
SendmailAddress
.sin_port
= htons(port
);
register struct servent
*sp
= getservbyname("smtp", "tcp");
syserr("makeconnection: server \"smtp\" unknown");
SendmailAddress
.sin_port
= sp
->s_port
;
** Try to actually open the connection.
printf("makeconnection (%s)\n", host
);
s
= socket(AF_INET
, SOCK_STREAM
, 0, 0);
s
= socket(AF_INET
, SOCK_STREAM
, 0);
syserr("makeconnection: no socket");
printf("makeconnection: %d\n", s
);
/* turn on network debugging? */
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_DEBUG
, (char *)&on
, sizeof on
);
(void) fflush(CurEnv
->e_xfp
); /* for debugging */
errno
= 0; /* for debugging */
bind(s
, &SendmailAddress
, sizeof SendmailAddress
, 0);
if (connect(s
, &SendmailAddress
, sizeof SendmailAddress
, 0) < 0)
SendmailAddress
.sin_family
= AF_INET
;
if (connect(s
, &SendmailAddress
, sizeof SendmailAddress
) < 0)
/* failure, decide if temporary or not */
/* there are others, I'm sure..... */
CurEnv
->e_flags
&= ~EF_FATALERRS
;
/* why is this happening? */
syserr("makeconnection: funny failure, addr=%lx, port=%x",
SendmailAddress
.sin_addr
.s_addr
, SendmailAddress
.sin_port
);
/* connection ok, put it into canonical form */
*outfile
= fdopen(s
, "w");
*infile
= fdopen(s
, "r");
** MYHOSTNAME -- return the name of this host.
** hostbuf -- a place to return the name of this host.
** size -- the size of hostbuf.
** A list of aliases for this host.
myhostname(hostbuf
, size
)
extern struct hostent
*gethostbyname();
if (gethostname(hostbuf
, size
) < 0)
(void) strcpy(hostbuf
, "localhost");
hp
= gethostbyname(hostbuf
);
(void) strcpy(hostbuf
, hp
->h_name
);
** MAPHOSTNAME -- turn a hostname into canonical form
** hbuf -- a buffer containing a hostname.
** hbsize -- the size of hbuf.
** Looks up the host specified in hbuf. If it is not
** the canonical name for that host, replace it with
** the canonical name. If the name is unknown, or it
** is already the canonical name, leave it unchanged.
maphostname(hbuf
, hbsize
)
register struct hostent
*hp
;
extern struct hostent
*gethostbyname();
hp
= gethostbyname(hbuf
);
int i
= strlen(hp
->h_name
);
(void) strcpy(hbuf
, hp
->h_name
);
/* code for systems without sophisticated networking */
** MYHOSTNAME -- stub version for case of no daemon code.
** Can't convert to upper case here because might be a UUCP name.
** Mark, you can change this to be anything you want......
myhostname(hostbuf
, size
)
f
= fopen("/usr/include/whoami", "r");
(void) fgets(hostbuf
, size
, f
);
** MAPHOSTNAME -- turn a hostname into canonical form
** hbuf -- a buffer containing a hostname.
** hbsize -- the size of hbuf.
** Looks up the host specified in hbuf. If it is not
** the canonical name for that host, replace it with
** the canonical name. If the name is unknown, or it
** is already the canonical name, leave it unchanged.
maphostname(hbuf
, hbsize
)