]> git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/asl_action.c
syslog-14.2.tar.gz
[apple/syslog.git] / syslogd.tproj / asl_action.c
1 /*
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 2004 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/uio.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <notify.h>
38 #include "daemon.h"
39
40 #define _PATH_ASL_CONF "/etc/asl.conf"
41 #define MY_ID "asl_action"
42
43 #define ASL_KEY_FACILITY "Facility"
44 #define IndexNull ((uint32_t)-1)
45 #define forever for(;;)
46
47 static asl_msg_t *query = NULL;
48 static int reset = 0;
49
50 struct action_rule
51 {
52 asl_msg_t *query;
53 char *action;
54 char *options;
55 TAILQ_ENTRY(action_rule) entries;
56 };
57
58 static TAILQ_HEAD(cr, action_rule) asl_action_rule;
59
60 int asl_action_close();
61 static int _parse_notify_file(const char *);
62
63 static void
64 _do_reset(void)
65 {
66 asl_action_close();
67 _parse_notify_file(_PATH_ASL_CONF);
68 }
69
70 /*
71 * Config File format:
72 * Q [k v] [k v] ... action args...
73 */
74
75 /* Skip over query */
76 static char *
77 _find_action(char *s)
78 {
79 char *p;
80
81 p = s;
82 if (p == NULL) return NULL;
83 if (*p != 'Q') return NULL;
84
85 p++;
86
87 forever
88 {
89 /* Find next [ */
90 while ((*p == ' ') || (*p == '\t')) p++;
91
92 if (*p == '\0') return NULL;
93 if (*p != '[') return p;
94
95 /* skip to closing ] */
96 while (*p != ']')
97 {
98 p++;
99 if (*p == '\\')
100 {
101 p++;
102 if (*p == ']') p++;
103 }
104 }
105
106 if (*p == ']') p++;
107 }
108
109 return NULL;
110 }
111
112 static int
113 _parse_line(char *s)
114 {
115 char *act, *p;
116 struct action_rule *out;
117
118 if (s == NULL) return -1;
119 while ((*s == ' ') || (*s == '\t')) s++;
120 if (*s == '#') return -1;
121
122 act = _find_action(s);
123
124 if (act == NULL) return -1;
125 out = (struct action_rule *)calloc(1, sizeof(struct action_rule));
126 if (out == NULL) return -1;
127
128 p = strchr(act, ' ');
129 if (p != NULL) *p = '\0';
130 out->action = strdup(act);
131
132 if (out->action == NULL)
133 {
134 free(out);
135 return -1;
136 }
137
138 if (p != NULL)
139 {
140 out->options = strdup(p+1);
141
142 if (out->options == NULL)
143 {
144 free(out->action);
145 free(out);
146 return -1;
147 }
148 }
149
150 p = act - 1;
151
152 *p = '\0';
153 out->query = asl_msg_from_string(s);
154
155 if (out->query == NULL)
156 {
157 free(out->action);
158 if (out->options != NULL) free(out->options);
159 free(out);
160 return -1;
161 }
162
163 TAILQ_INSERT_TAIL(&asl_action_rule, out, entries);
164
165 return 0;
166 }
167
168 static void
169 _act_notify(struct action_rule *r)
170 {
171 if (r == NULL) return;
172 if (r->options == NULL) return;
173 notify_post(r->options);
174 }
175
176 int
177 asl_action_sendmsg(asl_msg_t *msg, const char *outid)
178 {
179 struct action_rule *r;
180
181
182 if (reset != 0)
183 {
184 _do_reset();
185 reset = 0;
186 }
187
188 if (msg == NULL) return -1;
189
190 for (r = asl_action_rule.tqh_first; r != NULL; r = r->entries.tqe_next)
191 {
192 if (asl_msg_cmp(r->query, msg) == 1)
193 {
194 if (r->action == NULL) continue;
195 if (!strcmp(r->action, "notify")) _act_notify(r);
196 }
197 }
198
199 return 0;
200 }
201
202 static int
203 _parse_notify_file(const char *name)
204 {
205 FILE *cf;
206 char *line;
207
208 cf = fopen(name, "r");
209 if (cf == NULL) return 1;
210
211 while (NULL != (line = get_line_from_file(cf)))
212 {
213 _parse_line(line);
214 free(line);
215 }
216
217 fclose(cf);
218
219 return 0;
220 }
221
222 int
223 asl_action_init(void)
224 {
225 asldebug("%s: init\n", MY_ID);
226
227 TAILQ_INIT(&asl_action_rule);
228
229 query = asl_new(ASL_TYPE_QUERY);
230 aslevent_addmatch(query, MY_ID);
231 aslevent_addoutput(asl_action_sendmsg, MY_ID);
232
233 _parse_notify_file(_PATH_ASL_CONF);
234 return 0;
235 }
236
237 int
238 asl_action_reset(void)
239 {
240 reset = 1;
241 return 0;
242 }
243
244 int
245 asl_action_close(void)
246 {
247 struct action_rule *r, *n;
248
249 n = NULL;
250 for (r = asl_action_rule.tqh_first; r != NULL; r = n)
251 {
252 n = r->entries.tqe_next;
253
254 if (r->query != NULL) asl_free(r->query);
255 if (r->action != NULL) free(r->action);
256 if (r->options != NULL) free(r->options);
257
258 TAILQ_REMOVE(&asl_action_rule, r, entries);
259 free(r);
260 }
261
262 return 0;
263 }