]> andersk Git - moira.git/blame - lib/critical.c
Build shared libmoira via libtool.
[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
ea0caf4a 11#ifdef HAVE_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>
ea0caf4a 24#include <stdlib.h>
7ac48069 25#include <string.h>
ea0caf4a 26#ifndef HAVE_ZEPHYR
2f4a1cd0 27#include <syslog.h>
28#endif
ea0caf4a 29#include <time.h>
7ac48069 30
31RCSID("$Header$");
96d806a4 32
96d806a4 33/* mode to create the file with */
34#define LOGFILEMODE 0644
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
a816420b 42void critical_alert(char *whoami, 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{
ea0caf4a 96#ifdef HAVE_ZEPHYR
5eaef520 97 ZNotice_t znotice;
98
99 memset(&znotice, 0, sizeof(znotice));
100 znotice.z_kind = UNSAFE;
101 znotice.z_class = "MOIRA";
102 znotice.z_class_inst = inst;
103 znotice.z_default_format = "MOIRA $instance on $fromhost:\n $message\n";
104 ZInitialize();
105 znotice.z_message = msg;
106 znotice.z_message_len = strlen(msg) + 1;
107 znotice.z_opcode = "";
108 znotice.z_recipient = "";
109 ZSendNotice(&znotice, ZNOAUTH);
ea0caf4a 110#else
111 char *buf;
112
113 buf = malloc(9 + strlen(inst) + strlen(msg));
a3bbcc2b 114 if (buf)
115 {
116 sprintf(buf, "MOIRA: %s %s", inst, msg);
a6966127 117 syslog(LOG_ERR, "%s", buf);
a3bbcc2b 118 free(buf);
119 }
2f4a1cd0 120#endif
96d806a4 121}
This page took 0.113715 seconds and 5 git commands to generate.