DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ T p

⟦423570cd1⟧ TextFile

    Length: 2592 (0xa20)
    Types: TextFile
    Names: »ping.c«

Derivation

└─⟦87ddcff64⟧ Bits:30001253 CPHDIST85 Tape, 1985 Autumn Conference Copenhagen
    └─ ⟦this⟧ »cph85dist/rman/daemon/ping.c« 

TextFile

#ifndef lint
static char RCSid[] = "$Header: ping.c,v 1.5 85/08/27 15:17:08 broome Exp $";
#endif

/*
 * $Log:    ping.c,v $
 * Revision 1.5  85/08/27  15:17:08  broome
 * Last cleanup before release.
 * 
 * Revision 1.4  85/08/04  16:36:11  broome
 * Added load cutoff, such that if 1 minute load is above this, we
 * won't respond to pings from clients.
 * 
 * Revision 1.3  85/07/24  10:39:03  broome
 * 
 * Revision 1.2  85/07/18  12:05:20  broome
 * 
 * Revision 1.1  85/07/16  11:10:26  broome
 * Initial revision
 */

/*
 *  Routines to handle `ping' calls on a dgram socket.
 */

#include <syslog.h>
#include <nlist.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

struct  nlist nl[] = {
    { "_avenrun" },
    { 0 },
};

int     sock;            /* the datagram socket descriptor */
int     kmem;            /* and memory file descriptor     */
double  cutoff = -1.0;   /* don't respond if 1min load is above this */

/*
 *  Initialize everything ...
 */

open_ping (port)
int   port;
{
    struct  sockaddr_in sin;

    if ((sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {
        syslog (LOG_ERR, "cannot open ping (datagram) socket: %m");
        return (-1);
    }

    bzero ((char *)&sin, sizeof (sin));
    sin.sin_port = port;         /* should probably do a `getservice' */
    sin.sin_family = AF_INET;
    sin.sin_addr.s_addr = INADDR_ANY;

    if (bind (sock, (char *)&sin, sizeof (sin)) < 0) {
        syslog (LOG_ERR, "cannot bind ping socket: %m");
        return (-1);
    }

    if ((kmem = open ("/dev/kmem", 0, 0)) < 0) {
        syslog (LOG_ERR, "No kmem: %m");
        return (-1);
    }

    nlist ("/vmunix", nl);
    if (nl[0].n_type == 0) {
        syslog (LOG_ERR, "No namelist: %m");
        return (-1);
    }
    return (sock);
}

/*
 *  Routine to actually handle `ping' packets sent to the datagram socket.
 *  Finds the load average and sends it back to the sending socket.
 */

ping ()
{
    struct  sockaddr_in from;
    double  avenrun[3];
    char    buf[20];

    /* grab the load average */
    lseek (kmem, (long) nl[0].n_value, 0);
    read (kmem, avenrun, sizeof (avenrun));

    if (cutoff != -1.0 && avenrun[0] > cutoff)  /* load's too high */
        return;

    /* don't want to see what they sent, just need their address */
    if (recvfrom (sock, buf, 20, 0, &from, sizeof (from)) < 1)
        return;

    sprintf (buf,"%.2f %.2f %.2f\n", avenrun[0], avenrun[1], avenrun[2]);

    /* and send it on over */
    if (sendto (sock, buf, strlen (buf), 0, &from, sizeof (from)) < 0)
        perror ("sendto");
}