xref: /csrg-svn/libexec/talkd/talkd.c (revision 34360)
122401Sdist /*
222401Sdist  * Copyright (c) 1983 Regents of the University of California.
3*34360Sbostic  * All rights reserved.
4*34360Sbostic  *
5*34360Sbostic  * Redistribution and use in source and binary forms are permitted
6*34360Sbostic  * provided that this notice is preserved and that due credit is given
7*34360Sbostic  * to the University of California at Berkeley. The name of the University
8*34360Sbostic  * may not be used to endorse or promote products derived from this
9*34360Sbostic  * software without specific prior written permission. This software
10*34360Sbostic  * is provided ``as is'' without express or implied warranty.
1122401Sdist  */
1222401Sdist 
1316366Skarels #ifndef lint
1422401Sdist char copyright[] =
1522401Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
1622401Sdist  All rights reserved.\n";
17*34360Sbostic #endif /* not lint */
1816360Skarels 
1922401Sdist #ifndef lint
20*34360Sbostic static char sccsid[] = "@(#)talkd.c	5.3 (Berkeley) 05/20/88";
21*34360Sbostic #endif /* not lint */
2222401Sdist 
2316366Skarels /*
2416366Skarels  * The top level of the daemon, the format is heavily borrowed
2516366Skarels  * from rwhod.c. Basically: find out who and where you are;
2616366Skarels  * disconnect all descriptors and ttys, and then endless
2716366Skarels  * loop on waiting for and processing requests
2816360Skarels  */
2916360Skarels #include <stdio.h>
3016360Skarels #include <errno.h>
3116366Skarels #include <signal.h>
3226841Smckusick #include <syslog.h>
3316360Skarels 
3426841Smckusick #include <protocols/talkd.h>
3516360Skarels 
3616366Skarels CTL_MSG		request;
3716366Skarels CTL_RESPONSE	response;
3816360Skarels 
3916366Skarels int	sockt;
4016366Skarels int	debug = 0;
4116366Skarels int	timeout();
4216366Skarels long	lastmsgtime;
4316360Skarels 
4416366Skarels char	hostname[32];
4516360Skarels 
4616366Skarels #define TIMEOUT 30
4716366Skarels #define MAXIDLE 120
4816366Skarels 
4916366Skarels main(argc, argv)
5016366Skarels 	int argc;
5116366Skarels 	char *argv[];
5216360Skarels {
5326841Smckusick 	register CTL_MSG *mp = &request;
5426841Smckusick 	int cc;
5516360Skarels 
5616366Skarels 	if (getuid()) {
5726841Smckusick 		fprintf(stderr, "%s: getuid: not super-user", argv[0]);
5816366Skarels 		exit(1);
5916360Skarels 	}
6026841Smckusick 	openlog("talkd", LOG_PID, LOG_DAEMON);
6126841Smckusick 	if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
6226841Smckusick 		syslog(LOG_ERR, "gethostname: %m");
6326841Smckusick 		_exit(1);
6426841Smckusick 	}
6526841Smckusick 	if (chdir("/dev") < 0) {
6626841Smckusick 		syslog(LOG_ERR, "chdir: /dev: %m");
6726841Smckusick 		_exit(1);
6826841Smckusick 	}
6926841Smckusick 	if (argc > 1 && strcmp(argv[1], "-d") == 0)
7026841Smckusick 		debug = 1;
7116366Skarels 	signal(SIGALRM, timeout);
7216366Skarels 	alarm(TIMEOUT);
7316366Skarels 	for (;;) {
7416366Skarels 		extern int errno;
7516360Skarels 
7626841Smckusick 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
7726841Smckusick 		if (cc != sizeof (*mp)) {
7816366Skarels 			if (cc < 0 && errno != EINTR)
7926841Smckusick 				syslog(LOG_WARNING, "recv: %m");
8016366Skarels 			continue;
8116366Skarels 		}
8216366Skarels 		lastmsgtime = time(0);
8326841Smckusick 		process_request(mp, &response);
8416360Skarels 		/* can block here, is this what I want? */
8526841Smckusick 		cc = sendto(sockt, (char *)&response,
8626841Smckusick 		    sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
8726841Smckusick 		if (cc != sizeof (response))
8826841Smckusick 			syslog(LOG_WARNING, "sendto: %m");
8916360Skarels 	}
9016360Skarels }
9116360Skarels 
9216366Skarels timeout()
9316360Skarels {
9416360Skarels 
9516366Skarels 	if (time(0) - lastmsgtime >= MAXIDLE)
9626841Smckusick 		_exit(0);
9716366Skarels 	alarm(TIMEOUT);
9816360Skarels }
99