1/*
2 * Copyright (c) 2007-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#ifndef __ASL_FILE_H__
25#define __ASL_FILE_H__
26
27#include <stdio.h>
28#include <stdint.h>
29#include <sys/types.h>
30#include <asl.h>
31#include <asl_msg.h>
32#include <asl_msg_list.h>
33#include <Availability.h>
34#include <os/object.h>
35#include <os/object_private.h>
36
37#define DB_HEADER_LEN 80
38#define DB_HEADER_COOKIE_OFFSET 0
39#define DB_HEADER_VERS_OFFSET 12
40#define DB_HEADER_FIRST_OFFSET 16
41#define DB_HEADER_TIME_OFFSET 24
42#define DB_HEADER_CSIZE_OFFSET 32
43#define DB_HEADER_FILTER_MASK_OFFSET 36
44#define DB_HEADER_LAST_OFFSET 37
45
46/*
47 * Magic Cookie for database files.
48 * MAXIMUM 12 CHARS! (DB_HEADER_VERS_OFFSET)
49 */
50#define ASL_DB_COOKIE "ASL DB"
51#define ASL_DB_COOKIE_LEN 6
52#define DB_VERSION 2
53#define DB_VERSION_LEGACY_1 1
54
55#define ASL_FILE_FLAG_READ            0x00000001
56#define ASL_FILE_FLAG_WRITE           0x00000002
57#define ASL_FILE_FLAG_UNLIMITED_CACHE 0x00000004
58#define ASL_FILE_FLAG_PRESERVE_MSG_ID 0x00000008
59#define ASL_FILE_FLAG_LEGACY_STORE    0x00000010
60
61#define ASL_FILE_TYPE_MSG 0
62#define ASL_FILE_TYPE_STR 1
63
64#define ASL_FILE_POSITION_FIRST 0
65#define ASL_FILE_POSITION_PREVIOUS 1
66#define ASL_FILE_POSITION_NEXT 2
67#define ASL_FILE_POSITION_LAST 3
68
69/* flags for asl_file_filter */
70#define ASL_FILE_FILTER_FLAG_KEEP_MATCHES 0x00000001
71
72/* NB CACHE_SIZE must be > 1 */
73#define CACHE_SIZE 256
74
75/* Size of the fixed-length part of a MSG record */
76#define MSG_RECORD_FIXED_LENGTH 122
77
78/*
79 * The first record (header) in the database has the format:
80 *
81 * | 12     | 4    | 8      | 8    | 4                 | 8    | 1    | 35   | (80 bytes)
82 * | Cookie | Vers | First  | Time | String cache size | Last | Mask | Zero |
83 *
84 * MSG records have the format:
85 *
86 * | 2  | 4   | 8    | 8  | 8    | 4    | 2     | 2     | 4   | 4   | 4   | 4    | 4    | 4      | 4
87 * | 00 | Len | Next | ID | Time | Nano | Level | Flags | PID | UID | GID | RUID | RGID | RefPID | KV count ...
88 *
89 * | 8    | 8      | 8        | 8       | 8       | 8       | 8    | 8    |     | 8
90 * | Host | Sender | Facility | Message | RefProc | Session | Key0 | Val0 | ... | Previous |
91 *
92 * STR records have the format:
93 *
94 * | 2  | 4   | Len      | (Len + 6 bytes)
95 * | 01 | Len | Data+NUL |
96 *
97 */
98
99typedef struct file_string_s
100{
101	uint64_t where;
102	uint32_t hash;
103	struct file_string_s *next;
104	char str[];
105} file_string_t;
106
107typedef struct asl_file_s
108{
109	uint32_t asl_type;	//ASL OBJECT HEADER
110	int32_t refcount;	//ASL OBJECT HEADER
111	uint32_t flags;
112	uint32_t version;
113	uint32_t string_cache_count;
114	uint32_t msg_count;
115	file_string_t *string_list;
116	uint64_t first;
117	uint64_t last;
118	uint64_t prev;
119	uint64_t cursor;
120	uint64_t cursor_xid;
121	uint64_t dob;
122	size_t file_size;
123	FILE *store;
124	void *legacy;
125	char *scratch;
126} asl_file_t;
127
128typedef struct asl_file_list_s
129{
130	asl_file_t *file;
131	struct asl_file_list_s *next;
132} asl_file_list_t;
133
134__BEGIN_DECLS
135
136const asl_jump_table_t *asl_file_jump_table(void);
137
138asl_file_list_t *asl_file_list_add(asl_file_list_t *list, asl_file_t *f) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
139void asl_file_list_close(asl_file_list_t *list) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
140
141asl_file_t *asl_file_retain(asl_file_t *s) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_7_0);
142void asl_file_release(asl_file_t *s) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_7_0);
143
144uint32_t asl_file_open_write(const char *path, mode_t mode, uid_t uid, gid_t gid, asl_file_t **s) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
145uint32_t asl_file_close(asl_file_t *s) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
146
147uint32_t asl_file_save(asl_file_t *s, asl_msg_t *msg, uint64_t *mid) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
148
149uint32_t asl_file_open_read(const char *path, asl_file_t **s) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
150uint32_t asl_file_fetch(asl_file_t *s, uint64_t mid, asl_msg_t **msg) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
151
152uint32_t asl_file_read_set_position(asl_file_t *s, uint32_t pos) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
153uint32_t asl_file_fetch_next(asl_file_t *s, asl_msg_t **msg) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
154uint32_t asl_file_fetch_previous(asl_file_t *s, asl_msg_t **msg) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
155
156asl_msg_list_t *asl_file_match(asl_file_t *s, asl_msg_list_t *query, uint64_t *last, uint64_t start, uint32_t count, uint32_t duration, int32_t direction) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
157asl_msg_list_t *asl_file_list_match(asl_file_list_t *list, asl_msg_list_t *query, uint64_t *last, uint64_t start, uint32_t count, uint32_t duration, int32_t direction) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
158
159void *asl_file_list_match_start(asl_file_list_t *list, uint64_t start_id, int32_t direction) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
160uint32_t asl_file_list_match_next(void *token, asl_msg_list_t *query, asl_msg_list_t **res, uint32_t count) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
161void asl_file_list_match_end(void *token) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
162
163size_t asl_file_size(asl_file_t *s) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
164uint64_t asl_file_ctime(asl_file_t *s) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
165
166uint32_t asl_file_compact(asl_file_t *s, const char *path, mode_t mode, uid_t uid, gid_t gid) __OSX_AVAILABLE_STARTING(__MAC_10_5, __IPHONE_2_0);
167uint32_t asl_file_filter(asl_file_t *s, const char *path, asl_msg_list_t *filter, uint32_t flags, mode_t mode, uid_t uid, gid_t gid, uint32_t *dstcount, void (*aux_callback)(const char *auxfile)) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_7_0);
168uint32_t asl_file_filter_level(asl_file_t *s, const char *path, uint32_t keep_mask, mode_t mode, uid_t uid, gid_t gid, uint32_t *dstcount, void (*aux_callback)(const char *auxfile)) __OSX_AVAILABLE_STARTING(__MAC_10_10, __IPHONE_7_0);
169
170__END_DECLS
171
172#endif /* __ASL_FILE_H__ */
173