talkd.c revision 1592
11592Srgrimes/*
21592Srgrimes * Copyright (c) 1983, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
351592Srgrimesstatic char copyright[] =
361592Srgrimes"@(#) Copyright (c) 1983, 1993\n\
371592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381592Srgrimes#endif /* not lint */
391592Srgrimes
401592Srgrimes#ifndef lint
411592Srgrimesstatic char sccsid[] = "@(#)talkd.c	8.1 (Berkeley) 6/4/93";
421592Srgrimes#endif /* not lint */
431592Srgrimes
441592Srgrimes/*
451592Srgrimes * The top level of the daemon, the format is heavily borrowed
461592Srgrimes * from rwhod.c. Basically: find out who and where you are;
471592Srgrimes * disconnect all descriptors and ttys, and then endless
481592Srgrimes * loop on waiting for and processing requests
491592Srgrimes */
501592Srgrimes#include <sys/types.h>
511592Srgrimes#include <sys/socket.h>
521592Srgrimes#include <protocols/talkd.h>
531592Srgrimes#include <signal.h>
541592Srgrimes#include <syslog.h>
551592Srgrimes#include <time.h>
561592Srgrimes#include <errno.h>
571592Srgrimes#include <unistd.h>
581592Srgrimes#include <stdio.h>
591592Srgrimes#include <stdlib.h>
601592Srgrimes#include <string.h>
611592Srgrimes#include <paths.h>
621592Srgrimes
631592SrgrimesCTL_MSG		request;
641592SrgrimesCTL_RESPONSE	response;
651592Srgrimes
661592Srgrimesint	sockt;
671592Srgrimesint	debug = 0;
681592Srgrimesvoid	timeout();
691592Srgrimeslong	lastmsgtime;
701592Srgrimes
711592Srgrimeschar	hostname[32];
721592Srgrimes
731592Srgrimes#define TIMEOUT 30
741592Srgrimes#define MAXIDLE 120
751592Srgrimes
761592Srgrimesmain(argc, argv)
771592Srgrimes	int argc;
781592Srgrimes	char *argv[];
791592Srgrimes{
801592Srgrimes	register CTL_MSG *mp = &request;
811592Srgrimes	int cc;
821592Srgrimes
831592Srgrimes	if (getuid()) {
841592Srgrimes		fprintf(stderr, "%s: getuid: not super-user\n", argv[0]);
851592Srgrimes		exit(1);
861592Srgrimes	}
871592Srgrimes	openlog("talkd", LOG_PID, LOG_DAEMON);
881592Srgrimes	if (gethostname(hostname, sizeof (hostname) - 1) < 0) {
891592Srgrimes		syslog(LOG_ERR, "gethostname: %m");
901592Srgrimes		_exit(1);
911592Srgrimes	}
921592Srgrimes	if (chdir(_PATH_DEV) < 0) {
931592Srgrimes		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
941592Srgrimes		_exit(1);
951592Srgrimes	}
961592Srgrimes	if (argc > 1 && strcmp(argv[1], "-d") == 0)
971592Srgrimes		debug = 1;
981592Srgrimes	signal(SIGALRM, timeout);
991592Srgrimes	alarm(TIMEOUT);
1001592Srgrimes	for (;;) {
1011592Srgrimes		extern int errno;
1021592Srgrimes
1031592Srgrimes		cc = recv(0, (char *)mp, sizeof (*mp), 0);
1041592Srgrimes		if (cc != sizeof (*mp)) {
1051592Srgrimes			if (cc < 0 && errno != EINTR)
1061592Srgrimes				syslog(LOG_WARNING, "recv: %m");
1071592Srgrimes			continue;
1081592Srgrimes		}
1091592Srgrimes		lastmsgtime = time(0);
1101592Srgrimes		process_request(mp, &response);
1111592Srgrimes		/* can block here, is this what I want? */
1121592Srgrimes		cc = sendto(sockt, (char *)&response,
1131592Srgrimes		    sizeof (response), 0, (struct sockaddr *)&mp->ctl_addr,
1141592Srgrimes		    sizeof (mp->ctl_addr));
1151592Srgrimes		if (cc != sizeof (response))
1161592Srgrimes			syslog(LOG_WARNING, "sendto: %m");
1171592Srgrimes	}
1181592Srgrimes}
1191592Srgrimes
1201592Srgrimesvoid
1211592Srgrimestimeout()
1221592Srgrimes{
1231592Srgrimes
1241592Srgrimes	if (time(0) - lastmsgtime >= MAXIDLE)
1251592Srgrimes		_exit(0);
1261592Srgrimes	alarm(TIMEOUT);
1271592Srgrimes}
128