SCCSID(@
(#)daemon.c 3.46 %G% (w/o daemon mode));
SCCSID(@
(#)daemon.c 3.46 %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 */
int MaxConnections
= 4; /* maximum simultaneous sendmails */
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, 0);
/* probably another daemon already */
syserr("getrequests: can't create socket");
syslog(LOG_SALERT
, "cannot get connection");
/* turn on network debugging? */
(void) setsockopt(DaemonSocket
, SOL_SOCKET
, SO_DEBUG
, 0, 0);
if (bind(DaemonSocket
, &SendmailAddress
, sizeof SendmailAddress
, 0) < 0)
syserr("getrequests: cannot bind");
(void) close(DaemonSocket
);
listen(DaemonSocket
, 10);
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.
for (i
= 3; i
>= 0 && *p
!= ']' && *p
!= '\0'; i
--)
if (*p
!= (i
== 0 ? ']' : '.') || j
> 255 || j
< 0)
hid
|= j
<< ((3 - i
) * 8);
if (i
>= 0 || *p
!= ']' || *++p
!= '\0')
usrerr("Invalid numeric domain spec \"%s\"", host
);
SendmailAddress
.sin_addr
.s_addr
= hid
;
register struct hostent
*hp
= gethostbyname(host
);
bmove(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, 0);
syserr("makeconnection: no socket");
printf("makeconnection: %d\n", s
);
/* turn on network debugging? */
(void) setsockopt(s
, SOL_SOCKET
, SO_DEBUG
, 0, 0);
(void) fflush(CurEnv
->e_xfp
); /* for debugging */
bind(s
, &SendmailAddress
, sizeof SendmailAddress
, 0);
if (connect(s
, &SendmailAddress
, sizeof SendmailAddress
, 0) < 0)
SendmailAddress
.sin_family
= AF_INET
;
bind(s
, &SendmailAddress
, sizeof SendmailAddress
, 0);
if (connect(s
, &SendmailAddress
, sizeof SendmailAddress
, 0) < 0)
/* failure, decide if temporary or not */
/* there are others, I'm sure..... */
/* connection ok, put it into canonical form */
*outfile
= fdopen(s
, "w");
*infile
= fdopen(s
, "r");