1/*
2 * Copyright (c) 2012 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_COMMON_H__
25#define __ASL_COMMON_H__
26
27#include <stdio.h>
28#include <xpc/xpc.h>
29
30#define ASL_MODULE_NAME "com.apple.asl"
31#define _PATH_CRASHREPORTER "/Library/Logs/CrashReporter"
32#define _PATH_CRASHREPORTER_MOBILE "/var/mobile/Library/Logs/CrashReporter"
33
34#define ASL_SERVICE_NAME "com.apple.system.logger"
35
36#define CRASH_MOVER_WILL_START_NOTIFICATION  "CrashMoverWillStart"
37
38#define DEFAULT_TTL 7 /* days */
39#define SECONDS_PER_DAY 86400
40
41#define ACTION_NONE       0
42#define ACTION_SET_PARAM  1
43#define ACTION_OUT_DEST   2
44#define ACTION_IGNORE     3
45#define ACTION_SKIP       4
46#define ACTION_CLAIM      5
47#define ACTION_NOTIFY     6
48#define ACTION_BROADCAST  7
49#define ACTION_ACCESS     8
50#define ACTION_SET_KEY    9
51#define ACTION_UNSET_KEY 10
52#define ACTION_ASL_STORE 11 /* Save in main ASL Database */
53#define ACTION_ASL_FILE  12 /* Save in an ASL format data file */
54#define ACTION_ASL_DIR   13 /* Save in an ASL directory */
55#define ACTION_FILE      14 /* Save in a text file */
56#define ACTION_FORWARD   15
57#define ACTION_CONTROL   16
58#define ACTION_SET_FILE  17 /* = foo [File /a/b/c] */
59#define ACTION_SET_PLIST 18 /* = foo [Plist /a/b/c] ... */
60#define ACTION_SET_PROF  19 /* = foo [Profile abc] ... */
61
62#define STYLE_SEC_PREFIX_CHAR 'T'
63
64#define MODULE_FLAG_HAS_LOGGED   0x80000000
65#define MODULE_FLAG_CLEAR_LOGGED 0x7fffffff
66#define MODULE_FLAG_ENABLED      0x00000001
67#define MODULE_FLAG_LOCAL        0x00000002
68#define MODULE_FLAG_ROTATE       0x00000004
69#define MODULE_FLAG_COALESCE     0x00000008
70#define MODULE_FLAG_COMPRESS     0x00000010
71#define MODULE_FLAG_NONSTD_DIR   0x00000020
72#define MODULE_FLAG_EXTERNAL     0x00000040
73#define MODULE_FLAG_TRUNCATE     0x00000080
74#define MODULE_FLAG_STYLE_SEC    0x00000100 /* foo.T1332799722 (note STYLE_SEC_PREFIX_CHAR) */
75#define MODULE_FLAG_STYLE_SEQ    0x00000200 /* foo.0 */
76#define MODULE_FLAG_STYLE_UTC    0x00000400 /* foo.2012-04-06T13:45:00Z */
77#define MODULE_FLAG_STYLE_UTC_B  0x00000800 /* ("basic utc") foo.20120406T134500Z */
78#define MODULE_FLAG_STYLE_LCL    0x00001000 /* foo.2012-04-06T13:45:00-7 */
79#define MODULE_FLAG_STYLE_LCL_B  0x00002000 /* ("basic local") foo.20120406T134500-7 */
80#define MODULE_FLAG_BASESTAMP    0x00004000 /* base file has timestamp */
81#define MODULE_FLAG_CRASHLOG     0x00008000 /* checkpoint on CrashMoverWillStart notification */
82#define MODULE_FLAG_SOFT_WRITE   0x00010000 /* ignore write failures */
83#define MODULE_FLAG_TYPE_ASL     0x00020000 /* asl format file */
84#define MODULE_FLAG_TYPE_ASL_DIR 0x00040000 /* asl format directory */
85#define MODULE_FLAG_STD_BSD_MSG  0x00080000 /* print format is std, bsd, or msg */
86
87#define MODULE_FLAG_STYLE_BITS   (MODULE_FLAG_STYLE_SEC | MODULE_FLAG_STYLE_SEQ | MODULE_FLAG_STYLE_UTC | MODULE_FLAG_STYLE_UTC_B | MODULE_FLAG_STYLE_LCL | MODULE_FLAG_STYLE_LCL_B)
88#define CHECKPOINT_TEST   0x00000000
89#define CHECKPOINT_FORCE  0x00000001
90#define CHECKPOINT_CRASH  0x00000002
91
92#define LEVEL_ALL 8
93
94typedef struct
95{
96	char *path;
97	char *fname;
98	char *fmt;
99	const char *tfmt;
100	char *rotate_dir;
101	uint32_t pvt_flags;
102	uint32_t flags;
103	uint32_t fails;
104	uint32_t ttl[9];
105	mode_t mode;
106#if !TARGET_IPHONE_SIMULATOR
107	uid_t *uid;
108	uint32_t nuid;
109	gid_t *gid;
110	uint32_t ngid;
111#endif
112	size_t file_max;
113	size_t all_max;
114	uint32_t refcount;
115	time_t stamp;
116	size_t size;
117	void *private;
118} asl_out_dst_data_t;
119
120typedef struct asl_out_rule_s
121{
122	asl_msg_t *query;
123	uint32_t action;
124	char *options;
125	asl_out_dst_data_t *dst;
126	void *private;
127	struct asl_out_rule_s *next;
128} asl_out_rule_t;
129
130typedef struct asl_out_module_s
131{
132	char *name;
133	uint32_t flags;
134	asl_out_rule_t *ruleset;
135	struct asl_out_module_s *next;
136} asl_out_module_t;
137
138typedef struct asl_out_file_list_s
139{
140	char *name;
141	time_t ftime;
142	uint32_t seq;
143	size_t size;
144	struct asl_out_file_list_s *next;
145	struct asl_out_file_list_s *prev;
146} asl_out_file_list_t;
147
148char **explode(const char *s, const char *delim);
149void free_string_list(char **l);
150char *get_line_from_file(FILE *f);
151char *next_word_from_string(char **s);
152size_t asl_str_to_size(char *s);
153asl_msg_t *xpc_object_to_asl_msg(xpc_object_t xobj);
154
155int asl_check_option(asl_msg_t *msg, const char *opt);
156
157/* ASL OUT MODULES */
158asl_out_module_t *asl_out_module_new(const char *name);
159void asl_out_module_free(asl_out_module_t *m);
160asl_out_module_t *asl_out_module_init(void);
161asl_out_module_t *asl_out_module_init_from_file(const char *name, FILE *f);
162asl_out_rule_t *asl_out_module_parse_line(asl_out_module_t *m, char *s);
163
164void asl_out_module_print(FILE *f, asl_out_module_t *m);
165char *asl_out_module_rule_to_string(asl_out_rule_t *r);
166
167int asl_out_mkpath(asl_out_module_t *mlist, asl_out_rule_t *r);
168int asl_out_dst_checkpoint(asl_out_dst_data_t *dst, uint32_t force);
169int asl_out_dst_file_create_open(asl_out_dst_data_t *dst, char **pathp);
170int asl_out_dst_set_access(int fd, asl_out_dst_data_t *dst);
171void asl_make_timestamp(time_t stamp, uint32_t flags, char *buf, size_t len);
172void asl_make_dst_filename(asl_out_dst_data_t *dst, char *buf, size_t len);
173
174asl_out_dst_data_t *asl_out_dst_data_retain(asl_out_dst_data_t *dst);
175void asl_out_dst_data_release(asl_out_dst_data_t *dst);
176
177/* rotated log files */
178asl_out_file_list_t *asl_list_log_files(const char *dir, const char *base, bool src, uint32_t flags);
179asl_out_file_list_t * asl_list_src_files(asl_out_dst_data_t *dst);
180asl_out_file_list_t * asl_list_dst_files(asl_out_dst_data_t *dst);
181void asl_out_file_list_free(asl_out_file_list_t *l);
182
183asl_msg_t *configuration_profile_to_asl_msg(const char *ident);
184
185int asl_secure_chown_chmod_dir(const char *path, uid_t uid, gid_t gid, mode_t mode);
186
187#endif /* __ASL_COMMON_H__ */
188