]> git.saurik.com Git - apple/libc.git/blob - gen/asl.3
Libc-391.4.1.tar.gz
[apple/libc.git] / gen / asl.3
1 .\" Copyright (c) 2005 Apple Computer
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 4. Neither the name of Apple Computer nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"
29 .Dd January 5, 2005
30 .Dt asl 3
31 .Os "Mac OS X"
32 .Sh NAME
33 .Nm asl_open ,
34 .Nm asl_close ,
35 .Nm asl_new ,
36 .Nm asl_free ,
37 .Nm asl_set ,
38 .Nm asl_set_query ,
39 .Nm asl_get ,
40 .Nm asl_unset ,
41 .Nm asl_log ,
42 .Nm asl_vlog ,
43 .Nm asl_send ,
44 .Nm asl_key ,
45 .Nm asl_add_log_file ,
46 .Nm asl_remove_log_file ,
47 .Nm asl_set_cutoff_level ,
48 .Nm asl_search ,
49 .Nm aslresponse_next ,
50 .Nm aslresponse_free
51 .Nd system log message sending and searching functions
52 .Sh SYNOPSIS
53 .Fd #include <asl.h>
54 .Ft aslclient
55 .Fn asl_open "const char *ident, const char *facility, uint32_t opts"
56 .Ft void
57 .Fn asl_close "aslclient asl"
58 .Ft aslmsg
59 .Fn asl_new "uint32_t type"
60 .Ft void
61 .Fn asl_free "aslmsg msg"
62 .Ft int
63 .Fn asl_set "aslmsg msg, const char *key, const char *value"
64 .Ft int
65 .Fn asl_set_query "aslmsg msg, const char *key, const char *value, uint32_t op"
66 .Ft const char *
67 .Fn asl_key "aslmsg msg, uint32_t n"
68 .Ft const char *
69 .Fn asl_get "aslmsg msg, const char *key"
70 .Ft int
71 .Fn asl_unset "aslmsg msg, const char *key"
72 .Ft int
73 .Fn asl_log "aslclient asl, aslmsg msg, int level, const char *format, ..."
74 .Ft int
75 .Fn asl_vlog "aslclient asl, aslmsg msg, int level, const char *format, va_list ap"
76 .Ft int
77 .Fn asl_send "aslclient asl, aslmsg msg"
78 .Ft int
79 .Fn asl_add_log_file "aslclient asl, int fd"
80 .Ft int
81 .Fn asl_remove_log_file "aslclient asl, int fd"
82 .Ft int
83 .Fn asl_set_filter "aslclient asl, int f"
84 .Ft aslresponse
85 .Fn asl_search "aslclient asl, aslmsg msg"
86 .Ft aslmsg
87 .Fn aslresponse_next "aslresponse r"
88 .Ft void
89 .Fn aslresponse_free "aslresponse a"
90 .Sh DESCRIPTION
91 These routines provide an interface to the Apple system log facility.
92 They are intended to be a replacement for the
93 .Xr syslog 3
94 API, which will continue to be supported for backwards compatibility.
95 The new API allows client applications to create flexible, structured messages and send them to the
96 .Nm syslogd
97 server, where they may undergo additional processing.
98 Messages received by the server are saved in a data store (subject to input filtering constraints).
99 This API permits clients to create queries and search the message data store for matching messages.
100 .Ss MESSAGES
101 At the core of this API is the aslmsg structure.
102 Although the structure is opaque and may not be directly manipulated, it contains a list of key/value pairs.
103 All keys and values are NULL-terminated C language character strings.
104 UTF-8 encoding may be used for non-ASCII characters.
105 .Pp
106 Message structures are generally used to send log messages, and are created thusly:
107 .Pp
108 aslmsg m = asl_new(ASL_TYPE_MSG);
109 .Pp
110 Another message type, ASL_TYPE_QUERY, is used to create queries when searching the data store.
111 Query type messages and searching are described in detail in the
112 .Sx SEARCHING
113 section below.
114 For the remainder of this section, the messages described will be of the ASL_TYPE_MSG variety.
115 .Pp
116 Each aslmsg contains a default set of keys and values associated with them.
117 These keys are listed in the asl.h header file.
118 They are:
119 .Pp
120 #define ASL_KEY_TIME "Time"
121 #define ASL_KEY_HOST "Host"
122 #define ASL_KEY_SENDER "Sender"
123 #define ASL_KEY_PID "PID"
124 #define ASL_KEY_UID "UID"
125 #define ASL_KEY_GID "GID"
126 #define ASL_KEY_LEVEL "Level"
127 #define ASL_KEY_MSG "Message"
128 .Pp
129 Many of these correspond to equivalent parts of messages described in the
130 .Xr syslog 3
131 API.
132 Values associated with these message keys are assigned appropriate defaults.
133 The value for ASL_KEY_HOST is the local host name,
134 the value associated with ASL_KEY_SENDER is the process name,
135 the ASL_KEY_PID is the client's process ID number, and so on.
136 .Pp
137 Note the addition of the UID and GID keys.
138 The values for UID and GID are set in library code by the message sender.
139 The server will attempt to confirm the values, but no claim is made that these
140 values cannot be maliciously overridden in an attempt to deceive a log message
141 reader as to the identity of the sender of a message.
142 The contents of log messages must be regarded as insecure.
143 .Pp
144 Also note the absence of a Facility key.
145 The
146 .Xr asl 3
147 API does not require a process to choose a facility name.
148 The
149 .Nm syslogd
150 server will use a default value of
151 .Dq user
152 if a facility is not set.
153 However, a client may set a facility name using:
154 .Pp
155 asl_set(m, "Facility", "UsefulService");
156 .Pp
157 An application may choose any facility name at will.
158 .Pp
159 Default values are set in the message for each of the keys listed above except for
160 ASL_KEY_MSG, which may be explicitly set at any time using the
161 .Nm asl_set
162 routine, or implicitly set at the time the message is sent using the
163 .Nm asl_log
164 or
165 .Nm asl_vlog
166 routines.
167 These two routines also have an integer level parameter for specifying the log priority.
168 The ASL_KEY_LEVEL value is set accordingly.
169 Finally, the value associated with ASL_KEY_TIME is set in the sending routine.
170 .Pp
171 Although it may appear that there is significant overhead required to send a log message using this API,
172 the opposite is actually true.
173 A simple
174 .Dq Hello World
175 program requires only:
176 .Pp
177 #include <asl.h>
178 ...
179 asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
180 .Pp
181 Both
182 .Nm asl_log
183 and
184 .Nm asl_vlog
185 will provide the appropriate default values when passed a NULL aslmsg argument.
186 .Pp
187 .Pp
188 In this example, the aslclient argument is NULL.
189 This is sufficient for a single-threaded application,
190 or for an application which only sends log messages from a single thread.
191 When logging from multiple threads, each thread must open a separate client handle using
192 .Nm asl_open .
193 The client handle may then be closed when it is no longer required using
194 .Nm asl_close .
195 .Pp
196 When an application requires additional keys and values to be associated with each log message,
197 a single message structure may be allocated and set up as
198 .Dq template
199 message of sorts:
200 .Pp
201 aslmsg m = asl_new(ASL_TYPE_MSG);
202 asl_set(m, "Facility", "Spy vs. Spy");
203 asl_set(m, "Clearance", "Top Secret");
204 ...
205 asl_log(NULL, m, ASL_LEVEL_NOTICE, "Message One");
206 ...
207 asl_log(NULL, m, ASL_LEVEL_ERR, "Message Two");
208 .Pp
209 The message structure will carry the values set for the
210 .Dq Facility
211 and
212 .Dq Clearance
213 keys so that they are used in each call to
214 .Nm asl_log ,
215 while the log level and the message text are taken from the calling parameters.
216 .Pp
217 Key/value pairs may be removed from a message structure with
218 .Nm asl_unset .
219 A message may be freed using
220 .Nm asl_free .
221 .Pp
222 The
223 .Nm asl_send
224 routine is used by
225 .Nm asl_log
226 and
227 .Nm asl_vlog
228 to transmit a message to the server.
229 This routine sets the value associated with ASL_KEY_TIME and send the message.
230 It may be called directly if all of a message's key/value pairs have been created using
231 .Nm asl_set .
232 .Ss CLIENT HANDLES
233 When logging is done from a single thread,
234 a NULL value may be used in any of the routines that require an aslclient argument.
235 In this case the library will open an internal client handle on behalf of the application.
236 .Pp
237 If multiple threads must do logging,
238 or if client options are desired,
239 then the application should call
240 .Nm asl_open
241 to create a client handle for each thread.
242 As a convenience,
243 the
244 .Nm asl_open
245 routine may be given an ident argument,
246 which becomes the default value for the ASL_KEY_SENDER key,
247 and a facility argument,
248 which becomes the default facility name for the application.
249 .Pp
250 Several options are available when creating a client handle.
251 They are:
252 .Pp
253 .Bl -tag -width "ASL_OPT_NO_REMOTE" -compact
254 .It ASL_OPT_STDERR
255 adds stderr as an output file descriptor
256 .It ASL_OPT_NO_DELAY
257 connects to the server immediately
258 .It ASL_OPT_NO_REMOTE
259 disables remote-control filter adjustment
260 .El
261 .Pp
262 See the FILTERING section below, and the
263 .Xr syslog 1
264 for additional details on filter controls.
265 .Pp
266 A client handle is closed and it's resources released using
267 .Nm asl_close .
268 Note that if additional file descriptors were added to the handle either using the
269 ASL_OPT_STDERR option or afterwards with the
270 .Nm asl_add_log_file
271 routine, those file descriptors are not closed by
272 .Nm asl_close .
273 .Ss LOGGING TO ADDITIONAL FILES
274 If a client handle is opened with the ASL_OPT_STDERR option to
275 .Nm asl_open ,
276 a copy of each log message will be sent to stderr.
277 Additional output streams may be include using
278 .Nm asl_add_log_file .
279 File descriptors may be removed from the list of outputs associated with a client handle with
280 .Nm asl_remove_log_file .
281 This routine simply removes the file descriptor from the output list.
282 The file is not closed as a result.
283 .Pp
284 The ASL_OPT_STDERR option may not be unset after a client handle has been opened.
285 .Pp
286 In the present release of Mac OS X, a
287 .Dq raw
288 format is used to format messages sent to file descriptors added to a client handle.
289 Each message is preceded by a 10-character field containing a message length.
290 The message length is padded with leading white space.
291 The length gives the string length of the remainder of the output string.
292 Following the length is a space character, and then the message.
293 The message is encoded as a set of key/value pairs enclosed in square brackets,
294 which are themselves separated by a space character.
295 The key is separated from the value by space character.
296 Embedded closing square brackets are escaped by a backslash.
297 Embedded space characters in keys are escaped by a backslash;
298 Embedded newlines are summarily turned into semicolons.
299 The output is terminated by a trailing newline and a NUL character.
300 .Ss SEARCHING
301 The
302 .Nm syslogd
303 server archives received messages in a data store that may be searched using the
304 .Nm asl_search ,
305 .Nm aslresponse_next ,
306 and
307 .Nm aslresponse_free
308 routines.
309 A query message is created using:
310 .Pp
311 aslmsg q = asl_new(ASL_TYPE_QUERY);
312 .Pp
313 Search settings are made in the query using
314 .Nm asl_set_query .
315 A search is performed on the data store with
316 .Nm asl_search .
317 It returns an
318 .Ft aslresponse
319 structure.
320 The caller may then call
321 .Nm aslresponse_next
322 to iterate through matching messages.
323 The
324 .Ft aslresponse
325 structure may be freed with
326 .Nm aslresponse_free .
327 .Pp
328 Like other messages, ASL_TYPE_QUERY messages contain keys and values.
329 They also associate an operation with each key and value.
330 The operation is used to decide if a message matches the query.
331 The simplest operation is ASL_QUERY_OP_EQUAL, which tests for equality.
332 For example, the following code snippet searches for messages with a Sender value equal to
333 .Dq MyApp .
334 .Pp
335 aslmsg m;
336 aslresponse r;
337 q = asl_new(ASL_TYPE_QUERY);
338 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
339 r = asl_search(NULL, q);
340 .Pp
341 More complex searches may be performed using other query operations.
342 .Pp
343 .Bl -tag -width "ASL_QUERY_OP_GREATER_EQUAL" -compact
344 .It ASL_QUERY_OP_EQUAL
345 value equality
346 .It ASL_QUERY_OP_GREATER
347 value greater than
348 .It ASL_QUERY_OP_GREATER_EQUAL
349 value greater than or equal to
350 .It ASL_QUERY_OP_LESS
351 value less than
352 .It ASL_QUERY_OP_LESS_EQUAL
353 value less than or equal to
354 .It ASL_QUERY_OP_NOT_EQUAL
355 value not equal
356 .It ASL_QUERY_OP_REGEX
357 regular expression search
358 .It ASL_QUERY_OP_TRUE
359 always true - use to test for the existence of a key
360 .El
361 .Pp
362 Regular expression search uses
363 .Xr regex 3
364 library.
365 Patterns are compiled using the REG_EXTENDED and REG_NOSUB options.
366 .Pp
367 Modifiers that change the behavior of these operations may also be specified
368 by ORing the modifier value with the operation.
369 The modifiers are:
370 .Pp
371 .Bl -tag -width "ASL_QUERY_OP_SUBSTRING" -compact
372 .It ASL_QUERY_OP_CASEFOLD
373 string comparisons are case-folded
374 .It ASL_QUERY_OP_PREFIX
375 match a leading substring
376 .It ASL_QUERY_OP_SUFFIX
377 match a trailing substring
378 .It ASL_QUERY_OP_SUBSTRING
379 match any substring
380 .It ASL_QUERY_OP_NUMERIC
381 values are converted to integer using
382 .Nm atoi
383 .El
384 .Pp
385 The only modifier that is checked for ASL_QUERY_OP_REGEX search is ASL_QUERY_OP_CASEFOLD.
386 This causes the regular expression to be compiled with the REG_ICASE option.
387 .Pp
388 If a query message contains more than one set of key/value/operation triples,
389 the result will be a logical AND. For example, to find messages from
390 .Dq MyApp
391 with a priority level less than or equal to
392 .Dq 3 :
393 .Pp
394 aslmsg q;
395 aslresponse r;
396 q = asl_new(ASL_TYPE_QUERY);
397 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
398 asl_set_query(q, ASL_KEY_LEVEL, "3",
399 ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
400 r = asl_search(NULL, q);
401 .Pp
402 After calling
403 .Nm asl_search
404 to get an
405 .Ft aslresponse
406 structure, use
407 .Nm aslresponse_next
408 to iterate through all matching messages.
409 To iterate through the keys and values in a message, use
410 .Nm asl_key
411 to iterate through the keys, then call
412 .Nm asl_get
413 to get the value associated with each key.
414 .Pp
415 aslmsg q, m;
416 int i;
417 const char *key, *val;
418 .Pp
419 ...
420 r = asl_search(NULL, q);
421 while (NULL != (m = aslresponse_next(r)))
422 {
423 for (i = 0; (NULL != (key = asl_key(m, i))); i++)
424 {
425 val = asl_get(m, key);
426 ...
427 }
428 }
429 aslresponse_free(r);
430 .Pp
431 .Ss FILTERING AND REMOTE CONTROL
432 Clients may set a filter mask value with
433 .Nm asl_set_filter .
434 The mask specifies which messages should be sent to the
435 .Nm syslogd
436 daemon by specifying a yes/no setting for each priority level.
437 Clients typically set a filter mask to avoid sending relatively unimportant messages.
438 For example, Debug or Info priority level messages are generally only useful for debugging operations.
439 By setting a filter mask, a process can improve performance by avoiding
440 sending messages that are in most cases unnecessary.
441 .Pp
442 As a convenience, the macros ASL_FILTER_MASK(level) and ASL_FILTER_MASK_UPTO(level)
443 may be used to construct a bit mask corresponding to a given priority level,
444 or corresponding to a bit mask for all priority levels from ASL_LEVEL_EMERG to a
445 given input level.
446 .Pp
447 The default filter mask is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
448 This means that by default, and in the absence of remote-control changes (described below),
449 ASL_LEVEL_DEBUG and ASL_LEVEL_INFO priority level messages are not sent to the
450 .Mn syslogd
451 server.
452 .Pp
453 Three different filters exist for each application.
454 The first is the filter mask set using
455 .Nm asl_set_filter
456 as described above.
457 The Apple System Log facility also manages a
458 .Dq master
459 filter mask.
460 The master filter mask usually has a value that indicates to the library that it is
461 .Dq off ,
462 and thus it has no effect.
463 However, the mask filter mask may be enabled by giving it a value using the
464 .Nm syslog
465 command, using the
466 .Fl c
467 0 option.
468 When the master filter mask has been set,
469 it takes precedence over the client's filter mask.
470 The client's mask is unmodified, and will become active again if remote-control filtering is disabled.
471 .Pp
472 In addition to the master filter mask,
473 The Apple System Log facility also manages a per-client remote-control filter mask.
474 Like the master filter mask, the per-client mask is usually
475 .Dq off ,
476 having no effect on a client.
477 If a per-client filter mask is set using the
478 .Nm syslog
479 command, using the
480 .Fl c Ar process
481 option, then it takes precedence over both the client's filter mask and the master filter mask.
482 As is the case with the master filter mask, a per-client mask ceases having any effect when if is disabled.
483 .Pp
484 The ASL_OPT_NO_REMOTE option to
485 .Nm asl_open
486 causes both the master and per-client remote-control masks to be ignored in the library.
487 In that case, only the client's own filter mask is used to determine which messages are
488 sent to the server.
489 This may be useful for Applications that produce log messages that should never be filtered
490 due to security considerations.
491 Note that root (administrator) access is required to set or change the master filter mask,
492 and that only root may change a per-client remote-control filter mask for a root (UID 0) process.
493 .Sh HISTORY
494 These functions first appeared in
495 Mac OS X 10.4.
496 .Sh SEE ALSO
497 .Xr syslogd 8 ,
498 .Xr syslog 1