1 /* $NetBSD: talkd.c,v 1.20 2008/07/20 01:09:07 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)talkd.c 8.1 (Berkeley) 6/4/93";
42 __RCSID("$NetBSD: talkd.c,v 1.20 2008/07/20 01:09:07 lukem Exp $");
47 * The top level of the daemon, the format is heavily borrowed
48 * from rwhod.c. Basically: find out who and where you are;
49 * disconnect all descriptors and ttys, and then endless
50 * loop on waiting for and processing requests
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/param.h>
56 #include <protocols/talkd.h>
71 CTL_RESPONSE response
;
73 int sockt
= STDIN_FILENO
;
78 char hostname
[MAXHOSTNAMELEN
+ 1];
83 static void timeout(int);
84 int main(int, char *[]);
87 main(int argc
, char *argv
[])
89 CTL_MSG
*mp
= &request
;
91 struct sockaddr ctl_addr
;
93 openlog("talkd", LOG_PID
, LOG_DAEMON
);
94 while ((ch
= getopt(argc
, argv
, "dl")) != -1)
103 syslog(LOG_ERR
, "Usage: %s [-dl]", getprogname());
107 if (gethostname(hostname
, sizeof hostname
) < 0) {
108 syslog(LOG_ERR
, "gethostname: %m");
111 hostname
[MAXHOSTNAMELEN
] = '\0'; /* ensure null termination */
112 if (chdir(_PATH_DEV
) < 0) {
113 syslog(LOG_ERR
, "chdir: %s: %m", _PATH_DEV
);
116 signal(SIGALRM
, timeout
);
119 memset(&response
, 0, sizeof(response
));
120 cc
= recv(0, (char *)mp
, sizeof (*mp
), 0);
121 if (cc
!= sizeof (*mp
)) {
122 if (cc
< 0 && errno
!= EINTR
)
123 syslog(LOG_WARNING
, "recv: %m");
127 mp
->l_name
[sizeof(mp
->l_name
) - 1] = '\0';
128 mp
->r_name
[sizeof(mp
->r_name
) - 1] = '\0';
129 mp
->r_tty
[sizeof(mp
->r_tty
) - 1] = '\0';
131 lastmsgtime
= time(0);
132 process_request(mp
, &response
);
134 tsa2sa(&ctl_addr
, &mp
->ctl_addr
);
135 if (ctl_addr
.sa_family
!= AF_INET
)
138 /* can block here, is this what I want? */
139 cc
= sendto(sockt
, (char *)&response
, sizeof (response
), 0,
140 &ctl_addr
, sizeof (ctl_addr
));
141 if (cc
!= sizeof (response
))
142 syslog(LOG_WARNING
, "sendto: %m");
149 int save_errno
= errno
;
151 if (time(0) - lastmsgtime
>= MAXIDLE
)
158 tsa2sa(struct sockaddr
*sa
, const struct talkd_sockaddr
*tsa
)
160 (void)memcpy(sa
, tsa
, sizeof(*tsa
));
161 sa
->sa_len
= sizeof(*tsa
);
162 sa
->sa_family
= tsa
->sa_family
;