]> git.saurik.com Git - apple/libc.git/blame_incremental - gen/asl.3
Libc-498.tar.gz
[apple/libc.git] / gen / asl.3
... / ...
CommitLineData
1.\" Copyright (c) 2005-2007 Apple Inc.
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 11, 2007
30.Dt asl 3
31.Os "Mac OS X"
32.Sh NAME
33.Nm asl_add_log_file ,
34.Nm asl_close ,
35.Nm asl_free ,
36.Nm asl_get ,
37.Nm asl_key ,
38.Nm asl_log ,
39.Nm asl_new ,
40.Nm asl_open ,
41.Nm asl_remove_log_file ,
42.Nm asl_search ,
43.Nm asl_send ,
44.Nm asl_set ,
45.Nm asl_set_filter ,
46.Nm asl_set_query ,
47.Nm asl_unset ,
48.Nm asl_vlog ,
49.Nm aslresponse_free ,
50.Nm aslresponse_next
51.Nd system log message sending and searching functions
52.Sh SYNOPSIS
53.Fd #include <asl.h>
54.\"
55.Ft int
56.Fo asl_add_log_file
57.Fa "aslclient asl"
58.Fa "int fd"
59.Fc
60.Ft void
61.Fo asl_close
62.Fa "aslclient asl"
63.Fc
64.Ft void
65.Fo asl_free
66.Fa "aslmsg msg"
67.Fc
68.Ft const char *
69.Fo asl_get
70.Fa "aslmsg msg"
71.Fa "const char *key"
72.Fc
73.Ft const char *
74.Fo asl_key
75.Fa "aslmsg msg"
76.Fa "uint32_t n"
77.Fc
78.Ft int
79.Fo asl_log
80.Fa "aslclient asl"
81.Fa "aslmsg msg"
82.Fa "int level"
83.Fa "const char *format"
84.Fa "..."
85.Fc
86.Ft aslmsg
87.Fo asl_new
88.Fa "uint32_t type"
89.Fc
90.Ft aslclient
91.Fo asl_open
92.Fa "const char *ident"
93.Fa "const char *facility"
94.Fa "uint32_t opts"
95.Fc
96.Ft int
97.Fo asl_remove_log_file
98.Fa "aslclient asl"
99.Fa "int fd"
100.Fc
101.Ft aslresponse
102.Fo asl_search
103.Fa "aslclient asl"
104.Fa "aslmsg msg"
105.Fc
106.Ft int
107.Fo asl_send
108.Fa "aslclient asl"
109.Fa "aslmsg msg"
110.Fc
111.Ft int
112.Fo asl_set
113.Fa "aslmsg msg"
114.Fa "const char *key"
115.Fa "const char *value"
116.Fc
117.Ft int
118.Fo asl_set_filter
119.Fa "aslclient asl"
120.Fa "int f"
121.Fc
122.Ft int
123.Fo asl_set_query
124.Fa "aslmsg msg"
125.Fa "const char *key"
126.Fa "const char *value"
127.Fa "uint32_t op"
128.Fc
129.Ft int
130.Fo asl_unset
131.Fa "aslmsg msg"
132.Fa "const char *key"
133.Fc
134.Ft int
135.Fo asl_vlog
136.Fa "aslclient asl"
137.Fa "aslmsg msg"
138.Fa "int level"
139.Fa "const char *format"
140.Fa "va_list ap"
141.Fc
142.Ft void
143.Fo aslresponse_free
144.Fa "aslresponse a"
145.Fc
146.Ft aslmsg
147.Fo aslresponse_next
148.Fa "aslresponse r"
149.Fc
150.Sh DESCRIPTION
151These routines provide an interface to the Apple System Log facility.
152They are intended to be a replacement for the
153.Xr syslog 3
154API, which will continue to be supported for backwards compatibility.
155The new API allows client applications
156to create flexible, structured messages and send them to the
157.Nm syslogd
158server, where they may undergo additional processing.
159Messages received by the server are saved in a data store
160(subject to input filtering constraints).
161This API permits clients to create queries
162and search the message data store for matching messages.
163.Ss MESSAGES
164At the core of this API is the aslmsg structure.
165Although the structure is opaque and may not be directly manipulated,
166it contains a list of key/value pairs.
167All keys and values are NULL-terminated C language character strings.
168UTF-8 encoding may be used for non-ASCII characters.
169.Pp
170Message structures are generally used to send log messages,
171and are created thusly:
172.Pp
173 aslmsg m = asl_new(ASL_TYPE_MSG);
174.Pp
175Another message type, ASL_TYPE_QUERY,
176is used to create queries when searching the data store.
177Query type messages and searching are described in detail in the
178.Sx SEARCHING
179section below.
180For the remainder of this section,
181the messages described will be of the ASL_TYPE_MSG variety.
182.Pp
183Each aslmsg contains a default set of keys
184and values that are associated with them.
185These keys are listed in the asl.h header file.
186They are:
187.Pp
188 #define ASL_KEY_TIME "Time"
189 #define ASL_KEY_HOST "Host"
190 #define ASL_KEY_SENDER "Sender"
191 #define ASL_KEY_FACILITY "Facility"
192 #define ASL_KEY_PID "PID"
193 #define ASL_KEY_UID "UID"
194 #define ASL_KEY_GID "GID"
195 #define ASL_KEY_LEVEL "Level"
196 #define ASL_KEY_MSG "Message"
197.Pp
198Many of these correspond to equivalent parts of messages described in the
199.Xr syslog 3
200API.
201Values associated with these message keys are assigned appropriate defaults.
202The value for ASL_KEY_HOST is the local host name,
203the value associated with ASL_KEY_SENDER is the process name,
204the ASL_KEY_PID is the client's process ID number, and so on.
205.Pp
206Note the addition of the UID and GID keys.
207The values for UID and GID are set in library code by the message sender.
208The server will attempt to confirm the values,
209but no claim is made that these values cannot be maliciously overridden
210in an attempt to deceive a log message reader
211as to the identity of the sender of a message.
212The contents of log messages must be regarded as insecure.
213.Pp
214The
215.Xr asl 3
216API does not require a process to choose a facility name.
217The
218.Nm syslogd
219server will use a default value of
220.Dq user
221if a facility is not set.
222However, a client may set a facility name as an argument in the
223.Nm asl_open
224call, or by setting a specific value for the ASL_KEY_FACILITY in a message:
225.Pp
226 asl_set(m, ASL_KEY_FACILITY, "com.somename.greatservice");
227.Pp
228An application may choose any facility name at will.
229Different facility names may be attached to different messages, perhaps to distinguish different subsystems in log messages.
230Developers are encouraged to adopt a
231.Dq Reverse ICANN
232naming convention to avoid conflicting facility names.
233.Pp
234Default values are set in the message for each of the keys listed above,
235except for ASL_KEY_MSG,
236which may be explicitly set at any time using the
237.Nm asl_set
238routine, or implicitly set at the time the message is sent using the
239.Nm asl_log
240or
241.Nm asl_vlog
242routines.
243These two routines also have an integer-level parameter
244for specifying the log priority.
245The ASL_KEY_LEVEL value is set accordingly.
246Finally, the value associated with ASL_KEY_TIME
247is set in the sending routine.
248.Pp
249Although it may appear that there is significant overhead required
250to send a log message using this API,
251the opposite is actually true.
252A simple
253.Dq Hello World
254program requires only:
255.Pp
256 #include <asl.h>
257 ...
258 asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
259.Pp
260Both
261.Nm asl_log
262and
263.Nm asl_vlog
264will provide the appropriate default values
265when passed a NULL aslmsg argument.
266.Pp
267.Pp
268In this example, the aslclient argument is NULL.
269This is sufficient for a single-threaded application,
270or for an application which only sends log messages from a single thread.
271When logging from multiple threads,
272each thread must open a separate client handle using
273.Nm asl_open .
274The client handle may then be closed when it is no longer required using
275.Nm asl_close .
276.Pp
277When an application requires additional keys and values
278to be associated with each log message,
279a single message structure may be allocated and set up as
280.Dq template
281message of sorts:
282.Pp
283 aslmsg m = asl_new(ASL_TYPE_MSG);
284 asl_set(m, ASL_KEY_FACILITY, "com.secrets.r.us");
285 asl_set(m, "Clearance", "Top Secret");
286 ...
287 asl_log(NULL, m, ASL_LEVEL_NOTICE, "Message One");
288 ...
289 asl_log(NULL, m, ASL_LEVEL_ERR, "Message Two");
290.Pp
291The message structure will carry the values set for the
292.Dq Facility
293and
294.Dq Clearance
295keys so that they are used in each call to
296.Nm asl_log ,
297while the log level and the message text
298are taken from the calling parameters.
299.Pp
300The
301.Ar format
302argument to
303.Nm asl_log
304and
305.Nm asl_vlog
306is identical to
307.Xr printf 3 ,
308and may include
309.Ql %m ,
310which is replaced by the current error message
311(as denoted by the global variable
312.Va errno ;
313see
314.Xr strerror 3 . )
315.Pp
316Key/value pairs may be removed from a message structure with
317.Nm asl_unset .
318A message may be freed using
319.Nm asl_free .
320.Pp
321The
322.Nm asl_send
323routine is used by
324.Nm asl_log
325and
326.Nm asl_vlog
327to transmit a message to the server.
328This routine sets the value associated with ASL_KEY_TIME
329and sends the message.
330It may be called directly if all of a message's key/value pairs
331have been created using
332.Nm asl_set .
333.Ss SECURITY
334Messages that are sent to the
335.Nm syslogd
336server may be saved in a message store.
337The store may be searched using
338.Nm asl_search ,
339as described below.
340By default, all messages are readable by any user.
341However, some applications may wish to restrict read access
342for some messages.
343To accomodate this,
344a client may set a value for the "ReadUID" and "ReadGID" keys.
345These keys may be associated with a value
346containing an ASCII representation of a numeric UID or GID.
347Only the root user (UID 0),
348the user with the given UID,
349or a member of the group with the given GID
350may fetch access-controlled messages from the database.
351.Pp
352Although the ASL system does not require a "Facility" key in a message,
353many processes specify a "Facility" value similar
354to the common usage of the BSD
355.Nm syslog
356API, although developers are encouraged to adopt facility names that make sense for their application.
357A
358.Dq Reverse ICANN
359naming convention (e.g. "com.apple.system.syslog") should be adopted to avoid conflicting names.
360The ASL system generally allows any string to be used as a facility value,
361with one exception.
362The value "com.apple.system",
363or any string that has "com.apple.system" as a prefix,
364may only be used by processes running with the UID 0.
365This allows system processes to log messages that can not be "spoofed" by user processes.
366Non-UID 0 client processes that specify "com.apple.system" as a facility, will be assigned the value "user"
367by the
368.Nm syslogd
369server.
370.Ss CLIENT HANDLES
371When logging is done from a single thread,
372a NULL value may be used in any of the routines
373that require an aslclient argument.
374In this case, the library will open an internal client handle
375on behalf of the application.
376.Pp
377If multiple threads must do logging,
378or if client options are desired,
379then the application should call
380.Nm asl_open
381to create a client handle for each thread.
382As a convenience,
383the
384.Nm asl_open
385routine may be given an ident argument,
386which becomes the default value for the ASL_KEY_SENDER key,
387and a facility argument,
388which becomes the value associated with the ASL_KEY_FACILITY key.
389.Pp
390Several options are available when creating a client handle.
391They are:
392.Pp
393.Bl -tag -width "ASL_OPT_NO_REMOTE" -compact
394.It ASL_OPT_STDERR
395adds stderr as an output file descriptor
396.It ASL_OPT_NO_DELAY
397connects to the server immediately
398.It ASL_OPT_NO_REMOTE
399disables remote-control filter adjustment
400.El
401.Pp
402See the FILTERING section below, and the
403.Xr syslog 1
404for additional details on filter controls.
405.Pp
406A client handle is closed and it's resources released using
407.Nm asl_close .
408Note that if additional file descriptors were added to the handle,
409either using the ASL_OPT_STDERR option
410or afterwards with the
411.Nm asl_add_log_file
412routine, those file descriptors are not closed by
413.Nm asl_close .
414.Ss LOGGING TO ADDITIONAL FILES
415If a client handle is opened with the ASL_OPT_STDERR option to
416.Nm asl_open ,
417a copy of each log message will be sent to stderr.
418Additional output streams may be include using
419.Nm asl_add_log_file .
420.Pp
421Messages sent to stderr or other files are printed in the "standard" message format
422also used as a default format by the
423.Xr syslog 1
424command line utility.
425The
426.Xr strvis 3
427encoding with the VIS_CSTYLE, VIS_TAB, and VIS_NL options is used to print the message.
428.Pp
429File descriptors may be removed from the list of outputs associated
430with a client handle with
431.Nm asl_remove_log_file .
432This routine simply removes the file descriptor from the output list.
433The file is not closed as a result.
434.Pp
435The ASL_OPT_STDERR option may not be unset
436after a client handle has been opened.
437.Pp
438In the present release of Mac OS X, a
439.Dq raw
440format is used to format messages
441that are sent to file descriptors
442that have been added to a client handle.
443Each message is preceded by a 10-character field containing a message length.
444The message length is padded with leading white space.
445The length gives the string length of the remainder of the output string.
446Following the length is a space character, and then the message.
447The message is encoded as a set of key/value pairs
448enclosed in square brackets,
449which are themselves separated by a space character.
450The key is separated from the value by space character.
451Embedded closing square brackets are escaped by a backslash.
452Embedded space characters in keys are escaped by a backslash;
453Embedded newlines are summarily turned into semicolons.
454The output is terminated by a trailing newline and a NUL character.
455.Ss SEARCHING
456The
457.Nm syslogd
458server archives received messages in a data store
459that may be searched using the
460.Nm asl_search ,
461.Nm aslresponse_next ,
462and
463.Nm aslresponse_free
464routines.
465A query message is created using:
466.Pp
467 aslmsg q = asl_new(ASL_TYPE_QUERY);
468.Pp
469Search settings are made in the query using
470.Nm asl_set_query .
471A search is performed on the data store with
472.Nm asl_search .
473It returns an
474.Ft aslresponse
475structure.
476The caller may then call
477.Nm aslresponse_next
478to iterate through matching messages.
479The
480.Ft aslresponse
481structure may be freed with
482.Nm aslresponse_free .
483.Pp
484Like other messages, ASL_TYPE_QUERY messages contain keys and values.
485They also associate an operation with each key and value.
486The operation is used to decide if a message matches the query.
487The simplest operation is ASL_QUERY_OP_EQUAL, which tests for equality.
488For example, the following code snippet searches for messages
489with a Sender value equal to
490.Dq MyApp .
491.Pp
492 aslmsg m;
493 aslresponse r;
494 q = asl_new(ASL_TYPE_QUERY);
495 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
496 r = asl_search(NULL, q);
497.Pp
498More complex searches may be performed using other query operations.
499.Pp
500.Bl -tag -width "ASL_QUERY_OP_GREATER_EQUAL" -compact
501.It ASL_QUERY_OP_EQUAL
502value equality
503.It ASL_QUERY_OP_GREATER
504value greater than
505.It ASL_QUERY_OP_GREATER_EQUAL
506value greater than or equal to
507.It ASL_QUERY_OP_LESS
508value less than
509.It ASL_QUERY_OP_LESS_EQUAL
510value less than or equal to
511.It ASL_QUERY_OP_NOT_EQUAL
512value not equal
513.It ASL_QUERY_OP_REGEX
514regular expression search
515.It ASL_QUERY_OP_TRUE
516always true - use to test for the existence of a key
517.El
518.Pp
519Regular expression search uses
520.Xr regex 3
521library.
522Patterns are compiled using the REG_EXTENDED and REG_NOSUB options.
523.Pp
524Modifiers that change the behavior of these operations
525may also be specified by ORing the modifier value with the operation.
526The modifiers are:
527.Pp
528.Bl -tag -width "ASL_QUERY_OP_SUBSTRING" -compact
529.It ASL_QUERY_OP_CASEFOLD
530string comparisons are case-folded
531.It ASL_QUERY_OP_PREFIX
532match a leading substring
533.It ASL_QUERY_OP_SUFFIX
534match a trailing substring
535.It ASL_QUERY_OP_SUBSTRING
536match any substring
537.It ASL_QUERY_OP_NUMERIC
538values are converted to integer using
539.Nm atoi
540.El
541.Pp
542The only modifier that is checked
543for ASL_QUERY_OP_REGEX search is ASL_QUERY_OP_CASEFOLD.
544This causes the regular expression to be compiled
545with the REG_ICASE option.
546.Pp
547If a query message contains more than one set of key/value/operation triples,
548the result will be a logical AND. For example, to find messages from
549.Dq MyApp
550with a priority level less than or equal to
551.Dq 3 :
552.Pp
553 aslmsg q;
554 aslresponse r;
555 q = asl_new(ASL_TYPE_QUERY);
556 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
557 asl_set_query(q, ASL_KEY_LEVEL, "3",
558 ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
559 r = asl_search(NULL, q);
560.Pp
561After calling
562.Nm asl_search
563to get an
564.Ft aslresponse
565structure, use
566.Nm aslresponse_next
567to iterate through all matching messages.
568To iterate through the keys and values in a message, use
569.Nm asl_key
570to iterate through the keys, then call
571.Nm asl_get
572to get the value associated with each key.
573.Pp
574 aslmsg q, m;
575 int i;
576 const char *key, *val;
577.Pp
578 ...
579 r = asl_search(NULL, q);
580 while (NULL != (m = aslresponse_next(r)))
581 {
582 for (i = 0; (NULL != (key = asl_key(m, i))); i++)
583 {
584 val = asl_get(m, key);
585 ...
586 }
587 }
588 aslresponse_free(r);
589.Pp
590.Ss FILTERING AND REMOTE CONTROL
591Clients may set a filter mask value with
592.Nm asl_set_filter .
593The mask specifies which messages should be sent to the
594.Nm syslogd
595daemon by specifying a yes/no setting for each priority level.
596Clients typically set a filter mask
597to avoid sending relatively unimportant messages.
598For example, Debug or Info priority level messages
599are generally only useful for debugging operations.
600By setting a filter mask, a process can improve performance
601by avoiding sending messages that are in most cases unnecessary.
602.Pp
603.Nm asl_set_filter returns the previous value of the filter, i.e. the value of the filter before the routine was called.
604.Pp
605As a convenience, the macros ASL_FILTER_MASK(level) and ASL_FILTER_MASK_UPTO(level)
606may be used to construct a bit mask corresponding to a given priority level,
607or corresponding to a bit mask for all priority levels
608from ASL_LEVEL_EMERG to a given input level.
609.Pp
610The default filter mask is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
611This means that by default,
612and in the absence of remote-control changes (described below),
613ASL_LEVEL_DEBUG and ASL_LEVEL_INFO priority level messages
614are not sent to the
615.Mn syslogd
616server.
617.Pp
618Three different filters exist for each application.
619The first is the filter mask set using
620.Nm asl_set_filter
621as described above.
622The Apple System Log facility also manages a
623.Dq master
624filter mask.
625The master filter mask usually has a value
626that indicates to the library that it is
627.Dq off ,
628and thus it has no effect.
629However, the mask filter mask may be enabled
630by giving it a value using the
631.Nm syslog
632command, using the
633.Fl c
6340 option.
635When the master filter mask has been set,
636it takes precedence over the client's filter mask.
637The client's mask is unmodified,
638and will become active again if remote-control filtering is disabled.
639.Pp
640In addition to the master filter mask,
641The Apple System Log facility
642also manages a per-client remote-control filter mask.
643Like the master filter mask, the per-client mask is usually
644.Dq off ,
645having no effect on a client.
646If a per-client filter mask is set using the
647.Nm syslog
648command, using the
649.Fl c Ar process
650option, then it takes precedence
651over both the client's filter mask and the master filter mask.
652As is the case with the master filter mask,
653a per-client mask ceases having any effect when if is disabled.
654.Pp
655The ASL_OPT_NO_REMOTE option to
656.Nm asl_open
657causes both the master and per-client remote-control masks
658to be ignored in the library.
659In that case, only the client's own filter mask
660is used to determine which messages are sent to the server.
661This may be useful for Applications that produce log messages
662that should never be filtered, due to security considerations.
663Note that root (administrator) access is required
664to set or change the master filter mask,
665and that only root may change a per-client remote-control filter mask
666for a root (UID 0) process.
667.Sh HISTORY
668These functions first appeared in
669Mac OS X 10.4.
670.Sh SEE ALSO
671.Xr syslog 1 ,
672.Xr strvis 3 ,
673.Xr syslogd 8