]> git.saurik.com Git - apple/libc.git/blame - gen/asl.3
Libc-763.13.tar.gz
[apple/libc.git] / gen / asl.3
CommitLineData
1f2f436a 1.\" Copyright (c) 2005-2010 Apple Inc.
3d9156a7
A
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.\"
1f2f436a 29.Dd March 3, 2010
3d9156a7
A
30.Dt asl 3
31.Os "Mac OS X"
32.Sh NAME
224c7076 33.Nm asl_add_log_file ,
3d9156a7 34.Nm asl_close ,
1f2f436a
A
35.Nm asl_close_auxiliary_file ,
36.Nm asl_create_auxiliary_file ,
3d9156a7 37.Nm asl_free ,
3d9156a7 38.Nm asl_get ,
3d9156a7 39.Nm asl_key ,
224c7076 40.Nm asl_log ,
1f2f436a 41.Nm asl_log_auxiliary_location ,
224c7076
A
42.Nm asl_new ,
43.Nm asl_open ,
1f2f436a 44.Nm asl_open_from_file ,
3d9156a7 45.Nm asl_remove_log_file ,
3d9156a7 46.Nm asl_search ,
224c7076
A
47.Nm asl_send ,
48.Nm asl_set ,
49.Nm asl_set_filter ,
50.Nm asl_set_query ,
51.Nm asl_unset ,
52.Nm asl_vlog ,
53.Nm aslresponse_free ,
54.Nm aslresponse_next
3d9156a7
A
55.Nd system log message sending and searching functions
56.Sh SYNOPSIS
57.Fd #include <asl.h>
224c7076
A
58.\"
59.Ft int
60.Fo asl_add_log_file
61.Fa "aslclient asl"
62.Fa "int fd"
63.Fc
3d9156a7 64.Ft void
224c7076
A
65.Fo asl_close
66.Fa "aslclient asl"
67.Fc
3d9156a7 68.Ft void
1f2f436a
A
69.Fo asl_close_auxiliary_file
70.Fa "int fd"
71.Fc
72.Ft void
73.Fo asl_create_auxiliary_file
74.Fa "aslmsg msg"
75.Fa "const char *title"
76.Fa "const char *uti"
77.Fa "int *out_fd"
78.Fc
79.Ft void
224c7076
A
80.Fo asl_free
81.Fa "aslmsg msg"
82.Fc
3d9156a7 83.Ft const char *
224c7076
A
84.Fo asl_get
85.Fa "aslmsg msg"
86.Fa "const char *key"
87.Fc
3d9156a7 88.Ft const char *
224c7076
A
89.Fo asl_key
90.Fa "aslmsg msg"
91.Fa "uint32_t n"
92.Fc
3d9156a7 93.Ft int
224c7076
A
94.Fo asl_log
95.Fa "aslclient asl"
96.Fa "aslmsg msg"
97.Fa "int level"
98.Fa "const char *format"
99.Fa "..."
100.Fc
1f2f436a
A
101.Ft void
102.Fo asl_log_auxiliary_location
103.Fa "aslmsg msg"
104.Fa "const char *title"
105.Fa "const char *uti"
106.Fa "const char *url"
107.Fc
224c7076
A
108.Ft aslmsg
109.Fo asl_new
110.Fa "uint32_t type"
111.Fc
112.Ft aslclient
113.Fo asl_open
114.Fa "const char *ident"
115.Fa "const char *facility"
116.Fa "uint32_t opts"
117.Fc
1f2f436a
A
118.Ft aslclient
119.Fo asl_open_from_file
120.Fa "int fd"
121.Fa "const char *ident"
122.Fa "const char *facility"
123.Fc
3d9156a7 124.Ft int
224c7076
A
125.Fo asl_remove_log_file
126.Fa "aslclient asl"
127.Fa "int fd"
128.Fc
129.Ft aslresponse
130.Fo asl_search
131.Fa "aslclient asl"
132.Fa "aslmsg msg"
133.Fc
3d9156a7 134.Ft int
224c7076
A
135.Fo asl_send
136.Fa "aslclient asl"
137.Fa "aslmsg msg"
138.Fc
3d9156a7 139.Ft int
224c7076
A
140.Fo asl_set
141.Fa "aslmsg msg"
142.Fa "const char *key"
143.Fa "const char *value"
144.Fc
3d9156a7 145.Ft int
224c7076
A
146.Fo asl_set_filter
147.Fa "aslclient asl"
148.Fa "int f"
149.Fc
3d9156a7 150.Ft int
224c7076
A
151.Fo asl_set_query
152.Fa "aslmsg msg"
153.Fa "const char *key"
154.Fa "const char *value"
155.Fa "uint32_t op"
156.Fc
3d9156a7 157.Ft int
224c7076
A
158.Fo asl_unset
159.Fa "aslmsg msg"
160.Fa "const char *key"
161.Fc
162.Ft int
163.Fo asl_vlog
164.Fa "aslclient asl"
165.Fa "aslmsg msg"
166.Fa "int level"
167.Fa "const char *format"
168.Fa "va_list ap"
169.Fc
3d9156a7 170.Ft void
224c7076 171.Fo aslresponse_free
b5d655f7 172.Fa "aslresponse r"
224c7076
A
173.Fc
174.Ft aslmsg
175.Fo aslresponse_next
176.Fa "aslresponse r"
177.Fc
3d9156a7 178.Sh DESCRIPTION
224c7076 179These routines provide an interface to the Apple System Log facility.
3d9156a7
A
180They are intended to be a replacement for the
181.Xr syslog 3
182API, which will continue to be supported for backwards compatibility.
224c7076
A
183The new API allows client applications
184to create flexible, structured messages and send them to the
3d9156a7
A
185.Nm syslogd
186server, where they may undergo additional processing.
224c7076
A
187Messages received by the server are saved in a data store
188(subject to input filtering constraints).
189This API permits clients to create queries
190and search the message data store for matching messages.
b5d655f7
A
191.Pp
192An introduction to the concepts underlying this interface follows the interface summary below.
193.Ss INTERFACE SUMMARY
194.Fo asl_open
195.Fa ident
196.Fa facility
197.Fa opts
198.Fc
199creates and returns a client handle, or NULL if an error occurs in the library.
200Messages sent using this handle will default to having the string
201.Ar ident
1f2f436a 202as the value associated with the ASL_KEY_SENDER key, and the value
b5d655f7 203.Ar facility
1f2f436a 204associated with the ASL_KEY_FACILITY key.
b5d655f7
A
205Several options are available, as described in the
206.Sx CLIENT HANDLES
207section.
208.Pp
209Multi-threaded applications should create one client handle for each thread that logs messages.
210A client may use NULL as a client handle, in which case a default handle managed by the library will be used.
211A NULL handle may be used safely by multiple threads, but the threads will contend for a single internal lock when
212sending log messages using a NULL handle.
213.Pp
214.Fo asl_close
215.Fa asl
216.Fc
217closes the client handle
218.Ar asl
219and releases its associated resources.
220.Pp
221.Fo asl_add_log_file
222.Fa asl
223.Fa fd
224.Fc
225adds the file descriptor
226.Ar fd
227to the a set of file descriptors associated with the client handle
228.Ar asl .
229Each log message sent by that client handle is also written to these file descriptors.
230Returns 0 on success, non-zero on failure.
231.Pp
232.Fo asl_remove_log_file
233.Fa asl
234.Fa fd
235.Fc
236removes a file descriptor from the set of file descriptors associated with a client handle.
237Returns 0 on success, non-zero on failure.
238.Pp
239.Fo asl_new
240.Fa type
241.Fc
242allocates and returns an aslmsg structure, or NULL in the case of a failure in the library.
243The
244.Ar type
245argument must be ASL_TYPE_MSG or ASL_TYPE_QUERY.
246.Pp
247.Fo asl_free
248.Fa msg
249.Fc
250frees an aslmsg and releases resources associated with the structure.
251.Pp
252.Fo asl_set
253.Fa msg
254.Fa key
255.Fa value
256.Fc
257creates a new key and value in an aslmsg structure, or replaces the value of an existing key.
258Returns 0 on success, non-zero on failure.
259.Pp
260.Fo asl_set_query
261.Fa msg
262.Fa key
263.Fa op
264.Fa value
265.Fc
266is used to construct searches.
267It is similar to
268.Fn asl_set ,
269except that it takes an additional
270.Ar op
271(operation) argument.
272Creates a new (key, op, value) triple in an aslmsg structure,
273or replaces the value and operation for an existing key.
274See the
275.Sx SEARCHING
276section for more information.
277Returns 0 on success, non-zero on failure.
278.Pp
279.Fo asl_unset
280.Fa msg
281.Fa key
282.Fc
283removes a key and its associated value from an aslmsg structure.
284Returns 0 on success, non-zero on failure.
285.Pp
286.Fo asl_key
287.Fa msg
288.Fa n
289.Fc
290returns the nth key in an aslmsg (beginning at zero),
291allowing an application to iterate through the keys.
292Returns NULL if
293.Ar n
294indexes beyond the number of keys in
295.Ar msg .
296.Pp
297.Fo asl_get
298.Fa msg
299.Fa key
300.Fc
301returns the value associated with
302.Ar key
303in the aslmsg
304.Ar msg .
305Returns NULL if
306.Ar msg
307does not contain
308. Ar key .
309.Pp
310.Fo asl_set_filter
311.Fa asl
312.Fa f
313.Fc
314sets a filter for messages being sent to the server.
315The filter is a bitmask representing priority levels.
316Only messages having a priority level with a corresponding bit set in the filter mask are sent to the
317.Nm syslogd
318server.
319The filter does not control writes to additional files associated with the client handle using
320.Fn asl_add_log_file .
321Returns the previous filter value.
322.Pp
323.Fo asl_log
324.Fa asl
325.Fa msg
326.Fa level
327.Fa format
328.Fa args...
329.Fc
330sends a log to the server (subject to filtering, see
331.Fn asl_set_filter
332above) and to any file descriptors associated with the client handle
333.Ar asl .
334The
335.Ar msg
336argument may contain any keys and values, which will be formatted as part of the log message.
337The value for ASL_KEY_LEVEL is supplied by the
338.Ar level
339argument.
340The value for ASL_KEY_MESSAGE is computed from
341.Ar format
342and the associated arguments
343.Ar args... .
344Normal
345.Fn printf
346style argument processing is applied to the format and the arguments.
347The format may also contain
348.Dq %m
349which will be substituted with the string value corresponding to the current
350.Em errno .
351.Pp
352.Fo asl_vlog
353.Fa asl
354.Fa msg
355.Fa level
356.Fa format
357.Fa ap
358.Fc
359is similar to
360.Fn asl_log
361except that it takes a va_list argument.
362.Pp
363.Fo asl_send
364.Fa asl
365.Fa msg
366.Fc
367is similar to
368.Fn asl_log ,
369exceopt the value for ASL_KEY_MESSAGE is taken from
370.Ar msg
371rather than being constructed using a
372.Fn printf
373style syntax.
374.Pp
375.Fo asl_search
376.Fa asl
377.Fa msg
378.Fc
379searches for messages that match the keys and values in
380.Ar msg ,
381subject to matching operations associated with those keys and values.
382The
383.Ar msg
384argument should be constructed using
385.Fn asl_set_query .
386See the
387.Sx SEARCHING
388section for details on constructing queries.
389Returns an aslresponse structure that contains matching log messages.
390NULL is returned in case of error or if there are no matching messages in the ASL database.
391.Pp
392.Fo aslresponse_next
393.Fa r
394.Fc
395iterates over an aslresponse structure returned by
396.Fn asl_search .
397Each call returns the next aslmsg in the response.
398Returns NULL when there are no further messages.
399.Pp
400.Fo aslresponse_free
401.Fa r
402.Fc
403frees the aslresponse structure
404.Ar r
405and all of its associated resources.
1f2f436a
A
406.Pp
407.Fo asl_create_auxiliary_file
408.Fa msg
409.Fa title
410.Fa uti
411.Fa out_fd
412.Fc
413Creates an auxiliary file that may be used by the client to save arbitrary data.
414When the file is closed using
415.Fo asl_close_auxiliary_file
416.Fc ,
417.Nm syslogd
418will log the specified
419.Fa msg
420along with the
421.Fa title
422and the Uniform Type Identifier provided by
423.Fa uti .
424If a NULL value is supplied for
425.Fa uti
426the type
427.Dq public.data
428will be used.
429The
430.Nm Console
431application will display the message with a link to the file.
432.Pp
433Auxiliary files are saved in the ASL data store.
434They are automatically deleted at the same time that the log message expires.
435Messages expire in 7 days by default.
436A value set for the ASLExpireTime key will override the default.
437Read access for the auxiliary file will be the same as read access for
438.Fa msg .
439By default, messages (and auxiliary files) are world-readable.
440Access may be limited by setting values for the ReadUID and ReadGID keys.
441.Pp
442.Fo asl_close_auxiliary_file
443.Fa fd
444.Fc
445closes the file descriptor
446.Ar fd
447previously returned by a call to
448.Fn asl_create_auxiliary_file .
449.Pp
450.Fo asl_log_auxiliary_location
451.Fa msg
452.Fa title
453.Fa uti
454.Fa url
455.Fc
456will log the specified
457.Fa msg
458along with the
459.Fa title ,
460the Uniform Type Identifier provided by
461.Fa uti ,
462and the Uniform Resource Locator provided by
463.Fa url .
464The
465.Nm Console
466application will display the message with a link to the file.
467This allows a client to save data in an auxiliary file, but unlike
468.Fo asl_create_auxiliary_file
469.Fc ,
470the life-cycle of this file must be managed by some external system.
471The file will not be removed when the corresponding log message expired from the ASL data store.
472.Pp
473.Fo asl_open_from_file
474.Fa fd
475.Fa facility
476.Fa opts
477.Fc
478creates a client handle for an open file descriptor
479.Fa fd .
480This routine may be used in conjunction with
481.Fo asl_create_auxiliary_file
482.Fc
483or
484.Fo asl_log_auxiliary_location
485.Fc
486to save ASL format log messages in an auxiliary file.
487The UTI type
488.Dq com.apple.asl-file
489should be used for ASL format auxiliary files.
490.Pp
491Files with this format may be read from the command line using
492.Nm syslog Fl f Ar file ,
493or from the
494.Nm Console
495utility.
496.Pp
497The file must be open for read and write access.
498The file will be truncated and its existing contents will be lost.
499.Fo asl_close
500.Fc
501must be called to close the client handle when logging to this file is complete.
502The file should be closed using
503.Fo asl_close_auxiliary_file
504.Fc
505if it was returned by
506.Fo asl_create_auxiliary_file
507.Fc ,
508or
509.Fo close
510.Fc
511otherwise.
512.Pp
513The client handle may be used safely by multiple threads.
514The threads will contend for a single internal lock when saving log messages to a file.
515.Pp
516Note that messages with ReadUID or ReadGID values will simply be saved to the file,
517and will not effect read access to either the message or the file itself.
518Similarly, messages with ASLExpireTime values will be saved, but will not effect the
519life-cycle of either the individual messages or the file.
3d9156a7
A
520.Ss MESSAGES
521At the core of this API is the aslmsg structure.
224c7076
A
522Although the structure is opaque and may not be directly manipulated,
523it contains a list of key/value pairs.
b5d655f7 524All keys and values are NUL-character terminated C language strings.
3d9156a7
A
525UTF-8 encoding may be used for non-ASCII characters.
526.Pp
224c7076
A
527Message structures are generally used to send log messages,
528and are created thusly:
3d9156a7
A
529.Pp
530 aslmsg m = asl_new(ASL_TYPE_MSG);
531.Pp
224c7076
A
532Another message type, ASL_TYPE_QUERY,
533is used to create queries when searching the data store.
3d9156a7
A
534Query type messages and searching are described in detail in the
535.Sx SEARCHING
b5d655f7 536section.
224c7076
A
537For the remainder of this section,
538the messages described will be of the ASL_TYPE_MSG variety.
3d9156a7 539.Pp
224c7076
A
540Each aslmsg contains a default set of keys
541and values that are associated with them.
3d9156a7
A
542These keys are listed in the asl.h header file.
543They are:
544.Pp
224c7076
A
545 #define ASL_KEY_TIME "Time"
546 #define ASL_KEY_HOST "Host"
547 #define ASL_KEY_SENDER "Sender"
548 #define ASL_KEY_FACILITY "Facility"
549 #define ASL_KEY_PID "PID"
550 #define ASL_KEY_UID "UID"
551 #define ASL_KEY_GID "GID"
552 #define ASL_KEY_LEVEL "Level"
553 #define ASL_KEY_MSG "Message"
3d9156a7
A
554.Pp
555Many of these correspond to equivalent parts of messages described in the
556.Xr syslog 3
557API.
558Values associated with these message keys are assigned appropriate defaults.
559The value for ASL_KEY_HOST is the local host name,
560the value associated with ASL_KEY_SENDER is the process name,
561the ASL_KEY_PID is the client's process ID number, and so on.
562.Pp
563Note the addition of the UID and GID keys.
564The values for UID and GID are set in library code by the message sender.
224c7076
A
565The server will attempt to confirm the values,
566but no claim is made that these values cannot be maliciously overridden
567in an attempt to deceive a log message reader
568as to the identity of the sender of a message.
3d9156a7
A
569The contents of log messages must be regarded as insecure.
570.Pp
3d9156a7
A
571The
572.Xr asl 3
573API does not require a process to choose a facility name.
574The
575.Nm syslogd
576server will use a default value of
577.Dq user
578if a facility is not set.
224c7076
A
579However, a client may set a facility name as an argument in the
580.Nm asl_open
581call, or by setting a specific value for the ASL_KEY_FACILITY in a message:
3d9156a7 582.Pp
224c7076 583 asl_set(m, ASL_KEY_FACILITY, "com.somename.greatservice");
3d9156a7
A
584.Pp
585An application may choose any facility name at will.
224c7076
A
586Different facility names may be attached to different messages, perhaps to distinguish different subsystems in log messages.
587Developers are encouraged to adopt a
588.Dq Reverse ICANN
589naming convention to avoid conflicting facility names.
590.Pp
591Default values are set in the message for each of the keys listed above,
592except for ASL_KEY_MSG,
593which may be explicitly set at any time using the
3d9156a7
A
594.Nm asl_set
595routine, or implicitly set at the time the message is sent using the
596.Nm asl_log
597or
598.Nm asl_vlog
599routines.
224c7076
A
600These two routines also have an integer-level parameter
601for specifying the log priority.
3d9156a7 602The ASL_KEY_LEVEL value is set accordingly.
224c7076
A
603Finally, the value associated with ASL_KEY_TIME
604is set in the sending routine.
3d9156a7 605.Pp
224c7076
A
606Although it may appear that there is significant overhead required
607to send a log message using this API,
3d9156a7
A
608the opposite is actually true.
609A simple
610.Dq Hello World
611program requires only:
612.Pp
613 #include <asl.h>
614 ...
615 asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
616.Pp
617Both
618.Nm asl_log
619and
620.Nm asl_vlog
224c7076
A
621will provide the appropriate default values
622when passed a NULL aslmsg argument.
3d9156a7
A
623.Pp
624.Pp
625In this example, the aslclient argument is NULL.
626This is sufficient for a single-threaded application,
627or for an application which only sends log messages from a single thread.
224c7076 628When logging from multiple threads,
b5d655f7
A
629each thread
630.Em should
631open a separate client handle using
3d9156a7
A
632.Nm asl_open .
633The client handle may then be closed when it is no longer required using
634.Nm asl_close .
b5d655f7
A
635Multiple threads may log messages safely using a NULL aslclient argument,
636but the library will use an internal lock, so that in fact only one thread
637will log at a time.
3d9156a7 638.Pp
224c7076
A
639When an application requires additional keys and values
640to be associated with each log message,
3d9156a7
A
641a single message structure may be allocated and set up as
642.Dq template
643message of sorts:
644.Pp
645 aslmsg m = asl_new(ASL_TYPE_MSG);
224c7076 646 asl_set(m, ASL_KEY_FACILITY, "com.secrets.r.us");
3d9156a7
A
647 asl_set(m, "Clearance", "Top Secret");
648 ...
649 asl_log(NULL, m, ASL_LEVEL_NOTICE, "Message One");
650 ...
651 asl_log(NULL, m, ASL_LEVEL_ERR, "Message Two");
652.Pp
653The message structure will carry the values set for the
654.Dq Facility
655and
656.Dq Clearance
657keys so that they are used in each call to
658.Nm asl_log ,
224c7076
A
659while the log level and the message text
660are taken from the calling parameters.
661.Pp
662The
663.Ar format
664argument to
665.Nm asl_log
666and
667.Nm asl_vlog
668is identical to
669.Xr printf 3 ,
670and may include
671.Ql %m ,
672which is replaced by the current error message
673(as denoted by the global variable
674.Va errno ;
675see
676.Xr strerror 3 . )
3d9156a7
A
677.Pp
678Key/value pairs may be removed from a message structure with
679.Nm asl_unset .
680A message may be freed using
681.Nm asl_free .
682.Pp
683The
684.Nm asl_send
685routine is used by
686.Nm asl_log
687and
688.Nm asl_vlog
689to transmit a message to the server.
224c7076
A
690This routine sets the value associated with ASL_KEY_TIME
691and sends the message.
692It may be called directly if all of a message's key/value pairs
693have been created using
3d9156a7 694.Nm asl_set .
224c7076
A
695.Ss SECURITY
696Messages that are sent to the
697.Nm syslogd
698server may be saved in a message store.
699The store may be searched using
700.Nm asl_search ,
701as described below.
702By default, all messages are readable by any user.
703However, some applications may wish to restrict read access
704for some messages.
705To accomodate this,
706a client may set a value for the "ReadUID" and "ReadGID" keys.
707These keys may be associated with a value
708containing an ASCII representation of a numeric UID or GID.
709Only the root user (UID 0),
710the user with the given UID,
711or a member of the group with the given GID
712may fetch access-controlled messages from the database.
713.Pp
714Although the ASL system does not require a "Facility" key in a message,
715many processes specify a "Facility" value similar
716to the common usage of the BSD
717.Nm syslog
718API, although developers are encouraged to adopt facility names that make sense for their application.
719A
720.Dq Reverse ICANN
721naming convention (e.g. "com.apple.system.syslog") should be adopted to avoid conflicting names.
722The ASL system generally allows any string to be used as a facility value,
723with one exception.
724The value "com.apple.system",
725or any string that has "com.apple.system" as a prefix,
726may only be used by processes running with the UID 0.
727This allows system processes to log messages that can not be "spoofed" by user processes.
728Non-UID 0 client processes that specify "com.apple.system" as a facility, will be assigned the value "user"
729by the
730.Nm syslogd
731server.
3d9156a7
A
732.Ss CLIENT HANDLES
733When logging is done from a single thread,
224c7076
A
734a NULL value may be used in any of the routines
735that require an aslclient argument.
736In this case, the library will open an internal client handle
737on behalf of the application.
3d9156a7
A
738.Pp
739If multiple threads must do logging,
740or if client options are desired,
741then the application should call
742.Nm asl_open
743to create a client handle for each thread.
744As a convenience,
745the
746.Nm asl_open
747routine may be given an ident argument,
748which becomes the default value for the ASL_KEY_SENDER key,
749and a facility argument,
224c7076 750which becomes the value associated with the ASL_KEY_FACILITY key.
3d9156a7
A
751.Pp
752Several options are available when creating a client handle.
753They are:
754.Pp
755.Bl -tag -width "ASL_OPT_NO_REMOTE" -compact
756.It ASL_OPT_STDERR
757adds stderr as an output file descriptor
758.It ASL_OPT_NO_DELAY
759connects to the server immediately
760.It ASL_OPT_NO_REMOTE
761disables remote-control filter adjustment
762.El
763.Pp
b5d655f7
A
764ASL_OPT_NO_DELAY makes the client library connect to the
765.Nm syslogd
766server at the time that
767.Nm asl_open
768is called, rather than waiting for the first message to be sent.
769Opening the connection is quite fast, but some applications may want to avoid any unnecessary delays when calling
770.Nm asl_log ,
771.Nm asl_vlog ,
772or
773.Nm asl_send .
774.Pp
3d9156a7
A
775See the FILTERING section below, and the
776.Xr syslog 1
777for additional details on filter controls.
778.Pp
779A client handle is closed and it's resources released using
780.Nm asl_close .
224c7076
A
781Note that if additional file descriptors were added to the handle,
782either using the ASL_OPT_STDERR option
783or afterwards with the
3d9156a7
A
784.Nm asl_add_log_file
785routine, those file descriptors are not closed by
786.Nm asl_close .
787.Ss LOGGING TO ADDITIONAL FILES
788If a client handle is opened with the ASL_OPT_STDERR option to
789.Nm asl_open ,
790a copy of each log message will be sent to stderr.
791Additional output streams may be include using
792.Nm asl_add_log_file .
224c7076
A
793.Pp
794Messages sent to stderr or other files are printed in the "standard" message format
795also used as a default format by the
796.Xr syslog 1
797command line utility.
b5d655f7
A
798Non-ASCII characters in a message are encoded using the
799.Dq safe
800encoding style used by
801.Xr syslog 1
802with the
803.Fl E Ar safe
804option.
805Backspace characters are printed as ^H.
806Carriage returns are mapped to newlines.
807A tab character is appended after newlines so that message text is indented.
224c7076
A
808.Pp
809File descriptors may be removed from the list of outputs associated
810with a client handle with
3d9156a7
A
811.Nm asl_remove_log_file .
812This routine simply removes the file descriptor from the output list.
813The file is not closed as a result.
814.Pp
224c7076
A
815The ASL_OPT_STDERR option may not be unset
816after a client handle has been opened.
3d9156a7
A
817.Ss SEARCHING
818The
819.Nm syslogd
224c7076
A
820server archives received messages in a data store
821that may be searched using the
3d9156a7
A
822.Nm asl_search ,
823.Nm aslresponse_next ,
824and
825.Nm aslresponse_free
826routines.
827A query message is created using:
828.Pp
829 aslmsg q = asl_new(ASL_TYPE_QUERY);
830.Pp
831Search settings are made in the query using
832.Nm asl_set_query .
833A search is performed on the data store with
834.Nm asl_search .
835It returns an
836.Ft aslresponse
837structure.
838The caller may then call
839.Nm aslresponse_next
840to iterate through matching messages.
841The
842.Ft aslresponse
843structure may be freed with
844.Nm aslresponse_free .
845.Pp
846Like other messages, ASL_TYPE_QUERY messages contain keys and values.
847They also associate an operation with each key and value.
848The operation is used to decide if a message matches the query.
849The simplest operation is ASL_QUERY_OP_EQUAL, which tests for equality.
224c7076
A
850For example, the following code snippet searches for messages
851with a Sender value equal to
3d9156a7
A
852.Dq MyApp .
853.Pp
854 aslmsg m;
855 aslresponse r;
856 q = asl_new(ASL_TYPE_QUERY);
857 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
858 r = asl_search(NULL, q);
859.Pp
860More complex searches may be performed using other query operations.
861.Pp
862.Bl -tag -width "ASL_QUERY_OP_GREATER_EQUAL" -compact
863.It ASL_QUERY_OP_EQUAL
864value equality
865.It ASL_QUERY_OP_GREATER
866value greater than
867.It ASL_QUERY_OP_GREATER_EQUAL
868value greater than or equal to
869.It ASL_QUERY_OP_LESS
870value less than
871.It ASL_QUERY_OP_LESS_EQUAL
872value less than or equal to
873.It ASL_QUERY_OP_NOT_EQUAL
874value not equal
875.It ASL_QUERY_OP_REGEX
876regular expression search
877.It ASL_QUERY_OP_TRUE
878always true - use to test for the existence of a key
879.El
880.Pp
881Regular expression search uses
882.Xr regex 3
883library.
884Patterns are compiled using the REG_EXTENDED and REG_NOSUB options.
885.Pp
224c7076
A
886Modifiers that change the behavior of these operations
887may also be specified by ORing the modifier value with the operation.
3d9156a7
A
888The modifiers are:
889.Pp
890.Bl -tag -width "ASL_QUERY_OP_SUBSTRING" -compact
891.It ASL_QUERY_OP_CASEFOLD
892string comparisons are case-folded
893.It ASL_QUERY_OP_PREFIX
894match a leading substring
895.It ASL_QUERY_OP_SUFFIX
896match a trailing substring
897.It ASL_QUERY_OP_SUBSTRING
898match any substring
899.It ASL_QUERY_OP_NUMERIC
900values are converted to integer using
901.Nm atoi
902.El
903.Pp
224c7076
A
904The only modifier that is checked
905for ASL_QUERY_OP_REGEX search is ASL_QUERY_OP_CASEFOLD.
906This causes the regular expression to be compiled
907with the REG_ICASE option.
3d9156a7
A
908.Pp
909If a query message contains more than one set of key/value/operation triples,
910the result will be a logical AND. For example, to find messages from
911.Dq MyApp
912with a priority level less than or equal to
913.Dq 3 :
914.Pp
915 aslmsg q;
916 aslresponse r;
917 q = asl_new(ASL_TYPE_QUERY);
918 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
919 asl_set_query(q, ASL_KEY_LEVEL, "3",
920 ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
921 r = asl_search(NULL, q);
922.Pp
923After calling
924.Nm asl_search
925to get an
926.Ft aslresponse
927structure, use
928.Nm aslresponse_next
929to iterate through all matching messages.
930To iterate through the keys and values in a message, use
931.Nm asl_key
932to iterate through the keys, then call
933.Nm asl_get
934to get the value associated with each key.
935.Pp
936 aslmsg q, m;
937 int i;
938 const char *key, *val;
939.Pp
940 ...
941 r = asl_search(NULL, q);
942 while (NULL != (m = aslresponse_next(r)))
943 {
944 for (i = 0; (NULL != (key = asl_key(m, i))); i++)
945 {
946 val = asl_get(m, key);
947 ...
948 }
949 }
950 aslresponse_free(r);
951.Pp
952.Ss FILTERING AND REMOTE CONTROL
953Clients may set a filter mask value with
954.Nm asl_set_filter .
955The mask specifies which messages should be sent to the
956.Nm syslogd
957daemon by specifying a yes/no setting for each priority level.
224c7076
A
958Clients typically set a filter mask
959to avoid sending relatively unimportant messages.
960For example, Debug or Info priority level messages
961are generally only useful for debugging operations.
962By setting a filter mask, a process can improve performance
963by avoiding sending messages that are in most cases unnecessary.
964.Pp
965.Nm asl_set_filter returns the previous value of the filter, i.e. the value of the filter before the routine was called.
3d9156a7
A
966.Pp
967As a convenience, the macros ASL_FILTER_MASK(level) and ASL_FILTER_MASK_UPTO(level)
968may be used to construct a bit mask corresponding to a given priority level,
224c7076
A
969or corresponding to a bit mask for all priority levels
970from ASL_LEVEL_EMERG to a given input level.
3d9156a7
A
971.Pp
972The default filter mask is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
224c7076
A
973This means that by default,
974and in the absence of remote-control changes (described below),
975ASL_LEVEL_DEBUG and ASL_LEVEL_INFO priority level messages
976are not sent to the
3d9156a7
A
977.Mn syslogd
978server.
979.Pp
980Three different filters exist for each application.
981The first is the filter mask set using
982.Nm asl_set_filter
983as described above.
984The Apple System Log facility also manages a
985.Dq master
986filter mask.
224c7076
A
987The master filter mask usually has a value
988that indicates to the library that it is
3d9156a7
A
989.Dq off ,
990and thus it has no effect.
224c7076
A
991However, the mask filter mask may be enabled
992by giving it a value using the
3d9156a7
A
993.Nm syslog
994command, using the
995.Fl c
9960 option.
997When the master filter mask has been set,
998it takes precedence over the client's filter mask.
224c7076
A
999The client's mask is unmodified,
1000and will become active again if remote-control filtering is disabled.
3d9156a7
A
1001.Pp
1002In addition to the master filter mask,
224c7076
A
1003The Apple System Log facility
1004also manages a per-client remote-control filter mask.
3d9156a7
A
1005Like the master filter mask, the per-client mask is usually
1006.Dq off ,
1007having no effect on a client.
1008If a per-client filter mask is set using the
1009.Nm syslog
1010command, using the
1011.Fl c Ar process
224c7076
A
1012option, then it takes precedence
1013over both the client's filter mask and the master filter mask.
1014As is the case with the master filter mask,
1015a per-client mask ceases having any effect when if is disabled.
3d9156a7
A
1016.Pp
1017The ASL_OPT_NO_REMOTE option to
1018.Nm asl_open
224c7076
A
1019causes both the master and per-client remote-control masks
1020to be ignored in the library.
1021In that case, only the client's own filter mask
1022is used to determine which messages are sent to the server.
1023This may be useful for Applications that produce log messages
1024that should never be filtered, due to security considerations.
1025Note that root (administrator) access is required
1026to set or change the master filter mask,
1027and that only root may change a per-client remote-control filter mask
1028for a root (UID 0) process.
34e8f829
A
1029.Pp
1030The per-process remote control filter value is kept as a state value
1031associated with a key managed by
1032.Nm notifyd .
1033The key is protected by an access control mechanism that only permits the
1034filter value to be accessed and modified by the same effective UID as the
1035ASL client at the time that the first ASL connection was created.
1036Remote filter control using
1037.Nm syslog Fl c
1038will fail for processes that change effective UID after starting an ASL connection.
1039Those processes should close all ASL client handles and then re-open ASL connections
1040if remote filter control support is desired.
3d9156a7
A
1041.Sh HISTORY
1042These functions first appeared in
1043Mac OS X 10.4.
1044.Sh SEE ALSO
224c7076
A
1045.Xr syslog 1 ,
1046.Xr strvis 3 ,
1047.Xr syslogd 8