source: trunk/third/moira/lib/critical.c @ 25477

Revision 25477, 2.8 KB checked in by jdreed, 13 years ago (diff)
In moira: * Re-snapshot moira at r4077, which incorporates the -Werror=format-security fixes from 4.0.0-r4073-0debathena2, except from upstream instead of from us.
Line 
1/* $Id: critical.c 4077 2012-05-07 16:53:10Z zacheiss $
2 *
3 * Log and send a zephyrgram about any critical errors.
4 *
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>.
8 */
9
10
11#ifdef HAVE_ZEPHYR
12/* need to include before moira.h, which includes krb_et.h, because
13   zephyr.h is broken */
14#include <zephyr/zephyr.h>
15/* zephyr.h doesn't prototype this */
16extern Code_t ZSendNotice(ZNotice_t *notice, Z_AuthProc cert_routine);
17#endif
18
19#include <mit-copyright.h>
20#include <moira.h>
21#include <moira_site.h>
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#ifndef HAVE_ZEPHYR
27#include <syslog.h>
28#endif
29#include <time.h>
30
31RCSID("$HeadURL: svn+ssh://svn.mit.edu/moira/trunk/moira/lib/critical.c $ $Id: critical.c 4077 2012-05-07 16:53:10Z zacheiss $");
32
33/* mode to create the file with */
34#define LOGFILEMODE     0644
35
36/* This routine sends a class MOIRA zephyrgram of specified instance
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.
40 */
41
42void critical_alert(char *whoami, char *instance, char *msg, ...)
43{
44  FILE *crit;
45  char *buf;
46  va_list ap;
47  long start;
48
49  /* Log message to critical file */
50  if ((crit = fopen(CRITERRLOG, "a")))
51    {
52      time_t t;
53      char *time_s;
54
55      time(&t);
56      time_s = ctime(&t) + 4;
57      time_s[strlen(time_s) - 6] = '\0';
58
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);
67      fclose(crit);
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, "%s", buf);
77
78          free(buf);
79        }
80    }
81
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    }
87}
88
89
90/* Sends a zephyrgram of class "MOIRA", instance as a parameter.  Ignores
91 * errors while sending message.
92 */
93
94void send_zgram(char *inst, char *msg)
95{
96#ifdef HAVE_ZEPHYR
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);
110#else
111  char *buf;
112
113  buf = malloc(9 + strlen(inst) + strlen(msg));
114  if (buf)
115    {
116      sprintf(buf, "MOIRA: %s %s", inst, msg);
117      syslog(LOG_ERR, "%s", buf);
118      free(buf);
119    }
120#endif
121}
Note: See TracBrowser for help on using the repository browser.