]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/asl_action.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/types.h>
26 #include <sys/socket.h>
39 #define _PATH_ASL_CONF "/etc/asl.conf"
40 #define MY_ID "asl_action"
42 #define ASL_KEY_FACILITY "Facility"
43 #define IndexNull ((uint32_t)-1)
44 #define forever for(;;)
46 static asl_msg_t
*query
= NULL
;
54 TAILQ_ENTRY(action_rule
) entries
;
57 static TAILQ_HEAD(cr
, action_rule
) asl_action_rule
;
59 int asl_action_close();
60 static int _parse_notify_file(const char *);
66 _parse_notify_file(_PATH_ASL_CONF
);
71 * Q [k v] [k v] ... action args...
81 if (p
== NULL
) return NULL
;
82 if (*p
!= 'Q') return NULL
;
89 while ((*p
== ' ') || (*p
== '\t')) p
++;
91 if (*p
== '\0') return NULL
;
92 if (*p
!= '[') return p
;
94 /* skip to closing ] */
115 struct action_rule
*out
;
117 if (s
== NULL
) return -1;
118 while ((*s
== ' ') || (*s
== '\t')) s
++;
119 if (*s
== '#') return -1;
121 act
= _find_action(s
);
123 if (act
== NULL
) return -1;
124 out
= (struct action_rule
*)calloc(1, sizeof(struct action_rule
));
125 if (out
== NULL
) return -1;
127 p
= strchr(act
, ' ');
128 if (p
!= NULL
) *p
= '\0';
129 out
->action
= strdup(act
);
131 if (out
->action
== NULL
)
139 out
->options
= strdup(p
+1);
141 if (out
->options
== NULL
)
152 out
->query
= asl_msg_from_string(s
);
154 if (out
->query
== NULL
)
157 if (out
->options
!= NULL
) free(out
->options
);
162 TAILQ_INSERT_TAIL(&asl_action_rule
, out
, entries
);
168 _act_notify(struct action_rule
*r
)
170 if (r
== NULL
) return;
171 if (r
->options
== NULL
) return;
172 notify_post(r
->options
);
176 asl_action_sendmsg(asl_msg_t
*msg
, const char *outid
)
178 struct action_rule
*r
;
187 if (msg
== NULL
) return -1;
189 for (r
= asl_action_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
191 if (asl_msg_cmp(r
->query
, msg
) == 1)
193 if (r
->action
== NULL
) continue;
194 if (!strcmp(r
->action
, "notify")) _act_notify(r
);
202 _parse_notify_file(const char *name
)
207 cf
= fopen(name
, "r");
208 if (cf
== NULL
) return 1;
210 while (NULL
!= (line
= get_line_from_file(cf
)))
222 asl_action_init(void)
224 asldebug("%s: init\n", MY_ID
);
226 TAILQ_INIT(&asl_action_rule
);
228 query
= asl_new(ASL_TYPE_QUERY
);
229 aslevent_addmatch(query
, MY_ID
);
230 aslevent_addoutput(asl_action_sendmsg
, MY_ID
);
232 _parse_notify_file(_PATH_ASL_CONF
);
237 asl_action_reset(void)
244 asl_action_close(void)
246 struct action_rule
*r
, *n
;
249 for (r
= asl_action_rule
.tqh_first
; r
!= NULL
; r
= n
)
251 n
= r
->entries
.tqe_next
;
253 if (r
->query
!= NULL
) asl_free(r
->query
);
254 if (r
->action
!= NULL
) free(r
->action
);
255 if (r
->options
!= NULL
) free(r
->options
);
257 TAILQ_REMOVE(&asl_action_rule
, r
, entries
);