1 .\" Copyright (c) 2005-2007 Apple Inc.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
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.
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
33 .Nm asl_add_log_file ,
41 .Nm asl_remove_log_file ,
49 .Nm aslresponse_free ,
51 .Nd system log message sending and searching functions
83 .Fa "const char *format"
92 .Fa "const char *ident"
93 .Fa "const char *facility"
97 .Fo asl_remove_log_file
114 .Fa "const char *key"
115 .Fa "const char *value"
125 .Fa "const char *key"
126 .Fa "const char *value"
132 .Fa "const char *key"
139 .Fa "const char *format"
151 These routines provide an interface to the Apple System Log facility.
152 They are intended to be a replacement for the
154 API, which will continue to be supported for backwards compatibility.
155 The new API allows client applications
156 to create flexible, structured messages and send them to the
158 server, where they may undergo additional processing.
159 Messages received by the server are saved in a data store
160 (subject to input filtering constraints).
161 This API permits clients to create queries
162 and search the message data store for matching messages.
164 An introduction to the concepts underlying this interface follows the interface summary below.
165 .Ss INTERFACE SUMMARY
171 creates and returns a client handle, or NULL if an error occurs in the library.
172 Messages sent using this handle will default to having the string
174 as the value assocated with the ASL_KEY_SENDER key, and the value
176 assocated with the ASL_KEY_FACILITY key.
177 Several options are available, as described in the
181 Multi-threaded applications should create one client handle for each thread that logs messages.
182 A client may use NULL as a client handle, in which case a default handle managed by the library will be used.
183 A NULL handle may be used safely by multiple threads, but the threads will contend for a single internal lock when
184 sending log messages using a NULL handle.
189 closes the client handle
191 and releases its associated resources.
197 adds the file descriptor
199 to the a set of file descriptors associated with the client handle
201 Each log message sent by that client handle is also written to these file descriptors.
202 Returns 0 on success, non-zero on failure.
204 .Fo asl_remove_log_file
208 removes a file descriptor from the set of file descriptors associated with a client handle.
209 Returns 0 on success, non-zero on failure.
214 allocates and returns an aslmsg structure, or NULL in the case of a failure in the library.
217 argument must be ASL_TYPE_MSG or ASL_TYPE_QUERY.
222 frees an aslmsg and releases resources associated with the structure.
229 creates a new key and value in an aslmsg structure, or replaces the value of an existing key.
230 Returns 0 on success, non-zero on failure.
238 is used to construct searches.
241 except that it takes an additional
243 (operation) argument.
244 Creates a new (key, op, value) triple in an aslmsg structure,
245 or replaces the value and operation for an existing key.
248 section for more information.
249 Returns 0 on success, non-zero on failure.
255 removes a key and its associated value from an aslmsg structure.
256 Returns 0 on success, non-zero on failure.
262 returns the nth key in an aslmsg (beginning at zero),
263 allowing an application to iterate through the keys.
266 indexes beyond the number of keys in
273 returns the value associated with
286 sets a filter for messages being sent to the server.
287 The filter is a bitmask representing priority levels.
288 Only messages having a priority level with a corresponding bit set in the filter mask are sent to the
291 The filter does not control writes to additional files associated with the client handle using
292 .Fn asl_add_log_file .
293 Returns the previous filter value.
302 sends a log to the server (subject to filtering, see
304 above) and to any file descriptors associated with the client handle
308 argument may contain any keys and values, which will be formatted as part of the log message.
309 The value for ASL_KEY_LEVEL is supplied by the
312 The value for ASL_KEY_MESSAGE is computed from
314 and the associated arguments
318 style argument processing is applied to the format and the arguments.
319 The format may also contain
321 which will be substituted with the string value corresponding to the current
333 except that it takes a va_list argument.
341 exceopt the value for ASL_KEY_MESSAGE is taken from
343 rather than being constructed using a
351 searches for messages that match the keys and values in
353 subject to matching operations associated with those keys and values.
356 argument should be constructed using
360 section for details on constructing queries.
361 Returns an aslresponse structure that contains matching log messages.
362 NULL is returned in case of error or if there are no matching messages in the ASL database.
367 iterates over an aslresponse structure returned by
369 Each call returns the next aslmsg in the response.
370 Returns NULL when there are no further messages.
375 frees the aslresponse structure
377 and all of its associated resources.
379 At the core of this API is the aslmsg structure.
380 Although the structure is opaque and may not be directly manipulated,
381 it contains a list of key/value pairs.
382 All keys and values are NUL-character terminated C language strings.
383 UTF-8 encoding may be used for non-ASCII characters.
385 Message structures are generally used to send log messages,
386 and are created thusly:
388 aslmsg m = asl_new(ASL_TYPE_MSG);
390 Another message type, ASL_TYPE_QUERY,
391 is used to create queries when searching the data store.
392 Query type messages and searching are described in detail in the
395 For the remainder of this section,
396 the messages described will be of the ASL_TYPE_MSG variety.
398 Each aslmsg contains a default set of keys
399 and values that are associated with them.
400 These keys are listed in the asl.h header file.
403 #define ASL_KEY_TIME "Time"
404 #define ASL_KEY_HOST "Host"
405 #define ASL_KEY_SENDER "Sender"
406 #define ASL_KEY_FACILITY "Facility"
407 #define ASL_KEY_PID "PID"
408 #define ASL_KEY_UID "UID"
409 #define ASL_KEY_GID "GID"
410 #define ASL_KEY_LEVEL "Level"
411 #define ASL_KEY_MSG "Message"
413 Many of these correspond to equivalent parts of messages described in the
416 Values associated with these message keys are assigned appropriate defaults.
417 The value for ASL_KEY_HOST is the local host name,
418 the value associated with ASL_KEY_SENDER is the process name,
419 the ASL_KEY_PID is the client's process ID number, and so on.
421 Note the addition of the UID and GID keys.
422 The values for UID and GID are set in library code by the message sender.
423 The server will attempt to confirm the values,
424 but no claim is made that these values cannot be maliciously overridden
425 in an attempt to deceive a log message reader
426 as to the identity of the sender of a message.
427 The contents of log messages must be regarded as insecure.
431 API does not require a process to choose a facility name.
434 server will use a default value of
436 if a facility is not set.
437 However, a client may set a facility name as an argument in the
439 call, or by setting a specific value for the ASL_KEY_FACILITY in a message:
441 asl_set(m, ASL_KEY_FACILITY, "com.somename.greatservice");
443 An application may choose any facility name at will.
444 Different facility names may be attached to different messages, perhaps to distinguish different subsystems in log messages.
445 Developers are encouraged to adopt a
447 naming convention to avoid conflicting facility names.
449 Default values are set in the message for each of the keys listed above,
450 except for ASL_KEY_MSG,
451 which may be explicitly set at any time using the
453 routine, or implicitly set at the time the message is sent using the
458 These two routines also have an integer-level parameter
459 for specifying the log priority.
460 The ASL_KEY_LEVEL value is set accordingly.
461 Finally, the value associated with ASL_KEY_TIME
462 is set in the sending routine.
464 Although it may appear that there is significant overhead required
465 to send a log message using this API,
466 the opposite is actually true.
469 program requires only:
473 asl_log(NULL, NULL, ASL_LEVEL_INFO, "Hello World!");
479 will provide the appropriate default values
480 when passed a NULL aslmsg argument.
483 In this example, the aslclient argument is NULL.
484 This is sufficient for a single-threaded application,
485 or for an application which only sends log messages from a single thread.
486 When logging from multiple threads,
489 open a separate client handle using
491 The client handle may then be closed when it is no longer required using
493 Multiple threads may log messages safely using a NULL aslclient argument,
494 but the library will use an internal lock, so that in fact only one thread
497 When an application requires additional keys and values
498 to be associated with each log message,
499 a single message structure may be allocated and set up as
503 aslmsg m = asl_new(ASL_TYPE_MSG);
504 asl_set(m, ASL_KEY_FACILITY, "com.secrets.r.us");
505 asl_set(m, "Clearance", "Top Secret");
507 asl_log(NULL, m, ASL_LEVEL_NOTICE, "Message One");
509 asl_log(NULL, m, ASL_LEVEL_ERR, "Message Two");
511 The message structure will carry the values set for the
515 keys so that they are used in each call to
517 while the log level and the message text
518 are taken from the calling parameters.
530 which is replaced by the current error message
531 (as denoted by the global variable
536 Key/value pairs may be removed from a message structure with
538 A message may be freed using
547 to transmit a message to the server.
548 This routine sets the value associated with ASL_KEY_TIME
549 and sends the message.
550 It may be called directly if all of a message's key/value pairs
551 have been created using
554 Messages that are sent to the
556 server may be saved in a message store.
557 The store may be searched using
560 By default, all messages are readable by any user.
561 However, some applications may wish to restrict read access
564 a client may set a value for the "ReadUID" and "ReadGID" keys.
565 These keys may be associated with a value
566 containing an ASCII representation of a numeric UID or GID.
567 Only the root user (UID 0),
568 the user with the given UID,
569 or a member of the group with the given GID
570 may fetch access-controlled messages from the database.
572 Although the ASL system does not require a "Facility" key in a message,
573 many processes specify a "Facility" value similar
574 to the common usage of the BSD
576 API, although developers are encouraged to adopt facility names that make sense for their application.
579 naming convention (e.g. "com.apple.system.syslog") should be adopted to avoid conflicting names.
580 The ASL system generally allows any string to be used as a facility value,
582 The value "com.apple.system",
583 or any string that has "com.apple.system" as a prefix,
584 may only be used by processes running with the UID 0.
585 This allows system processes to log messages that can not be "spoofed" by user processes.
586 Non-UID 0 client processes that specify "com.apple.system" as a facility, will be assigned the value "user"
591 When logging is done from a single thread,
592 a NULL value may be used in any of the routines
593 that require an aslclient argument.
594 In this case, the library will open an internal client handle
595 on behalf of the application.
597 If multiple threads must do logging,
598 or if client options are desired,
599 then the application should call
601 to create a client handle for each thread.
605 routine may be given an ident argument,
606 which becomes the default value for the ASL_KEY_SENDER key,
607 and a facility argument,
608 which becomes the value associated with the ASL_KEY_FACILITY key.
610 Several options are available when creating a client handle.
613 .Bl -tag -width "ASL_OPT_NO_REMOTE" -compact
615 adds stderr as an output file descriptor
617 connects to the server immediately
618 .It ASL_OPT_NO_REMOTE
619 disables remote-control filter adjustment
622 ASL_OPT_NO_DELAY makes the client library connect to the
624 server at the time that
626 is called, rather than waiting for the first message to be sent.
627 Opening the connection is quite fast, but some applications may want to avoid any unnecessary delays when calling
633 See the FILTERING section below, and the
635 for additional details on filter controls.
637 A client handle is closed and it's resources released using
639 Note that if additional file descriptors were added to the handle,
640 either using the ASL_OPT_STDERR option
641 or afterwards with the
643 routine, those file descriptors are not closed by
645 .Ss LOGGING TO ADDITIONAL FILES
646 If a client handle is opened with the ASL_OPT_STDERR option to
648 a copy of each log message will be sent to stderr.
649 Additional output streams may be include using
650 .Nm asl_add_log_file .
652 Messages sent to stderr or other files are printed in the "standard" message format
653 also used as a default format by the
655 command line utility.
656 Non-ASCII characters in a message are encoded using the
658 encoding style used by
663 Backspace characters are printed as ^H.
664 Carriage returns are mapped to newlines.
665 A tab character is appended after newlines so that message text is indented.
667 File descriptors may be removed from the list of outputs associated
668 with a client handle with
669 .Nm asl_remove_log_file .
670 This routine simply removes the file descriptor from the output list.
671 The file is not closed as a result.
673 The ASL_OPT_STDERR option may not be unset
674 after a client handle has been opened.
678 server archives received messages in a data store
679 that may be searched using the
681 .Nm aslresponse_next ,
685 A query message is created using:
687 aslmsg q = asl_new(ASL_TYPE_QUERY);
689 Search settings are made in the query using
691 A search is performed on the data store with
696 The caller may then call
698 to iterate through matching messages.
701 structure may be freed with
702 .Nm aslresponse_free .
704 Like other messages, ASL_TYPE_QUERY messages contain keys and values.
705 They also associate an operation with each key and value.
706 The operation is used to decide if a message matches the query.
707 The simplest operation is ASL_QUERY_OP_EQUAL, which tests for equality.
708 For example, the following code snippet searches for messages
709 with a Sender value equal to
714 q = asl_new(ASL_TYPE_QUERY);
715 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
716 r = asl_search(NULL, q);
718 More complex searches may be performed using other query operations.
720 .Bl -tag -width "ASL_QUERY_OP_GREATER_EQUAL" -compact
721 .It ASL_QUERY_OP_EQUAL
723 .It ASL_QUERY_OP_GREATER
725 .It ASL_QUERY_OP_GREATER_EQUAL
726 value greater than or equal to
727 .It ASL_QUERY_OP_LESS
729 .It ASL_QUERY_OP_LESS_EQUAL
730 value less than or equal to
731 .It ASL_QUERY_OP_NOT_EQUAL
733 .It ASL_QUERY_OP_REGEX
734 regular expression search
735 .It ASL_QUERY_OP_TRUE
736 always true - use to test for the existence of a key
739 Regular expression search uses
742 Patterns are compiled using the REG_EXTENDED and REG_NOSUB options.
744 Modifiers that change the behavior of these operations
745 may also be specified by ORing the modifier value with the operation.
748 .Bl -tag -width "ASL_QUERY_OP_SUBSTRING" -compact
749 .It ASL_QUERY_OP_CASEFOLD
750 string comparisons are case-folded
751 .It ASL_QUERY_OP_PREFIX
752 match a leading substring
753 .It ASL_QUERY_OP_SUFFIX
754 match a trailing substring
755 .It ASL_QUERY_OP_SUBSTRING
757 .It ASL_QUERY_OP_NUMERIC
758 values are converted to integer using
762 The only modifier that is checked
763 for ASL_QUERY_OP_REGEX search is ASL_QUERY_OP_CASEFOLD.
764 This causes the regular expression to be compiled
765 with the REG_ICASE option.
767 If a query message contains more than one set of key/value/operation triples,
768 the result will be a logical AND. For example, to find messages from
770 with a priority level less than or equal to
775 q = asl_new(ASL_TYPE_QUERY);
776 asl_set_query(q, ASL_KEY_SENDER, "MyApp", ASL_QUERY_OP_EQUAL);
777 asl_set_query(q, ASL_KEY_LEVEL, "3",
778 ASL_QUERY_OP_LESS_EQUAL | ASL_QUERY_OP_NUMERIC);
779 r = asl_search(NULL, q);
787 to iterate through all matching messages.
788 To iterate through the keys and values in a message, use
790 to iterate through the keys, then call
792 to get the value associated with each key.
796 const char *key, *val;
799 r = asl_search(NULL, q);
800 while (NULL != (m = aslresponse_next(r)))
802 for (i = 0; (NULL != (key = asl_key(m, i))); i++)
804 val = asl_get(m, key);
810 .Ss FILTERING AND REMOTE CONTROL
811 Clients may set a filter mask value with
813 The mask specifies which messages should be sent to the
815 daemon by specifying a yes/no setting for each priority level.
816 Clients typically set a filter mask
817 to avoid sending relatively unimportant messages.
818 For example, Debug or Info priority level messages
819 are generally only useful for debugging operations.
820 By setting a filter mask, a process can improve performance
821 by avoiding sending messages that are in most cases unnecessary.
823 .Nm asl_set_filter returns the previous value of the filter, i.e. the value of the filter before the routine was called.
825 As a convenience, the macros ASL_FILTER_MASK(level) and ASL_FILTER_MASK_UPTO(level)
826 may be used to construct a bit mask corresponding to a given priority level,
827 or corresponding to a bit mask for all priority levels
828 from ASL_LEVEL_EMERG to a given input level.
830 The default filter mask is ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE).
831 This means that by default,
832 and in the absence of remote-control changes (described below),
833 ASL_LEVEL_DEBUG and ASL_LEVEL_INFO priority level messages
838 Three different filters exist for each application.
839 The first is the filter mask set using
842 The Apple System Log facility also manages a
845 The master filter mask usually has a value
846 that indicates to the library that it is
848 and thus it has no effect.
849 However, the mask filter mask may be enabled
850 by giving it a value using the
855 When the master filter mask has been set,
856 it takes precedence over the client's filter mask.
857 The client's mask is unmodified,
858 and will become active again if remote-control filtering is disabled.
860 In addition to the master filter mask,
861 The Apple System Log facility
862 also manages a per-client remote-control filter mask.
863 Like the master filter mask, the per-client mask is usually
865 having no effect on a client.
866 If a per-client filter mask is set using the
870 option, then it takes precedence
871 over both the client's filter mask and the master filter mask.
872 As is the case with the master filter mask,
873 a per-client mask ceases having any effect when if is disabled.
875 The ASL_OPT_NO_REMOTE option to
877 causes both the master and per-client remote-control masks
878 to be ignored in the library.
879 In that case, only the client's own filter mask
880 is used to determine which messages are sent to the server.
881 This may be useful for Applications that produce log messages
882 that should never be filtered, due to security considerations.
883 Note that root (administrator) access is required
884 to set or change the master filter mask,
885 and that only root may change a per-client remote-control filter mask
886 for a root (UID 0) process.
888 The per-process remote control filter value is kept as a state value
889 associated with a key managed by
891 The key is protected by an access control mechanism that only permits the
892 filter value to be accessed and modified by the same effective UID as the
893 ASL client at the time that the first ASL connection was created.
894 Remote filter control using
896 will fail for processes that change effective UID after starting an ASL connection.
897 Those processes should close all ASL client handles and then re-open ASL connections
898 if remote filter control support is desired.
900 These functions first appeared in