summaryrefslogtreecommitdiff
path: root/src/daemontools-extras/ucspilogd.c
blob: cf1ae3752d20743a1f50a02e29d37721511f7ed3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* ISC license. */

#undef INTERNAL_MARK
#ifndef SYSLOG_NAMES
#define SYSLOG_NAMES
#endif

#include <skalibs/nonposix.h>
#include <stdlib.h>
#include <syslog.h>
#include <skalibs/uint.h>
#include <skalibs/sgetopt.h>
#include <skalibs/bytestr.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <skalibs/skamisc.h>

#define USAGE "ucspilogd [ -D default ] [ var... ]"
#define dieusage() strerr_dieusage(100, USAGE)

static inline void die (void)
{
  strerr_diefu1sys(111, "write to stdout") ;
}


 /*
   Hack: INTERNAL_MARK is defined by all systems that
   use the CODE stuff, and not by others (Solaris).
*/

#ifndef INTERNAL_MARK

typedef struct CODE_s CODE, *CODE_ref ;
struct CODE_s
{
  char *c_name ;
  unsigned int c_val ;
} ;

static const CODE prioritynames[] =
{
  { "emerg", LOG_EMERG },
  { "alert", LOG_ALERT },
  { "crit", LOG_CRIT },
  { "err", LOG_ERR },
  { "warning", LOG_WARNING },
  { "notice", LOG_NOTICE },
  { "info", LOG_INFO },
  { "debug", LOG_DEBUG },
  { 0, -1 }
} ;

static const CODE facilitynames[] =
{
  { "kern", LOG_KERN },
  { "user", LOG_USER },
  { "mail", LOG_MAIL },
  { "news", LOG_NEWS },
  { "uucp", LOG_UUCP },
  { "daemon", LOG_DAEMON },
  { "auth", LOG_AUTH },
  { "cron", LOG_CRON },
  { "lpr", LOG_LPR },
#ifdef LOG_SYSLOG
  { "syslog", LOG_SYSLOG },
#endif
#ifdef LOG_AUDIT
  { "audit", LOG_AUDIT },
#endif
  { "local0", LOG_LOCAL0 },
  { "local1", LOG_LOCAL1 },
  { "local2", LOG_LOCAL2 },
  { "local3", LOG_LOCAL3 },
  { "local4", LOG_LOCAL4 },
  { "local5", LOG_LOCAL5 },
  { "local6", LOG_LOCAL6 },
  { "local7", LOG_LOCAL7 },
  { 0, -1 }
} ;

#define LOG_PRI(p) ((p) & LOG_PRIMASK)
#define LOG_FAC(p) (((p) & LOG_FACMASK) / (LOG_PRIMASK + 1))

#endif


static unsigned int syslog_names (char const *line)
{
  unsigned int fpr, i ;
  int fp ;
  CODE *p = facilitynames ;

  if (line[0] != '<') return 0 ;
  i = uint_scan(line+1, &fpr) ;
  if (!i || (line[i+1] != '>')) return 0 ;
  i += 2 ;
  
  fp = LOG_FAC(fpr) << 3 ;
  for (; p->c_name ; p++) if (p->c_val == fp) break ;
  if (p->c_name)
  {
    if ((buffer_puts(buffer_1, p->c_name) < 0)
     || (buffer_put(buffer_1, ".", 1) < 1)) die() ;
  }
  else
  {
    if (buffer_put(buffer_1, "unknown.", 8) < 8) die() ;
    i = 0 ;
  }

  fp = LOG_PRI(fpr) ;
  for (p = prioritynames ; p->c_name ; p++) if (p->c_val == fp) break ;
  if (p->c_name)
  {
    if ((buffer_puts(buffer_1, p->c_name) < 0)
     || (buffer_put(buffer_1, ": ", 2) < 2)) die() ;
  }
  else
  {
    if (buffer_put(buffer_1, "unknown: ", 9) < 9) die() ;
    i = 0 ;
  }
  return i ;
}


int main (int argc, char const *const *argv, char const *const *envp)
{
  char const *d = "<undefined>" ;
  PROG = "ucspilogd" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      register int opt = subgetopt_r(argc, argv, "D:", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'D' : d = l.arg ; break ;
        default : dieusage() ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }

  {
    char const *envs[argc] ;
    unsigned int i = 0 ;
    for (; i < (unsigned int)argc ; i++)
    {
      envs[i] = env_get2(envp, argv[i]) ;
      if (!envs[i]) envs[i] = d ;
    }
    for (;;)
    {
      unsigned int pos = 0 ;
      satmp.len = 0 ;
      {
        register int r = skagetlnsep(buffer_0f1, &satmp, "\n", 2) ;
        if (r < 0) strerr_diefu1sys(111, "read from stdin") ;
        if (!r) break ;
      }
      if (!satmp.len) continue ;
      satmp.s[satmp.len-1] = '\n' ;
      if ((satmp.s[0] == '@') && (satmp.len > 26) && (byte_chr(satmp.s, 26, ' ') == 25))
      {
        if (buffer_put(buffer_1, satmp.s, 26) < 26) die() ;
        pos += 26 ;
      }
      for (i = 0 ; i < (unsigned int)argc ; i++)
        if ((buffer_puts(buffer_1, envs[i]) < 0)
         || (buffer_put(buffer_1, ": ", 2) < 2)) die() ;
      pos += syslog_names(satmp.s + pos) ;
      if (buffer_put(buffer_1, satmp.s + pos, satmp.len - pos) < (int)(satmp.len - pos)) die() ;
    }
  }
  return 0 ;
}