xref: /csrg-svn/libexec/talkd/talkd.c (revision 37991)
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
634776Sbostic  * provided that the above copyright notice and this paragraph are
734776Sbostic  * duplicated in all such forms and that any documentation,
834776Sbostic  * advertising materials, and other materials related to such
934776Sbostic  * distribution and use acknowledge that the software was developed
1034776Sbostic  * by the University of California, Berkeley.  The name of the
1134776Sbostic  * University may not be used to endorse or promote products derived
1234776Sbostic  * from this software without specific prior written permission.
1334776Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434776Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534776Sbostic  * 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*37991Sbostic static char sccsid[] = "@(#)talkd.c	5.5 (Berkeley) 05/11/89";
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>
40*37991Sbostic #include <paths.h>
4116360Skarels 
4216366Skarels CTL_MSG		request;
4316366Skarels CTL_RESPONSE	response;
4416360Skarels 
4516366Skarels int	sockt;
4616366Skarels int	debug = 0;
4716366Skarels int	timeout();
4816366Skarels long	lastmsgtime;
4916360Skarels 
5016366Skarels char	hostname[32];
5116360Skarels 
5216366Skarels #define TIMEOUT 30
5316366Skarels #define MAXIDLE 120
5416366Skarels 
5516366Skarels main(argc, argv)
5616366Skarels 	int argc;
5716366Skarels 	char *argv[];
5816360Skarels {
5926841Smckusick 	register CTL_MSG *mp = &request;
6026841Smckusick 	int cc;
6116360Skarels 
6216366Skarels 	if (getuid()) {
6326841Smckusick 		fprintf(stderr, "%s: getuid: not super-user", argv[0]);
6416366Skarels 		exit(1);
6516360Skarels 	}
6626841Smckusick 	openlog("talkd", LOG_PID, LOG_DAEMON);
6726841Smckusick 	if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
6826841Smckusick 		syslog(LOG_ERR, "gethostname: %m");
6926841Smckusick 		_exit(1);
7026841Smckusick 	}
71*37991Sbostic 	if (chdir(_PATH_DEV) < 0) {
72*37991Sbostic 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
7326841Smckusick 		_exit(1);
7426841Smckusick 	}
7526841Smckusick 	if (argc > 1 && strcmp(argv[1], "-d") == 0)
7626841Smckusick 		debug = 1;
7716366Skarels 	signal(SIGALRM, timeout);
7816366Skarels 	alarm(TIMEOUT);
7916366Skarels 	for (;;) {
8016366Skarels 		extern int errno;
8116360Skarels 
8226841Smckusick 		cc = recv(0, (char *)mp, sizeof (*mp), 0);
8326841Smckusick 		if (cc != sizeof (*mp)) {
8416366Skarels 			if (cc < 0 && errno != EINTR)
8526841Smckusick 				syslog(LOG_WARNING, "recv: %m");
8616366Skarels 			continue;
8716366Skarels 		}
8816366Skarels 		lastmsgtime = time(0);
8926841Smckusick 		process_request(mp, &response);
9016360Skarels 		/* can block here, is this what I want? */
9126841Smckusick 		cc = sendto(sockt, (char *)&response,
9226841Smckusick 		    sizeof (response), 0, &mp->ctl_addr, sizeof (mp->ctl_addr));
9326841Smckusick 		if (cc != sizeof (response))
9426841Smckusick 			syslog(LOG_WARNING, "sendto: %m");
9516360Skarels 	}
9616360Skarels }
9716360Skarels 
9816366Skarels timeout()
9916360Skarels {
10016360Skarels 
10116366Skarels 	if (time(0) - lastmsgtime >= MAXIDLE)
10226841Smckusick 		_exit(0);
10316366Skarels 	alarm(TIMEOUT);
10416360Skarels }
105