]> andersk Git - moira.git/blobdiff - lib/critical.c
Build shared libmoira via libtool.
[moira.git] / lib / critical.c
index 3e84ad21c5ab443db8b5866d73c5fe159770e78c..f4ef4b2d90af3b0bd853ef4412daa0961484198b 100644 (file)
-/* $Header$
+/* $Id$
  *
  * Log and send a zephyrgram about any critical errors.
+ *
+ * (c) Copyright 1988-1998 by the Massachusetts Institute of Technology.
+ * For copying and distribution information, please see the file
+ * <mit-copyright.h>.
  */
 
-#include <stdio.h>
-#include <sys/file.h>
+
+#ifdef HAVE_ZEPHYR
+/* need to include before moira.h, which includes krb_et.h, because
+   zephyr.h is broken */
 #include <zephyr/zephyr.h>
+/* zephyr.h doesn't prototype this */
+extern Code_t ZSendNotice(ZNotice_t *notice, Z_AuthProc cert_routine);
+#endif
 
+#include <mit-copyright.h>
+#include <moira.h>
+#include <moira_site.h>
 
-/* log file for critical events that require human intervention */
-#define CRITERRLOG     "/u1/sms/critical.log"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#ifndef HAVE_ZEPHYR
+#include <syslog.h>
+#endif
+#include <time.h>
+
+RCSID("$Header$");
 
 /* mode to create the file with */
 #define LOGFILEMODE    0644
 
+/* This routine sends a class MOIRA zephyrgram of specified instance
+ * and logs to a special logfile the message passed to it via msg
+ * and args in printf format.  *** It expects the global variable
+ * whoami to be defined and contain the name of the calling program.
+ */
 
-void critical_alert(instance, msg, args)
-  char *instance;              /* Instance for zephyr gram */
-  char *msg;                   /* printf format message */
-                               /* args = arguements, printf style */
-  /* This routine sends a class SMS zephyrgram of specified instance
-     and logs to a special logfile the message passed to it via msg
-     and args in printf format.  *** It expects the global variable
-     whoami to be defined and contain the name of the calling program. */
-  /* Note: The part of this code that process the variable arguements
-     was stolen from sprintf(). */
+void critical_alert(char *whoami, char *instance, char *msg, ...)
 {
-    FILE _bufstr;              /* For _doprnt() */
-    FILE *crit;                        /* FILE for critical log file */
-    char buf[BUFSIZ];          /* Holds the formatted message */
-
-    /* Put the fully formatted message into buf */
-    _bufstr._flag = _IOWRT + _IOSTRG;
-    _bufstr._ptr = buf;
-    _bufstr._cnt = BUFSIZ;
-    _doprnt(msg, &args, &_bufstr);
-    putc('\0', &_bufstr);
-
-    /* Send zephyr notice */
-    send_zgram(instance, buf);
-
-    /* Log message to critical file */
-    if ((crit = fopen(CRITERRLOG, "a")) != (FILE *)NULL) 
-    {
-       long t;
-       char  *time_s;
+  FILE *crit;
+  char *buf;
+  va_list ap;
+  long start;
 
-       time(&t);
-       time_s = ctime(&t) + 4;
-       time_s[strlen(time_s)-6] = '\0';
-
-       fprintf(crit, "%s <%d> %s\n", time_s, getpid(), buf);
-       fclose(crit);
+  /* Log message to critical file */
+  if ((crit = fopen(CRITERRLOG, "a")))
+    {
+      time_t t;
+      char *time_s;
+
+      time(&t);
+      time_s = ctime(&t) + 4;
+      time_s[strlen(time_s) - 6] = '\0';
+
+      fprintf(crit, "%s <%ld>", time_s, (long)getpid());
+      start = ftell(crit);
+      va_start(ap, msg);
+      vfprintf(crit, msg, ap);
+      va_end(ap);
+      fprintf(crit, "\n");
+
+      buf = malloc(ftell(crit) - start);
+      fclose(crit);
+
+      if (buf)
+       {
+         va_start(ap, msg);
+         vsprintf(buf, msg, ap);
+         va_end(ap);
+
+         send_zgram(instance, buf);
+         com_err(whoami, 0, buf);
+
+         free(buf);
+       }
     }
 
-    com_err(whoami, 0, buf);
+  if (!crit || !buf)
+    {
+      send_zgram(instance, "Couldn't format critical syslog!");
+      com_err(whoami, 0, "Couldn't format critical syslog!");
+    }
 }
 
 
-
-/* Sends a zephyrgram of class "SMS", instance as a parameter.  Ignores
+/* Sends a zephyrgram of class "MOIRA", instance as a parameter.  Ignores
  * errors while sending message.
  */
 
-send_zgram(inst, msg)
-char *inst;
-char *msg;
+void send_zgram(char *inst, char *msg)
 {
-    ZNotice_t znotice;
-
-    bzero (&znotice, sizeof (znotice));
-    znotice.z_kind = UNSAFE;
-    znotice.z_class = "SMS";
-    znotice.z_class_inst = inst;
-    znotice.z_default_format = "SMS $instance:\n $message\n";
-    (void) ZInitialize ();
-    znotice.z_message = msg;
-    znotice.z_message_len = strlen(msg) + 1;
-    znotice.z_opcode = "";
-    znotice.z_recipient = "";
-    ZSendNotice(&znotice, ZNOAUTH);
+#ifdef HAVE_ZEPHYR
+  ZNotice_t znotice;
+
+  memset(&znotice, 0, sizeof(znotice));
+  znotice.z_kind = UNSAFE;
+  znotice.z_class = "MOIRA";
+  znotice.z_class_inst = inst;
+  znotice.z_default_format = "MOIRA $instance on $fromhost:\n $message\n";
+  ZInitialize();
+  znotice.z_message = msg;
+  znotice.z_message_len = strlen(msg) + 1;
+  znotice.z_opcode = "";
+  znotice.z_recipient = "";
+  ZSendNotice(&znotice, ZNOAUTH);
+#else
+  char *buf;
+
+  buf = malloc(9 + strlen(inst) + strlen(msg));
+  if (buf)
+    {
+      sprintf(buf, "MOIRA: %s %s", inst, msg);
+      syslog(LOG_ERR, "%s", buf);
+      free(buf);
+    }
+#endif
 }
This page took 0.072743 seconds and 4 git commands to generate.