summaryrefslogtreecommitdiff
path: root/zhm/zhm.c
blob: 7e7c4acd7ada9ac7cbd0ecd42d0fa73eb3276d78 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/* This file is part of the Project Athena Zephyr Notification System.
 * It contains the hostmanager client program.
 *
 *      Created by:     David C. Jedlinsky
 *
 *      $Source$
 *      $Author$
 *
 *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h". 
 */

#include "hm.h"

static char rcsid_hm_c[] = "$Header$";

#include <ctype.h>
#include <signal.h>
#include <hesiod.h>
#include <sys/ioctl.h>
#include <sys/file.h>

int hmdebug = 0; /* kerberos stole variable called 'debug' */
int no_server = 1, nservchang = 0, nserv = 0, nclt = 0;
int booting = 1, timeout_type = 0, deactivated = 1;
long starttime;
u_short cli_port;
struct sockaddr_in cli_sin, serv_sin, from;
char **serv_list, **cur_serv_list;
char prim_serv[MAXHOSTNAMELEN], cur_serv[MAXHOSTNAMELEN];
int sig_type = 0;
struct hostent *hp;
char **clust_info;
char hostname[MAXHOSTNAMELEN], loopback[4];
char *PidFile = "/etc/athena/hm.pid";

extern int errno;
extern char *index();

void init_hm(), detach(), handle_timeout(), resend_notices(), die_gracefully();
void set_sig_type();
char *upcase();

main(argc, argv)
char *argv[];
{
    ZPacket_t packet;
    ZNotice_t notice;
    Code_t ret;
    int pak_len;
    extern int optind;

    /* Override server argument? */
    if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
	  printf("Can't find my hostname?!\n");
	  exit(-1);
    }
    *prim_serv = NULL;
    if (getopt(argc, argv, "d") != EOF)
      hmdebug++;
    if (optind <= argc) {
	  (void)strcpy(prim_serv, argv[optind]);
	  if ((hp = gethostbyname(prim_serv)) == NULL) {
		printf("Unknown server name: %s\n", prim_serv);
		*prim_serv = NULL;
	  }
    }
    if (*prim_serv == NULL) {
	  if ((clust_info = hes_resolve(hostname, "CLUSTER")) == NULL) {
		printf("No hesiod information available.\n");
		exit(ZERR_SERVNAK);
	  }
	  for ( ; *clust_info; clust_info++)
	    if (!strncmp("ZEPHYR", upcase(*clust_info), 6)) {
		  register char *c;

		  if ((c = index(*clust_info, ' ')) == 0) {
			printf("Hesiod error getting cluster info.\n");
		  } else
		    (void)strcpy(prim_serv, c+1);
		  break;
	    }
    }
    
    init_hm();

    DPR2 ("zephyr server port: %u\n", ntohs(serv_sin.sin_port));
    DPR2 ("zephyr client port: %u\n", ntohs(cli_port));
    
    /* Main loop */
    for ever {
	  DPR ("Waiting for a packet...");
	  switch(sig_type) {
	      case 0:
		break;
	      case SIGHUP:
		sig_type = 0;
		syslog(LOG_INFO, "Flushing this client...");
		send_flush_notice(HM_FLUSH);
		deactivated = 1;
		break;
	      case SIGTERM:
		sig_type = 0;
		die_gracefully();
		break;
	      case SIGALRM:
		sig_type = 0;
		handle_timeout();
		break;
	      default:
		sig_type = 0;
		syslog (LOG_INFO, "Unknown system interrupt.");
		break;
	  }
	  ret = ZReceivePacket(packet, Z_MAXPKTLEN, &pak_len, &from);
	  if ((ret != ZERR_NONE) && (ret != EINTR)){
		Zperr(ret);
		com_err("hm", ret, "receiving notice");
	  } else if (ret != EINTR) {
		/* Where did it come from? */
		if ((ret = ZParseNotice(packet, pak_len, &notice))
		    != ZERR_NONE) {
		      Zperr(ret);
		      com_err("hm", ret, "parsing notice");
		} else {
		      DPR ("Got a packet.\n");
		      DPR ("notice:\n");
		      DPR2("\tz_kind: %d\n", notice.z_kind);
		      DPR2("\tz_port: %u\n", ntohs(notice.z_port));
		      DPR2("\tz_class: %s\n", notice.z_class);
		      DPR2("\tz_class_inst: %s\n", notice.z_class_inst);
		      DPR2("\tz_opcode: %s\n", notice.z_opcode);
		      DPR2("\tz_sender: %s\n", notice.z_sender);
		      DPR2("\tz_recip: %s\n", notice.z_recipient);
		      DPR2("\tz_def_format: %s\n", notice.z_default_format);
		      if ((bcmp(loopback, &from.sin_addr, 4) != 0) &&
			  ((notice.z_kind == SERVACK) ||
			   (notice.z_kind == SERVNAK) ||
			   (notice.z_kind == HMCTL))) {
			    server_manager(&notice);
		      } else {
			    if ((bcmp(loopback, &from.sin_addr, 4) == 0) &&
				((notice.z_kind == UNSAFE) ||
				 (notice.z_kind == UNACKED) ||
				 (notice.z_kind == ACKED) ||
				 (notice.z_kind == HMCTL))) {
				  /* Client program... */
				  if (deactivated) {
					send_boot_notice(HM_BOOT);
					deactivated = 0;
				  }
				  transmission_tower(&notice, packet, pak_len);
				  DPR2 ("Pending = %d\n", ZPending());
			    } else {
				  if (notice.z_kind == STAT) {
					send_stats(&notice, &from);
				  } else {
					syslog(LOG_INFO,
					       "Unknown notice type: %d",
					       notice.z_kind);
				  }
			    }
		      }
		}
	  }
    }
}

void init_hm()
{
      struct servent *sp;
      Code_t ret;
      FILE *fp;

      starttime = time(0);
      openlog("hm", LOG_PID, LOG_DAEMON);

      if ((ret = ZInitialize()) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "initializing");
	    closelog();
	    exit(-1);
      }
      (void)ZSetServerState(1);  /* Aargh!!! */
      init_queue();
      if ((serv_list = hes_resolve("zephyr", "sloc")) == (char **)NULL) {
	    syslog(LOG_ERR, "No servers or no hesiod");
	    serv_list = (char **)malloc(2 * sizeof(char *));
	    serv_list[0] = (char *)malloc(MAXHOSTNAMELEN);
	    (void)strcpy(serv_list[0], prim_serv);
	    serv_list[1] = "";
	    if (*prim_serv == NULL) {
		  printf("No hesiod, no valid server found, exiting.\n");
		  exit(ZERR_SERVNAK);
	    }
      }
      cur_serv_list = serv_list;
      if (*prim_serv == NULL)
	(void)strcpy(prim_serv, *cur_serv_list);
      
      loopback[0] = 127;
      loopback[1] = 0;
      loopback[2] = 0;
      loopback[3] = 1;
      
      /* Open client socket, for receiving client and server notices */
      
      if ((sp = getservbyname("zephyr-hm", "udp")) == NULL) {
	    printf("No zephyr-hm entry in /etc/services.\n");
	    exit(1);
      }
      cli_port = sp->s_port;
      
      if ((ret = ZOpenPort(&cli_port)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "opening port");
	    exit(ret);
      }
      cli_sin = ZGetDestAddr();
      cli_sin.sin_port = sp->s_port;
      
      /* Open the server socket */
      
      if ((sp = getservbyname("zephyr-clt", "udp")) == NULL) {
	    printf("No zephyr-clt entry in /etc/services.\n");
	    exit(1);
      }

#ifndef DEBUG
      detach();

      /* Write pid to file */
      fp = fopen(PidFile, "w");
      if (fp != NULL) {
	    fprintf(fp, "%d\n", getpid());
	    (void) fclose(fp);
      }
#endif DEBUG

      if (hmdebug)
	syslog(LOG_DEBUG, "Debugging on.");

      bzero(&serv_sin, sizeof(struct sockaddr_in));
      serv_sin.sin_port = sp->s_port;
      
      /* Set up communications with server */
      /* target is "zephyr-clt" port on server machine */

      serv_sin.sin_family = AF_INET;

      /* who to talk to */
      if ((hp = gethostbyname(prim_serv)) == NULL) {
	    DPR("gethostbyname failed\n");
	    find_next_server(NULL);
      } else {
	    DPR2 ("Server = %s\n", prim_serv);
	    (void)strcpy(cur_serv, prim_serv);
	    bcopy(hp->h_addr, &serv_sin.sin_addr, hp->h_length);
      }

      send_boot_notice(HM_BOOT);
      deactivated = 0;

      (void)signal (SIGHUP,  set_sig_type);
      (void)signal (SIGALRM, set_sig_type);
      (void)signal (SIGTERM, set_sig_type);
}

char *upcase(s)
     register char *s;
{
      char *r = s;

      for (; *s; s++)
	if (islower(*s)) *s = toupper(*s);
      return(r);
}

void detach()
{
        /* detach from terminal and fork. */
        register int i, x = ZGetFD(), size = getdtablesize();

        if (i = fork()) {
                if (i < 0)
                        perror("fork");
                exit(0);
        }

        for (i = 0; i < size; i++)
	  if (i != x)
	    (void) close(i);

        if ((i = open("/dev/tty", O_RDWR, 666)) < 0)
	  syslog(LOG_ERR, "Cannot open /dev/tty to detach.");
	else {
	      (void) ioctl(i, TIOCNOTTY, (caddr_t) 0);
	      (void) close(i);
	}
}

void set_sig_type(sig)
     int sig;
{
      sig_type = sig;
}

send_stats(notice, sin)
     ZNotice_t *notice;
     struct sockaddr_in *sin;
{
      Code_t ret;
      ZPacket_t bfr;
      char *list[20];
      int len, i, nitems = 10;

      if ((ret = ZSetDestAddr(sin)) != ZERR_NONE) {
	    Zperr(ret);
	    com_err("hm", ret, "setting destination");
      }
      notice->z_kind = HMACK;

      list[0] = (char *)malloc(MAXHOSTNAMELEN);
      (void)strcpy(list[0], cur_serv);
      list[1] = (char *)malloc(64);
      sprintf(list[1], "%d", queue_len());
      list[2] = (char *)malloc(64);
      sprintf(list[2], "%d", nclt);
      list[3] = (char *)malloc(64);
      sprintf(list[3], "%d", nserv);
      list[4] = (char *)malloc(64);
      sprintf(list[4], "%d", nservchang);
      list[5] = (char *)malloc(64);
      (void)strcpy(list[5], rcsid_hm_c);
      list[6] = (char *)malloc(64);
      if (no_server)
	sprintf(list[6], "yes");
      else
	sprintf(list[6], "no");
      list[7] = (char *)malloc(64);
      sprintf(list[7], "%ld", time(0) - starttime);
      list[8] = (char *)malloc(64);
      sprintf(list[8], "%ld", sbrk(0));
      list[9] = (char *)malloc(32);
      (void)strcpy(list[9], MACHINE);

      if ((ret = ZFormatRawNoticeList(notice, list, nitems, bfr,
				      Z_MAXPKTLEN, &len)) != ZERR_NONE) {
	    syslog(LOG_INFO, "Couldn't format stats packet");
      } else
	if ((ret = ZSendPacket(bfr, len)) != ZERR_NONE) {
	      Zperr(ret);
	      com_err("hm", ret, "sending stats");
	}
      for(i=0;i<nitems;i++)
	free(list[i]);
}

void handle_timeout()
{
      switch(timeout_type) {
	  case BOOTING:
	    new_server(NULL);
	    break;
	  case NOTICES:
	    DPR ("Notice timeout\n");
	    resend_notices(&serv_sin);
	    break;
	  default:
	    syslog (LOG_ERR, "Unknown timeout type: %d\n", timeout_type);
	    break;
      }
}

void die_gracefully()
{
      syslog(LOG_INFO, "Terminate signal caught...");
      send_flush_notice(HM_FLUSH);
      unlink(PidFile);
      closelog();
      exit(0);
}