summaryrefslogtreecommitdiffstats
path: root/loadavgd.c
blob: f9cffffb1e54e418ce6c4870c591b09bd1ee146b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 1997 Transmeta Corporation - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version.
 *   
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 * ----------------------------------------------------------------------- */

/* loadavgd.c - load average reporting daemon (UDP) */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <errno.h>

#include "rxload_inet.h"

void init_loadavg();
double get_loadavg();

char *ProgramName;

void server(port,timeout)
     char *port;
     int timeout;
{
  struct sockaddr_in  saddr;
  struct sockaddr     fromaddr;
  struct servent     *serv;
  int sockfd, addrlen;
  int socktype, typelen;
  char buffer[64];
  double loadavg;
  int inet_flag;

  /* First find out if we were spawned by inetd.  If so, we should have
     a datagram socket on stdin. */
  typelen = sizeof(int);
  if ( getsockopt(0, SOL_SOCKET, SO_TYPE, &socktype, &typelen) == 0
       /* && typelen == sizeof(int) && socktype == SOCK_DGRAM */ ) {
    inet_flag = 1;
    sockfd = 0;			/* This was easy */
    close(1);
    close(2);
  } else {
    inet_flag = 0;

    bzero((char *) &saddr, sizeof(saddr));

    if ( port ) {
      serv = getservbyname(port, "udp");
      if ( serv )
	saddr.sin_port = serv->s_port;
      else
	saddr.sin_port = htons(atoi(port));
    } else {
      serv = getservbyname(DEFAULT_SERVICE, "udp");
      if ( serv )
	saddr.sin_port = serv->s_port;
      else
	saddr.sin_port = htons(DEFAULT_PORT);
    }
    
    if ( !saddr.sin_port ) {
      fprintf(stderr, "%s: bad port\n", ProgramName);
      exit(12);
    }
    
    saddr.sin_family      = AF_INET;
    saddr.sin_addr.s_addr = htonl(INADDR_ANY);
    
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
      perror(ProgramName);
      exit(13);
    }
    if ( bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0 ) {
      fprintf(stderr, "%s: cannot bind to server port\n", ProgramName);
      exit(14);
    }
  }

  while(1) {
    if ( inet_flag && timeout > 0 ) {
      struct timeval tv;
      fd_set sockset;
      
      tv.tv_sec = timeout;
      tv.tv_usec = 0;
      
      FD_ZERO(&sockset);
      FD_SET(sockfd,&sockset);
      
      if ( select(sockfd+1, &sockset, NULL, NULL, &tv) < 0 &&
	      errno == EINTR )
	continue;
      
      if ( !FD_ISSET(sockfd,&sockset) )
	break;
    }
    
    addrlen = sizeof(struct sockaddr);
    if ( recvfrom(sockfd, buffer, 64, 0, &fromaddr, &addrlen) < 0 )
      break;
   
    /* The first 8 bytes of the packet are returned in the reply;
       reserved for client matching requests with replies */

    bzero(buffer+8, 64-8);
    loadavg = get_loadavg();
    if ( loadavg >= 0.0 ) {
      sprintf(buffer+8, "%g", loadavg);
      sendto(sockfd, buffer, 64, 0, &fromaddr, addrlen);
    }
  }
  
  close(sockfd);
}


int main(argc,argv)
     int argc;
     char *argv[];
{
  char *port = NULL;
  int timeout = 900;
  int i;

  ProgramName = argv[0];

  for ( i = 1 ; i < argc ; i++ ) {
    if ( !strcmp("-port", argv[i]) && i != argc-1 ) {
      port = argv[++i];
    } else if ( !strcmp("-timeout", argv[i]) && i != argc-1 ) {
      timeout = atoi(argv[++i]);
    } else {
      fprintf(stderr, "Usage: %s [-port port] [-timeout timeout]\n", argv[0]);
      exit(15);
    }
  }

  init_loadavg();
  server(port,timeout);
  exit(0);
}