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"
46 #define _PATH_ASL_CONF "/etc/asl.conf"
47 #define _PATH_ASL_CONF_DIR "/etc/asl"
49 #define PATH_VAR_LOG "/var/log/"
50 #define PATH_VAR_LOG_LEN 9
52 #define PATH_LIBRARY_LOGS "/Library/Logs/"
53 #define PATH_LIBRARY_LOGS_LEN 14
55 #if !TARGET_IPHONE_SIMULATOR
56 #define _PATH_ASL_CONF_LOCAL_DIR "/usr/local/etc/asl"
59 //#define DEBUG_LIST_FILES 1
61 static const char *asl_out_action_name
[] =
85 #define forever for(;;)
86 #define KEYMATCH(S,K) ((strncasecmp(S, K, strlen(K)) == 0))
89 xpc_object_to_asl_msg(xpc_object_t xobj
)
91 __block asl_msg_t
*out
;
93 if (xobj
== NULL
) return NULL
;
94 if (xpc_get_type(xobj
) != XPC_TYPE_DICTIONARY
) return NULL
;
96 out
= asl_msg_new(ASL_TYPE_MSG
);
97 xpc_dictionary_apply(xobj
, ^bool(const char *key
, xpc_object_t xval
) {
100 if (xpc_get_type(xval
) == XPC_TYPE_NULL
)
102 asl_msg_set_key_val_op(out
, key
, NULL
, 0);
104 else if (xpc_get_type(xval
) == XPC_TYPE_BOOL
)
106 if (xpc_bool_get_value(xval
)) asl_msg_set_key_val_op(out
, key
, "1", 0);
107 else asl_msg_set_key_val_op(out
, key
, "0", 0);
109 else if (xpc_get_type(xval
) == XPC_TYPE_INT64
)
111 snprintf(tmp
, sizeof(tmp
), "%lld", xpc_int64_get_value(xval
));
112 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
114 else if (xpc_get_type(xval
) == XPC_TYPE_UINT64
)
116 snprintf(tmp
, sizeof(tmp
), "%llu", xpc_uint64_get_value(xval
));
117 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
119 else if (xpc_get_type(xval
) == XPC_TYPE_DOUBLE
)
121 snprintf(tmp
, sizeof(tmp
), "%f", xpc_double_get_value(xval
));
122 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
124 else if (xpc_get_type(xval
) == XPC_TYPE_DATE
)
126 snprintf(tmp
, sizeof(tmp
), "%lld", xpc_date_get_value(xval
));
127 asl_msg_set_key_val_op(out
, key
, tmp
, 0);
129 else if (xpc_get_type(xval
) == XPC_TYPE_DATA
)
131 size_t len
= xpc_data_get_length(xval
);
132 char *encoded
= asl_core_encode_buffer(xpc_data_get_bytes_ptr(xval
), len
);
133 asl_msg_set_key_val_op(out
, key
, encoded
, 0);
136 else if (xpc_get_type(xval
) == XPC_TYPE_STRING
)
138 asl_msg_set_key_val_op(out
, key
, xpc_string_get_string_ptr(xval
), 0);
140 else if (xpc_get_type(xval
) == XPC_TYPE_UUID
)
143 uuid_unparse(xpc_uuid_get_bytes(xval
), us
);
144 asl_msg_set_key_val_op(out
, key
, us
, 0);
146 else if (xpc_get_type(xval
) == XPC_TYPE_FD
)
148 /* XPC_TYPE_FD is not supported */
149 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_FD}", 0);
151 else if (xpc_get_type(xval
) == XPC_TYPE_SHMEM
)
153 /* XPC_TYPE_SHMEM is not supported */
154 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_SHMEM}", 0);
156 else if (xpc_get_type(xval
) == XPC_TYPE_ARRAY
)
158 /* XPC_TYPE_ARRAY is not supported */
159 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_ARRAY}", 0);
161 else if (xpc_get_type(xval
) == XPC_TYPE_DICTIONARY
)
163 /* XPC_TYPE_DICTIONARY is not supported */
164 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_DICTIONARY}", 0);
166 else if (xpc_get_type(xval
) == XPC_TYPE_ERROR
)
168 /* XPC_TYPE_ERROR is not supported */
169 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_ERROR}", 0);
174 asl_msg_set_key_val_op(out
, key
, "{XPC_TYPE_???}", 0);
184 configuration_profile_to_asl_msg(const char *ident
)
186 xpc_object_t xobj
= configuration_profile_copy_property_list(ident
);
187 asl_msg_t
*out
= xpc_object_to_asl_msg(xobj
);
188 if (xobj
!= NULL
) xpc_release(xobj
);
192 /* strdup + skip leading and trailing whitespace */
194 _strdup_clean(const char *s
)
197 const char *first
, *last
;
200 if (s
== NULL
) return NULL
;
203 while ((*first
== ' ') || (*first
== '\t')) first
++;
205 if (len
== 0) return NULL
;
207 last
= first
+ len
- 1;
208 while ((len
> 0) && ((*last
== ' ') || (*last
== '\t')))
214 if (len
== 0) return NULL
;
216 out
= malloc(len
+ 1);
217 if (out
== NULL
) return NULL
;
219 memcpy(out
, first
, len
);
225 _insert_string(char *s
, char **l
, uint32_t x
)
229 if (s
== NULL
) return l
;
232 l
= (char **)malloc(2 * sizeof(char *));
233 if (l
== NULL
) return NULL
;
246 for (i
= 0; l
[i
] != NULL
; i
++);
248 /* len includes the NULL at the end of the list */
251 l
= (char **)reallocf(l
, (len
+ 1) * sizeof(char *));
252 if (l
== NULL
) return NULL
;
254 if ((x
>= (len
- 1)) || (x
== IndexNull
))
256 l
[len
- 1] = strdup(s
);
257 if (l
[len
- 1] == NULL
)
267 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
269 if (l
[x
] == NULL
) return NULL
;
275 explode(const char *s
, const char *delim
)
282 if (s
== NULL
) return NULL
;
290 for (i
= 0; p
[i
] != '\0'; i
++)
294 /* not inside a quoted string: check for delimiters and quotes */
295 if (strchr(delim
, p
[i
]) != NULL
) break;
296 else if (p
[i
] == '\'') quote
= p
[i
];
297 else if (p
[i
] == '"') quote
= p
[i
];
301 /* inside a quoted string - look for matching quote */
302 if (p
[i
] == quote
) quote
= '\0';
308 if (t
== NULL
) return NULL
;
310 for (i
= 0; i
< n
; i
++) t
[i
] = p
[i
];
312 l
= _insert_string(t
, l
, IndexNull
);
315 if (p
[i
] == '\0') return l
;
316 if (p
[i
+ 1] == '\0') l
= _insert_string("", l
, IndexNull
);
324 free_string_list(char **l
)
328 if (l
== NULL
) return;
329 for (i
= 0; l
[i
] != NULL
; i
++) free(l
[i
]);
334 get_line_from_file(FILE *f
)
339 out
= fgetln(f
, &len
);
340 if (out
== NULL
) return NULL
;
341 if (len
== 0) return NULL
;
344 if (s
== NULL
) return NULL
;
348 if (s
[len
- 1] != '\n') len
++;
354 next_word_from_string(char **s
)
356 char *a
, *p
, *e
, *out
, s0
;
357 int quote1
, quote2
, len
;
359 if (s
== NULL
) return NULL
;
360 if (*s
== NULL
) return NULL
;
369 /* allow whole word to be contained in quotes */
405 if (quote1
== 0) quote1
= 1;
411 if (quote2
== 0) quote2
= 1;
415 if (((*p
== ' ') || (*p
== '\t')) && (quote1
== 0) && (quote2
== 0))
429 /* check for quoted string */
430 if (((s0
== '\'') || (s0
== '"')) && (s0
== a
[len
-1])) len
--;
432 if (len
== 0) return NULL
;
434 out
= malloc(len
+ 1);
435 if (out
== NULL
) return NULL
;
443 asl_out_dest_for_path(asl_out_module_t
*m
, const char *path
)
445 if (m
== NULL
) return NULL
;
446 if (path
== NULL
) return NULL
;
450 asl_out_rule_t
*r
= m
->ruleset
;
453 if ((r
->action
== ACTION_OUT_DEST
) && (r
->dst
!= NULL
) && (r
->dst
->path
!= NULL
) && (streq(r
->dst
->path
, path
))) return r
->dst
;
464 * Create a directory path.
466 * mlist provides owner, group, and access mode, which are required for "non-standard"
467 * directories. Directories for standard paths (/var/log or /Library/Logs) default
468 * to root/admin/0755 if there is no mlist rule for them.
471 _asl_common_make_dir_path(asl_out_module_t
*mlist
, uint32_t flags
, const char *path
)
475 asl_string_t
*processed_path
;
478 if (path
== NULL
) return 0;
480 path_parts
= explode(path
, "/");
481 if (path_parts
== NULL
) return 0;
483 processed_path
= asl_string_new(ASL_ENCODE_NONE
);
486 if (path
[0] == '/') i
= 1;
488 for (; path_parts
[i
] != NULL
; i
++)
493 asl_out_dst_data_t
*dst
;
496 asl_string_append_char_no_encoding(processed_path
, '/');
497 asl_string_append_no_encoding(processed_path
, path_parts
[i
]);
498 tmp
= asl_string_bytes(processed_path
);
500 memset(&sb
, 0, sizeof(struct stat
));
501 status
= lstat(tmp
, &sb
);
502 if ((status
== 0) && S_ISLNK(sb
.st_mode
))
504 char real
[MAXPATHLEN
];
505 if (realpath(tmp
, real
) == NULL
)
507 asl_string_release(processed_path
);
508 free_string_list(path_parts
);
512 memset(&sb
, 0, sizeof(struct stat
));
513 status
= stat(real
, &sb
);
518 if (!S_ISDIR(sb
.st_mode
))
520 /* path component is not a directory! */
521 asl_string_release(processed_path
);
522 free_string_list(path_parts
);
526 /* exists and is a directory or a link to a directory */
529 else if (errno
!= ENOENT
)
531 /* unexpected status from stat() */
532 asl_string_release(processed_path
);
533 free_string_list(path_parts
);
537 dst
= asl_out_dest_for_path(mlist
, tmp
);
538 if ((dst
== NULL
) && (flags
& MODULE_FLAG_NONSTD_DIR
))
540 /* no rule to create a non-standard path component! */
541 asl_string_release(processed_path
);
542 free_string_list(path_parts
);
550 if (mode
== 010000) mode
= 0755;
554 status
= mkdir(tmp
, mode
);
557 #if !TARGET_IPHONE_SIMULATOR
563 if (dst
->nuid
> 0) u
= dst
->uid
[0];
564 if (dst
->ngid
> 0) g
= dst
->gid
[0];
571 asl_string_release(processed_path
);
572 free_string_list(path_parts
);
578 asl_out_mkpath(asl_out_module_t
*mlist
, asl_out_rule_t
*r
)
580 char tmp
[MAXPATHLEN
], *p
;
584 if (r
== NULL
) return -1;
585 if (r
->dst
== NULL
) return -1;
586 if (r
->dst
->path
== NULL
) return -1;
588 snprintf(tmp
, sizeof(tmp
), "%s", r
->dst
->path
);
590 if (r
->action
!= ACTION_ASL_DIR
)
592 p
= strrchr(tmp
, '/');
593 if (p
== NULL
) return -1;
597 memset(&sb
, 0, sizeof(struct stat
));
598 status
= stat(tmp
, &sb
);
601 if (S_ISDIR(sb
.st_mode
)) return 0;
607 uint32_t dirflag
= r
->dst
->flags
& MODULE_FLAG_NONSTD_DIR
;
608 status
= _asl_common_make_dir_path(mlist
, dirflag
, tmp
);
616 asl_make_database_dir(const char *dir
, char **out
)
618 const char *asldir
, *path
;
624 if (out
!= NULL
) *out
= NULL
;
626 asldir
= asl_filesystem_path(ASL_PLACE_DATABASE
);
627 if (asldir
== NULL
) return -1;
631 /* create the database directory itself */
636 if (strchr(dir
, '/') != NULL
) return -1;
638 asprintf(&str
, "%s/%s", asldir
, dir
);
639 if (str
== NULL
) return -1;
643 memset(&sb
, 0, sizeof(struct stat
));
645 status
= stat(path
, &sb
);
648 if (S_ISDIR(sb
.st_mode
))
650 if (out
== NULL
) free(str
);
666 status
= mkdir(path
, 0755);
671 if (out
== NULL
) free(str
);
680 asl_make_timestamp(time_t stamp
, uint32_t flags
, char *buf
, size_t len
)
685 if (buf
== NULL
) return;
687 if (flags
& MODULE_NAME_STYLE_STAMP_UTC
)
689 memset(&t
, 0, sizeof(t
));
690 gmtime_r(&stamp
, &t
);
691 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
);
693 else if (flags
& MODULE_NAME_STYLE_STAMP_UTC_B
)
695 memset(&t
, 0, sizeof(t
));
696 gmtime_r(&stamp
, &t
);
697 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
);
699 else if (flags
& MODULE_NAME_STYLE_STAMP_LCL
)
702 memset(&t
, 0, sizeof(t
));
703 localtime_r(&stamp
, &t
);
705 if ((neg
= (t
.tm_gmtoff
< 0))) t
.tm_gmtoff
*= -1;
713 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
);
714 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
);
715 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
);
717 else if (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
)
720 memset(&t
, 0, sizeof(t
));
721 localtime_r(&stamp
, &t
);
723 if ((neg
= (t
.tm_gmtoff
< 0))) t
.tm_gmtoff
*= -1;
731 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
);
732 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
);
733 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
);
737 snprintf(buf
, len
, "%c%llu", STYLE_SEC_PREFIX_CHAR
, (unsigned long long)stamp
);
742 asl_dst_make_current_name(asl_out_dst_data_t
*dst
, uint32_t xflags
, char *buf
, size_t len
)
746 if (dst
== NULL
) return;
747 if (buf
== NULL
) return;
749 xflags
|= dst
->flags
;
751 if (dst
->timestamp
== 0) dst
->timestamp
= time(NULL
);
752 asl_make_timestamp(dst
->timestamp
, dst
->style_flags
, tstamp
, sizeof(tstamp
));
754 if (xflags
& MODULE_FLAG_TYPE_ASL_DIR
)
756 snprintf(buf
, len
, "%s.%s", dst
->current_name
, tstamp
);
758 else if (xflags
& MODULE_FLAG_BASESTAMP
)
760 if ((dst
->dir
!= NULL
) && (dst
->style_flags
& MODULE_NAME_STYLE_FORMAT_BSE
))
762 snprintf(buf
, len
, "%s/%s.%s.%s", dst
->dir
, dst
->base
, tstamp
, dst
->ext
);
766 snprintf(buf
, len
, "%s.%s", dst
->path
, tstamp
);
771 snprintf(buf
, len
, "%s", dst
->path
);
776 asl_check_option(asl_msg_t
*msg
, const char *opt
)
781 if (msg
== NULL
) return 0;
782 if (opt
== NULL
) return 0;
785 if (len
== 0) return 0;
787 p
= asl_msg_get_val_for_key(msg
, ASL_KEY_OPTION
);
788 if (p
== NULL
) return 0;
792 while ((*p
== ' ') || (*p
== '\t') || (*p
== ',')) p
++;
793 if (*p
== '\0') return 0;
795 if (strncasecmp(p
, opt
, len
) == 0)
798 if ((*p
== ' ') || (*p
== '\t') || (*p
== ',') || (*p
== '\0')) return 1;
801 while ((*p
!= ' ') && (*p
!= '\t') && (*p
!= ',') && (*p
!= '\0')) p
++;
808 asl_out_dst_data_release(asl_out_dst_data_t
*dst
)
810 if (dst
== NULL
) return;
812 if (dst
->refcount
> 0) dst
->refcount
--;
813 if (dst
->refcount
> 0) return;
817 free(dst
->current_name
);
820 free(dst
->rotate_dir
);
822 #if !TARGET_IPHONE_SIMULATOR
830 asl_out_dst_data_retain(asl_out_dst_data_t
*dst
)
832 if (dst
== NULL
) return NULL
;
837 /* set owner, group, mode, and acls for a file */
839 asl_out_dst_set_access(int fd
, asl_out_dst_data_t
*dst
)
841 #if !TARGET_IPHONE_SIMULATOR
844 #if !TARGET_OS_EMBEDDED
854 if (dst
== NULL
) return -1;
855 if (fd
< 0) return -1;
857 #if TARGET_IPHONE_SIMULATOR
861 if (dst
->nuid
> 0) fuid
= dst
->uid
[0];
862 if (dst
->ngid
> 0) fgid
= dst
->gid
[0];
864 fchown(fd
, fuid
, fgid
);
866 #if TARGET_OS_EMBEDDED
871 for (i
= 0; i
< dst
->ngid
; i
++)
873 if (dst
->gid
[i
] == -2) continue;
876 * Don't bother setting group access if this is
877 * file's group and the file is group-readable.
879 if ((dst
->gid
[i
] == fgid
) && (dst
->mode
& 00040)) continue;
881 status
= mbr_gid_to_uuid(dst
->gid
[i
], uuid
);
888 status
= acl_create_entry_np(&acl
, &entry
, ACL_FIRST_ENTRY
);
889 if (status
!= 0) goto asl_file_create_return
;
891 status
= acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
892 if (status
!= 0) goto asl_file_create_return
;
894 status
= acl_set_qualifier(entry
, &uuid
);
895 if (status
!= 0) goto asl_file_create_return
;
897 status
= acl_get_permset(entry
, &perms
);
898 if (status
!= 0) goto asl_file_create_return
;
900 status
= acl_add_perm(perms
, ACL_READ_DATA
);
901 if (status
!= 0) goto asl_file_create_return
;
904 for (i
= 0; i
< dst
->nuid
; i
++)
906 if (dst
->uid
[i
] == -2) continue;
909 * Don't bother setting user access if this is
910 * file's owner and the file is owner-readable.
912 if ((dst
->uid
[i
] == fuid
) && (dst
->mode
& 00400)) continue;
914 status
= mbr_uid_to_uuid(dst
->uid
[i
], uuid
);
921 status
= acl_create_entry_np(&acl
, &entry
, ACL_FIRST_ENTRY
);
922 if (status
!= 0) goto asl_file_create_return
;
924 status
= acl_set_tag_type(entry
, ACL_EXTENDED_ALLOW
);
925 if (status
!= 0) goto asl_file_create_return
;
927 status
= acl_set_qualifier(entry
, &uuid
);
928 if (status
!= 0) goto asl_file_create_return
;
930 status
= acl_get_permset(entry
, &perms
);
931 if (status
!= 0) goto asl_file_create_return
;
933 status
= acl_add_perm(perms
, ACL_READ_DATA
);
934 if (status
!= 0) goto asl_file_create_return
;
937 status
= acl_set_fd(fd
, acl
);
944 asl_file_create_return
:
948 #endif /* !TARGET_OS_EMBEDDED */
949 #endif /* !TARGET_IPHONE_SIMULATOR */
952 /* create a file with acls */
954 asl_out_dst_file_create_open(asl_out_dst_data_t
*dst
, char **pathp
)
958 char outpath
[MAXPATHLEN
];
960 if (dst
== NULL
) return -1;
961 if (dst
->path
== NULL
) return -1;
963 asl_dst_make_current_name(dst
, 0, outpath
, sizeof(outpath
));
964 free(dst
->current_name
);
966 dst
->current_name
= strdup(outpath
);
967 if (dst
->current_name
== NULL
) return -1;
969 if (pathp
!= NULL
) *pathp
= strdup(outpath
);
971 memset(&sb
, 0, sizeof(struct stat
));
972 status
= stat(outpath
, &sb
);
975 /* must be a regular file */
976 if (!S_ISREG(sb
.st_mode
)) return -1;
979 fd
= open(outpath
, O_RDWR
| O_APPEND
| O_EXCL
, 0);
981 if (dst
->timestamp
== 0) dst
->timestamp
= sb
.st_birthtimespec
.tv_sec
;
982 if (dst
->timestamp
== 0) dst
->timestamp
= sb
.st_mtimespec
.tv_sec
;
983 dst
->size
= sb
.st_size
;
985 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_FLAG_SYMLINK
)) symlink(outpath
, dst
->path
);
988 else if (errno
!= ENOENT
)
990 /* stat error other than non-existant file */
994 fd
= open(outpath
, O_RDWR
| O_CREAT
| O_EXCL
, (dst
->mode
& 00666));
995 if (fd
< 0) return -1;
997 dst
->timestamp
= time(NULL
);
999 fd
= asl_out_dst_set_access(fd
, dst
);
1000 if (fd
< 0) unlink(outpath
);
1002 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_FLAG_SYMLINK
))
1004 /* remove old symlink, make a new link to the "current" file */
1006 symlink(outpath
, dst
->path
);
1013 asl_out_module_free(asl_out_module_t
*m
)
1015 asl_out_rule_t
*r
, *n
;
1016 asl_out_module_t
*x
;
1030 if (r
->dst
!= NULL
) asl_out_dst_data_release(r
->dst
);
1032 if (r
->query
!= NULL
) asl_msg_release(r
->query
);
1044 asl_out_module_new(const char *name
)
1046 asl_out_module_t
*out
= (asl_out_module_t
*)calloc(1, sizeof(asl_out_module_t
));
1048 if (out
== NULL
) return NULL
;
1049 if (name
== NULL
) return NULL
;
1051 out
->name
= strdup(name
);
1052 if (out
->name
== NULL
)
1058 out
->flags
= MODULE_FLAG_ENABLED
;
1063 /* Skip over query */
1065 _asl_out_module_find_action(char *s
)
1070 if (p
== NULL
) return NULL
;
1072 /* Skip command character (?, Q, *, or =) */
1078 while ((*p
== ' ') || (*p
== '\t')) p
++;
1080 if (*p
== '\0') return NULL
;
1081 if (*p
!= '[') return p
;
1083 /* skip to closing ] */
1097 /* skip whitespace */
1098 while ((*p
== ' ') || (*p
== '\t')) p
++;
1104 * Parse parameter setting line
1107 * evaluated once when module is initialized
1109 * = [query] param options
1110 * evaluated for each message, param set if message matches query
1112 * = param [File path]
1113 * evaluated once when module is initialized
1114 * evaluated when change notification received for path
1116 * = param [Plist path] ...
1117 * evaluated once when module is initialized
1118 * evaluated when change notification received for path
1120 * = param [Profile name] ...
1121 * evaluated once when module is initialized
1122 * evaluated when change notification received for profile
1124 static asl_out_rule_t
*
1125 _asl_out_module_parse_set_param(asl_out_module_t
*m
, char *s
)
1128 asl_out_rule_t
*out
, *rule
;
1130 if (m
== NULL
) return NULL
;
1132 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1133 if (out
== NULL
) return NULL
;
1136 while ((*q
== ' ') || (*q
== '\'')) q
++;
1137 out
->action
= ACTION_SET_PARAM
;
1141 /* = [query] param options */
1142 act
= _asl_out_module_find_action(s
);
1149 out
->options
= _strdup_clean(act
);
1152 if (*p
== ']') p
= act
;
1156 out
->query
= asl_msg_from_string(s
);
1157 if (out
->query
== NULL
)
1170 /* = param options */
1171 out
->options
= _strdup_clean(q
);
1175 /* = param [query] */
1176 if (streq_len(p
, "[File ", 6) || streq_len(p
, "[File\t", 6)) out
->action
= ACTION_SET_FILE
;
1177 else if (streq_len(p
, "[Plist ", 7) || streq_len(p
, "[Plist\t", 7)) out
->action
= ACTION_SET_PLIST
;
1178 else if (streq_len(p
, "[Profile ", 9) || streq_len(p
, "[Profile\t", 9)) out
->action
= ACTION_SET_PROF
;
1182 out
->options
= _strdup_clean(q
);
1187 out
->query
= asl_msg_from_string(p
);
1188 if (out
->query
== NULL
)
1197 if (m
->ruleset
== NULL
) m
->ruleset
= out
;
1200 for (rule
= m
->ruleset
; rule
->next
!= NULL
; rule
= rule
->next
);
1207 #if !TARGET_IPHONE_SIMULATOR
1209 _dst_add_uid(asl_out_dst_data_t
*dst
, char *s
)
1214 if (dst
== NULL
) return;
1215 if (s
== NULL
) return;
1219 #if TARGET_OS_IPHONE
1222 struct passwd
* pw
= getpwnam("mobile");
1230 for (i
= 0 ; i
< dst
->nuid
; i
++)
1232 if (dst
->uid
[i
] == uid
) return;
1235 dst
->uid
= reallocf(dst
->uid
, (dst
->nuid
+ 1) * sizeof(uid_t
));
1236 if (dst
->uid
== NULL
)
1242 dst
->uid
[dst
->nuid
++] = uid
;
1246 _dst_add_gid(asl_out_dst_data_t
*dst
, char *s
)
1251 if (dst
== NULL
) return;
1252 if (s
== NULL
) return;
1256 #if TARGET_OS_IPHONE
1259 struct passwd
* pw
= getpwnam("mobile");
1267 for (i
= 0 ; i
< dst
->ngid
; i
++)
1269 if (dst
->gid
[i
] == gid
) return;
1272 dst
->gid
= reallocf(dst
->gid
, (dst
->ngid
+ 1) * sizeof(gid_t
));
1273 if (dst
->gid
== NULL
)
1279 dst
->gid
[dst
->ngid
++] = gid
;
1281 #endif /* !TARGET_IPHONE_SIMULATOR */
1284 _dst_format_string(char *s
)
1289 if (s
== NULL
) return NULL
;
1293 /* format string can be enclosed by quotes */
1294 if ((len
>= 2) && ((s
[0] == '\'') || (s
[0] == '"')) && (s
[len
-1] == s
[0]))
1301 for (i
= 0; i
< len
; i
++) if (s
[i
] == '\\') n
++;
1303 fmt
= malloc(1 + len
- n
);
1304 if (fmt
== NULL
) return NULL
;
1306 for (i
= 0, n
= 0; i
< len
; i
++) if (s
[i
] != '\\') fmt
[n
++] = s
[i
];
1312 _dst_path_match(const char *newpath
, const char *existingpath
)
1314 if (newpath
== NULL
) return (existingpath
== NULL
);
1315 if (existingpath
== NULL
) return false;
1316 if (newpath
[0] == '/') return (strcmp(newpath
, existingpath
) == 0);
1318 const char *trailing
= strrchr(existingpath
, '/');
1319 if (trailing
== NULL
) return (strcmp(newpath
, existingpath
) == 0);
1321 return (strcmp(newpath
, trailing
) == 0);
1325 _parse_stamp_string(const char *in
)
1330 if (in
== NULL
) return 0;
1332 for (x
= 0; (((in
[x
] >= 'a') && (in
[x
] <= 'z')) || (in
[x
] == '-')) && (x
< 11); x
++) buf
[x
] = in
[x
];
1335 if (streq(buf
, "sec") || streq(buf
, "seconds")) return MODULE_NAME_STYLE_STAMP_SEC
;
1336 if (streq(buf
, "zulu") || streq(buf
, "utc")) return MODULE_NAME_STYLE_STAMP_UTC
;
1337 if (streq(buf
, "utc-b") || streq(buf
, "utc-basic")) return MODULE_NAME_STYLE_STAMP_UTC_B
;
1338 if (streq(buf
, "local") || streq(buf
, "lcl")) return MODULE_NAME_STYLE_STAMP_LCL
;
1339 if (streq(buf
, "local-b") || streq(buf
, "lcl-b") || streq(buf
, "local-basic") || streq(buf
, "lcl-basic")) return MODULE_NAME_STYLE_STAMP_LCL_B
;
1340 if (streq(buf
, "#") || streq(buf
, "seq") || streq(buf
, "sequence"))return MODULE_NAME_STYLE_STAMP_SEQ
;
1346 * Parse a file-rotation naming style.
1348 * Legacy: sec / seconds, utc / date / zulu [-b], local / lcl [-b], # / seq / sequence
1349 * We scan the whole line and match to one of these.
1351 * New scheme: 2 or 3 components: base and style, or base, style, and extension.
1352 * these define a name format. base is the file name without a leading directory path
1353 * and with no extension (e.g. "foo"). style is one of the styles above. extension is
1354 * the file name extension (e.g. "log", "txt", etc).
1361 * The leading base name may be ommitted (E.G. ".lcl.log", ".log.seq")
1362 * If the leading base name AND extension are omitted, it is taken from the path. E.G. ".lcl", ".seq"
1364 * If we get input without a stamp spec, we default to "sec".
1367 _parse_dst_style(asl_out_dst_data_t
*dst
, const char *in
)
1372 if ((dst
== NULL
) || (in
== NULL
)) return -1;
1374 /* check for base. or just . for shorthand */
1382 if (dst
->base
== NULL
) return -1;
1384 len
= strlen(dst
->base
);
1385 if (streq_len(in
, dst
->base
, len
) && (in
[len
] == '.')) p
= in
+ len
+ 1;
1390 /* input does not start with '.' or base, so this is legacy style */
1391 dst
->style_flags
= _parse_stamp_string(in
);
1392 if (dst
->style_flags
== 0) return -1;
1394 if (dst
->ext
== NULL
) dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BS
;
1395 else dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BES
;
1400 /* look for another dot in the name */
1401 for (q
= p
; (*q
!= '.') && (*q
!= ' ') && (*q
!= '\t') && (*q
!= '\0'); q
++);
1402 if (*q
!= '.') q
= NULL
;
1406 /* we require a stamp spec, so we are expecting base.stamp */
1407 dst
->style_flags
= _parse_stamp_string(p
);
1409 if (dst
->style_flags
== 0) return -1;
1412 * We got a valid stamp style ("base.stamp").
1413 * Note that we might have skipped the extention if the file name was "foo.log".
1414 * That's OK - syslogd writes "foo.log", but the rotated files are e.g. foo.20141018T1745Z.
1416 dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BS
;
1420 /* set q to the char past the dot */
1423 /* either base.stamp.ext or base.ext.stamp */
1424 if (dst
->ext
== NULL
) return -1;
1426 len
= strlen(dst
->ext
);
1427 if (streq_len(p
, dst
->ext
, len
) && (p
[len
] == '.'))
1429 /* got base.ext.stamp */
1430 dst
->style_flags
= _parse_stamp_string(q
);
1431 if (dst
->style_flags
== 0) return -1;
1433 dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BES
;
1437 /* must be base.stamp.ext */
1438 if (strneq_len(q
, dst
->ext
, len
)) return -1;
1440 dst
->style_flags
= _parse_stamp_string(p
);
1441 if (dst
->style_flags
== 0) return -1;
1443 dst
->style_flags
|= MODULE_NAME_STYLE_FORMAT_BSE
;
1447 static asl_out_dst_data_t
*
1448 _asl_out_module_parse_dst(asl_out_module_t
*m
, char *s
, mode_t def_mode
)
1450 asl_out_rule_t
*out
, *rule
;
1451 asl_out_dst_data_t
*dst
;
1452 char *p
, *dot
, *opts
, *path
;
1454 int has_dotdot
, recursion_limit
;
1455 uint32_t i
, flags
= 0;
1457 if (m
== NULL
) return NULL
;
1458 if (s
== NULL
) return NULL
;
1460 /* skip whitespace */
1461 while ((*s
== ' ') || (*s
== '\t')) s
++;
1464 path
= next_word_from_string(&opts
);
1465 if (path
== NULL
) return NULL
;
1468 * Check path for ".." component (not permitted).
1469 * Also substitute environment variables.
1472 path_parts
= explode(path
, "/");
1473 asl_string_t
*processed_path
= asl_string_new(ASL_ENCODE_NONE
);
1474 recursion_limit
= 5;
1476 while ((recursion_limit
> 0) && (path_parts
!= NULL
) && (processed_path
!= NULL
))
1481 for (i
= 0; path_parts
[i
] != NULL
; i
++)
1483 if (streq_len(path_parts
[i
], "$ENV(", 5))
1485 char *p
= strchr(path_parts
[i
], ')');
1486 if (p
!= NULL
) *p
= '\0';
1487 char *env_val
= getenv(path_parts
[i
] + 5);
1488 if (env_val
!= NULL
)
1492 if (env_val
[0] != '/') asl_string_append_char_no_encoding(processed_path
, '/');
1493 asl_string_append_no_encoding(processed_path
, env_val
);
1500 if (path_parts
[0][0] != '\0') asl_string_append_no_encoding(processed_path
, path_parts
[i
]);
1504 asl_string_append_char_no_encoding(processed_path
, '/');
1505 asl_string_append_no_encoding(processed_path
, path_parts
[i
]);
1509 if ((has_dotdot
== 0) && streq(path_parts
[i
], "..")) has_dotdot
= 1;
1512 free_string_list(path_parts
);
1515 if ((did_sub
== 1) && (has_dotdot
== 0))
1517 /* substitution might have added a ".." so check the new path */
1519 path
= asl_string_release_return_bytes(processed_path
);
1520 processed_path
= asl_string_new(ASL_ENCODE_NONE
);
1521 path_parts
= explode(path
, "/");
1527 free_string_list(path_parts
);
1530 if ((has_dotdot
!= 0) || (recursion_limit
== 0))
1532 asl_string_release(processed_path
);
1536 path
= asl_string_release_return_bytes(processed_path
);
1538 /* check if there's already a dst for this path */
1539 for (rule
= m
->ruleset
; rule
!= NULL
; rule
= rule
->next
)
1541 if (rule
->action
!= ACTION_OUT_DEST
) continue;
1544 if (dst
== NULL
) continue;
1546 if (_dst_path_match(path
, dst
->path
))
1553 flags
|= MODULE_FLAG_NONSTD_DIR
;
1558 const char *log_root
= "/var/log";
1560 #if TARGET_IPHONE_SIMULATOR
1561 log_root
= getenv("SIMULATOR_LOG_ROOT");
1562 if (log_root
== NULL
) log_root
= "/tmp/log";
1565 if (streq(m
->name
, ASL_MODULE_NAME
))
1567 asprintf(&path
, "%s/%s", log_root
, t
);
1571 asprintf(&path
, "%s/module/%s/%s", log_root
, m
->name
, t
);
1575 flags
&= ~MODULE_FLAG_NONSTD_DIR
;
1580 * Standard log directories get marked so that syslogd
1581 * will create them without explicit rules.
1583 if (streq_len(path
, PATH_VAR_LOG
, PATH_VAR_LOG_LEN
)) flags
&= ~MODULE_FLAG_NONSTD_DIR
;
1584 else if (streq_len(path
, PATH_LIBRARY_LOGS
, PATH_LIBRARY_LOGS_LEN
)) flags
&= ~MODULE_FLAG_NONSTD_DIR
;
1587 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1588 dst
= (asl_out_dst_data_t
*)calloc(1, sizeof(asl_out_dst_data_t
));
1589 if ((out
== NULL
) || (dst
== NULL
))
1600 p
= strrchr(dst
->path
, '/');
1604 dst
->dir
= strdup(dst
->path
);
1608 dst
->mode
= def_mode
;
1609 dst
->ttl
[LEVEL_ALL
] = DEFAULT_TTL
;
1610 dst
->flags
= flags
| MODULE_FLAG_COALESCE
;
1613 * Break out base and extension (if present) from path.
1614 * Note this only supports a '.' as a separator.
1616 p
= strrchr(path
, '/');
1617 if (p
== NULL
) p
= path
;
1620 dot
= strrchr(path
, '.');
1624 dst
->ext
= strdup(dot
+ 1);
1627 dst
->base
= strdup(p
);
1628 if (dot
!= NULL
) *dot
= '.';
1630 while (NULL
!= (p
= next_word_from_string(&opts
)))
1632 if (KEYMATCH(p
, "mode=")) dst
->mode
= strtol(p
+5, NULL
, 0);
1633 #if !TARGET_IPHONE_SIMULATOR
1634 else if (KEYMATCH(p
, "uid=")) _dst_add_uid(dst
, p
+4);
1635 else if (KEYMATCH(p
, "gid=")) _dst_add_gid(dst
, p
+4);
1637 else if (KEYMATCH(p
, "fmt=")) dst
->fmt
= _dst_format_string(p
+4);
1638 else if (KEYMATCH(p
, "format=")) dst
->fmt
= _dst_format_string(p
+7);
1639 else if (KEYMATCH(p
, "dest=")) dst
->rotate_dir
= _strdup_clean(p
+5);
1640 else if (KEYMATCH(p
, "dst=")) dst
->rotate_dir
= _strdup_clean(p
+4);
1641 else if (KEYMATCH(p
, "coalesce="))
1643 if (KEYMATCH(p
+9, "0")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1644 else if (KEYMATCH(p
+9, "off")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1645 else if (KEYMATCH(p
+9, "false")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1647 else if (KEYMATCH(p
, "compress")) dst
->flags
|= MODULE_FLAG_COMPRESS
;
1648 else if (KEYMATCH(p
, "activity")) dst
->flags
|= MODULE_FLAG_ACTIVITY
;
1649 else if (KEYMATCH(p
, "extern")) dst
->flags
|= MODULE_FLAG_EXTERNAL
;
1650 else if (KEYMATCH(p
, "truncate")) dst
->flags
|= MODULE_FLAG_TRUNCATE
;
1651 else if (KEYMATCH(p
, "dir")) dst
->flags
|= MODULE_FLAG_TYPE_ASL_DIR
;
1652 else if (KEYMATCH(p
, "soft")) dst
->flags
|= MODULE_FLAG_SOFT_WRITE
;
1653 else if (KEYMATCH(p
, "file_max=")) dst
->file_max
= asl_core_str_to_size(p
+9);
1654 else if (KEYMATCH(p
, "all_max=")) dst
->all_max
= asl_core_str_to_size(p
+8);
1655 else if (KEYMATCH(p
, "style=") || KEYMATCH(p
, "rotate="))
1657 const char *x
= p
+ 6;
1660 if (_parse_dst_style(dst
, x
) == 0) dst
->flags
|= MODULE_FLAG_ROTATE
;
1662 else if (KEYMATCH(p
, "rotate"))
1664 if (dst
->ext
== NULL
) dst
->style_flags
= MODULE_NAME_STYLE_FORMAT_BS
| MODULE_NAME_STYLE_STAMP_SEC
;
1665 else dst
->style_flags
= MODULE_NAME_STYLE_FORMAT_BES
| MODULE_NAME_STYLE_STAMP_SEC
;
1667 dst
->flags
|= MODULE_FLAG_ROTATE
;
1669 else if (KEYMATCH(p
, "crashlog"))
1671 /* crashlog implies rotation */
1672 dst
->flags
|= MODULE_FLAG_ROTATE
;
1673 dst
->flags
|= MODULE_FLAG_CRASHLOG
;
1674 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1675 dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1677 else if (KEYMATCH(p
, "basestamp"))
1679 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1681 else if (KEYMATCH(p
, "link") || KEYMATCH(p
, "symlink"))
1683 dst
->flags
|= MODULE_FLAG_SYMLINK
;
1685 else if (KEYMATCH(p
, "ttl"))
1690 dst
->ttl
[LEVEL_ALL
] = asl_core_str_to_time(p
+4, SECONDS_PER_DAY
);
1692 else if ((*q
>= '0') && (*q
<= '7') && (*(q
+1) == '='))
1694 uint32_t x
= *q
- '0';
1695 dst
->ttl
[x
] = asl_core_str_to_time(p
+5, SECONDS_PER_DAY
);
1703 #if TARGET_OS_EMBEDDED
1704 /* check for crashreporter files */
1705 if ((KEYMATCH(dst
->path
, _PATH_CRASHREPORTER
)) || (KEYMATCH(dst
->path
, _PATH_CRASHREPORTER_MOBILE_1
)) || (KEYMATCH(dst
->path
, _PATH_CRASHREPORTER_MOBILE_2
)))
1707 dst
->flags
|= MODULE_FLAG_ROTATE
;
1708 dst
->flags
|= MODULE_FLAG_CRASHLOG
;
1709 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1710 dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1714 /* ttl[LEVEL_ALL] must be max of all level-specific ttls */
1715 for (i
= 0; i
<= 7; i
++) if (dst
->ttl
[i
] > dst
->ttl
[LEVEL_ALL
]) dst
->ttl
[LEVEL_ALL
] = dst
->ttl
[i
];
1717 /* default text file format is "std" */
1718 if (dst
->fmt
== NULL
) dst
->fmt
= strdup("std");
1720 /* duplicate compression is only possible for std and bsd formats */
1721 if (strcmp(dst
->fmt
, "std") && strcmp(dst
->fmt
, "bsd")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1723 /* note if format is one of std, bsd, or msg */
1724 if (streq(dst
->fmt
, "std") || streq(dst
->fmt
, "bsd") || streq(dst
->fmt
, "msg")) dst
->flags
|= MODULE_FLAG_STD_BSD_MSG
;
1726 /* MODULE_NAME_STYLE_STAMP_SEQ can not be used with MODULE_FLAG_BASESTAMP */
1727 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_NAME_STYLE_STAMP_SEQ
))
1729 dst
->flags
&= ~MODULE_NAME_STYLE_STAMP_SEQ
;
1730 dst
->flags
|= MODULE_NAME_STYLE_STAMP_SEC
;
1733 /* set time format for raw output */
1734 if (streq(dst
->fmt
, "raw")) dst
->tfmt
= "sec";
1736 /* check for ASL_PLACE_DATABASE_DEFAULT */
1737 if (streq(dst
->path
, ASL_PLACE_DATABASE_DEFAULT
))
1739 dst
->flags
= MODULE_FLAG_TYPE_ASL_DIR
;
1742 /* set file_max to all_max if it is zero */
1743 if (dst
->file_max
== 0) dst
->file_max
= dst
->all_max
;
1745 out
->action
= ACTION_OUT_DEST
;
1748 /* dst rules go first */
1749 out
->next
= m
->ruleset
;
1755 static asl_out_rule_t
*
1756 _asl_out_module_parse_query_action(asl_out_module_t
*m
, char *s
)
1759 asl_out_rule_t
*out
, *rule
;
1761 if (m
== NULL
) return NULL
;
1763 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1764 if (out
== NULL
) return NULL
;
1766 act
= _asl_out_module_find_action(s
);
1767 if (act
== NULL
) return NULL
;
1769 /* find whitespace delimiter */
1770 p
= strchr(act
, ' ');
1771 if (p
== NULL
) p
= strchr(act
, '\t');
1772 if (p
!= NULL
) *p
= '\0';
1774 if (strcaseeq(act
, "ignore")) out
->action
= ACTION_IGNORE
;
1775 else if (strcaseeq(act
, "skip")) out
->action
= ACTION_SKIP
;
1776 else if (strcaseeq(act
, "claim")) out
->action
= ACTION_CLAIM
;
1777 else if (strcaseeq(act
, "notify")) out
->action
= ACTION_NOTIFY
;
1778 else if (strcaseeq(act
, "file")) out
->action
= ACTION_FILE
;
1779 else if (strcaseeq(act
, "asl_file")) out
->action
= ACTION_ASL_FILE
;
1780 else if (strcaseeq(act
, "directory")) out
->action
= ACTION_ASL_DIR
;
1781 else if (strcaseeq(act
, "dir")) out
->action
= ACTION_ASL_DIR
;
1782 else if (strcaseeq(act
, "asl_directory")) out
->action
= ACTION_ASL_DIR
;
1783 else if (strcaseeq(act
, "asl_dir")) out
->action
= ACTION_ASL_DIR
;
1784 else if (strcaseeq(act
, "store_dir")) out
->action
= ACTION_ASL_DIR
;
1785 else if (strcaseeq(act
, "store_directory")) out
->action
= ACTION_ASL_DIR
;
1786 else if (strcaseeq(act
, "control")) out
->action
= ACTION_CONTROL
;
1787 else if (strcaseeq(act
, "save")) out
->action
= ACTION_ASL_STORE
;
1788 else if (strcaseeq(act
, "store")) out
->action
= ACTION_ASL_STORE
;
1789 else if (strcaseeq(act
, "access")) out
->action
= ACTION_ACCESS
;
1790 else if (strcaseeq(act
, "set")) out
->action
= ACTION_SET_KEY
;
1791 else if (strcaseeq(act
, "unset")) out
->action
= ACTION_UNSET_KEY
;
1792 else if (streq(m
->name
, ASL_MODULE_NAME
))
1794 /* actions only allowed in com.apple.asl */
1795 if (strcaseeq(act
, "broadcast")) out
->action
= ACTION_BROADCAST
;
1796 else if (strcaseeq(act
, "forward")) out
->action
= ACTION_FORWARD
;
1799 if (out
->action
== ACTION_NONE
)
1805 /* options follow delimited (now zero) */
1808 /* skip whitespace */
1809 while ((*p
== ' ') || (*p
== '\t')) p
++;
1811 out
->options
= _strdup_clean(p
+1);
1813 if (out
->options
== NULL
)
1826 out
->query
= asl_msg_new(ASL_TYPE_QUERY
);
1831 out
->query
= asl_msg_from_string(s
);
1834 if (out
->query
== NULL
)
1841 /* store /some/path means save to an asl file */
1842 if (out
->action
== ACTION_ASL_STORE
)
1844 if (out
->options
== NULL
) out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, ASL_PLACE_DATABASE_DEFAULT
, 0755));
1845 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));
1846 else if (out
->options
!= NULL
) out
->action
= ACTION_ASL_FILE
;
1849 if ((out
->action
== ACTION_FILE
) || (out
->action
== ACTION_ASL_FILE
) || (out
->action
== ACTION_ASL_DIR
))
1851 mode_t def_mode
= 0644;
1852 if (out
->action
== ACTION_ASL_DIR
) def_mode
= 0755;
1854 out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, out
->options
, def_mode
));
1855 if (out
->dst
== NULL
)
1857 out
->action
= ACTION_NONE
;
1862 * dst might have been set up by a previous ACTION_OUT_DEST ('>') rule with no mode.
1863 * If so, mode would be 010000. Set it now, since we know whether it is a file or dir.
1865 if (out
->dst
->mode
== 010000) out
->dst
->mode
= def_mode
;
1867 if ((out
->action
== ACTION_FILE
) && (out
->dst
!= NULL
) && (out
->dst
->fmt
!= NULL
) && (strcaseeq(out
->dst
->fmt
, "asl")))
1869 out
->action
= ACTION_ASL_FILE
;
1872 if ((out
->action
== ACTION_ASL_FILE
) && (out
->dst
!= NULL
))
1874 out
->dst
->flags
|= MODULE_FLAG_TYPE_ASL
;
1877 if (out
->action
== ACTION_ASL_DIR
)
1879 /* coalesce is meaningless for ASL directories */
1880 out
->dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1882 out
->dst
->flags
|= MODULE_FLAG_TYPE_ASL_DIR
;
1884 /* set style bits for basestamp asl_dirs */
1885 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
;
1888 /* only ACTION_FILE and ACTION_ASL_FILE may rotate */
1889 if ((out
->action
!= ACTION_FILE
) && (out
->action
!= ACTION_ASL_FILE
))
1891 out
->dst
->flags
&= ~MODULE_FLAG_ROTATE
;
1894 #if !TARGET_IPHONE_SIMULATOR
1895 if (out
->dst
->nuid
== 0) _dst_add_uid(out
->dst
, "0");
1896 if (out
->dst
->ngid
== 0) _dst_add_gid(out
->dst
, "80");
1900 if (m
->ruleset
== NULL
) m
->ruleset
= out
;
1903 for (rule
= m
->ruleset
; rule
->next
!= NULL
; rule
= rule
->next
);
1911 asl_out_module_parse_line(asl_out_module_t
*m
, char *s
)
1913 while ((*s
== ' ') || (*s
== '\t')) s
++;
1915 if ((*s
== 'Q') || (*s
== '?') || (*s
== '*'))
1917 return _asl_out_module_parse_query_action(m
, s
);
1921 return _asl_out_module_parse_set_param(m
, s
);
1925 _asl_out_module_parse_dst(m
, s
+ 1, 010000);
1932 asl_out_module_init_from_file(const char *name
, FILE *f
)
1934 asl_out_module_t
*out
;
1937 if (f
== NULL
) return NULL
;
1939 out
= asl_out_module_new(name
);
1940 if (out
== NULL
) return NULL
;
1942 /* read and parse config file */
1943 while (NULL
!= (line
= get_line_from_file(f
)))
1945 asl_out_module_parse_line(out
, line
);
1952 static asl_out_module_t
*
1953 _asl_out_module_find(asl_out_module_t
*list
, const char *name
)
1955 asl_out_module_t
*x
;
1957 if (list
== NULL
) return NULL
;
1958 if (name
== NULL
) return NULL
;
1960 for (x
= list
; x
!= NULL
; x
= x
->next
)
1962 if ((x
->name
!= NULL
) && (streq(x
->name
, name
))) return x
;
1969 _asl_out_module_read_and_merge_dir(asl_out_module_t
**list
, const char *path
, uint32_t flags
)
1974 asl_out_module_t
*last
, *x
;
1976 if (list
== NULL
) return;
1977 if (path
== NULL
) return;
1982 while (last
->next
!= NULL
) last
= last
->next
;
1988 while (NULL
!= (ent
= readdir(d
)))
1990 if (ent
->d_name
[0] != '.')
1992 /* merge: skip this file if we already have a module with this name */
1993 if (_asl_out_module_find(*list
, ent
->d_name
) != NULL
) continue;
1995 char tmp
[MAXPATHLEN
];
1996 snprintf(tmp
, sizeof(tmp
), "%s/%s", path
, ent
->d_name
);
1997 f
= fopen(tmp
, "r");
2000 x
= asl_out_module_init_from_file(ent
->d_name
, f
);
2007 if (streq(ent
->d_name
, ASL_MODULE_NAME
))
2009 /* com.apple.asl goes at the head of the list */
2012 if (last
== NULL
) last
= *list
;
2014 else if (*list
== NULL
)
2034 asl_out_module_init(void)
2036 asl_out_module_t
*out
= NULL
;
2038 #if TARGET_IPHONE_SIMULATOR
2039 char *sim_root_path
, *sim_resources_path
;
2040 char *asl_conf
, *asl_conf_dir
, *asl_conf_local_dir
;
2042 sim_root_path
= getenv("IPHONE_SIMULATOR_ROOT");
2043 if (sim_root_path
== NULL
) return NULL
;
2045 sim_resources_path
= getenv("IPHONE_SHARED_RESOURCES_DIRECTORY");
2046 if (sim_resources_path
== NULL
) return NULL
;
2048 asprintf(&asl_conf
, "%s%s", sim_root_path
, _PATH_ASL_CONF
);
2049 asprintf(&asl_conf_dir
, "%s%s", sim_root_path
, _PATH_ASL_CONF_DIR
);
2050 asprintf(&asl_conf_local_dir
, "%s%s", sim_resources_path
, _PATH_ASL_CONF_DIR
);
2052 _asl_out_module_read_and_merge_dir(&out
, asl_conf_local_dir
, MODULE_FLAG_LOCAL
);
2053 free(asl_conf_local_dir
);
2055 _asl_out_module_read_and_merge_dir(&out
, asl_conf_dir
, 0);
2058 _asl_out_module_read_and_merge_dir(&out
, _PATH_ASL_CONF_LOCAL_DIR
, MODULE_FLAG_LOCAL
);
2059 _asl_out_module_read_and_merge_dir(&out
, _PATH_ASL_CONF_DIR
, 0);
2062 if (_asl_out_module_find(out
, ASL_MODULE_NAME
) == NULL
)
2064 /* system just has old-style /etc/asl.conf */
2065 #if TARGET_IPHONE_SIMULATOR
2066 FILE *f
= fopen(asl_conf
, "r");
2069 FILE *f
= fopen(_PATH_ASL_CONF
, "r");
2073 asl_out_module_t
*x
= asl_out_module_init_from_file(ASL_MODULE_NAME
, f
);
2090 asl_out_module_rule_to_string(asl_out_rule_t
*r
)
2097 asprintf(&out
, "NULL rule");
2101 str
= asl_msg_to_string(r
->query
, &len
);
2103 asprintf(&out
, " %s%s%s%s%s",
2104 asl_out_action_name
[r
->action
],
2105 (r
->query
== NULL
) ? "" : " ",
2106 (r
->query
== NULL
) ? "" : str
,
2107 (r
->options
== NULL
) ? "" : " ",
2108 (r
->options
== NULL
) ? "" : r
->options
);
2115 _stamp_style_name(uint32_t flags
)
2117 if (flags
& MODULE_NAME_STYLE_STAMP_SEC
) return "<seconds>";
2118 if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
) return "<sequence>";
2119 if (flags
& MODULE_NAME_STYLE_STAMP_UTC
) return "<utc>";
2120 if (flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) return "<utc-basic>";
2121 if (flags
& MODULE_NAME_STYLE_STAMP_LCL
) return "<local>";
2122 if (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
) return "<local-basic>";
2130 asl_out_module_print(FILE *f
, asl_out_module_t
*m
)
2132 asl_out_rule_t
*r
, *n
;
2133 asl_out_dst_data_t
*o
;
2134 uint32_t i
, ttlnset
;
2138 for (r
= m
->ruleset
; r
!= NULL
; r
= n
)
2141 char *str
= asl_msg_to_string(r
->query
, &len
);
2143 fprintf(f
, " %s", asl_out_action_name
[r
->action
]);
2144 if (r
->query
!= NULL
) fprintf(f
, " %s", str
);
2145 if (r
->options
!= NULL
) fprintf(f
, " %s", r
->options
);
2146 if (r
->action
== ACTION_OUT_DEST
)
2151 fprintf(f
, " data: NULL");
2155 fprintf(f
, "%s\n", o
->path
);
2156 fprintf(f
, " rules: %u\n", o
->refcount
- 1);
2157 fprintf(f
, " dest: %s\n", (o
->rotate_dir
== NULL
) ? "(none)" : o
->rotate_dir
);
2158 fprintf(f
, " format: %s\n", (o
->fmt
== NULL
) ? "std" : o
->fmt
);
2159 fprintf(f
, " time_format: %s\n", (o
->tfmt
== NULL
) ? "lcl" : o
->tfmt
);
2160 fprintf(f
, " flags: 0x%08x", o
->flags
);
2165 if (o
->flags
& MODULE_FLAG_ENABLED
)
2167 fprintf(f
, "%cenabled", c
);
2170 if (o
->flags
& MODULE_FLAG_SOFT_WRITE
)
2172 fprintf(f
, "%csoft", c
);
2175 if (o
->flags
& MODULE_FLAG_ROTATE
)
2177 fprintf(f
, "%crotate", c
);
2180 if (o
->flags
& MODULE_FLAG_COALESCE
)
2182 fprintf(f
, "%ccoalesce", c
);
2185 if (o
->flags
& MODULE_FLAG_COMPRESS
)
2187 fprintf(f
, "%ccompress", c
);
2190 if (o
->flags
& MODULE_FLAG_BASESTAMP
)
2192 fprintf(f
, "%cbasestamp", c
);
2195 if (o
->flags
& MODULE_FLAG_SYMLINK
)
2197 fprintf(f
, "%csymlink", c
);
2200 if (o
->flags
& MODULE_FLAG_NONSTD_DIR
)
2202 fprintf(f
, "%cnon-std_dir", c
);
2205 if (o
->flags
& MODULE_FLAG_EXTERNAL
)
2207 fprintf(f
, "%cexternal", c
);
2210 if (o
->flags
& MODULE_FLAG_ACTIVITY
)
2212 fprintf(f
, "%cactivity", c
);
2215 if (o
->flags
& MODULE_FLAG_CRASHLOG
)
2217 fprintf(f
, "%ccrashlog", c
);
2220 if (o
->flags
& MODULE_FLAG_TYPE_ASL
)
2222 fprintf(f
, "%casl_file", c
);
2225 if (o
->flags
& MODULE_FLAG_TYPE_ASL_DIR
)
2227 fprintf(f
, "%casl_directory", c
);
2234 if (o
->flags
& MODULE_FLAG_ROTATE
)
2236 fprintf(f
, " rotatation style: ");
2237 if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BS
)
2239 fprintf(f
, "[base=%s].%s\n", o
->base
, _stamp_style_name(o
->style_flags
));
2241 else if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BES
)
2243 fprintf(f
, "[base=%s].[ext=%s].%s\n", o
->base
, o
->ext
, _stamp_style_name(o
->style_flags
));
2245 else if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BSE
)
2247 fprintf(f
, "[base=%s].%s.[ext=%s]\n", o
->base
, _stamp_style_name(o
->style_flags
), o
->ext
);
2251 fprintf(f
, "0x%08x\n", o
->style_flags
);
2255 asl_core_time_to_str(o
->ttl
[LEVEL_ALL
], tstr
, sizeof(tstr
));
2256 fprintf(f
, " ttl: %s\n", tstr
);
2259 for (i
= 0; (i
<= 7) & (ttlnset
== 0); i
++) if (o
->ttl
[i
] != 0) ttlnset
= 1;
2262 for (i
= 0; i
<= 7; i
++)
2264 time_t x
= o
->ttl
[i
];
2265 if (x
== 0) x
= o
->ttl
[LEVEL_ALL
];
2266 asl_core_time_to_str(x
, tstr
, sizeof(tstr
));
2268 fprintf(f
, " [%d %s]", i
, tstr
);
2274 fprintf(f
, " mode: 0%o\n", o
->mode
);
2275 fprintf(f
, " file_max: %lu\n", o
->file_max
);
2276 fprintf(f
, " all_max: %lu\n", o
->all_max
);
2277 #if !TARGET_IPHONE_SIMULATOR
2278 fprintf(f
, " uid:");
2279 for (i
= 0; i
< o
->nuid
; i
++) fprintf(f
, " %d", o
->uid
[i
]);
2281 fprintf(f
, " gid:");
2282 for (i
= 0; i
< o
->ngid
; i
++) fprintf(f
, " %d", o
->gid
[i
]);
2295 asl_out_file_list_free(asl_out_file_list_t
*l
)
2297 asl_out_file_list_t
*n
;
2299 if (l
== NULL
) return;
2311 * Checks input name for one of the forms:
2312 * MODULE_NAME_STYLE_FORMAT_BS base (src only) or base.stamp[.gz]
2313 * MODULE_NAME_STYLE_FORMAT_BES base.ext (src only) or base.ext.stamp[.gz]
2314 * MODULE_NAME_STYLE_FORMAT_BSE base.ext (src only) or base.stamp.ext[.gz]
2316 * name == base[.ext] is allowed if src is true.
2317 * base.gz is not allowed.
2318 * Output parameter stamp must be freed by caller.
2321 _check_file_name(const char *name
, const char *base
, const char *ext
, uint32_t flags
, bool src
, char **stamp
)
2323 size_t baselen
, extlen
;
2327 #ifdef DEBUG_LIST_FILES
2328 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");
2331 if (name
== NULL
) return false;
2332 if (base
== NULL
) return false;
2334 baselen
= strlen(base
);
2335 if (baselen
== 0) return false;
2338 if (ext
!= NULL
) extlen
= strlen(ext
);
2340 if (stamp
!= NULL
) *stamp
= NULL
;
2342 if (strneq_len(name
, base
, baselen
))
2344 #ifdef DEBUG_LIST_FILES
2345 fprintf(stderr
, " base match failed [%u]\n", __LINE__
);
2350 z
= strrchr(name
, '.');
2351 if ((z
!= NULL
) && streq(z
, ".gz")) isgz
= true;
2353 #ifdef DEBUG_LIST_FILES
2354 fprintf(stderr
, "z=%s isgz=%s\n", (z
== NULL
) ? "NULL" : z
, isgz
? "true" : "false");
2359 if (flags
& MODULE_NAME_STYLE_FORMAT_BS
)
2361 #ifdef DEBUG_LIST_FILES
2362 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BS\n");
2366 /* name == base OK if src is true */
2367 #ifdef DEBUG_LIST_FILES
2368 fprintf(stderr
, " name == base %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2373 /* expecting p == .stamp[.gz] */
2376 #ifdef DEBUG_LIST_FILES
2377 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2386 /* base.gz is not allowed */
2387 #ifdef DEBUG_LIST_FILES
2388 fprintf(stderr
, " got base.gz - failed [%u]\n", __LINE__
);
2393 /* got base.stamp[.gz] */
2397 char *x
= strchr(*stamp
, '.');
2398 if (x
!= NULL
) *x
= '\0';
2401 #ifdef DEBUG_LIST_FILES
2402 fprintf(stderr
, " got base.stamp%s - OK\n", isgz
? ".gz" : "");
2406 else if (flags
& MODULE_NAME_STYLE_FORMAT_BES
)
2408 #ifdef DEBUG_LIST_FILES
2409 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BES\n");
2413 #ifdef DEBUG_LIST_FILES
2414 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2421 if (strneq_len(p
, ext
, extlen
))
2423 #ifdef DEBUG_LIST_FILES
2424 fprintf(stderr
, " ext match failed [%u]\n", __LINE__
);
2429 /* expecting p == .ext[.stamp][.gz] */
2434 /* name == base.ext OK if src is true */
2435 #ifdef DEBUG_LIST_FILES
2436 fprintf(stderr
, " name == base.ext %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2441 /* expecting p == .stamp[.gz] */
2444 #ifdef DEBUG_LIST_FILES
2445 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2454 /* base.ext.gz is not allowed */
2455 #ifdef DEBUG_LIST_FILES
2456 fprintf(stderr
, " got base.ext.gz - failed [%u]\n", __LINE__
);
2461 /* got base.ext.stamp[.gz] */
2465 char *x
= strchr(*stamp
, '.');
2466 if (x
!= NULL
) *x
= '\0';
2469 #ifdef DEBUG_LIST_FILES
2470 fprintf(stderr
, " got base.ext.stamp%s - OK\n", isgz
? ".gz" : "");
2474 else if (flags
& MODULE_NAME_STYLE_FORMAT_BSE
)
2476 #ifdef DEBUG_LIST_FILES
2477 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");
2482 #ifdef DEBUG_LIST_FILES
2483 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2490 if (streq_len(p
, ext
, extlen
))
2495 /* name == base.ext OK if src is true */
2496 #ifdef DEBUG_LIST_FILES
2497 fprintf(stderr
, " name == base.ext %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2503 /* expecting p == stamp.ext[.gz] */
2507 if (strneq_len(z
- extlen
, ext
, extlen
))
2509 #ifdef DEBUG_LIST_FILES
2510 fprintf(stderr
, " ext match (%s) isgz failed [%u]\n", z
-extlen
, __LINE__
);
2517 if (strneq_len(z
+ 1, ext
, extlen
))
2519 #ifdef DEBUG_LIST_FILES
2520 fprintf(stderr
, " ext match (%s) failed [%u]\n", z
, __LINE__
);
2526 /* got base.stamp.ext[.gz] */
2530 char *x
= strchr(*stamp
, '.');
2531 if (x
!= NULL
) *x
= '\0';
2534 #ifdef DEBUG_LIST_FILES
2535 fprintf(stderr
, " got base.stamp.ext%s - OK\n", isgz
? ".gz" : "");
2540 #ifdef DEBUG_LIST_FILES
2541 fprintf(stderr
, " unknown format - failed\n");
2548 * Find files in a directory (dir) that all have a common prefix (base).
2549 * Bits in flags further control the search.
2551 * MODULE_NAME_STYLE_STAMP_SEQ means a numeric sequence number is expected, although not required.
2552 * E.g. foo.log foo.log.0
2554 * MODULE_NAME_STYLE_STAMP_SEC also means a numeric sequence number is required following an 'T' character.
2555 * The numeric value is the file's timestamp in seconds. E.g foo.log.T1335200452
2557 * MODULE_NAME_STYLE_STAMP_UTC requires a date/time component as the file's timestamp.
2558 * E.g. foo.2012-04-06T15:30:00Z
2560 * MODULE_NAME_STYLE_STAMP_UTC_B requires a date/time component as the file's timestamp.
2561 * E.g. foo.20120406T153000Z
2563 * MODULE_NAME_STYLE_STAMP_LCL requires a date/time component as the file's timestamp.
2564 * E.g. foo.2012-04-06T15:30:00-7
2566 * MODULE_NAME_STYLE_STAMP_LCL_B requires a date/time component as the file's timestamp.
2567 * E.g. foo.20120406T153000-07
2570 _parse_stamp_style(char *stamp
, uint32_t flags
, uint32_t *sp
, time_t *tp
)
2577 long utc_offset
= 0;
2580 /* check for NULL (no stamp) */
2581 if (stamp
== NULL
) return STAMP_STYLE_NULL
;
2583 /* check for MODULE_NAME_STYLE_STAMP_SEC (foo.T12345678) */
2584 if (stamp
[0] == 'T')
2586 n
= atoi(stamp
+ 1);
2587 if ((n
== 0) && strcmp(stamp
+ 1, "0")) return STAMP_STYLE_INVALID
;
2588 if (tp
!= NULL
) *tp
= (time_t)n
;
2590 return STAMP_STYLE_SEC
;
2593 /* check for MODULE_NAME_STYLE_STAMP_SEQ (foo.0 or foo.2.gz) */
2595 for (i
= 0; digits
&& (stamp
[i
] != '\0'); i
++) digits
= (stamp
[i
] >= '0') && (stamp
[i
] <= '9');
2597 if (!digits
&& (streq(stamp
+ i
, ".gz"))) digits
= true;
2602 if (sp
!= NULL
) *sp
= (uint32_t)n
;
2603 return STAMP_STYLE_SEQ
;
2606 /* check for MODULE_NAME_STYLE_STAMP_UTC, UTC_B, LCL, or LCL_B */
2607 memset(&t
, 0, sizeof(t
));
2611 if ((flags
& MODULE_NAME_STYLE_STAMP_UTC
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL
))
2613 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
);
2615 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
))
2617 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
);
2621 #ifdef DEBUG_LIST_FILES
2622 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2624 return STAMP_STYLE_INVALID
;
2629 #ifdef DEBUG_LIST_FILES
2630 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2632 return STAMP_STYLE_INVALID
;
2639 else if ((zone
== '-') || (zone
== '+'))
2641 if (n
>= 8) utc_offset
+= (3600 * h
);
2642 if (n
>= 9) utc_offset
+= (60 * m
);
2643 if (n
== 10) utc_offset
+= s
;
2644 if (zone
== '+') utc_offset
*= -1;
2646 else if ((zone
>= 'A') && (zone
<= 'Z'))
2648 if (zone
< 'J') utc_offset
= 3600 * ((zone
- 'A') + 1);
2649 else if ((zone
>= 'K') && (zone
<= 'M')) utc_offset
= 3600 * (zone
- 'A');
2650 else if (zone
<= 'Y') utc_offset
= -3600 * ((zone
- 'N') + 1);
2652 else if ((zone
>= 'a') && (zone
<= 'z'))
2654 if (zone
< 'j') utc_offset
= 3600 * ((zone
- 'a') + 1);
2655 else if ((zone
>= 'k') && (zone
<= 'm')) utc_offset
= 3600 * (zone
- 'a');
2656 else if (zone
<= 'y') utc_offset
= -3600 * ((zone
- 'n') + 1);
2660 #ifdef DEBUG_LIST_FILES
2661 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2663 return STAMP_STYLE_INVALID
;
2668 t
.tm_sec
+= utc_offset
;
2671 if ((zone
== 'J') || (zone
== 'j')) ftime
= mktime(&t
);
2672 else ftime
= timegm(&t
);
2674 if (tp
!= NULL
) *tp
= ftime
;
2676 return STAMP_STYLE_ISO8601
;
2679 asl_out_file_list_t
*
2680 asl_list_log_files(const char *dir
, const char *base
, const char *ext
, uint32_t flags
, bool src
)
2684 char path
[MAXPATHLEN
];
2689 asl_out_file_list_t
*out
, *x
, *y
;
2691 #ifdef DEBUG_LIST_FILES
2692 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");
2695 if (dir
== NULL
) return NULL
;
2696 if (base
== NULL
) return NULL
;
2701 if (d
== NULL
) return NULL
;
2703 while (NULL
!= (ent
= readdir(d
)))
2707 bool stat_ok
= false;
2709 check
= _check_file_name(ent
->d_name
, base
, ext
, flags
, src
, &stamp
);
2710 if (!check
) continue;
2715 pstyle
= _parse_stamp_style(stamp
, flags
, &seq
, &ftime
);
2718 if (pstyle
== STAMP_STYLE_INVALID
) continue;
2720 snprintf(path
, sizeof(path
), "%s/%s", dir
, ent
->d_name
);
2721 memset(&sb
, 0, sizeof(sb
));
2722 if (lstat(path
, &sb
) == 0)
2724 /* ignore symlinks (created for basestamp / symlink files) */
2726 if ((sb
.st_mode
& S_IFMT
) == S_IFLNK
) continue;
2729 fstyle
= STAMP_STYLE_NULL
;
2730 if (flags
& MODULE_NAME_STYLE_STAMP_SEC
) fstyle
= STAMP_STYLE_SEC
;
2731 else if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
) fstyle
= STAMP_STYLE_SEQ
;
2732 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL
)) fstyle
= STAMP_STYLE_ISO8601
;
2733 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
)) fstyle
= STAMP_STYLE_ISO8601
;
2735 #ifdef DEBUG_LIST_FILES
2736 fprintf(stderr
, "%s %s %u fstyle %u pstyle %u\n", __func__
, path
, __LINE__
, fstyle
, pstyle
);
2740 * accept the file if:
2741 * style is STAMP_STYLE_NULL (no timestamp)
2742 * src is true and style is STAMP_STYLE_SEC
2743 * actual style matches the style implied by the input flags
2747 if (pstyle
== STAMP_STYLE_NULL
) check
= true;
2748 if ((pstyle
== STAMP_STYLE_SEC
) && src
) check
= true;
2749 if (pstyle
== fstyle
) check
= true;
2753 #ifdef DEBUG_LIST_FILES
2754 fprintf(stderr
, "%s reject %s at line %u\n", __func__
, path
, __LINE__
);
2759 x
= (asl_out_file_list_t
*)calloc(1, sizeof(asl_out_file_list_t
));
2762 asl_out_file_list_free(out
);
2766 x
->name
= strdup(ent
->d_name
);
2773 x
->size
= sb
.st_size
;
2774 if (pstyle
== STAMP_STYLE_SEQ
)
2776 x
->ftime
= sb
.st_birthtimespec
.tv_sec
;
2777 if (x
->ftime
== 0) x
->ftime
= sb
.st_mtimespec
.tv_sec
;
2781 if (pstyle
== STAMP_STYLE_SEQ
)
2783 #ifdef DEBUG_LIST_FILES
2784 fprintf(stderr
, "asl_list_log_files SEQ %s %u %ld\n", path
, x
->seq
, x
->ftime
);
2790 else if ((x
->seq
== IndexNull
) || ((x
->seq
> out
->seq
) && (out
->seq
!= IndexNull
)))
2798 for (y
= out
; y
!= NULL
; y
= y
->next
)
2800 if (y
->next
== NULL
)
2806 else if ((x
->seq
> y
->next
->seq
) && (y
->next
->seq
!= IndexNull
))
2819 #ifdef DEBUG_LIST_FILES
2820 fprintf(stderr
, "asl_list_log_files TIME %s %ld\n", path
, x
->ftime
);
2826 else if (x
->ftime
< out
->ftime
)
2834 for (y
= out
; y
!= NULL
; y
= y
->next
)
2836 if (y
->next
== NULL
)
2842 else if (x
->ftime
< y
->next
->ftime
)
2860 * List the source files for an output asl_out_dst_data_t
2862 asl_out_file_list_t
*
2863 asl_list_src_files(asl_out_dst_data_t
*dst
)
2866 #ifdef DEBUG_LIST_FILES
2867 fprintf(stderr
, "asl_list_src_files\n");
2870 if (dst
== NULL
) return NULL
;
2871 if (dst
->path
== NULL
) return NULL
;
2872 if (dst
->base
== NULL
) return NULL
;
2875 * MODULE_FLAG_EXTERNAL means some process other than syslogd writes the file.
2876 * We check for its existence, and that it is non-zero in size.
2878 if (dst
->flags
& MODULE_FLAG_EXTERNAL
)
2882 memset(&sb
, 0, sizeof(struct stat
));
2884 if (stat(dst
->path
, &sb
) == 0)
2886 if (S_ISREG(sb
.st_mode
) && (sb
.st_size
!= 0))
2888 asl_out_file_list_t
*out
= (asl_out_file_list_t
*)calloc(1, sizeof(asl_out_file_list_t
));
2891 char *p
= strrchr(dst
->path
, '/');
2892 if (p
== NULL
) p
= dst
->path
;
2894 out
->name
= strdup(p
);
2895 out
->ftime
= sb
.st_birthtimespec
.tv_sec
;
2896 if (out
->ftime
== 0) out
->ftime
= sb
.st_mtimespec
.tv_sec
;
2905 if ((dst
->rotate_dir
== NULL
) && (dst
->flags
& MODULE_FLAG_BASESTAMP
) && ((dst
->flags
& MODULE_FLAG_COMPRESS
) == 0))
2907 /* files do not move to a dest dir, get renamed, or get compressed - nothing to do */
2912 * MODULE_NAME_STYLE_STAMP_SEC format is used for sequenced (MODULE_NAME_STYLE_STAMP_SEQ) files.
2913 * aslmanager converts the file names.
2915 flags
= dst
->style_flags
;
2916 if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
)
2918 flags
&= ~MODULE_NAME_STYLE_STAMP_SEQ
;
2919 flags
|= MODULE_NAME_STYLE_STAMP_SEC
;
2922 return asl_list_log_files(dst
->dir
, dst
->base
, dst
->ext
, flags
, true);
2926 * List the destination files for an output asl_out_dst_data_t
2928 asl_out_file_list_t
*
2929 asl_list_dst_files(asl_out_dst_data_t
*dst
)
2932 #ifdef DEBUG_LIST_FILES
2933 fprintf(stderr
, "asl_list_dst_files\n");
2936 if (dst
== NULL
) return NULL
;
2937 if (dst
->path
== NULL
) return NULL
;
2938 if (dst
->base
== NULL
) return NULL
;
2940 dst_dir
= dst
->rotate_dir
;
2941 if (dst_dir
== NULL
) dst_dir
= dst
->dir
;
2943 return asl_list_log_files(dst_dir
, dst
->base
, dst
->ext
, dst
->style_flags
, false);
2947 asl_secure_open_dir(const char *path
)
2952 if (path
== NULL
) return -1;
2953 if (path
[0] != '/') return -1;
2955 path_parts
= explode(path
+ 1, "/");
2956 if (path_parts
== NULL
) return 0;
2958 fd
= open("/", O_RDONLY
| O_NOFOLLOW
, 0);
2961 free_string_list(path_parts
);
2965 for (i
= 0; path_parts
[i
] != NULL
; i
++)
2967 int fd_next
, status
;
2970 fd_next
= openat(fd
, path_parts
[i
], O_RDONLY
| O_NOFOLLOW
, 0);
2975 free_string_list(path_parts
);
2979 memset(&sb
, 0, sizeof(sb
));
2981 status
= fstat(fd
, &sb
);
2984 free_string_list(path_parts
);
2988 if (!S_ISDIR(sb
.st_mode
))
2990 free_string_list(path_parts
);
2995 free_string_list(path_parts
);
3000 asl_secure_chown_chmod_dir(const char *path
, uid_t uid
, gid_t gid
, mode_t mode
)
3004 fd
= asl_secure_open_dir(path
);
3005 if (fd
< 0) return fd
;
3007 status
= fchown(fd
, uid
, gid
);
3014 if (mode
>= 0) status
= fchmod(fd
, mode
);
3017 if (status
< 0) return -1;