2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
33 #include <sys/param.h>
36 #include <membership.h>
38 #include <TargetConditionals.h>
39 #include <configuration_profile.h>
43 #include "asl_common.h"
45 #define _PATH_ASL_CONF "/etc/asl.conf"
46 #define _PATH_ASL_CONF_DIR "/etc/asl"
48 #define PATH_VAR_LOG "/var/log/"
49 #define PATH_VAR_LOG_LEN 9
51 #define PATH_LIBRARY_LOGS "/Library/Logs/"
52 #define PATH_LIBRARY_LOGS_LEN 14
54 #if !TARGET_IPHONE_SIMULATOR
55 #define _PATH_ASL_CONF_LOCAL_DIR "/usr/local/etc/asl"
58 //#define DEBUG_LIST_FILES 1
60 static const char *asl_out_action_name
[] =
84 #define forever for(;;)
85 #define KEYMATCH(S,K) ((strncasecmp(S, K, strlen(K)) == 0))
88 xpc_object_to_asl_msg(xpc_object_t xobj
)
90 __block asl_msg_t
*out
;
92 if (xobj
== NULL
) return NULL
;
93 if (xpc_get_type(xobj
) != XPC_TYPE_DICTIONARY
) return NULL
;
95 out
= asl_msg_new(ASL_TYPE_MSG
);
96 xpc_dictionary_apply(xobj
, ^bool(const char *key
, xpc_object_t xval
) {
99 if (xpc_get_type(xval
) == XPC_TYPE_NULL
)
101 asl_msg_set_key_val_op(out
, key
, NULL
, 0);
103 else if (xpc_get_type(xval
) == XPC_TYPE_BOOL
)
105 if (xpc_bool_get_value(xval
)) asl_msg_set_key_val_op(out
, key
, "1", 0);
106 else asl_msg_set_key_val_op(out
, key
, "0", 0);
108 else if (xpc_get_type(xval
) == XPC_TYPE_INT64
)
110 snprintf(tmp
, sizeof(tmp
), "%lld", xpc_int64_get_value(xval
));
111 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
113 else if (xpc_get_type(xval
) == XPC_TYPE_UINT64
)
115 snprintf(tmp
, sizeof(tmp
), "%llu", xpc_uint64_get_value(xval
));
116 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
118 else if (xpc_get_type(xval
) == XPC_TYPE_DOUBLE
)
120 snprintf(tmp
, sizeof(tmp
), "%f", xpc_double_get_value(xval
));
121 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
123 else if (xpc_get_type(xval
) == XPC_TYPE_DATE
)
125 snprintf(tmp
, sizeof(tmp
), "%lld", xpc_date_get_value(xval
));
126 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
128 else if (xpc_get_type(xval
) == XPC_TYPE_DATA
)
130 size_t len
= xpc_data_get_length(xval
);
131 char *encoded
= asl_core_encode_buffer(xpc_data_get_bytes_ptr(xval
), len
);
132 asl_msg_set_key_val_op(out
, key
, encoded
, 0);
135 else if (xpc_get_type(xval
) == XPC_TYPE_STRING
)
137 asl_msg_set_key_val_op(out
, key
, xpc_string_get_string_ptr(xval
), 0);
139 else if (xpc_get_type(xval
) == XPC_TYPE_UUID
)
142 uuid_unparse(xpc_uuid_get_bytes(xval
), us
);
143 asl_msg_set_key_val_op(out
, key
, us
, 0);
145 else if (xpc_get_type(xval
) == XPC_TYPE_FD
)
147 /* XPC_TYPE_FD is not supported */
148 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_FD}", 0);
150 else if (xpc_get_type(xval
) == XPC_TYPE_SHMEM
)
152 /* XPC_TYPE_SHMEM is not supported */
153 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_SHMEM}", 0);
155 else if (xpc_get_type(xval
) == XPC_TYPE_ARRAY
)
157 /* XPC_TYPE_ARRAY is not supported */
158 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_ARRAY}", 0);
160 else if (xpc_get_type(xval
) == XPC_TYPE_DICTIONARY
)
162 /* XPC_TYPE_DICTIONARY is not supported */
163 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_DICTIONARY}", 0);
165 else if (xpc_get_type(xval
) == XPC_TYPE_ERROR
)
167 /* XPC_TYPE_ERROR is not supported */
168 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_ERROR}", 0);
173 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_???}", 0);
183 configuration_profile_to_asl_msg(const char *ident
)
185 xpc_object_t xobj
= configuration_profile_copy_property_list(ident
);
186 asl_msg_t
*out
= xpc_object_to_asl_msg(xobj
);
187 if (xobj
!= NULL
) xpc_release(xobj
);
191 /* strdup + skip leading and trailing whitespace */
193 _strdup_clean(const char *s
)
196 const char *first
, *last
;
199 if (s
== NULL
) return NULL
;
202 while ((*first
== ' ') || (*first
== '\t')) first
++;
204 if (len
== 0) return NULL
;
206 last
= first
+ len
- 1;
207 while ((len
> 0) && ((*last
== ' ') || (*last
== '\t')))
213 if (len
== 0) return NULL
;
215 out
= malloc(len
+ 1);
216 if (out
== NULL
) return NULL
;
218 memcpy(out
, first
, len
);
224 _insert_string(char *s
, char **l
, uint32_t x
)
228 if (s
== NULL
) return l
;
231 l
= (char **)malloc(2 * sizeof(char *));
232 if (l
== NULL
) return NULL
;
245 for (i
= 0; l
[i
] != NULL
; i
++);
247 /* len includes the NULL at the end of the list */
250 l
= (char **)reallocf(l
, (len
+ 1) * sizeof(char *));
251 if (l
== NULL
) return NULL
;
253 if ((x
>= (len
- 1)) || (x
== IndexNull
))
255 l
[len
- 1] = strdup(s
);
256 if (l
[len
- 1] == NULL
)
266 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
268 if (l
[x
] == NULL
) return NULL
;
274 explode(const char *s
, const char *delim
)
281 if (s
== NULL
) return NULL
;
289 for (i
= 0; p
[i
] != '\0'; i
++)
293 /* not inside a quoted string: check for delimiters and quotes */
294 if (strchr(delim
, p
[i
]) != NULL
) break;
295 else if (p
[i
] == '\'') quote
= p
[i
];
296 else if (p
[i
] == '"') quote
= p
[i
];
300 /* inside a quoted string - look for matching quote */
301 if (p
[i
] == quote
) quote
= '\0';
307 if (t
== NULL
) return NULL
;
309 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
311 l
= _insert_string(t
, l
, IndexNull
);
314 if (p
[i
] == '\0') return l
;
315 if (p
[i
+ 1] == '\0') l
= _insert_string("", l
, IndexNull
);
323 free_string_list(char **l
)
327 if (l
== NULL
) return;
328 for (i
= 0; l
[i
] != NULL
; i
++) free(l
[i
]);
333 get_line_from_file(FILE *f
)
338 out
= fgetln(f
, &len
);
339 if (out
== NULL
) return NULL
;
340 if (len
== 0) return NULL
;
343 if (s
== NULL
) return NULL
;
347 if (s
[len
- 1] != '\n') len
++;
353 next_word_from_string(char **s
)
355 char *a
, *p
, *e
, *out
, s0
;
356 int quote1
, quote2
, len
;
358 if (s
== NULL
) return NULL
;
359 if (*s
== NULL
) return NULL
;
368 /* allow whole word to be contained in quotes */
404 if (quote1
== 0) quote1
= 1;
410 if (quote2
== 0) quote2
= 1;
414 if (((*p
== ' ') || (*p
== '\t')) && (quote1
== 0) && (quote2
== 0))
428 /* check for quoted string */
429 if (((s0
== '\'') || (s0
== '"')) && (s0
== a
[len
-1])) len
--;
431 if (len
== 0) return NULL
;
433 out
= malloc(len
+ 1);
434 if (out
== NULL
) return NULL
;
442 asl_out_dest_for_path(asl_out_module_t
*m
, const char *path
)
444 if (m
== NULL
) return NULL
;
445 if (path
== NULL
) return NULL
;
449 asl_out_rule_t
*r
= m
->ruleset
;
452 if ((r
->action
== ACTION_OUT_DEST
) && (r
->dst
!= NULL
) && (r
->dst
->path
!= NULL
) && (streq(r
->dst
->path
, path
))) return r
->dst
;
463 * Create a directory path.
465 * mlist provides owner, group, and access mode, which are required for "non-standard"
466 * directories. Directories for standard paths (/var/log or /Library/Logs) default
467 * to root/admin/0755 if there is no mlist rule for them.
470 _asl_common_make_dir_path(asl_out_module_t
*mlist
, uint32_t flags
, const char *path
)
474 asl_string_t
*processed_path
;
477 if (path
== NULL
) return 0;
479 path_parts
= explode(path
, "/");
480 if (path_parts
== NULL
) return 0;
482 processed_path
= asl_string_new(ASL_ENCODE_NONE
);
485 if (path
[0] == '/') i
= 1;
487 for (; path_parts
[i
] != NULL
; i
++)
492 asl_out_dst_data_t
*dst
;
495 asl_string_append_char_no_encoding(processed_path
, '/');
496 asl_string_append_no_encoding(processed_path
, path_parts
[i
]);
497 tmp
= asl_string_bytes(processed_path
);
499 memset(&sb
, 0, sizeof(struct stat
));
500 status
= lstat(tmp
, &sb
);
501 if ((status
== 0) && S_ISLNK(sb
.st_mode
))
503 char real
[MAXPATHLEN
];
504 if (realpath(tmp
, real
) == NULL
)
506 asl_string_release(processed_path
);
507 free_string_list(path_parts
);
511 memset(&sb
, 0, sizeof(struct stat
));
512 status
= stat(real
, &sb
);
517 if (!S_ISDIR(sb
.st_mode
))
519 /* path component is not a directory! */
520 asl_string_release(processed_path
);
521 free_string_list(path_parts
);
525 /* exists and is a directory or a link to a directory */
528 else if (errno
!= ENOENT
)
530 /* unexpected status from stat() */
531 asl_string_release(processed_path
);
532 free_string_list(path_parts
);
536 dst
= asl_out_dest_for_path(mlist
, tmp
);
537 if ((dst
== NULL
) && (flags
& MODULE_FLAG_NONSTD_DIR
))
539 /* no rule to create a non-standard path component! */
540 asl_string_release(processed_path
);
541 free_string_list(path_parts
);
549 if (mode
== 010000) mode
= 0755;
553 status
= mkdir(tmp
, mode
);
556 #if !TARGET_IPHONE_SIMULATOR
562 if (dst
->nuid
> 0) u
= dst
->uid
[0];
563 if (dst
->ngid
> 0) g
= dst
->gid
[0];
570 asl_string_release(processed_path
);
571 free_string_list(path_parts
);
577 asl_out_mkpath(asl_out_module_t
*mlist
, asl_out_rule_t
*r
)
579 char tmp
[MAXPATHLEN
], *p
;
583 if (r
== NULL
) return -1;
584 if (r
->dst
== NULL
) return -1;
585 if (r
->dst
->path
== NULL
) return -1;
587 snprintf(tmp
, sizeof(tmp
), "%s", r
->dst
->path
);
589 if (r
->action
!= ACTION_ASL_DIR
)
591 p
= strrchr(tmp
, '/');
592 if (p
== NULL
) return -1;
596 memset(&sb
, 0, sizeof(struct stat
));
597 status
= stat(tmp
, &sb
);
600 if (S_ISDIR(sb
.st_mode
)) return 0;
606 uint32_t dirflag
= r
->dst
->flags
& MODULE_FLAG_NONSTD_DIR
;
607 status
= _asl_common_make_dir_path(mlist
, dirflag
, tmp
);
615 asl_make_database_dir(const char *dir
, char **out
)
617 const char *asldir
, *path
;
623 if (out
!= NULL
) *out
= NULL
;
625 asldir
= asl_filesystem_path(ASL_PLACE_DATABASE
);
626 if (asldir
== NULL
) return -1;
630 /* create the database directory itself */
635 if (strchr(dir
, '/') != NULL
) return -1;
637 asprintf(&str
, "%s/%s", asldir
, dir
);
638 if (str
== NULL
) return -1;
642 memset(&sb
, 0, sizeof(struct stat
));
644 status
= stat(path
, &sb
);
647 if (S_ISDIR(sb
.st_mode
))
649 if (out
== NULL
) free(str
);
665 status
= mkdir(path
, 0755);
670 if (out
== NULL
) free(str
);
679 asl_make_timestamp(time_t stamp
, uint32_t flags
, char *buf
, size_t len
)
684 if (buf
== NULL
) return;
686 if (flags
& MODULE_NAME_STYLE_STAMP_UTC
)
688 memset(&t
, 0, sizeof(t
));
689 gmtime_r(&stamp
, &t
);
690 snprintf(buf
, len
, "%d-%02d-%02dT%02d:%02d:%02dZ", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
);
692 else if (flags
& MODULE_NAME_STYLE_STAMP_UTC_B
)
694 memset(&t
, 0, sizeof(t
));
695 gmtime_r(&stamp
, &t
);
696 snprintf(buf
, len
, "%d%02d%02dT%02d%02d%02dZ", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
);
698 else if (flags
& MODULE_NAME_STYLE_STAMP_LCL
)
701 memset(&t
, 0, sizeof(t
));
702 localtime_r(&stamp
, &t
);
704 if ((neg
= (t
.tm_gmtoff
< 0))) t
.tm_gmtoff
*= -1;
712 if (s
> 0) snprintf(buf
, len
, "%d-%02d-%02dT%02d:%02d:%02d%c%u:%02u:%02u", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
, neg
? '-' : '+', h
, m
, s
);
713 else if (m
> 0) snprintf(buf
, len
, "%d-%02d-%02dT%02d:%02d:%02d%c%u:%02u", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
, neg
? '-' : '+', h
, m
);
714 else snprintf(buf
, len
, "%d-%02d-%02dT%02d:%02d:%02d%c%u", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
, neg
? '-' : '+', h
);
716 else if (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
)
719 memset(&t
, 0, sizeof(t
));
720 localtime_r(&stamp
, &t
);
722 if ((neg
= (t
.tm_gmtoff
< 0))) t
.tm_gmtoff
*= -1;
730 if (s
> 0) snprintf(buf
, len
, "%d%02d%02dT%02d%02d%02d%c%02u%02u%02u", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
, neg
? '-' : '+', h
, m
, s
);
731 else if (m
> 0) snprintf(buf
, len
, "%d%02d%02dT%02d%02d%02d%c%02u%02u", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
, neg
? '-' : '+', h
, m
);
732 else snprintf(buf
, len
, "%d%02d%02dT%02d%02d%02d%c%02u", t
.tm_year
+ 1900, t
.tm_mon
+ 1, t
.tm_mday
, t
.tm_hour
, t
.tm_min
, t
.tm_sec
, neg
? '-' : '+', h
);
736 snprintf(buf
, len
, "%c%llu", STYLE_SEC_PREFIX_CHAR
, (unsigned long long)stamp
);
741 asl_dst_make_current_name(asl_out_dst_data_t
*dst
, uint32_t xflags
, char *buf
, size_t len
)
745 if (dst
== NULL
) return;
746 if (buf
== NULL
) return;
748 xflags
|= dst
->flags
;
750 if (dst
->timestamp
== 0) dst
->timestamp
= time(NULL
);
751 asl_make_timestamp(dst
->timestamp
, dst
->style_flags
, tstamp
, sizeof(tstamp
));
753 if (xflags
& MODULE_FLAG_TYPE_ASL_DIR
)
755 snprintf(buf
, len
, "%s.%s", dst
->current_name
, tstamp
);
757 else if (xflags
& MODULE_FLAG_BASESTAMP
)
759 if ((dst
->dir
!= NULL
) && (dst
->style_flags
& MODULE_NAME_STYLE_FORMAT_BSE
))
761 snprintf(buf
, len
, "%s/%s.%s.%s", dst
->dir
, dst
->base
, tstamp
, dst
->ext
);
765 snprintf(buf
, len
, "%s.%s", dst
->path
, tstamp
);
770 snprintf(buf
, len
, "%s", dst
->path
);
775 asl_check_option(asl_msg_t
*msg
, const char *opt
)
780 if (msg
== NULL
) return 0;
781 if (opt
== NULL
) return 0;
784 if (len
== 0) return 0;
786 p
= asl_msg_get_val_for_key(msg
, ASL_KEY_OPTION
);
787 if (p
== NULL
) return 0;
791 while ((*p
== ' ') || (*p
== '\t') || (*p
== ',')) p
++;
792 if (*p
== '\0') return 0;
794 if (strncasecmp(p
, opt
, len
) == 0)
797 if ((*p
== ' ') || (*p
== '\t') || (*p
== ',') || (*p
== '\0')) return 1;
800 while ((*p
!= ' ') && (*p
!= '\t') && (*p
!= ',') && (*p
!= '\0')) p
++;
807 asl_out_dst_data_release(asl_out_dst_data_t
*dst
)
809 if (dst
== NULL
) return;
811 if (dst
->refcount
> 0) dst
->refcount
--;
812 if (dst
->refcount
> 0) return;
816 free(dst
->current_name
);
819 free(dst
->rotate_dir
);
821 #if !TARGET_IPHONE_SIMULATOR
829 asl_out_dst_data_retain(asl_out_dst_data_t
*dst
)
831 if (dst
== NULL
) return NULL
;
836 /* set owner, group, mode, and acls for a file */
838 asl_out_dst_set_access(int fd
, asl_out_dst_data_t
*dst
)
840 #if !TARGET_IPHONE_SIMULATOR
843 #if !TARGET_OS_EMBEDDED
853 if (dst
== NULL
) return -1;
854 if (fd
< 0) return -1;
856 #if TARGET_IPHONE_SIMULATOR
860 if (dst
->nuid
> 0) fuid
= dst
->uid
[0];
861 if (dst
->ngid
> 0) fgid
= dst
->gid
[0];
863 fchown(fd
, fuid
, fgid
);
865 #if TARGET_OS_EMBEDDED
870 for (i
= 0; i
< dst
->ngid
; i
++)
872 if (dst
->gid
[i
] == -2) continue;
875 * Don't bother setting group access if this is
876 * file's group and the file is group-readable.
878 if ((dst
->gid
[i
] == fgid
) && (dst
->mode
& 00040)) continue;
880 status
= mbr_gid_to_uuid(dst
->gid
[i
], uuid
);
887 status
= acl_create_entry_np(&acl
, &entry
, ACL_FIRST_ENTRY
);
888 if (status
!= 0) goto asl_file_create_return
;
890 status
= acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
891 if (status
!= 0) goto asl_file_create_return
;
893 status
= acl_set_qualifier(entry
, &uuid
);
894 if (status
!= 0) goto asl_file_create_return
;
896 status
= acl_get_permset(entry
, &perms
);
897 if (status
!= 0) goto asl_file_create_return
;
899 status
= acl_add_perm(perms
, ACL_READ_DATA
);
900 if (status
!= 0) goto asl_file_create_return
;
903 for (i
= 0; i
< dst
->nuid
; i
++)
905 if (dst
->uid
[i
] == -2) continue;
908 * Don't bother setting user access if this is
909 * file's owner and the file is owner-readable.
911 if ((dst
->uid
[i
] == fuid
) && (dst
->mode
& 00400)) continue;
913 status
= mbr_uid_to_uuid(dst
->uid
[i
], uuid
);
920 status
= acl_create_entry_np(&acl
, &entry
, ACL_FIRST_ENTRY
);
921 if (status
!= 0) goto asl_file_create_return
;
923 status
= acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
924 if (status
!= 0) goto asl_file_create_return
;
926 status
= acl_set_qualifier(entry
, &uuid
);
927 if (status
!= 0) goto asl_file_create_return
;
929 status
= acl_get_permset(entry
, &perms
);
930 if (status
!= 0) goto asl_file_create_return
;
932 status
= acl_add_perm(perms
, ACL_READ_DATA
);
933 if (status
!= 0) goto asl_file_create_return
;
936 status
= acl_set_fd(fd
, acl
);
943 asl_file_create_return
:
947 #endif /* !TARGET_OS_EMBEDDED */
948 #endif /* !TARGET_IPHONE_SIMULATOR */
951 /* create a file with acls */
953 asl_out_dst_file_create_open(asl_out_dst_data_t
*dst
, char **pathp
)
957 char outpath
[MAXPATHLEN
];
959 if (dst
== NULL
) return -1;
960 if (dst
->path
== NULL
) return -1;
962 asl_dst_make_current_name(dst
, 0, outpath
, sizeof(outpath
));
963 free(dst
->current_name
);
965 dst
->current_name
= strdup(outpath
);
966 if (dst
->current_name
== NULL
) return -1;
968 if (pathp
!= NULL
) *pathp
= strdup(outpath
);
970 memset(&sb
, 0, sizeof(struct stat
));
971 status
= stat(outpath
, &sb
);
974 /* must be a regular file */
975 if (!S_ISREG(sb
.st_mode
)) return -1;
978 fd
= open(outpath
, O_RDWR
| O_APPEND
| O_EXCL
, 0);
980 if (dst
->timestamp
== 0) dst
->timestamp
= sb
.st_birthtimespec
.tv_sec
;
981 if (dst
->timestamp
== 0) dst
->timestamp
= sb
.st_mtimespec
.tv_sec
;
982 dst
->size
= sb
.st_size
;
984 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_FLAG_SYMLINK
)) symlink(outpath
, dst
->path
);
987 else if (errno
!= ENOENT
)
989 /* stat error other than non-existant file */
993 fd
= open(outpath
, O_RDWR
| O_CREAT
| O_EXCL
, (dst
->mode
& 00666));
994 if (fd
< 0) return -1;
996 dst
->timestamp
= time(NULL
);
998 fd
= asl_out_dst_set_access(fd
, dst
);
999 if (fd
< 0) unlink(outpath
);
1001 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_FLAG_SYMLINK
))
1003 /* remove old symlink, make a new link to the "current" file */
1005 symlink(outpath
, dst
->path
);
1012 asl_out_module_free(asl_out_module_t
*m
)
1014 asl_out_rule_t
*r
, *n
;
1015 asl_out_module_t
*x
;
1029 if (r
->dst
!= NULL
) asl_out_dst_data_release(r
->dst
);
1031 if (r
->query
!= NULL
) asl_msg_release(r
->query
);
1043 asl_out_module_new(const char *name
)
1045 asl_out_module_t
*out
= (asl_out_module_t
*)calloc(1, sizeof(asl_out_module_t
));
1047 if (out
== NULL
) return NULL
;
1048 if (name
== NULL
) return NULL
;
1050 out
->name
= strdup(name
);
1051 if (out
->name
== NULL
)
1057 out
->flags
= MODULE_FLAG_ENABLED
;
1062 /* Skip over query */
1064 _asl_out_module_find_action(char *s
)
1069 if (p
== NULL
) return NULL
;
1071 /* Skip command character (?, Q, *, or =) */
1077 while ((*p
== ' ') || (*p
== '\t')) p
++;
1079 if (*p
== '\0') return NULL
;
1080 if (*p
!= '[') return p
;
1082 /* skip to closing ] */
1096 /* skip whitespace */
1097 while ((*p
== ' ') || (*p
== '\t')) p
++;
1103 * Parse parameter setting line
1106 * evaluated once when module is initialized
1108 * = [query] param options
1109 * evaluated for each message, param set if message matches query
1111 * = param [File path]
1112 * evaluated once when module is initialized
1113 * evaluated when change notification received for path
1115 * = param [Plist path] ...
1116 * evaluated once when module is initialized
1117 * evaluated when change notification received for path
1119 * = param [Profile name] ...
1120 * evaluated once when module is initialized
1121 * evaluated when change notification received for profile
1123 static asl_out_rule_t
*
1124 _asl_out_module_parse_set_param(asl_out_module_t
*m
, char *s
)
1127 asl_out_rule_t
*out
, *rule
;
1129 if (m
== NULL
) return NULL
;
1131 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1132 if (out
== NULL
) return NULL
;
1135 while ((*q
== ' ') || (*q
== '\'')) q
++;
1136 out
->action
= ACTION_SET_PARAM
;
1140 /* = [query] param options */
1141 act
= _asl_out_module_find_action(s
);
1148 out
->options
= _strdup_clean(act
);
1151 if (*p
== ']') p
= act
;
1155 out
->query
= asl_msg_from_string(s
);
1156 if (out
->query
== NULL
)
1169 /* = param options */
1170 out
->options
= _strdup_clean(q
);
1174 /* = param [query] */
1175 if (streq_len(p
, "[File ", 6) || streq_len(p
, "[File\t", 6)) out
->action
= ACTION_SET_FILE
;
1176 else if (streq_len(p
, "[Plist ", 7) || streq_len(p
, "[Plist\t", 7)) out
->action
= ACTION_SET_PLIST
;
1177 else if (streq_len(p
, "[Profile ", 9) || streq_len(p
, "[Profile\t", 9)) out
->action
= ACTION_SET_PROF
;
1181 out
->options
= _strdup_clean(q
);
1186 out
->query
= asl_msg_from_string(p
);
1187 if (out
->query
== NULL
)
1196 if (m
->ruleset
== NULL
) m
->ruleset
= out
;
1199 for (rule
= m
->ruleset
; rule
->next
!= NULL
; rule
= rule
->next
);
1206 #if !TARGET_IPHONE_SIMULATOR
1208 _dst_add_uid(asl_out_dst_data_t
*dst
, char *s
)
1213 if (dst
== NULL
) return;
1214 if (s
== NULL
) return;
1218 for (i
= 0 ; i
< dst
->nuid
; i
++)
1220 if (dst
->uid
[i
] == uid
) return;
1223 dst
->uid
= reallocf(dst
->uid
, (dst
->nuid
+ 1) * sizeof(uid_t
));
1224 if (dst
->uid
== NULL
)
1230 dst
->uid
[dst
->nuid
++] = uid
;
1234 _dst_add_gid(asl_out_dst_data_t
*dst
, char *s
)
1239 if (dst
== NULL
) return;
1240 if (s
== NULL
) return;
1244 for (i
= 0 ; i
< dst
->ngid
; i
++)
1246 if (dst
->gid
[i
] == gid
) return;
1249 dst
->gid
= reallocf(dst
->gid
, (dst
->ngid
+ 1) * sizeof(gid_t
));
1250 if (dst
->gid
== NULL
)
1256 dst
->gid
[dst
->ngid
++] = gid
;
1258 #endif /* !TARGET_IPHONE_SIMULATOR */
1261 _dst_format_string(char *s
)
1266 if (s
== NULL
) return NULL
;
1270 /* format string can be enclosed by quotes */
1271 if ((len
>= 2) && ((s
[0] == '\'') || (s
[0] == '"')) && (s
[len
-1] == s
[0]))
1278 for (i
= 0; i
< len
; i
++) if (s
[i
] == '\\') n
++;
1280 fmt
= malloc(1 + len
- n
);
1281 if (fmt
== NULL
) return NULL
;
1283 for (i
= 0, n
= 0; i
< len
; i
++) if (s
[i
] != '\\') fmt
[n
++] = s
[i
];
1289 _dst_path_match(const char *newpath
, const char *existingpath
)
1291 if (newpath
== NULL
) return (existingpath
== NULL
);
1292 if (existingpath
== NULL
) return false;
1293 if (newpath
[0] == '/') return (strcmp(newpath
, existingpath
) == 0);
1295 const char *trailing
= strrchr(existingpath
, '/');
1296 if (trailing
== NULL
) return (strcmp(newpath
, existingpath
) == 0);
1298 return (strcmp(newpath
, trailing
) == 0);
1302 _parse_stamp_string(const char *in
)
1307 if (in
== NULL
) return 0;
1309 for (x
= 0; (((in
[x
] >= 'a') && (in
[x
] <= 'z')) || (in
[x
] == '-')) && (x
< 11); x
++) buf
[x
] = in
[x
];
1312 if (streq(buf
, "sec") || streq(buf
, "seconds")) return MODULE_NAME_STYLE_STAMP_SEC
;
1313 if (streq(buf
, "zulu") || streq(buf
, "utc")) return MODULE_NAME_STYLE_STAMP_UTC
;
1314 if (streq(buf
, "utc-b") || streq(buf
, "utc-basic")) return MODULE_NAME_STYLE_STAMP_UTC_B
;
1315 if (streq(buf
, "local") || streq(buf
, "lcl")) return MODULE_NAME_STYLE_STAMP_LCL
;
1316 if (streq(buf
, "local-b") || streq(buf
, "lcl-b") || streq(buf
, "local-basic") || streq(buf
, "lcl-basic")) return MODULE_NAME_STYLE_STAMP_LCL_B
;
1317 if (streq(buf
, "#") || streq(buf
, "seq") || streq(buf
, "sequence"))return MODULE_NAME_STYLE_STAMP_SEQ
;
1323 * Parse a file-rotation naming style.
1325 * Legacy: sec / seconds, utc / date / zulu [-b], local / lcl [-b], # / seq / sequence
1326 * We scan the whole line and match to one of these.
1328 * New scheme: 2 or 3 components: base and style, or base, style, and extension.
1329 * these define a name format. base is the file name without a leading directory path
1330 * and with no extension (e.g. "foo"). style is one of the styles above. extension is
1331 * the file name extension (e.g. "log", "txt", etc).
1338 * The leading base name may be ommitted (E.G. ".lcl.log", ".log.seq")
1339 * If the leading base name AND extension are omitted, it is taken from the path. E.G. ".lcl", ".seq"
1341 * If we get input without a stamp spec, we default to "sec".
1344 _parse_dst_style(asl_out_dst_data_t
*dst
, const char *in
)
1349 if ((dst
== NULL
) || (in
== NULL
)) return -1;
1351 /* check for base. or just . for shorthand */
1359 if (dst
->base
== NULL
) return -1;
1361 len
= strlen(dst
->base
);
1362 if (streq_len(in
, dst
->base
, len
) && (in
[len
] == '.')) p
= in
+ len
+ 1;
1367 /* input does not start with '.' or base, so this is legacy style */
1368 dst
->style_flags
= _parse_stamp_string(in
);
1369 if (dst
->style_flags
== 0) return -1;
1371 if (dst
->ext
== NULL
) dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BS
;
1372 else dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BES
;
1377 /* look for another dot in the name */
1378 for (q
= p
; (*q
!= '.') && (*q
!= ' ') && (*q
!= '\t') && (*q
!= '\0'); q
++);
1379 if (*q
!= '.') q
= NULL
;
1383 /* we require a stamp spec, so we are expecting base.stamp */
1384 dst
->style_flags
= _parse_stamp_string(p
);
1386 if (dst
->style_flags
== 0) return -1;
1389 * We got a valid stamp style ("base.stamp").
1390 * Note that we might have skipped the extention if the file name was "foo.log".
1391 * That's OK - syslogd writes "foo.log", but the rotated files are e.g. foo.20141018T1745Z.
1393 dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BS
;
1397 /* set q to the char past the dot */
1400 /* either base.stamp.ext or base.ext.stamp */
1401 if (dst
->ext
== NULL
) return -1;
1403 len
= strlen(dst
->ext
);
1404 if (streq_len(p
, dst
->ext
, len
) && (p
[len
] == '.'))
1406 /* got base.ext.stamp */
1407 dst
->style_flags
= _parse_stamp_string(q
);
1408 if (dst
->style_flags
== 0) return -1;
1410 dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BES
;
1414 /* must be base.stamp.ext */
1415 if (strneq_len(q
, dst
->ext
, len
)) return -1;
1417 dst
->style_flags
= _parse_stamp_string(p
);
1418 if (dst
->style_flags
== 0) return -1;
1420 dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BSE
;
1424 static asl_out_dst_data_t
*
1425 _asl_out_module_parse_dst(asl_out_module_t
*m
, char *s
, mode_t def_mode
)
1427 asl_out_rule_t
*out
, *rule
;
1428 asl_out_dst_data_t
*dst
;
1429 char *p
, *dot
, *opts
, *path
;
1431 int has_dotdot
, recursion_limit
;
1432 uint32_t i
, flags
= 0;
1434 if (m
== NULL
) return NULL
;
1435 if (s
== NULL
) return NULL
;
1437 /* skip whitespace */
1438 while ((*s
== ' ') || (*s
== '\t')) s
++;
1441 path
= next_word_from_string(&opts
);
1442 if (path
== NULL
) return NULL
;
1445 * Check path for ".." component (not permitted).
1446 * Also substitute environment variables.
1449 path_parts
= explode(path
, "/");
1450 asl_string_t
*processed_path
= asl_string_new(ASL_ENCODE_NONE
);
1451 recursion_limit
= 5;
1453 while ((recursion_limit
> 0) && (path_parts
!= NULL
) && (processed_path
!= NULL
))
1458 for (i
= 0; path_parts
[i
] != NULL
; i
++)
1460 if (streq_len(path_parts
[i
], "$ENV(", 5))
1462 char *p
= strchr(path_parts
[i
], ')');
1463 if (p
!= NULL
) *p
= '\0';
1464 char *env_val
= getenv(path_parts
[i
] + 5);
1465 if (env_val
!= NULL
)
1469 if (env_val
[0] != '/') asl_string_append_char_no_encoding(processed_path
, '/');
1470 asl_string_append_no_encoding(processed_path
, env_val
);
1477 if (path_parts
[0][0] != '\0') asl_string_append_no_encoding(processed_path
, path_parts
[i
]);
1481 asl_string_append_char_no_encoding(processed_path
, '/');
1482 asl_string_append_no_encoding(processed_path
, path_parts
[i
]);
1486 if ((has_dotdot
== 0) && streq(path_parts
[i
], "..")) has_dotdot
= 1;
1489 free_string_list(path_parts
);
1492 if ((did_sub
== 1) && (has_dotdot
== 0))
1494 /* substitution might have added a ".." so check the new path */
1496 path
= asl_string_release_return_bytes(processed_path
);
1497 processed_path
= asl_string_new(ASL_ENCODE_NONE
);
1498 path_parts
= explode(path
, "/");
1504 free_string_list(path_parts
);
1507 if ((has_dotdot
!= 0) || (recursion_limit
== 0))
1509 asl_string_release(processed_path
);
1513 path
= asl_string_release_return_bytes(processed_path
);
1515 /* check if there's already a dst for this path */
1516 for (rule
= m
->ruleset
; rule
!= NULL
; rule
= rule
->next
)
1518 if (rule
->action
!= ACTION_OUT_DEST
) continue;
1521 if (dst
== NULL
) continue;
1523 if (_dst_path_match(path
, dst
->path
))
1530 flags
|= MODULE_FLAG_NONSTD_DIR
;
1535 const char *log_root
= "/var/log";
1537 #if TARGET_IPHONE_SIMULATOR
1538 log_root
= getenv("SIMULATOR_LOG_ROOT");
1539 if (log_root
== NULL
) log_root
= "/tmp/log";
1542 if (streq(m
->name
, ASL_MODULE_NAME
))
1544 asprintf(&path
, "%s/%s", log_root
, t
);
1548 asprintf(&path
, "%s/module/%s/%s", log_root
, m
->name
, t
);
1552 flags
&= ~MODULE_FLAG_NONSTD_DIR
;
1557 * Standard log directories get marked so that syslogd
1558 * will create them without explicit rules.
1560 if (streq_len(path
, PATH_VAR_LOG
, PATH_VAR_LOG_LEN
)) flags
&= ~MODULE_FLAG_NONSTD_DIR
;
1561 else if (streq_len(path
, PATH_LIBRARY_LOGS
, PATH_LIBRARY_LOGS_LEN
)) flags
&= ~MODULE_FLAG_NONSTD_DIR
;
1564 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1565 dst
= (asl_out_dst_data_t
*)calloc(1, sizeof(asl_out_dst_data_t
));
1566 if ((out
== NULL
) || (dst
== NULL
))
1577 p
= strrchr(dst
->path
, '/');
1581 dst
->dir
= strdup(dst
->path
);
1585 dst
->mode
= def_mode
;
1586 dst
->ttl
[LEVEL_ALL
] = DEFAULT_TTL
;
1587 dst
->flags
= flags
| MODULE_FLAG_COALESCE
;
1590 * Break out base and extension (if present) from path.
1591 * Note this only supports a '.' as a separator.
1593 p
= strrchr(path
, '/');
1594 if (p
== NULL
) p
= path
;
1597 dot
= strrchr(path
, '.');
1601 dst
->ext
= strdup(dot
+ 1);
1604 dst
->base
= strdup(p
);
1605 if (dot
!= NULL
) *dot
= '.';
1607 while (NULL
!= (p
= next_word_from_string(&opts
)))
1609 if (KEYMATCH(p
, "mode=")) dst
->mode
= strtol(p
+5, NULL
, 0);
1610 #if !TARGET_IPHONE_SIMULATOR
1611 else if (KEYMATCH(p
, "uid=")) _dst_add_uid(dst
, p
+4);
1612 else if (KEYMATCH(p
, "gid=")) _dst_add_gid(dst
, p
+4);
1614 else if (KEYMATCH(p
, "fmt=")) dst
->fmt
= _dst_format_string(p
+4);
1615 else if (KEYMATCH(p
, "format=")) dst
->fmt
= _dst_format_string(p
+7);
1616 else if (KEYMATCH(p
, "dest=")) dst
->rotate_dir
= _strdup_clean(p
+5);
1617 else if (KEYMATCH(p
, "dst=")) dst
->rotate_dir
= _strdup_clean(p
+4);
1618 else if (KEYMATCH(p
, "coalesce="))
1620 if (KEYMATCH(p
+9, "0")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1621 else if (KEYMATCH(p
+9, "off")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1622 else if (KEYMATCH(p
+9, "false")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1624 else if (KEYMATCH(p
, "compress")) dst
->flags
|= MODULE_FLAG_COMPRESS
;
1625 else if (KEYMATCH(p
, "activity")) dst
->flags
|= MODULE_FLAG_ACTIVITY
;
1626 else if (KEYMATCH(p
, "extern")) dst
->flags
|= MODULE_FLAG_EXTERNAL
;
1627 else if (KEYMATCH(p
, "truncate")) dst
->flags
|= MODULE_FLAG_TRUNCATE
;
1628 else if (KEYMATCH(p
, "dir")) dst
->flags
|= MODULE_FLAG_TYPE_ASL_DIR
;
1629 else if (KEYMATCH(p
, "soft")) dst
->flags
|= MODULE_FLAG_SOFT_WRITE
;
1630 else if (KEYMATCH(p
, "file_max=")) dst
->file_max
= asl_core_str_to_size(p
+9);
1631 else if (KEYMATCH(p
, "all_max=")) dst
->all_max
= asl_core_str_to_size(p
+8);
1632 else if (KEYMATCH(p
, "style=") || KEYMATCH(p
, "rotate="))
1634 const char *x
= p
+ 6;
1637 if (_parse_dst_style(dst
, x
) == 0) dst
->flags
|= MODULE_FLAG_ROTATE
;
1639 else if (KEYMATCH(p
, "rotate"))
1641 if (dst
->ext
== NULL
) dst
->style_flags
= MODULE_NAME_STYLE_FORMAT_BS
| MODULE_NAME_STYLE_STAMP_SEC
;
1642 else dst
->style_flags
= MODULE_NAME_STYLE_FORMAT_BES
| MODULE_NAME_STYLE_STAMP_SEC
;
1644 dst
->flags
|= MODULE_FLAG_ROTATE
;
1646 else if (KEYMATCH(p
, "crashlog"))
1648 /* crashlog implies rotation */
1649 dst
->flags
|= MODULE_FLAG_ROTATE
;
1650 dst
->flags
|= MODULE_FLAG_CRASHLOG
;
1651 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1652 dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1654 else if (KEYMATCH(p
, "basestamp"))
1656 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1658 else if (KEYMATCH(p
, "link") || KEYMATCH(p
, "symlink"))
1660 dst
->flags
|= MODULE_FLAG_SYMLINK
;
1662 else if (KEYMATCH(p
, "ttl"))
1667 dst
->ttl
[LEVEL_ALL
] = asl_core_str_to_time(p
+4, SECONDS_PER_DAY
);
1669 else if ((*q
>= '0') && (*q
<= '7') && (*(q
+1) == '='))
1671 uint32_t x
= *q
- '0';
1672 dst
->ttl
[x
] = asl_core_str_to_time(p
+5, SECONDS_PER_DAY
);
1680 #if TARGET_OS_EMBEDDED
1681 /* check for crashreporter files */
1682 if ((KEYMATCH(dst
->path
, _PATH_CRASHREPORTER
)) || (KEYMATCH(dst
->path
, _PATH_CRASHREPORTER_MOBILE_1
)) || (KEYMATCH(dst
->path
, _PATH_CRASHREPORTER_MOBILE_2
)))
1684 dst
->flags
|= MODULE_FLAG_ROTATE
;
1685 dst
->flags
|= MODULE_FLAG_CRASHLOG
;
1686 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1687 dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1691 /* ttl[LEVEL_ALL] must be max of all level-specific ttls */
1692 for (i
= 0; i
<= 7; i
++) if (dst
->ttl
[i
] > dst
->ttl
[LEVEL_ALL
]) dst
->ttl
[LEVEL_ALL
] = dst
->ttl
[i
];
1694 /* default text file format is "std" */
1695 if (dst
->fmt
== NULL
) dst
->fmt
= strdup("std");
1697 /* duplicate compression is only possible for std and bsd formats */
1698 if (strcmp(dst
->fmt
, "std") && strcmp(dst
->fmt
, "bsd")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1700 /* note if format is one of std, bsd, or msg */
1701 if (streq(dst
->fmt
, "std") || streq(dst
->fmt
, "bsd") || streq(dst
->fmt
, "msg")) dst
->flags
|= MODULE_FLAG_STD_BSD_MSG
;
1703 /* MODULE_NAME_STYLE_STAMP_SEQ can not be used with MODULE_FLAG_BASESTAMP */
1704 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_NAME_STYLE_STAMP_SEQ
))
1706 dst
->flags
&= ~MODULE_NAME_STYLE_STAMP_SEQ
;
1707 dst
->flags
|= MODULE_NAME_STYLE_STAMP_SEC
;
1710 /* set time format for raw output */
1711 if (streq(dst
->fmt
, "raw")) dst
->tfmt
= "sec";
1713 /* check for ASL_PLACE_DATABASE_DEFAULT */
1714 if (streq(dst
->path
, ASL_PLACE_DATABASE_DEFAULT
))
1716 dst
->flags
= MODULE_FLAG_TYPE_ASL_DIR
;
1719 /* set file_max to all_max if it is zero */
1720 if (dst
->file_max
== 0) dst
->file_max
= dst
->all_max
;
1722 out
->action
= ACTION_OUT_DEST
;
1725 /* dst rules go first */
1726 out
->next
= m
->ruleset
;
1732 static asl_out_rule_t
*
1733 _asl_out_module_parse_query_action(asl_out_module_t
*m
, char *s
)
1736 asl_out_rule_t
*out
, *rule
;
1738 if (m
== NULL
) return NULL
;
1740 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1741 if (out
== NULL
) return NULL
;
1743 act
= _asl_out_module_find_action(s
);
1744 if (act
== NULL
) return NULL
;
1746 /* find whitespace delimiter */
1747 p
= strchr(act
, ' ');
1748 if (p
== NULL
) p
= strchr(act
, '\t');
1749 if (p
!= NULL
) *p
= '\0';
1751 if (strcaseeq(act
, "ignore")) out
->action
= ACTION_IGNORE
;
1752 else if (strcaseeq(act
, "skip")) out
->action
= ACTION_SKIP
;
1753 else if (strcaseeq(act
, "claim")) out
->action
= ACTION_CLAIM
;
1754 else if (strcaseeq(act
, "notify")) out
->action
= ACTION_NOTIFY
;
1755 else if (strcaseeq(act
, "file")) out
->action
= ACTION_FILE
;
1756 else if (strcaseeq(act
, "asl_file")) out
->action
= ACTION_ASL_FILE
;
1757 else if (strcaseeq(act
, "directory")) out
->action
= ACTION_ASL_DIR
;
1758 else if (strcaseeq(act
, "dir")) out
->action
= ACTION_ASL_DIR
;
1759 else if (strcaseeq(act
, "asl_directory")) out
->action
= ACTION_ASL_DIR
;
1760 else if (strcaseeq(act
, "asl_dir")) out
->action
= ACTION_ASL_DIR
;
1761 else if (strcaseeq(act
, "store_dir")) out
->action
= ACTION_ASL_DIR
;
1762 else if (strcaseeq(act
, "store_directory")) out
->action
= ACTION_ASL_DIR
;
1763 else if (strcaseeq(act
, "control")) out
->action
= ACTION_CONTROL
;
1764 else if (strcaseeq(act
, "save")) out
->action
= ACTION_ASL_STORE
;
1765 else if (strcaseeq(act
, "store")) out
->action
= ACTION_ASL_STORE
;
1766 else if (strcaseeq(act
, "access")) out
->action
= ACTION_ACCESS
;
1767 else if (strcaseeq(act
, "set")) out
->action
= ACTION_SET_KEY
;
1768 else if (strcaseeq(act
, "unset")) out
->action
= ACTION_UNSET_KEY
;
1769 else if (streq(m
->name
, ASL_MODULE_NAME
))
1771 /* actions only allowed in com.apple.asl */
1772 if (strcaseeq(act
, "broadcast")) out
->action
= ACTION_BROADCAST
;
1773 else if (strcaseeq(act
, "forward")) out
->action
= ACTION_FORWARD
;
1776 if (out
->action
== ACTION_NONE
)
1782 /* options follow delimited (now zero) */
1785 /* skip whitespace */
1786 while ((*p
== ' ') || (*p
== '\t')) p
++;
1788 out
->options
= _strdup_clean(p
+1);
1790 if (out
->options
== NULL
)
1803 out
->query
= asl_msg_new(ASL_TYPE_QUERY
);
1808 out
->query
= asl_msg_from_string(s
);
1811 if (out
->query
== NULL
)
1818 /* store /some/path means save to an asl file */
1819 if (out
->action
== ACTION_ASL_STORE
)
1821 if (out
->options
== NULL
) out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, ASL_PLACE_DATABASE_DEFAULT
, 0755));
1822 else if (streq_len(out
->options
, ASL_PLACE_DATABASE_DEFAULT
, strlen(ASL_PLACE_DATABASE_DEFAULT
))) out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, out
->options
, 0755));
1823 else if (out
->options
!= NULL
) out
->action
= ACTION_ASL_FILE
;
1826 if ((out
->action
== ACTION_FILE
) || (out
->action
== ACTION_ASL_FILE
) || (out
->action
== ACTION_ASL_DIR
))
1828 mode_t def_mode
= 0644;
1829 if (out
->action
== ACTION_ASL_DIR
) def_mode
= 0755;
1831 out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, out
->options
, def_mode
));
1832 if (out
->dst
== NULL
)
1834 out
->action
= ACTION_NONE
;
1839 * dst might have been set up by a previous ACTION_OUT_DEST ('>') rule with no mode.
1840 * If so, mode would be 010000. Set it now, since we know whether it is a file or dir.
1842 if (out
->dst
->mode
== 010000) out
->dst
->mode
= def_mode
;
1844 if ((out
->action
== ACTION_FILE
) && (out
->dst
!= NULL
) && (out
->dst
->fmt
!= NULL
) && (strcaseeq(out
->dst
->fmt
, "asl")))
1846 out
->action
= ACTION_ASL_FILE
;
1849 if ((out
->action
== ACTION_ASL_FILE
) && (out
->dst
!= NULL
))
1851 out
->dst
->flags
|= MODULE_FLAG_TYPE_ASL
;
1854 if (out
->action
== ACTION_ASL_DIR
)
1856 /* coalesce is meaningless for ASL directories */
1857 out
->dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1859 out
->dst
->flags
|= MODULE_FLAG_TYPE_ASL_DIR
;
1861 /* set style bits for basestamp asl_dirs */
1862 if (((out
->dst
->style_flags
& MODULE_NAME_STYLE_STAMP_MASK
) == 0) && (out
->dst
->flags
& MODULE_FLAG_BASESTAMP
)) out
->dst
->style_flags
|= MODULE_NAME_STYLE_STAMP_LCL_B
;
1865 /* only ACTION_FILE and ACTION_ASL_FILE may rotate */
1866 if ((out
->action
!= ACTION_FILE
) && (out
->action
!= ACTION_ASL_FILE
))
1868 out
->dst
->flags
&= ~MODULE_FLAG_ROTATE
;
1871 #if !TARGET_IPHONE_SIMULATOR
1872 if (out
->dst
->nuid
== 0) _dst_add_uid(out
->dst
, "0");
1873 if (out
->dst
->ngid
== 0) _dst_add_gid(out
->dst
, "80");
1877 if (m
->ruleset
== NULL
) m
->ruleset
= out
;
1880 for (rule
= m
->ruleset
; rule
->next
!= NULL
; rule
= rule
->next
);
1888 asl_out_module_parse_line(asl_out_module_t
*m
, char *s
)
1890 while ((*s
== ' ') || (*s
== '\t')) s
++;
1892 if ((*s
== 'Q') || (*s
== '?') || (*s
== '*'))
1894 return _asl_out_module_parse_query_action(m
, s
);
1898 return _asl_out_module_parse_set_param(m
, s
);
1902 _asl_out_module_parse_dst(m
, s
+ 1, 010000);
1909 asl_out_module_init_from_file(const char *name
, FILE *f
)
1911 asl_out_module_t
*out
;
1914 if (f
== NULL
) return NULL
;
1916 out
= asl_out_module_new(name
);
1917 if (out
== NULL
) return NULL
;
1919 /* read and parse config file */
1920 while (NULL
!= (line
= get_line_from_file(f
)))
1922 asl_out_module_parse_line(out
, line
);
1929 static asl_out_module_t
*
1930 _asl_out_module_find(asl_out_module_t
*list
, const char *name
)
1932 asl_out_module_t
*x
;
1934 if (list
== NULL
) return NULL
;
1935 if (name
== NULL
) return NULL
;
1937 for (x
= list
; x
!= NULL
; x
= x
->next
)
1939 if ((x
->name
!= NULL
) && (streq(x
->name
, name
))) return x
;
1946 _asl_out_module_read_and_merge_dir(asl_out_module_t
**list
, const char *path
, uint32_t flags
)
1951 asl_out_module_t
*last
, *x
;
1953 if (list
== NULL
) return;
1954 if (path
== NULL
) return;
1959 while (last
->next
!= NULL
) last
= last
->next
;
1965 while (NULL
!= (ent
= readdir(d
)))
1967 if (ent
->d_name
[0] != '.')
1969 /* merge: skip this file if we already have a module with this name */
1970 if (_asl_out_module_find(*list
, ent
->d_name
) != NULL
) continue;
1972 char tmp
[MAXPATHLEN
];
1973 snprintf(tmp
, sizeof(tmp
), "%s/%s", path
, ent
->d_name
);
1974 f
= fopen(tmp
, "r");
1977 x
= asl_out_module_init_from_file(ent
->d_name
, f
);
1984 if (streq(ent
->d_name
, ASL_MODULE_NAME
))
1986 /* com.apple.asl goes at the head of the list */
1989 if (last
== NULL
) last
= *list
;
1991 else if (*list
== NULL
)
2011 asl_out_module_init(void)
2013 asl_out_module_t
*out
= NULL
;
2015 #if TARGET_IPHONE_SIMULATOR
2016 char *sim_root_path
, *sim_resources_path
;
2017 char *asl_conf
, *asl_conf_dir
, *asl_conf_local_dir
;
2019 sim_root_path
= getenv("IPHONE_SIMULATOR_ROOT");
2020 if (sim_root_path
== NULL
) return NULL
;
2022 sim_resources_path
= getenv("IPHONE_SHARED_RESOURCES_DIRECTORY");
2023 if (sim_resources_path
== NULL
) return NULL
;
2025 asprintf(&asl_conf
, "%s%s", sim_root_path
, _PATH_ASL_CONF
);
2026 asprintf(&asl_conf_dir
, "%s%s", sim_root_path
, _PATH_ASL_CONF_DIR
);
2027 asprintf(&asl_conf_local_dir
, "%s%s", sim_resources_path
, _PATH_ASL_CONF_DIR
);
2029 _asl_out_module_read_and_merge_dir(&out
, asl_conf_local_dir
, MODULE_FLAG_LOCAL
);
2030 free(asl_conf_local_dir
);
2032 _asl_out_module_read_and_merge_dir(&out
, asl_conf_dir
, 0);
2035 _asl_out_module_read_and_merge_dir(&out
, _PATH_ASL_CONF_LOCAL_DIR
, MODULE_FLAG_LOCAL
);
2036 _asl_out_module_read_and_merge_dir(&out
, _PATH_ASL_CONF_DIR
, 0);
2039 if (_asl_out_module_find(out
, ASL_MODULE_NAME
) == NULL
)
2041 /* system just has old-style /etc/asl.conf */
2042 #if TARGET_IPHONE_SIMULATOR
2043 FILE *f
= fopen(asl_conf
, "r");
2046 FILE *f
= fopen(_PATH_ASL_CONF
, "r");
2050 asl_out_module_t
*x
= asl_out_module_init_from_file(ASL_MODULE_NAME
, f
);
2067 asl_out_module_rule_to_string(asl_out_rule_t
*r
)
2074 asprintf(&out
, "NULL rule");
2078 str
= asl_msg_to_string(r
->query
, &len
);
2080 asprintf(&out
, " %s%s%s%s%s",
2081 asl_out_action_name
[r
->action
],
2082 (r
->query
== NULL
) ? "" : " ",
2083 (r
->query
== NULL
) ? "" : str
,
2084 (r
->options
== NULL
) ? "" : " ",
2085 (r
->options
== NULL
) ? "" : r
->options
);
2092 _stamp_style_name(uint32_t flags
)
2094 if (flags
& MODULE_NAME_STYLE_STAMP_SEC
) return "<seconds>";
2095 if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
) return "<sequence>";
2096 if (flags
& MODULE_NAME_STYLE_STAMP_UTC
) return "<utc>";
2097 if (flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) return "<utc-basic>";
2098 if (flags
& MODULE_NAME_STYLE_STAMP_LCL
) return "<local>";
2099 if (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
) return "<local-basic>";
2107 asl_out_module_print(FILE *f
, asl_out_module_t
*m
)
2109 asl_out_rule_t
*r
, *n
;
2110 asl_out_dst_data_t
*o
;
2111 uint32_t i
, ttlnset
;
2115 for (r
= m
->ruleset
; r
!= NULL
; r
= n
)
2118 char *str
= asl_msg_to_string(r
->query
, &len
);
2120 fprintf(f
, " %s", asl_out_action_name
[r
->action
]);
2121 if (r
->query
!= NULL
) fprintf(f
, " %s", str
);
2122 if (r
->options
!= NULL
) fprintf(f
, " %s", r
->options
);
2123 if (r
->action
== ACTION_OUT_DEST
)
2128 fprintf(f
, " data: NULL");
2132 fprintf(f
, "%s\n", o
->path
);
2133 fprintf(f
, " rules: %u\n", o
->refcount
- 1);
2134 fprintf(f
, " dest: %s\n", (o
->rotate_dir
== NULL
) ? "(none)" : o
->rotate_dir
);
2135 fprintf(f
, " format: %s\n", (o
->fmt
== NULL
) ? "std" : o
->fmt
);
2136 fprintf(f
, " time_format: %s\n", (o
->tfmt
== NULL
) ? "lcl" : o
->tfmt
);
2137 fprintf(f
, " flags: 0x%08x", o
->flags
);
2142 if (o
->flags
& MODULE_FLAG_ENABLED
)
2144 fprintf(f
, "%cenabled", c
);
2147 if (o
->flags
& MODULE_FLAG_SOFT_WRITE
)
2149 fprintf(f
, "%csoft", c
);
2152 if (o
->flags
& MODULE_FLAG_ROTATE
)
2154 fprintf(f
, "%crotate", c
);
2157 if (o
->flags
& MODULE_FLAG_COALESCE
)
2159 fprintf(f
, "%ccoalesce", c
);
2162 if (o
->flags
& MODULE_FLAG_COMPRESS
)
2164 fprintf(f
, "%ccompress", c
);
2167 if (o
->flags
& MODULE_FLAG_BASESTAMP
)
2169 fprintf(f
, "%cbasestamp", c
);
2172 if (o
->flags
& MODULE_FLAG_SYMLINK
)
2174 fprintf(f
, "%csymlink", c
);
2177 if (o
->flags
& MODULE_FLAG_NONSTD_DIR
)
2179 fprintf(f
, "%cnon-std_dir", c
);
2182 if (o
->flags
& MODULE_FLAG_EXTERNAL
)
2184 fprintf(f
, "%cexternal", c
);
2187 if (o
->flags
& MODULE_FLAG_ACTIVITY
)
2189 fprintf(f
, "%cactivity", c
);
2192 if (o
->flags
& MODULE_FLAG_CRASHLOG
)
2194 fprintf(f
, "%ccrashlog", c
);
2197 if (o
->flags
& MODULE_FLAG_TYPE_ASL
)
2199 fprintf(f
, "%casl_file", c
);
2202 if (o
->flags
& MODULE_FLAG_TYPE_ASL_DIR
)
2204 fprintf(f
, "%casl_directory", c
);
2211 if (o
->flags
& MODULE_FLAG_ROTATE
)
2213 fprintf(f
, " rotatation style: ");
2214 if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BS
)
2216 fprintf(f
, "[base=%s].%s\n", o
->base
, _stamp_style_name(o
->style_flags
));
2218 else if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BES
)
2220 fprintf(f
, "[base=%s].[ext=%s].%s\n", o
->base
, o
->ext
, _stamp_style_name(o
->style_flags
));
2222 else if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BSE
)
2224 fprintf(f
, "[base=%s].%s.[ext=%s]\n", o
->base
, _stamp_style_name(o
->style_flags
), o
->ext
);
2228 fprintf(f
, "0x%08x\n", o
->style_flags
);
2232 asl_core_time_to_str(o
->ttl
[LEVEL_ALL
], tstr
, sizeof(tstr
));
2233 fprintf(f
, " ttl: %s\n", tstr
);
2236 for (i
= 0; (i
<= 7) & (ttlnset
== 0); i
++) if (o
->ttl
[i
] != 0) ttlnset
= 1;
2239 for (i
= 0; i
<= 7; i
++)
2241 time_t x
= o
->ttl
[i
];
2242 if (x
== 0) x
= o
->ttl
[LEVEL_ALL
];
2243 asl_core_time_to_str(x
, tstr
, sizeof(tstr
));
2245 fprintf(f
, " [%d %s]", i
, tstr
);
2251 fprintf(f
, " mode: 0%o\n", o
->mode
);
2252 fprintf(f
, " file_max: %lu\n", o
->file_max
);
2253 fprintf(f
, " all_max: %lu\n", o
->all_max
);
2254 #if !TARGET_IPHONE_SIMULATOR
2255 fprintf(f
, " uid:");
2256 for (i
= 0; i
< o
->nuid
; i
++) fprintf(f
, " %d", o
->uid
[i
]);
2258 fprintf(f
, " gid:");
2259 for (i
= 0; i
< o
->ngid
; i
++) fprintf(f
, " %d", o
->gid
[i
]);
2272 asl_out_file_list_free(asl_out_file_list_t
*l
)
2274 asl_out_file_list_t
*n
;
2276 if (l
== NULL
) return;
2288 * Checks input name for one of the forms:
2289 * MODULE_NAME_STYLE_FORMAT_BS base (src only) or base.stamp[.gz]
2290 * MODULE_NAME_STYLE_FORMAT_BES base.ext (src only) or base.ext.stamp[.gz]
2291 * MODULE_NAME_STYLE_FORMAT_BSE base.ext (src only) or base.stamp.ext[.gz]
2293 * name == base[.ext] is allowed if src is true.
2294 * base.gz is not allowed.
2295 * Output parameter stamp must be freed by caller.
2298 _check_file_name(const char *name
, const char *base
, const char *ext
, uint32_t flags
, bool src
, char **stamp
)
2300 size_t baselen
, extlen
;
2304 #ifdef DEBUG_LIST_FILES
2305 fprintf(stderr
, "_check_file_name name=%s base=%s ext=%s flags=0x%08x %s\n", name
, base
, (ext
== NULL
) ? "(NULL)" : ext
, flags
, src
? "src" : "dst");
2308 if (name
== NULL
) return false;
2309 if (base
== NULL
) return false;
2311 baselen
= strlen(base
);
2312 if (baselen
== 0) return false;
2315 if (ext
!= NULL
) extlen
= strlen(ext
);
2317 if (stamp
!= NULL
) *stamp
= NULL
;
2319 if (strneq_len(name
, base
, baselen
))
2321 #ifdef DEBUG_LIST_FILES
2322 fprintf(stderr
, " base match failed [%u]\n", __LINE__
);
2327 z
= strrchr(name
, '.');
2328 if ((z
!= NULL
) && streq(z
, ".gz")) isgz
= true;
2330 #ifdef DEBUG_LIST_FILES
2331 fprintf(stderr
, "z=%s isgz=%s\n", (z
== NULL
) ? "NULL" : z
, isgz
? "true" : "false");
2336 if (flags
& MODULE_NAME_STYLE_FORMAT_BS
)
2338 #ifdef DEBUG_LIST_FILES
2339 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BS\n");
2343 /* name == base OK if src is true */
2344 #ifdef DEBUG_LIST_FILES
2345 fprintf(stderr
, " name == base %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2350 /* expecting p == .stamp[.gz] */
2353 #ifdef DEBUG_LIST_FILES
2354 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2363 /* base.gz is not allowed */
2364 #ifdef DEBUG_LIST_FILES
2365 fprintf(stderr
, " got base.gz - failed [%u]\n", __LINE__
);
2370 /* got base.stamp[.gz] */
2374 char *x
= strchr(*stamp
, '.');
2375 if (x
!= NULL
) *x
= '\0';
2378 #ifdef DEBUG_LIST_FILES
2379 fprintf(stderr
, " got base.stamp%s - OK\n", isgz
? ".gz" : "");
2383 else if (flags
& MODULE_NAME_STYLE_FORMAT_BES
)
2385 #ifdef DEBUG_LIST_FILES
2386 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BES\n");
2390 #ifdef DEBUG_LIST_FILES
2391 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2398 if (strneq_len(p
, ext
, extlen
))
2400 #ifdef DEBUG_LIST_FILES
2401 fprintf(stderr
, " ext match failed [%u]\n", __LINE__
);
2406 /* expecting p == .ext[.stamp][.gz] */
2411 /* name == base.ext OK if src is true */
2412 #ifdef DEBUG_LIST_FILES
2413 fprintf(stderr
, " name == base.ext %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2418 /* expecting p == .stamp[.gz] */
2421 #ifdef DEBUG_LIST_FILES
2422 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2431 /* base.ext.gz is not allowed */
2432 #ifdef DEBUG_LIST_FILES
2433 fprintf(stderr
, " got base.ext.gz - failed [%u]\n", __LINE__
);
2438 /* got base.ext.stamp[.gz] */
2442 char *x
= strchr(*stamp
, '.');
2443 if (x
!= NULL
) *x
= '\0';
2446 #ifdef DEBUG_LIST_FILES
2447 fprintf(stderr
, " got base.ext.stamp%s - OK\n", isgz
? ".gz" : "");
2451 else if (flags
& MODULE_NAME_STYLE_FORMAT_BSE
)
2453 #ifdef DEBUG_LIST_FILES
2454 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BSE name=%s base=%s ext=%s flags=0x%08x %s\n", name
, base
, (ext
== NULL
) ? "(NULL)" : ext
, flags
, src
? "src" : "dst");
2459 #ifdef DEBUG_LIST_FILES
2460 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2467 if (streq_len(p
, ext
, extlen
))
2472 /* name == base.ext OK if src is true */
2473 #ifdef DEBUG_LIST_FILES
2474 fprintf(stderr
, " name == base.ext %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2480 /* expecting p == stamp.ext[.gz] */
2484 if (strneq_len(z
- extlen
, ext
, extlen
))
2486 #ifdef DEBUG_LIST_FILES
2487 fprintf(stderr
, " ext match (%s) isgz failed [%u]\n", z
-extlen
, __LINE__
);
2494 if (strneq_len(z
+ 1, ext
, extlen
))
2496 #ifdef DEBUG_LIST_FILES
2497 fprintf(stderr
, " ext match (%s) failed [%u]\n", z
, __LINE__
);
2503 /* got base.stamp.ext[.gz] */
2507 char *x
= strchr(*stamp
, '.');
2508 if (x
!= NULL
) *x
= '\0';
2511 #ifdef DEBUG_LIST_FILES
2512 fprintf(stderr
, " got base.stamp.ext%s - OK\n", isgz
? ".gz" : "");
2517 #ifdef DEBUG_LIST_FILES
2518 fprintf(stderr
, " unknown format - failed\n");
2525 * Find files in a directory (dir) that all have a common prefix (base).
2526 * Bits in flags further control the search.
2528 * MODULE_NAME_STYLE_STAMP_SEQ means a numeric sequence number is expected, although not required.
2529 * E.g. foo.log foo.log.0
2531 * MODULE_NAME_STYLE_STAMP_SEC also means a numeric sequence number is required following an 'T' character.
2532 * The numeric value is the file's timestamp in seconds. E.g foo.log.T1335200452
2534 * MODULE_NAME_STYLE_STAMP_UTC requires a date/time component as the file's timestamp.
2535 * E.g. foo.2012-04-06T15:30:00Z
2537 * MODULE_NAME_STYLE_STAMP_UTC_B requires a date/time component as the file's timestamp.
2538 * E.g. foo.20120406T153000Z
2540 * MODULE_NAME_STYLE_STAMP_LCL requires a date/time component as the file's timestamp.
2541 * E.g. foo.2012-04-06T15:30:00-7
2543 * MODULE_NAME_STYLE_STAMP_LCL_B requires a date/time component as the file's timestamp.
2544 * E.g. foo.20120406T153000-07
2547 _parse_stamp_style(char *stamp
, uint32_t flags
, uint32_t *sp
, time_t *tp
)
2554 long utc_offset
= 0;
2557 /* check for NULL (no stamp) */
2558 if (stamp
== NULL
) return STAMP_STYLE_NULL
;
2560 /* check for MODULE_NAME_STYLE_STAMP_SEC (foo.T12345678) */
2561 if (stamp
[0] == 'T')
2563 n
= atoi(stamp
+ 1);
2564 if ((n
== 0) && strcmp(stamp
+ 1, "0")) return STAMP_STYLE_INVALID
;
2565 if (tp
!= NULL
) *tp
= (time_t)n
;
2567 return STAMP_STYLE_SEC
;
2570 /* check for MODULE_NAME_STYLE_STAMP_SEQ (foo.0 or foo.2.gz) */
2572 for (i
= 0; digits
&& (stamp
[i
] != '\0'); i
++) digits
= (stamp
[i
] >= '0') && (stamp
[i
] <= '9');
2574 if (!digits
&& (streq(stamp
+ i
, ".gz"))) digits
= true;
2579 if (sp
!= NULL
) *sp
= (uint32_t)n
;
2580 return STAMP_STYLE_SEQ
;
2583 /* check for MODULE_NAME_STYLE_STAMP_UTC, UTC_B, LCL, or LCL_B */
2584 memset(&t
, 0, sizeof(t
));
2588 if ((flags
& MODULE_NAME_STYLE_STAMP_UTC
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL
))
2590 n
= sscanf(stamp
, "%d-%d-%dT%d:%d:%d%c%u:%u:%u", &t
.tm_year
, &t
.tm_mon
, &t
.tm_mday
, &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
, &zone
, &h
, &m
, &s
);
2592 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
))
2594 n
= sscanf(stamp
, "%4d%2d%2dT%2d%2d%2d%c%2u%2u%2u", &t
.tm_year
, &t
.tm_mon
, &t
.tm_mday
, &t
.tm_hour
, &t
.tm_min
, &t
.tm_sec
, &zone
, &h
, &m
, &s
);
2598 #ifdef DEBUG_LIST_FILES
2599 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2601 return STAMP_STYLE_INVALID
;
2606 #ifdef DEBUG_LIST_FILES
2607 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2609 return STAMP_STYLE_INVALID
;
2616 else if ((zone
== '-') || (zone
== '+'))
2618 if (n
>= 8) utc_offset
+= (3600 * h
);
2619 if (n
>= 9) utc_offset
+= (60 * m
);
2620 if (n
== 10) utc_offset
+= s
;
2621 if (zone
== '+') utc_offset
*= -1;
2623 else if ((zone
>= 'A') && (zone
<= 'Z'))
2625 if (zone
< 'J') utc_offset
= 3600 * ((zone
- 'A') + 1);
2626 else if ((zone
>= 'K') && (zone
<= 'M')) utc_offset
= 3600 * (zone
- 'A');
2627 else if (zone
<= 'Y') utc_offset
= -3600 * ((zone
- 'N') + 1);
2629 else if ((zone
>= 'a') && (zone
<= 'z'))
2631 if (zone
< 'j') utc_offset
= 3600 * ((zone
- 'a') + 1);
2632 else if ((zone
>= 'k') && (zone
<= 'm')) utc_offset
= 3600 * (zone
- 'a');
2633 else if (zone
<= 'y') utc_offset
= -3600 * ((zone
- 'n') + 1);
2637 #ifdef DEBUG_LIST_FILES
2638 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2640 return STAMP_STYLE_INVALID
;
2645 t
.tm_sec
+= utc_offset
;
2648 if ((zone
== 'J') || (zone
== 'j')) ftime
= mktime(&t
);
2649 else ftime
= timegm(&t
);
2651 if (tp
!= NULL
) *tp
= ftime
;
2653 return STAMP_STYLE_ISO8601
;
2656 asl_out_file_list_t
*
2657 asl_list_log_files(const char *dir
, const char *base
, const char *ext
, uint32_t flags
, bool src
)
2661 char path
[MAXPATHLEN
];
2666 asl_out_file_list_t
*out
, *x
, *y
;
2668 #ifdef DEBUG_LIST_FILES
2669 fprintf(stderr
, "asl_list_log_files dir=%s base=%s ext=%s flags=0x%08x %s\n", dir
, base
, (ext
== NULL
) ? "(NULL)" : ext
, flags
, src
? "src" : "dst");
2672 if (dir
== NULL
) return NULL
;
2673 if (base
== NULL
) return NULL
;
2678 if (d
== NULL
) return NULL
;
2680 while (NULL
!= (ent
= readdir(d
)))
2684 bool stat_ok
= false;
2686 check
= _check_file_name(ent
->d_name
, base
, ext
, flags
, src
, &stamp
);
2687 if (!check
) continue;
2692 pstyle
= _parse_stamp_style(stamp
, flags
, &seq
, &ftime
);
2695 if (pstyle
== STAMP_STYLE_INVALID
) continue;
2697 snprintf(path
, sizeof(path
), "%s/%s", dir
, ent
->d_name
);
2698 memset(&sb
, 0, sizeof(sb
));
2699 if (lstat(path
, &sb
) == 0)
2701 /* ignore symlinks (created for basestamp / symlink files) */
2703 if ((sb
.st_mode
& S_IFMT
) == S_IFLNK
) continue;
2706 fstyle
= STAMP_STYLE_NULL
;
2707 if (flags
& MODULE_NAME_STYLE_STAMP_SEC
) fstyle
= STAMP_STYLE_SEC
;
2708 else if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
) fstyle
= STAMP_STYLE_SEQ
;
2709 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL
)) fstyle
= STAMP_STYLE_ISO8601
;
2710 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
)) fstyle
= STAMP_STYLE_ISO8601
;
2712 #ifdef DEBUG_LIST_FILES
2713 fprintf(stderr
, "%s %s %u fstyle %u pstyle %u\n", __func__
, path
, __LINE__
, fstyle
, pstyle
);
2717 * accept the file if:
2718 * style is STAMP_STYLE_NULL (no timestamp)
2719 * src is true and style is STAMP_STYLE_SEC
2720 * actual style matches the style implied by the input flags
2724 if (pstyle
== STAMP_STYLE_NULL
) check
= true;
2725 if ((pstyle
== STAMP_STYLE_SEC
) && src
) check
= true;
2726 if (pstyle
== fstyle
) check
= true;
2730 #ifdef DEBUG_LIST_FILES
2731 fprintf(stderr
, "%s reject %s at line %u\n", __func__
, path
, __LINE__
);
2736 x
= (asl_out_file_list_t
*)calloc(1, sizeof(asl_out_file_list_t
));
2739 asl_out_file_list_free(out
);
2743 x
->name
= strdup(ent
->d_name
);
2750 x
->size
= sb
.st_size
;
2751 if (pstyle
== STAMP_STYLE_SEQ
)
2753 x
->ftime
= sb
.st_birthtimespec
.tv_sec
;
2754 if (x
->ftime
== 0) x
->ftime
= sb
.st_mtimespec
.tv_sec
;
2758 if (pstyle
== STAMP_STYLE_SEQ
)
2760 #ifdef DEBUG_LIST_FILES
2761 fprintf(stderr
, "asl_list_log_files SEQ %s %u %ld\n", path
, x
->seq
, x
->ftime
);
2767 else if ((x
->seq
== IndexNull
) || ((x
->seq
> out
->seq
) && (out
->seq
!= IndexNull
)))
2775 for (y
= out
; y
!= NULL
; y
= y
->next
)
2777 if (y
->next
== NULL
)
2783 else if ((x
->seq
> y
->next
->seq
) && (y
->next
->seq
!= IndexNull
))
2796 #ifdef DEBUG_LIST_FILES
2797 fprintf(stderr
, "asl_list_log_files TIME %s %ld\n", path
, x
->ftime
);
2803 else if (x
->ftime
< out
->ftime
)
2811 for (y
= out
; y
!= NULL
; y
= y
->next
)
2813 if (y
->next
== NULL
)
2819 else if (x
->ftime
< y
->next
->ftime
)
2837 * List the source files for an output asl_out_dst_data_t
2839 asl_out_file_list_t
*
2840 asl_list_src_files(asl_out_dst_data_t
*dst
)
2843 #ifdef DEBUG_LIST_FILES
2844 fprintf(stderr
, "asl_list_src_files\n");
2847 if (dst
== NULL
) return NULL
;
2848 if (dst
->path
== NULL
) return NULL
;
2849 if (dst
->base
== NULL
) return NULL
;
2852 * MODULE_FLAG_EXTERNAL means some process other than syslogd writes the file.
2853 * We check for its existence, and that it is non-zero in size.
2855 if (dst
->flags
& MODULE_FLAG_EXTERNAL
)
2859 memset(&sb
, 0, sizeof(struct stat
));
2861 if (stat(dst
->path
, &sb
) == 0)
2863 if (S_ISREG(sb
.st_mode
) && (sb
.st_size
!= 0))
2865 asl_out_file_list_t
*out
= (asl_out_file_list_t
*)calloc(1, sizeof(asl_out_file_list_t
));
2868 char *p
= strrchr(dst
->path
, '/');
2869 if (p
== NULL
) p
= dst
->path
;
2871 out
->name
= strdup(p
);
2872 out
->ftime
= sb
.st_birthtimespec
.tv_sec
;
2873 if (out
->ftime
== 0) out
->ftime
= sb
.st_mtimespec
.tv_sec
;
2882 if ((dst
->rotate_dir
== NULL
) && (dst
->flags
& MODULE_FLAG_BASESTAMP
) && ((dst
->flags
& MODULE_FLAG_COMPRESS
) == 0))
2884 /* files do not move to a dest dir, get renamed, or get compressed - nothing to do */
2889 * MODULE_NAME_STYLE_STAMP_SEC format is used for sequenced (MODULE_NAME_STYLE_STAMP_SEQ) files.
2890 * aslmanager converts the file names.
2892 flags
= dst
->style_flags
;
2893 if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
)
2895 flags
&= ~MODULE_NAME_STYLE_STAMP_SEQ
;
2896 flags
|= MODULE_NAME_STYLE_STAMP_SEC
;
2899 return asl_list_log_files(dst
->dir
, dst
->base
, dst
->ext
, flags
, true);
2903 * List the destination files for an output asl_out_dst_data_t
2905 asl_out_file_list_t
*
2906 asl_list_dst_files(asl_out_dst_data_t
*dst
)
2909 #ifdef DEBUG_LIST_FILES
2910 fprintf(stderr
, "asl_list_dst_files\n");
2913 if (dst
== NULL
) return NULL
;
2914 if (dst
->path
== NULL
) return NULL
;
2915 if (dst
->base
== NULL
) return NULL
;
2917 dst_dir
= dst
->rotate_dir
;
2918 if (dst_dir
== NULL
) dst_dir
= dst
->dir
;
2920 return asl_list_log_files(dst_dir
, dst
->base
, dst
->ext
, dst
->style_flags
, false);
2924 asl_secure_open_dir(const char *path
)
2929 if (path
== NULL
) return -1;
2930 if (path
[0] != '/') return -1;
2932 path_parts
= explode(path
+ 1, "/");
2933 if (path_parts
== NULL
) return 0;
2935 fd
= open("/", O_RDONLY
| O_NOFOLLOW
, 0);
2938 free_string_list(path_parts
);
2942 for (i
= 0; path_parts
[i
] != NULL
; i
++)
2944 int fd_next
, status
;
2947 fd_next
= openat(fd
, path_parts
[i
], O_RDONLY
| O_NOFOLLOW
, 0);
2952 free_string_list(path_parts
);
2956 memset(&sb
, 0, sizeof(sb
));
2958 status
= fstat(fd
, &sb
);
2961 free_string_list(path_parts
);
2965 if (!S_ISDIR(sb
.st_mode
))
2967 free_string_list(path_parts
);
2972 free_string_list(path_parts
);
2977 asl_secure_chown_chmod_dir(const char *path
, uid_t uid
, gid_t gid
, mode_t mode
)
2981 fd
= asl_secure_open_dir(path
);
2982 if (fd
< 0) return fd
;
2984 status
= fchown(fd
, uid
, gid
);
2991 if (mode
>= 0) status
= fchmod(fd
, mode
);
2994 if (status
< 0) return -1;