]>
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 * "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
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
22 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
27 #include <sys/socket.h>
40 #define _PATH_ASL_CONF "/etc/asl.conf"
41 #define MY_ID "asl_action"
43 #define ASL_KEY_FACILITY "Facility"
44 #define IndexNull ((uint32_t)-1)
45 #define forever for(;;)
47 static asl_msg_t
*query
= NULL
;
55 TAILQ_ENTRY(action_rule
) entries
;
58 static TAILQ_HEAD(cr
, action_rule
) asl_action_rule
;
60 int asl_action_close();
61 static int _parse_notify_file(const char *);
67 _parse_notify_file(_PATH_ASL_CONF
);
72 * Q [k v] [k v] ... action args...
82 if (p
== NULL
) return NULL
;
83 if (*p
!= 'Q') return NULL
;
90 while ((*p
== ' ') || (*p
== '\t')) p
++;
92 if (*p
== '\0') return NULL
;
93 if (*p
!= '[') return p
;
95 /* skip to closing ] */
116 struct action_rule
*out
;
118 if (s
== NULL
) return -1;
119 while ((*s
== ' ') || (*s
== '\t')) s
++;
120 if (*s
== '#') return -1;
122 act
= _find_action(s
);
124 if (act
== NULL
) return -1;
125 out
= (struct action_rule
*)calloc(1, sizeof(struct action_rule
));
126 if (out
== NULL
) return -1;
128 p
= strchr(act
, ' ');
129 if (p
!= NULL
) *p
= '\0';
130 out
->action
= strdup(act
);
132 if (out
->action
== NULL
)
140 out
->options
= strdup(p
+1);
142 if (out
->options
== NULL
)
153 out
->query
= asl_msg_from_string(s
);
155 if (out
->query
== NULL
)
158 if (out
->options
!= NULL
) free(out
->options
);
163 TAILQ_INSERT_TAIL(&asl_action_rule
, out
, entries
);
169 _act_notify(struct action_rule
*r
)
171 if (r
== NULL
) return;
172 if (r
->options
== NULL
) return;
173 notify_post(r
->options
);
177 asl_action_sendmsg(asl_msg_t
*msg
, const char *outid
)
179 struct action_rule
*r
;
188 if (msg
== NULL
) return -1;
190 for (r
= asl_action_rule
.tqh_first
; r
!= NULL
; r
= r
->entries
.tqe_next
)
192 if (asl_msg_cmp(r
->query
, msg
) == 1)
194 if (r
->action
== NULL
) continue;
195 if (!strcmp(r
->action
, "notify")) _act_notify(r
);
203 _parse_notify_file(const char *name
)
208 cf
= fopen(name
, "r");
209 if (cf
== NULL
) return 1;
211 while (NULL
!= (line
= get_line_from_file(cf
)))
223 asl_action_init(void)
225 asldebug("%s: init\n", MY_ID
);
227 TAILQ_INIT(&asl_action_rule
);
229 query
= asl_new(ASL_TYPE_QUERY
);
230 aslevent_addmatch(query
, MY_ID
);
231 aslevent_addoutput(asl_action_sendmsg
, MY_ID
);
233 _parse_notify_file(_PATH_ASL_CONF
);
238 asl_action_reset(void)
245 asl_action_close(void)
247 struct action_rule
*r
, *n
;
250 for (r
= asl_action_rule
.tqh_first
; r
!= NULL
; r
= n
)
252 n
= r
->entries
.tqe_next
;
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
);
258 TAILQ_REMOVE(&asl_action_rule
, r
, entries
);