[BACK]Return to talkd.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / libexec / talkd

Annotation of src/libexec/talkd/talkd.c, Revision 1.20.6.1

1.20.6.1! jym         1: /*     $NetBSD: talkd.c,v 1.21 2009/03/16 01:04:32 lukem Exp $ */
1.5       christos    2:
1.1       cgd         3: /*
1.5       christos    4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     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.
1.17      agc        15:  * 3. Neither the name of the University nor the names of its contributors
1.1       cgd        16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     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
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
1.5       christos   32: #include <sys/cdefs.h>
1.1       cgd        33: #ifndef lint
1.20      lukem      34: __COPYRIGHT("@(#) Copyright (c) 1983, 1993\
                     35:  The Regents of the University of California.  All rights reserved.");
1.1       cgd        36: #endif /* not lint */
                     37:
                     38: #ifndef lint
1.5       christos   39: #if 0
                     40: static char sccsid[] = "@(#)talkd.c    8.1 (Berkeley) 6/4/93";
                     41: #else
1.20.6.1! jym        42: __RCSID("$NetBSD: talkd.c,v 1.21 2009/03/16 01:04:32 lukem Exp $");
1.5       christos   43: #endif
1.1       cgd        44: #endif /* not lint */
                     45:
                     46: /*
                     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
                     51:  */
                     52: #include <sys/types.h>
                     53: #include <sys/socket.h>
1.4       explorer   54: #include <sys/param.h>
                     55:
1.1       cgd        56: #include <protocols/talkd.h>
1.8       mrg        57:
                     58: #include <errno.h>
                     59: #include <paths.h>
1.1       cgd        60: #include <signal.h>
1.8       mrg        61: #include <stdio.h>
                     62: #include <stdlib.h>
                     63: #include <string.h>
1.1       cgd        64: #include <syslog.h>
                     65: #include <time.h>
                     66: #include <unistd.h>
1.8       mrg        67:
1.6       christos   68: #include "extern.h"
1.1       cgd        69:
                     70: CTL_MSG                request;
                     71: CTL_RESPONSE   response;
                     72:
1.6       christos   73: int    sockt = STDIN_FILENO;
1.1       cgd        74: int    debug = 0;
1.9       mrg        75: int    logging = 0;
1.19      dholland   76: time_t lastmsgtime;
1.1       cgd        77:
1.4       explorer   78: char   hostname[MAXHOSTNAMELEN + 1];
1.1       cgd        79:
                     80: #define TIMEOUT 30
                     81: #define MAXIDLE 120
                     82:
1.20.6.1! jym        83: static void timeout(int);
        !            84: int    main(int, char *[]);
1.6       christos   85:
                     86: int
1.20.6.1! jym        87: main(int argc, char *argv[])
1.1       cgd        88: {
1.6       christos   89:        CTL_MSG *mp = &request;
1.9       mrg        90:        int cc, ch;
1.13      christos   91:        struct sockaddr ctl_addr;
1.1       cgd        92:
                     93:        openlog("talkd", LOG_PID, LOG_DAEMON);
1.9       mrg        94:        while ((ch = getopt(argc, argv, "dl")) != -1)
                     95:                switch (ch) {
                     96:                case 'd':
                     97:                        debug = 1;
                     98:                        break;
                     99:                case 'l':
                    100:                        logging = 1;
                    101:                        break;
                    102:                default:
1.12      cgd       103:                        syslog(LOG_ERR, "Usage: %s [-dl]", getprogname());
1.9       mrg       104:                        exit(1);
                    105:                }
                    106:
1.10      mrg       107:        if (gethostname(hostname, sizeof hostname) < 0) {
1.1       cgd       108:                syslog(LOG_ERR, "gethostname: %m");
                    109:                _exit(1);
                    110:        }
1.5       christos  111:        hostname[MAXHOSTNAMELEN] = '\0';  /* ensure null termination */
1.1       cgd       112:        if (chdir(_PATH_DEV) < 0) {
                    113:                syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
                    114:                _exit(1);
                    115:        }
                    116:        signal(SIGALRM, timeout);
                    117:        alarm(TIMEOUT);
                    118:        for (;;) {
1.14      itojun    119:                memset(&response, 0, sizeof(response));
1.1       cgd       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");
                    124:                        continue;
                    125:                }
1.13      christos  126:
1.14      itojun    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';
                    130:
1.16      itojun    131:                lastmsgtime = time(0);
                    132:                process_request(mp, &response);
                    133:
1.18      christos  134:                tsa2sa(&ctl_addr, &mp->ctl_addr);
1.14      itojun    135:                if (ctl_addr.sa_family != AF_INET)
                    136:                        continue;
1.13      christos  137:
1.1       cgd       138:                /* can block here, is this what I want? */
1.13      christos  139:                cc = sendto(sockt, (char *)&response, sizeof (response), 0,
                    140:                    &ctl_addr, sizeof (ctl_addr));
1.1       cgd       141:                if (cc != sizeof (response))
                    142:                        syslog(LOG_WARNING, "sendto: %m");
                    143:        }
                    144: }
                    145:
                    146: void
1.20.6.1! jym       147: timeout(int n)
1.1       cgd       148: {
1.14      itojun    149:        int save_errno = errno;
1.1       cgd       150:
                    151:        if (time(0) - lastmsgtime >= MAXIDLE)
                    152:                _exit(0);
                    153:        alarm(TIMEOUT);
1.14      itojun    154:        errno = save_errno;
1.1       cgd       155: }
1.18      christos  156:
                    157: void
                    158: tsa2sa(struct sockaddr *sa, const struct talkd_sockaddr *tsa)
                    159: {
                    160:        (void)memcpy(sa, tsa, sizeof(*tsa));
                    161:        sa->sa_len = sizeof(*tsa);
                    162:        sa->sa_family = tsa->sa_family;
                    163: }

CVSweb <webmaster@jp.NetBSD.org>