xref: /csrg-svn/libexec/talkd/talkd.c (revision 34776)
122401Sdist /*
222401Sdist  * Copyright (c) 1983 Regents of the University of California.
334360Sbostic  * All rights reserved.
434360Sbostic  *
534360Sbostic  * Redistribution and use in source and binary forms are permitted
6*34776Sbostic  * provided that the above copyright notice and this paragraph are
7*34776Sbostic  * duplicated in all such forms and that any documentation,
8*34776Sbostic  * advertising materials, and other materials related to such
9*34776Sbostic  * distribution and use acknowledge that the software was developed
10*34776Sbostic  * by the University of California, Berkeley.  The name of the
11*34776Sbostic  * University may not be used to endorse or promote products derived
12*34776Sbostic  * from this software without specific prior written permission.
13*34776Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34776Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34776Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1622401Sdist  */
1722401Sdist 
1816366Skarels #ifndef lint
1922401Sdist char copyright[] =
2022401Sdist "@(#) Copyright (c) 1983 Regents of the University of California.\n\
2122401Sdist  All rights reserved.\n";
2234360Sbostic #endif /* not lint */
2316360Skarels 
2422401Sdist #ifndef lint
25*34776Sbostic static char sccsid[] = "@(#)talkd.c	5.4 (Berkeley) 06/18/88";
2634360Sbostic #endif /* not lint */
2722401Sdist 
2816366Skarels /*
2916366Skarels  * The top level of the daemon, the format is heavily borrowed
3016366Skarels  * from rwhod.c. Basically: find out who and where you are;
3116366Skarels  * disconnect all descriptors and ttys, and then endless
3216366Skarels  * loop on waiting for and processing requests
3316360Skarels  */
3416360Skarels #include <stdio.h>
3516360Skarels #include <errno.h>
3616366Skarels #include <signal.h>
3726841Smckusick #include <syslog.h>
3816360Skarels 
3926841Smckusick #include <protocols/talkd.h>
4016360Skarels 
4116366Skarels CTL_MSG		request;
4216366Skarels CTL_RESPONSE	response;
4316360Skarels 
4416366Skarels int	sockt;
4516366Skarels int	debug = 0;
4616366Skarels int	timeout();
4716366Skarels long	lastmsgtime;
4816360Skarels 
4916366Skarels char	hostname[32];
5016360Skarels 
5116366Skarels #define TIMEOUT 30
5216366Skarels #define MAXIDLE 120
5316366Skarels 
5416366Skarels main(argc, argv)
5516366Skarels 	int argc;
5616366Skarels 	char *argv[];
5716360Skarels {
5826841Smckusick 	register CTL_MSG *mp = &request;
5926841Smckusick 	int cc;
6016360Skarels 
6116366Skarels 	if (getuid()) {
6226841Smckusick 		fprintf(stderr, "%s: getuid: not super-user", argv[0]);
6316366Skarels 		exit(1);
6416360Skarels 	}
6526841Smckusick 	openlog("talkd", LOG_PID, LOG_DAEMON);
6626841Smckusick 	if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
6726841Smckusick 		syslog(LOG_ERR, "gethostname: %m");
6826841Smckusick 		_exit(1);
6926841Smckusick 	}
7026841Smckusick 	if (chdir("/dev") < 0) {
7126841Smckusick 		syslog(LOG_ERR, "chdir: /dev: %m");
7226841Smckusick 		_exit(1);
7326841Smckusick 	}
7426841Smckusick 	if (argc > 1 && strcmp(argv[1], "-d") == 0)
7526841Smckusick 		debug = 1;
7616366Skarels 	signal(SIGALRM, timeout);
7716366Skarels 	alarm(TIMEOUT);
7816366Skarels 	for (;;) {
7916366Skarels 		extern int errno;
8016360Skarels 
8126841Smckusick 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
8226841Smckusick 		if (cc != sizeof (*mp)) {
8316366Skarels 			if (cc < 0 && errno != EINTR)
8426841Smckusick 				syslog(LOG_WARNING, "recv: %m");
8516366Skarels 			continue;
8616366Skarels 		}
8716366Skarels 		lastmsgtime = time(0);
8826841Smckusick 		process_request(mp, &response);
8916360Skarels 		/* can block here, is this what I want? */
9026841Smckusick 		cc = sendto(sockt, (char *)&response,
9126841Smckusick 		    sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
9226841Smckusick 		if (cc != sizeof (response))
9326841Smckusick 			syslog(LOG_WARNING, "sendto: %m");
9416360Skarels 	}
9516360Skarels }
9616360Skarels 
9716366Skarels timeout()
9816360Skarels {
9916360Skarels 
10016366Skarels 	if (time(0) - lastmsgtime >= MAXIDLE)
10126841Smckusick 		_exit(0);
10216366Skarels 	alarm(TIMEOUT);
10316360Skarels }
104