1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains the hostmanager client program.
4 * Created by: David C. Jedlinsky
6 * $Id: zhm.c,v 1.62 2000/05/19 23:19:21 zacheiss Exp $
8 * Copyright (c) 1987,1991 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, see the file
15 static const char rcsid_hm_c[] = "$Id: zhm.c,v 1.62 2000/05/19 23:19:21 zacheiss Exp $";
22 #define srandom srand48
25 #define PIDDIR "/var/athena/"
27 int hmdebug, rebootflag, noflushflag, errflg, dieflag, inetd, oldpid, nofork;
28 int no_server = 1, nservchang, nserv, nclt;
29 int booting = 1, timeout_type, deactivated = 1;
32 struct sockaddr_in cli_sin, serv_sin, from;
34 char **serv_list = NULL;
35 char prim_serv[MAXHOSTNAMELEN], cur_serv[MAXHOSTNAMELEN];
40 char hostname[MAXHOSTNAMELEN], loopback[4];
43 static RETSIGTYPE deactivate __P((void));
44 static RETSIGTYPE terminate __P((void));
45 static void choose_server __P((void));
46 static void init_hm __P((void));
47 static void detach __P((void));
48 static void send_stats __P((ZNotice_t *, struct sockaddr_in *));
49 static char *strsave __P((const char *));
52 static RETSIGTYPE deactivate()
57 static RETSIGTYPE terminate()
68 int opt, pak_len, i, j = 0, fd, count;
72 sprintf(PidFile, "%szhm.pid", PIDDIR);
74 if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
75 printf("Can't find my hostname?!\n");
79 while ((opt = getopt(argc, argv, "drhinf")) != EOF)
89 /* Reboot host -- send boot notice -- and exit */
93 /* inetd operation: don't do bind ourselves, fd 0 is
94 already connected to a socket. Implies -h */
110 fprintf(stderr, "Usage: %s [-d] [-h] [-r] [-n] [-f] [server]\n",
117 /* Override server argument? */
119 if ((hp = gethostbyname(argv[optind++])) == NULL) {
120 printf("Unknown server name: %s\n", argv[optind-1]);
122 strncpy(prim_serv, hp->h_name, sizeof(prim_serv));
123 prim_serv[sizeof(prim_serv) - 1] = '\0';
126 /* argc-optind is the # of other servers on the command line */
127 serv_list = (char **) malloc((argc - optind + 2) * sizeof(char *));
128 if (serv_list == NULL) {
129 printf("Out of memory.\n");
132 serv_list[numserv++] = prim_serv;
133 for (; optind < argc; optind++) {
134 if ((hp = gethostbyname(argv[optind])) == NULL) {
135 printf("Unknown server name '%s', ignoring\n", argv[optind]);
138 serv_list[numserv++] = strsave(hp->h_name);
140 serv_list[numserv] = NULL;
148 if (*prim_serv == '\0') {
149 printf("No valid primary server found, exiting.\n");
154 DPR2("zephyr server port: %u\n", ntohs(serv_sin.sin_port));
155 DPR2("zephyr client port: %u\n", ntohs(cli_port));
159 /* Wait for incoming packets or queue timeouts. */
160 DPR("Waiting for a packet...");
163 FD_SET(fd, &readers);
164 count = select(fd + 1, &readers, NULL, NULL, timer_timeout(&tv));
165 if (count == -1 && errno != EINTR) {
166 syslog(LOG_CRIT, "select() failed: %m");
179 send_flush_notice(HM_FLUSH);
187 ret = ZReceivePacket(packet, &pak_len, &from);
188 if ((ret != ZERR_NONE) && (ret != EINTR)){
190 com_err("hm", ret, "receiving notice");
191 } else if (ret != EINTR) {
192 /* Where did it come from? */
193 if ((ret = ZParseNotice(packet, pak_len, ¬ice))
196 com_err("hm", ret, "parsing notice");
198 DPR("Got a packet.\n");
200 DPR2("\tz_kind: %d\n", notice.z_kind);
201 DPR2("\tz_port: %u\n", ntohs(notice.z_port));
202 DPR2("\tz_class: %s\n", notice.z_class);
203 DPR2("\tz_class_inst: %s\n", notice.z_class_inst);
204 DPR2("\tz_opcode: %s\n", notice.z_opcode);
205 DPR2("\tz_sender: %s\n", notice.z_sender);
206 DPR2("\tz_recip: %s\n", notice.z_recipient);
207 DPR2("\tz_def_format: %s\n", notice.z_default_format);
208 DPR2("\tz_message: %s\n", notice.z_message);
209 if (memcmp(loopback, &from.sin_addr, 4) &&
210 ((notice.z_kind == SERVACK) ||
211 (notice.z_kind == SERVNAK) ||
212 (notice.z_kind == HMCTL))) {
213 server_manager(¬ice);
215 if (!memcmp(loopback, &from.sin_addr, 4) &&
216 ((notice.z_kind == UNSAFE) ||
217 (notice.z_kind == UNACKED) ||
218 (notice.z_kind == ACKED) ||
219 (notice.z_kind == HMCTL))) {
220 /* Client program... */
222 send_boot_notice(HM_BOOT);
225 transmission_tower(¬ice, packet, pak_len);
226 DPR2("Pending = %d\n", ZPending());
228 if (notice.z_kind == STAT) {
229 send_stats(¬ice, &from);
232 "Unknown notice type: %d",
243 static void choose_server()
246 char **clust_info, **cpp;
251 /* Free up any previously used resources */
255 free(serv_list[i++]);
262 if ((clust_info = hes_resolve(hostname, "CLUSTER")) == NULL) {
265 for (cpp = clust_info; *cpp; cpp++) {
266 /* Remove the following check once we have changed over to
267 * new Hesiod format (i.e. ZCLUSTER.sloc lookup, no primary
270 if (!strncasecmp("ZEPHYR", *cpp, 6)) {
273 if ((c = strchr(*cpp, ' ')) == 0) {
274 printf("Hesiod error getting primary server info.\n");
276 strncpy(prim_serv, c+1, sizeof(prim_serv));
277 prim_serv[sizeof(prim_serv) - 1] = '\0';
281 if (!strncasecmp("ZCLUSTER", *cpp, 9)) {
284 if ((c = strchr(*cpp, ' ')) == 0) {
285 printf("Hesiod error getting zcluster info.\n");
287 if ((zcluster = malloc((unsigned)(strlen(c+1)+1)))
289 strcpy(zcluster, c+1);
291 printf("Out of memory.\n");
298 for (cpp = clust_info; *cpp; cpp++)
302 if (zcluster == NULL) {
303 if ((zcluster = malloc((unsigned)(strlen("zephyr")+1))) != NULL)
304 strcpy(zcluster, "zephyr");
306 printf("Out of memory.\n");
310 while ((serv_list = hes_resolve(zcluster, "sloc")) == (char **)NULL) {
311 syslog(LOG_ERR, "No servers or no hesiod");
312 /* wait a bit, and try again */
315 cpp = (char **) malloc(2 * sizeof(char *));
317 printf("Out of memory.\n");
321 cpp[numserv++] = prim_serv;
322 for (i = 0; serv_list[i]; i++) {
323 /* copy in non-duplicates */
324 /* assume the names returned in the sloc are full domain names */
325 if (!prim_serv[0] || strcasecmp(prim_serv, serv_list[i])) {
326 cpp = (char **) realloc(cpp, (numserv+2) * sizeof(char *));
328 printf("Out of memory.\n");
331 cpp[numserv++] = strsave(serv_list[i]);
334 for (i = 0; serv_list[i]; i++)
341 if (!prim_serv[0] && numserv) {
343 strncpy(prim_serv, serv_list[random() % numserv], sizeof(prim_serv));
344 prim_serv[sizeof(prim_serv) - 1] = '\0';
348 static void init_hm()
353 #ifdef _POSIX_VERSION
357 starttime = time((time_t *)0);
358 OPENLOG("hm", LOG_PID, LOG_DAEMON);
360 ZSetServerState(1); /* Aargh!!! */
361 if ((ret = ZInitialize()) != ZERR_NONE) {
363 com_err("hm", ret, "initializing");
369 if (*prim_serv == '\0') {
370 strncpy(prim_serv, *serv_list, sizeof(prim_serv));
371 prim_serv[sizeof(prim_serv) - 1] = '\0';
380 ZSetFD(0); /* fd 0 is on the socket, thanks to inetd */
382 /* Open client socket, for receiving client and server notices */
383 sp = getservbyname(HM_SVCNAME, "udp");
384 cli_port = (sp) ? sp->s_port : HM_SVC_FALLBACK;
386 if ((ret = ZOpenPort(&cli_port)) != ZERR_NONE) {
388 com_err("hm", ret, "opening port");
392 cli_sin = ZGetDestAddr();
394 sp = getservbyname(SERVER_SVCNAME, "udp");
395 memset(&serv_sin, 0, sizeof(struct sockaddr_in));
396 serv_sin.sin_port = (sp) ? sp->s_port : SERVER_SVC_FALLBACK;
399 if (!inetd && !nofork)
402 /* Write pid to file */
403 fp = fopen(PidFile, "w");
405 fprintf(fp, "%d\n", getpid());
411 syslog(LOG_INFO, "Debugging on.");
414 /* Set up communications with server */
415 /* target is SERVER_SVCNAME port on server machine */
417 serv_sin.sin_family = AF_INET;
420 if ((hp = gethostbyname(prim_serv)) == NULL) {
421 DPR("gethostbyname failed\n");
422 find_next_server(NULL);
424 DPR2("Server = %s\n", prim_serv);
425 strncpy(cur_serv, prim_serv, sizeof(cur_serv));
426 cur_serv[sizeof(cur_serv) - 1] = '\0';
427 memcpy(&serv_sin.sin_addr, hp->h_addr, 4);
430 send_boot_notice(HM_BOOT);
433 #ifdef _POSIX_VERSION
434 sigemptyset(&sa.sa_mask);
436 sa.sa_handler = deactivate;
437 sigaction(SIGHUP, &sa, (struct sigaction *)0);
438 sa.sa_handler = terminate;
439 sigaction(SIGTERM, &sa, (struct sigaction *)0);
441 signal(SIGHUP, deactivate);
442 signal(SIGTERM, terminate);
448 /* detach from terminal and fork. */
449 register int i, x = ZGetFD();
457 #ifdef _POSIX_VERSION
458 size = sysconf(_SC_OPEN_MAX);
460 size = getdtablesize();
462 for (i = 0; i < size; i++)
466 if ((i = open("/dev/tty", O_RDWR, 0666)) < 0)
467 ; /* Can't open tty, but don't flame about it. */
470 /* Necessary for old non-POSIX systems which automatically assign
471 * an opened tty as the controlling terminal of a process which
472 * doesn't already have one. POSIX systems won't include
473 * <sys/ioctl.h> (see ../h/sysdep.h); if TIOCNOTTY is defined anyway,
474 * this is unnecessary but won't hurt. */
475 ioctl(i, TIOCNOTTY, (caddr_t) 0);
479 #ifdef _POSIX_VERSION
484 static char version[BUFSIZ];
486 static void send_stats(notice, sin)
488 struct sockaddr_in *sin;
494 int len, i, nitems = 10;
499 if ((ret = ZSetDestAddr(sin)) != ZERR_NONE) {
501 com_err("hm", ret, "setting destination");
503 newnotice.z_kind = HMACK;
505 list[0] = (char *) malloc(MAXHOSTNAMELEN);
506 if (list[0] == NULL) {
507 printf("Out of memory.\n");
510 strcpy(list[0], cur_serv);
511 list[1] = (char *) malloc(64);
512 if (list[1] == NULL) {
513 printf("Out of memory.\n");
516 sprintf(list[1], "%d", queue_len());
517 list[2] = (char *) malloc(64);
518 if (list[2] == NULL) {
519 printf("Out of memory.\n");
522 sprintf(list[2], "%d", nclt);
523 list[3] = (char *) malloc(64);
524 if (list[3] == NULL) {
525 printf("Out of memory.\n");
528 sprintf(list[3], "%d", nserv);
529 list[4] = (char *) malloc(64);
530 if (list[4] == NULL) {
531 printf("Out of memory.\n");
534 sprintf(list[4], "%d", nservchang);
535 list[5] = (char *) malloc(64);
536 if (list[5] == NULL) {
537 printf("Out of memory.\n");
540 strncpy(list[5], rcsid_hm_c, 64);
543 list[6] = (char *) malloc(64);
544 if (list[6] == NULL) {
545 printf("Out of memory.\n");
549 sprintf(list[6], "yes");
551 sprintf(list[6], "no");
552 list[7] = (char *) malloc(64);
553 if (list[7] == NULL) {
554 printf("Out of memory.\n");
557 sprintf(list[7], "%ld", time((time_t *)0) - starttime);
559 size = (unsigned long)sbrk(0);
564 list[8] = (char *)malloc(64);
565 if (list[8] == NULL) {
566 printf("Out of memory.\n");
569 sprintf(list[8], "%ld", size);
570 list[9] = (char *)malloc(32);
571 if (list[9] == NULL) {
572 printf("Out of memory.\n");
575 strncpy(list[9], MACHINE_TYPE, 32);
578 /* Since ZFormatRaw* won't change the version number on notices,
579 we need to set the version number explicitly. This code is taken
580 from Zinternal.c, function Z_FormatHeader */
582 sprintf(version, "%s%d.%d", ZVERSIONHDR, ZVERSIONMAJOR,
584 newnotice.z_version = version;
586 if ((ret = ZFormatRawNoticeList(&newnotice, list, nitems, &bfr,
587 &len)) != ZERR_NONE) {
588 syslog(LOG_INFO, "Couldn't format stats packet");
590 if ((ret = ZSendPacket(bfr, len, 0)) != ZERR_NONE) {
592 com_err("hm", ret, "sending stats");
596 for(i=0;i<nitems;i++)
600 void die_gracefully()
602 syslog(LOG_INFO, "Terminate signal caught...");
603 send_flush_notice(HM_FLUSH);
609 static char *strsave(sp)
614 if((ret = malloc((unsigned) strlen(sp)+1)) == NULL) {