]> andersk Git - moira.git/blame - lib/critical.c
don't call the callback if it's NULL...
[moira.git] / lib / critical.c
CommitLineData
fa59b86f 1/* $Id$
96d806a4 2 *
3 * Log and send a zephyrgram about any critical errors.
babbc197 4 *
7ac48069 5 * (c) Copyright 1988-1998 by the Massachusetts Institute of Technology.
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
96d806a4 8 */
9
7ac48069 10
2f4a1cd0 11#ifdef ZEPHYR
7ac48069 12/* need to include before moira.h, which includes krb_et.h, because
13 zephyr.h is broken */
96d806a4 14#include <zephyr/zephyr.h>
7ac48069 15/* zephyr.h doesn't prototype this */
16extern Code_t ZSendNotice(ZNotice_t *notice, Z_AuthProc cert_routine);
2f4a1cd0 17#endif
7ac48069 18
19#include <mit-copyright.h>
20#include <moira.h>
21#include <moira_site.h>
22
d5aa7842 23#include <stdio.h>
7ac48069 24#include <string.h>
2f4a1cd0 25#ifdef SYSLOG
26#include <syslog.h>
27#endif
7ac48069 28
29RCSID("$Header$");
96d806a4 30
96d806a4 31/* mode to create the file with */
32#define LOGFILEMODE 0644
33
5a77c71e 34extern char *whoami;
35
8defc06b 36/* This routine sends a class MOIRA zephyrgram of specified instance
955e02e2 37 * and logs to a special logfile the message passed to it via msg
38 * and args in printf format. *** It expects the global variable
39 * whoami to be defined and contain the name of the calling program.
955e02e2 40 */
41
7ac48069 42void critical_alert(char *instance, char *msg, ...)
96d806a4 43{
a3bbcc2b 44 FILE *crit;
45 char *buf;
7ac48069 46 va_list ap;
a3bbcc2b 47 long start;
96d806a4 48
5eaef520 49 /* Log message to critical file */
50 if ((crit = fopen(CRITERRLOG, "a")))
96d806a4 51 {
5eaef520 52 time_t t;
53 char *time_s;
3f6b08ec 54
5eaef520 55 time(&t);
56 time_s = ctime(&t) + 4;
57 time_s[strlen(time_s) - 6] = '\0';
3f6b08ec 58
a3bbcc2b 59 fprintf(crit, "%s <%ld>", time_s, (long)getpid());
60 start = ftell(crit);
61 va_start(ap, msg);
62 vfprintf(crit, msg, ap);
63 va_end(ap);
64 fprintf(crit, "\n");
65
66 buf = malloc(ftell(crit) - start);
5eaef520 67 fclose(crit);
a3bbcc2b 68
69 if (buf)
70 {
71 va_start(ap, msg);
72 vsprintf(buf, msg, ap);
73 va_end(ap);
74
75 send_zgram(instance, buf);
76 com_err(whoami, 0, buf);
77
78 free(buf);
79 }
96d806a4 80 }
923cef2a 81
a3bbcc2b 82 if (!crit || !buf)
83 {
84 send_zgram(instance, "Couldn't format critical syslog!");
85 com_err(whoami, 0, "Couldn't format critical syslog!");
86 }
96d806a4 87}
88
89
8defc06b 90/* Sends a zephyrgram of class "MOIRA", instance as a parameter. Ignores
96d806a4 91 * errors while sending message.
92 */
93
7ac48069 94void send_zgram(char *inst, char *msg)
96d806a4 95{
a3bbcc2b 96#ifdef SYSLOG
97 char *buf;
98#endif
99
2f4a1cd0 100#ifdef ZEPHYR
5eaef520 101 ZNotice_t znotice;
102
103 memset(&znotice, 0, sizeof(znotice));
104 znotice.z_kind = UNSAFE;
105 znotice.z_class = "MOIRA";
106 znotice.z_class_inst = inst;
107 znotice.z_default_format = "MOIRA $instance on $fromhost:\n $message\n";
108 ZInitialize();
109 znotice.z_message = msg;
110 znotice.z_message_len = strlen(msg) + 1;
111 znotice.z_opcode = "";
112 znotice.z_recipient = "";
113 ZSendNotice(&znotice, ZNOAUTH);
2f4a1cd0 114#endif
115#ifdef SYSLOG
a3bbcc2b 116 buf = malloc(9 + strlen(instance) + strlen(msg));
117 if (buf)
118 {
119 sprintf(buf, "MOIRA: %s %s", inst, msg);
120 syslog(LOG_ERR, buf);
121 free(buf);
122 }
2f4a1cd0 123#endif
96d806a4 124}
This page took 0.110281 seconds and 5 git commands to generate.