/*1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1983, 19934* The Regents of the University of California. All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031/*32* The top level of the daemon, the format is heavily borrowed33* from rwhod.c. Basically: find out who and where you are;34* disconnect all descriptors and ttys, and then endless35* loop on waiting for and processing requests36*/37#include <sys/types.h>38#include <sys/socket.h>39#include <sys/param.h>40#include <netinet/in.h>41#include <protocols/talkd.h>42#include <err.h>43#include <errno.h>44#include <paths.h>45#include <signal.h>46#include <stdio.h>47#include <stdlib.h>48#include <string.h>49#include <syslog.h>50#include <time.h>51#include <unistd.h>5253#include "extern.h"5455static CTL_MSG request;56static CTL_RESPONSE response;5758int debug = 0;59static long lastmsgtime;6061char hostname[MAXHOSTNAMELEN];6263#define TIMEOUT 3064#define MAXIDLE 1206566int67main(int argc, char *argv[])68{69register CTL_MSG *mp = &request;70int cc;71struct sockaddr ctl_addr;7273#ifdef NOTDEF74/*75* removed so ntalkd can run in tty sandbox76*/77if (getuid())78errx(1, "getuid: not super-user");79#endif80openlog("talkd", LOG_PID, LOG_DAEMON);81if (gethostname(hostname, sizeof(hostname) - 1) < 0) {82syslog(LOG_ERR, "gethostname: %m");83_exit(1);84}85hostname[sizeof(hostname) - 1] = '\0';86if (chdir(_PATH_DEV) < 0) {87syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);88_exit(1);89}90if (argc > 1 && strcmp(argv[1], "-d") == 0)91debug = 1;92signal(SIGALRM, timeout);93alarm(TIMEOUT);94for (;;) {95cc = recv(0, (char *)mp, sizeof(*mp), 0);96if (cc != sizeof (*mp)) {97if (cc < 0 && errno != EINTR)98syslog(LOG_WARNING, "recv: %m");99continue;100}101lastmsgtime = time(0);102(void)memcpy(&ctl_addr.sa_data, &mp->ctl_addr.sa_data,103sizeof(ctl_addr.sa_data));104ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);105ctl_addr.sa_len = sizeof(ctl_addr);106process_request(mp, &response);107/* can block here, is this what I want? */108cc = sendto(STDIN_FILENO, (char *)&response,109sizeof(response), 0, &ctl_addr, sizeof(ctl_addr));110if (cc != sizeof (response))111syslog(LOG_WARNING, "sendto: %m");112}113}114115void116timeout(int sig __unused)117{118int save_errno = errno;119120if (time(0) - lastmsgtime >= MAXIDLE)121_exit(0);122alarm(TIMEOUT);123errno = save_errno;124}125126127