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_OS_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_OS_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_OS_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_OS_SIMULATOR
844 #if !TARGET_OS_EMBEDDED
854 if (dst
== NULL
) return -1;
855 if (fd
< 0) return -1;
857 #if TARGET_OS_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_OS_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_OS_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_OS_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_OS_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_OS_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
, "extern")) dst
->flags
|= MODULE_FLAG_EXTERNAL
;
1649 else if (KEYMATCH(p
, "truncate")) dst
->flags
|= MODULE_FLAG_TRUNCATE
;
1650 else if (KEYMATCH(p
, "dir")) dst
->flags
|= MODULE_FLAG_TYPE_ASL_DIR
;
1651 else if (KEYMATCH(p
, "soft")) dst
->flags
|= MODULE_FLAG_SOFT_WRITE
;
1652 else if (KEYMATCH(p
, "file_max=")) dst
->file_max
= asl_core_str_to_size(p
+9);
1653 else if (KEYMATCH(p
, "all_max=")) dst
->all_max
= asl_core_str_to_size(p
+8);
1654 else if (KEYMATCH(p
, "style=") || KEYMATCH(p
, "rotate="))
1656 const char *x
= p
+ 6;
1659 if (_parse_dst_style(dst
, x
) == 0) dst
->flags
|= MODULE_FLAG_ROTATE
;
1661 else if (KEYMATCH(p
, "rotate"))
1663 if (dst
->ext
== NULL
) dst
->style_flags
= MODULE_NAME_STYLE_FORMAT_BS
| MODULE_NAME_STYLE_STAMP_SEC
;
1664 else dst
->style_flags
= MODULE_NAME_STYLE_FORMAT_BES
| MODULE_NAME_STYLE_STAMP_SEC
;
1666 dst
->flags
|= MODULE_FLAG_ROTATE
;
1668 else if (KEYMATCH(p
, "crashlog"))
1670 /* crashlog implies rotation */
1671 dst
->flags
|= MODULE_FLAG_ROTATE
;
1672 dst
->flags
|= MODULE_FLAG_CRASHLOG
;
1673 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1674 dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1676 else if (KEYMATCH(p
, "basestamp"))
1678 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1680 else if (KEYMATCH(p
, "link") || KEYMATCH(p
, "symlink"))
1682 dst
->flags
|= MODULE_FLAG_SYMLINK
;
1684 else if (KEYMATCH(p
, "ttl"))
1689 dst
->ttl
[LEVEL_ALL
] = asl_core_str_to_time(p
+4, SECONDS_PER_DAY
);
1691 else if ((*q
>= '0') && (*q
<= '7') && (*(q
+1) == '='))
1693 uint32_t x
= *q
- '0';
1694 dst
->ttl
[x
] = asl_core_str_to_time(p
+5, SECONDS_PER_DAY
);
1697 else if (KEYMATCH(p
, "size_only"))
1699 dst
->flags
|= MODULE_FLAG_SIZE_ONLY
;
1706 #if TARGET_OS_EMBEDDED
1707 /* check for crashreporter files */
1708 if ((KEYMATCH(dst
->path
, _PATH_CRASHREPORTER
)) || (KEYMATCH(dst
->path
, _PATH_CRASHREPORTER_MOBILE_1
)) || (KEYMATCH(dst
->path
, _PATH_CRASHREPORTER_MOBILE_2
)))
1710 dst
->flags
|= MODULE_FLAG_ROTATE
;
1711 dst
->flags
|= MODULE_FLAG_CRASHLOG
;
1712 dst
->flags
|= MODULE_FLAG_BASESTAMP
;
1713 dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1717 /* ttl[LEVEL_ALL] must be max of all level-specific ttls */
1718 for (i
= 0; i
<= 7; i
++) if (dst
->ttl
[i
] > dst
->ttl
[LEVEL_ALL
]) dst
->ttl
[LEVEL_ALL
] = dst
->ttl
[i
];
1720 /* default text file format is "std" */
1721 if (dst
->fmt
== NULL
) dst
->fmt
= strdup("std");
1723 /* duplicate compression is only possible for std and bsd formats */
1724 if (strcmp(dst
->fmt
, "std") && strcmp(dst
->fmt
, "bsd")) dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1726 /* note if format is one of std, bsd, or msg */
1727 if (streq(dst
->fmt
, "std") || streq(dst
->fmt
, "bsd") || streq(dst
->fmt
, "msg")) dst
->flags
|= MODULE_FLAG_STD_BSD_MSG
;
1729 /* MODULE_NAME_STYLE_STAMP_SEQ can not be used with MODULE_FLAG_BASESTAMP */
1730 if ((dst
->flags
& MODULE_FLAG_BASESTAMP
) && (dst
->flags
& MODULE_NAME_STYLE_STAMP_SEQ
))
1732 dst
->flags
&= ~MODULE_NAME_STYLE_STAMP_SEQ
;
1733 dst
->flags
|= MODULE_NAME_STYLE_STAMP_SEC
;
1736 /* set time format for raw output */
1737 if (streq(dst
->fmt
, "raw")) dst
->tfmt
= "sec";
1739 /* check for ASL_PLACE_DATABASE_DEFAULT */
1740 if (streq(dst
->path
, ASL_PLACE_DATABASE_DEFAULT
))
1742 dst
->flags
= MODULE_FLAG_TYPE_ASL_DIR
;
1745 /* set file_max to all_max if it is zero */
1746 if (dst
->file_max
== 0) dst
->file_max
= dst
->all_max
;
1748 /* size_only option requires non-zero file_max and all_max settings */
1749 if ((dst
->flags
& MODULE_FLAG_SIZE_ONLY
) && (dst
->file_max
== 0 || dst
->all_max
== 0))
1751 dst
->flags
&= ~MODULE_FLAG_SIZE_ONLY
;
1754 /* size_only option cannot be used with crashlog option */
1755 if ((dst
->flags
& MODULE_FLAG_SIZE_ONLY
) && (dst
->flags
& MODULE_FLAG_CRASHLOG
))
1757 dst
->flags
&= ~MODULE_FLAG_SIZE_ONLY
;
1760 out
->action
= ACTION_OUT_DEST
;
1763 /* dst rules go first */
1764 out
->next
= m
->ruleset
;
1770 static asl_out_rule_t
*
1771 _asl_out_module_parse_query_action(asl_out_module_t
*m
, char *s
)
1774 asl_out_rule_t
*out
, *rule
;
1776 if (m
== NULL
) return NULL
;
1778 out
= (asl_out_rule_t
*)calloc(1, sizeof(asl_out_rule_t
));
1779 if (out
== NULL
) return NULL
;
1781 act
= _asl_out_module_find_action(s
);
1782 if (act
== NULL
) return NULL
;
1784 /* find whitespace delimiter */
1785 p
= strchr(act
, ' ');
1786 if (p
== NULL
) p
= strchr(act
, '\t');
1787 if (p
!= NULL
) *p
= '\0';
1789 if (strcaseeq(act
, "ignore")) out
->action
= ACTION_IGNORE
;
1790 else if (strcaseeq(act
, "skip")) out
->action
= ACTION_SKIP
;
1791 else if (strcaseeq(act
, "claim")) out
->action
= ACTION_CLAIM
;
1792 else if (strcaseeq(act
, "notify")) out
->action
= ACTION_NOTIFY
;
1793 else if (strcaseeq(act
, "file")) out
->action
= ACTION_FILE
;
1794 else if (strcaseeq(act
, "asl_file")) out
->action
= ACTION_ASL_FILE
;
1795 else if (strcaseeq(act
, "directory")) out
->action
= ACTION_ASL_DIR
;
1796 else if (strcaseeq(act
, "dir")) out
->action
= ACTION_ASL_DIR
;
1797 else if (strcaseeq(act
, "asl_directory")) out
->action
= ACTION_ASL_DIR
;
1798 else if (strcaseeq(act
, "asl_dir")) out
->action
= ACTION_ASL_DIR
;
1799 else if (strcaseeq(act
, "store_dir")) out
->action
= ACTION_ASL_DIR
;
1800 else if (strcaseeq(act
, "store_directory")) out
->action
= ACTION_ASL_DIR
;
1801 else if (strcaseeq(act
, "control")) out
->action
= ACTION_CONTROL
;
1802 else if (strcaseeq(act
, "save")) out
->action
= ACTION_ASL_STORE
;
1803 else if (strcaseeq(act
, "store")) out
->action
= ACTION_ASL_STORE
;
1804 else if (strcaseeq(act
, "access")) out
->action
= ACTION_ACCESS
;
1805 else if (strcaseeq(act
, "set")) out
->action
= ACTION_SET_KEY
;
1806 else if (strcaseeq(act
, "unset")) out
->action
= ACTION_UNSET_KEY
;
1807 else if (streq(m
->name
, ASL_MODULE_NAME
))
1809 /* actions only allowed in com.apple.asl */
1810 if (strcaseeq(act
, "broadcast")) out
->action
= ACTION_BROADCAST
;
1811 else if (strcaseeq(act
, "forward")) out
->action
= ACTION_FORWARD
;
1814 if (out
->action
== ACTION_NONE
)
1820 /* options follow delimited (now zero) */
1823 /* skip whitespace */
1824 while ((*p
== ' ') || (*p
== '\t')) p
++;
1826 out
->options
= _strdup_clean(p
+1);
1828 if (out
->options
== NULL
)
1841 out
->query
= asl_msg_new(ASL_TYPE_QUERY
);
1846 out
->query
= asl_msg_from_string(s
);
1849 if (out
->query
== NULL
)
1856 /* store /some/path means save to an asl file */
1857 if (out
->action
== ACTION_ASL_STORE
)
1859 if (out
->options
== NULL
) out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, ASL_PLACE_DATABASE_DEFAULT
, 0755));
1860 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));
1861 else if (out
->options
!= NULL
) out
->action
= ACTION_ASL_FILE
;
1864 if ((out
->action
== ACTION_FILE
) || (out
->action
== ACTION_ASL_FILE
) || (out
->action
== ACTION_ASL_DIR
))
1866 mode_t def_mode
= 0644;
1867 if (out
->action
== ACTION_ASL_DIR
) def_mode
= 0755;
1869 out
->dst
= asl_out_dst_data_retain(_asl_out_module_parse_dst(m
, out
->options
, def_mode
));
1870 if (out
->dst
== NULL
)
1872 out
->action
= ACTION_NONE
;
1877 * dst might have been set up by a previous ACTION_OUT_DEST ('>') rule with no mode.
1878 * If so, mode would be 010000. Set it now, since we know whether it is a file or dir.
1880 if (out
->dst
->mode
== 010000) out
->dst
->mode
= def_mode
;
1882 if ((out
->action
== ACTION_FILE
) && (out
->dst
!= NULL
) && (out
->dst
->fmt
!= NULL
) && (strcaseeq(out
->dst
->fmt
, "asl")))
1884 out
->action
= ACTION_ASL_FILE
;
1887 if ((out
->action
== ACTION_ASL_FILE
) && (out
->dst
!= NULL
))
1889 out
->dst
->flags
|= MODULE_FLAG_TYPE_ASL
;
1892 if (out
->action
== ACTION_ASL_DIR
)
1894 /* coalesce is meaningless for ASL directories */
1895 out
->dst
->flags
&= ~MODULE_FLAG_COALESCE
;
1897 out
->dst
->flags
|= MODULE_FLAG_TYPE_ASL_DIR
;
1899 /* set style bits for basestamp asl_dirs */
1900 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
;
1903 /* only ACTION_FILE and ACTION_ASL_FILE may rotate */
1904 if ((out
->action
!= ACTION_FILE
) && (out
->action
!= ACTION_ASL_FILE
))
1906 out
->dst
->flags
&= ~MODULE_FLAG_ROTATE
;
1909 #if !TARGET_OS_SIMULATOR
1910 if (out
->dst
->nuid
== 0) _dst_add_uid(out
->dst
, "0");
1911 if (out
->dst
->ngid
== 0) _dst_add_gid(out
->dst
, "80");
1915 if (m
->ruleset
== NULL
) m
->ruleset
= out
;
1918 for (rule
= m
->ruleset
; rule
->next
!= NULL
; rule
= rule
->next
);
1926 asl_out_module_parse_line(asl_out_module_t
*m
, char *s
)
1928 while ((*s
== ' ') || (*s
== '\t')) s
++;
1930 if ((*s
== 'Q') || (*s
== '?') || (*s
== '*'))
1932 return _asl_out_module_parse_query_action(m
, s
);
1936 return _asl_out_module_parse_set_param(m
, s
);
1940 _asl_out_module_parse_dst(m
, s
+ 1, 010000);
1947 asl_out_module_init_from_file(const char *name
, FILE *f
)
1949 asl_out_module_t
*out
;
1952 if (f
== NULL
) return NULL
;
1954 out
= asl_out_module_new(name
);
1955 if (out
== NULL
) return NULL
;
1957 /* read and parse config file */
1958 while (NULL
!= (line
= get_line_from_file(f
)))
1960 asl_out_module_parse_line(out
, line
);
1967 static asl_out_module_t
*
1968 _asl_out_module_find(asl_out_module_t
*list
, const char *name
)
1970 asl_out_module_t
*x
;
1972 if (list
== NULL
) return NULL
;
1973 if (name
== NULL
) return NULL
;
1975 for (x
= list
; x
!= NULL
; x
= x
->next
)
1977 if ((x
->name
!= NULL
) && (streq(x
->name
, name
))) return x
;
1984 _asl_out_module_read_and_merge_dir(asl_out_module_t
**list
, const char *path
, uint32_t flags
)
1989 asl_out_module_t
*last
, *x
;
1991 if (list
== NULL
) return;
1992 if (path
== NULL
) return;
1997 while (last
->next
!= NULL
) last
= last
->next
;
2003 while (NULL
!= (ent
= readdir(d
)))
2005 if (ent
->d_name
[0] != '.')
2007 /* merge: skip this file if we already have a module with this name */
2008 if (_asl_out_module_find(*list
, ent
->d_name
) != NULL
) continue;
2010 char tmp
[MAXPATHLEN
];
2011 snprintf(tmp
, sizeof(tmp
), "%s/%s", path
, ent
->d_name
);
2012 f
= fopen(tmp
, "r");
2015 x
= asl_out_module_init_from_file(ent
->d_name
, f
);
2022 if (streq(ent
->d_name
, ASL_MODULE_NAME
))
2024 /* com.apple.asl goes at the head of the list */
2027 if (last
== NULL
) last
= *list
;
2029 else if (*list
== NULL
)
2049 asl_out_module_init(void)
2051 asl_out_module_t
*out
= NULL
;
2053 #if TARGET_OS_SIMULATOR
2054 char *sim_root_path
, *sim_resources_path
;
2055 char *asl_conf
, *asl_conf_dir
, *asl_conf_local_dir
;
2057 sim_root_path
= getenv("IPHONE_SIMULATOR_ROOT");
2058 if (sim_root_path
== NULL
) return NULL
;
2060 sim_resources_path
= getenv("IPHONE_SHARED_RESOURCES_DIRECTORY");
2061 if (sim_resources_path
== NULL
) return NULL
;
2063 asprintf(&asl_conf
, "%s%s", sim_root_path
, _PATH_ASL_CONF
);
2064 asprintf(&asl_conf_dir
, "%s%s", sim_root_path
, _PATH_ASL_CONF_DIR
);
2065 asprintf(&asl_conf_local_dir
, "%s%s", sim_resources_path
, _PATH_ASL_CONF_DIR
);
2067 _asl_out_module_read_and_merge_dir(&out
, asl_conf_local_dir
, MODULE_FLAG_LOCAL
);
2068 free(asl_conf_local_dir
);
2070 _asl_out_module_read_and_merge_dir(&out
, asl_conf_dir
, 0);
2073 _asl_out_module_read_and_merge_dir(&out
, _PATH_ASL_CONF_LOCAL_DIR
, MODULE_FLAG_LOCAL
);
2074 _asl_out_module_read_and_merge_dir(&out
, _PATH_ASL_CONF_DIR
, 0);
2077 if (_asl_out_module_find(out
, ASL_MODULE_NAME
) == NULL
)
2079 /* system just has old-style /etc/asl.conf */
2080 #if TARGET_OS_SIMULATOR
2081 FILE *f
= fopen(asl_conf
, "r");
2084 FILE *f
= fopen(_PATH_ASL_CONF
, "r");
2088 asl_out_module_t
*x
= asl_out_module_init_from_file(ASL_MODULE_NAME
, f
);
2105 asl_out_module_rule_to_string(asl_out_rule_t
*r
)
2112 asprintf(&out
, "NULL rule");
2116 str
= asl_msg_to_string(r
->query
, &len
);
2118 asprintf(&out
, " %s%s%s%s%s",
2119 asl_out_action_name
[r
->action
],
2120 (r
->query
== NULL
) ? "" : " ",
2121 (r
->query
== NULL
) ? "" : str
,
2122 (r
->options
== NULL
) ? "" : " ",
2123 (r
->options
== NULL
) ? "" : r
->options
);
2130 _stamp_style_name(uint32_t flags
)
2132 if (flags
& MODULE_NAME_STYLE_STAMP_SEC
) return "<seconds>";
2133 if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
) return "<sequence>";
2134 if (flags
& MODULE_NAME_STYLE_STAMP_UTC
) return "<utc>";
2135 if (flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) return "<utc-basic>";
2136 if (flags
& MODULE_NAME_STYLE_STAMP_LCL
) return "<local>";
2137 if (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
) return "<local-basic>";
2145 asl_out_module_print(FILE *f
, asl_out_module_t
*m
)
2147 asl_out_rule_t
*r
, *n
;
2148 asl_out_dst_data_t
*o
;
2149 uint32_t i
, ttlnset
;
2153 for (r
= m
->ruleset
; r
!= NULL
; r
= n
)
2156 char *str
= asl_msg_to_string(r
->query
, &len
);
2158 fprintf(f
, " %s", asl_out_action_name
[r
->action
]);
2159 if (r
->query
!= NULL
) fprintf(f
, " %s", str
);
2160 if (r
->options
!= NULL
) fprintf(f
, " %s", r
->options
);
2161 if (r
->action
== ACTION_OUT_DEST
)
2166 fprintf(f
, " data: NULL");
2170 fprintf(f
, "%s\n", o
->path
);
2171 fprintf(f
, " rules: %u\n", o
->refcount
- 1);
2172 fprintf(f
, " dest: %s\n", (o
->rotate_dir
== NULL
) ? "(none)" : o
->rotate_dir
);
2173 fprintf(f
, " format: %s\n", (o
->fmt
== NULL
) ? "std" : o
->fmt
);
2174 fprintf(f
, " time_format: %s\n", (o
->tfmt
== NULL
) ? "lcl" : o
->tfmt
);
2175 fprintf(f
, " flags: 0x%08x", o
->flags
);
2180 if (o
->flags
& MODULE_FLAG_ENABLED
)
2182 fprintf(f
, "%cenabled", c
);
2185 if (o
->flags
& MODULE_FLAG_SOFT_WRITE
)
2187 fprintf(f
, "%csoft", c
);
2190 if (o
->flags
& MODULE_FLAG_ROTATE
)
2192 fprintf(f
, "%crotate", c
);
2195 if (o
->flags
& MODULE_FLAG_COALESCE
)
2197 fprintf(f
, "%ccoalesce", c
);
2200 if (o
->flags
& MODULE_FLAG_COMPRESS
)
2202 fprintf(f
, "%ccompress", c
);
2205 if (o
->flags
& MODULE_FLAG_BASESTAMP
)
2207 fprintf(f
, "%cbasestamp", c
);
2210 if (o
->flags
& MODULE_FLAG_SYMLINK
)
2212 fprintf(f
, "%csymlink", c
);
2215 if (o
->flags
& MODULE_FLAG_NONSTD_DIR
)
2217 fprintf(f
, "%cnon-std_dir", c
);
2220 if (o
->flags
& MODULE_FLAG_EXTERNAL
)
2222 fprintf(f
, "%cexternal", c
);
2225 if (o
->flags
& MODULE_FLAG_CRASHLOG
)
2227 fprintf(f
, "%ccrashlog", c
);
2230 if (o
->flags
& MODULE_FLAG_TYPE_ASL
)
2232 fprintf(f
, "%casl_file", c
);
2235 if (o
->flags
& MODULE_FLAG_TYPE_ASL_DIR
)
2237 fprintf(f
, "%casl_directory", c
);
2240 if (o
->flags
& MODULE_FLAG_SIZE_ONLY
)
2242 fprintf(f
, "%csize_only", c
);
2249 if (o
->flags
& MODULE_FLAG_ROTATE
)
2251 fprintf(f
, " rotatation style: ");
2252 if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BS
)
2254 fprintf(f
, "[base=%s].%s\n", o
->base
, _stamp_style_name(o
->style_flags
));
2256 else if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BES
)
2258 fprintf(f
, "[base=%s].[ext=%s].%s\n", o
->base
, o
->ext
, _stamp_style_name(o
->style_flags
));
2260 else if (o
->style_flags
& MODULE_NAME_STYLE_FORMAT_BSE
)
2262 fprintf(f
, "[base=%s].%s.[ext=%s]\n", o
->base
, _stamp_style_name(o
->style_flags
), o
->ext
);
2266 fprintf(f
, "0x%08x\n", o
->style_flags
);
2270 asl_core_time_to_str(o
->ttl
[LEVEL_ALL
], tstr
, sizeof(tstr
));
2271 fprintf(f
, " ttl: %s\n", tstr
);
2274 for (i
= 0; (i
<= 7) & (ttlnset
== 0); i
++) if (o
->ttl
[i
] != 0) ttlnset
= 1;
2277 for (i
= 0; i
<= 7; i
++)
2279 time_t x
= o
->ttl
[i
];
2280 if (x
== 0) x
= o
->ttl
[LEVEL_ALL
];
2281 asl_core_time_to_str(x
, tstr
, sizeof(tstr
));
2283 fprintf(f
, " [%d %s]", i
, tstr
);
2289 fprintf(f
, " mode: 0%o\n", o
->mode
);
2290 fprintf(f
, " file_max: %lu\n", o
->file_max
);
2291 fprintf(f
, " all_max: %lu\n", o
->all_max
);
2292 #if !TARGET_OS_SIMULATOR
2293 fprintf(f
, " uid:");
2294 for (i
= 0; i
< o
->nuid
; i
++) fprintf(f
, " %d", o
->uid
[i
]);
2296 fprintf(f
, " gid:");
2297 for (i
= 0; i
< o
->ngid
; i
++) fprintf(f
, " %d", o
->gid
[i
]);
2310 asl_out_file_list_free(asl_out_file_list_t
*l
)
2312 asl_out_file_list_t
*n
;
2314 if (l
== NULL
) return;
2326 * Checks input name for one of the forms:
2327 * MODULE_NAME_STYLE_FORMAT_BS base (src only) or base.stamp[.gz]
2328 * MODULE_NAME_STYLE_FORMAT_BES base.ext (src only) or base.ext.stamp[.gz]
2329 * MODULE_NAME_STYLE_FORMAT_BSE base.ext (src only) or base.stamp.ext[.gz]
2331 * name == base[.ext] is allowed if src is true.
2332 * base.gz is not allowed.
2333 * Output parameter stamp must be freed by caller.
2336 _check_file_name(const char *name
, const char *base
, const char *ext
, uint32_t flags
, bool src
, char **stamp
)
2338 size_t baselen
, extlen
;
2342 #ifdef DEBUG_LIST_FILES
2343 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");
2346 if (name
== NULL
) return false;
2347 if (base
== NULL
) return false;
2349 baselen
= strlen(base
);
2350 if (baselen
== 0) return false;
2353 if (ext
!= NULL
) extlen
= strlen(ext
);
2355 if (stamp
!= NULL
) *stamp
= NULL
;
2357 if (strneq_len(name
, base
, baselen
))
2359 #ifdef DEBUG_LIST_FILES
2360 fprintf(stderr
, " base match failed [%u]\n", __LINE__
);
2365 z
= strrchr(name
, '.');
2366 if ((z
!= NULL
) && streq(z
, ".gz")) isgz
= true;
2368 #ifdef DEBUG_LIST_FILES
2369 fprintf(stderr
, "z=%s isgz=%s\n", (z
== NULL
) ? "NULL" : z
, isgz
? "true" : "false");
2374 if (flags
& MODULE_NAME_STYLE_FORMAT_BS
)
2376 #ifdef DEBUG_LIST_FILES
2377 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BS\n");
2381 /* name == base OK if src is true */
2382 #ifdef DEBUG_LIST_FILES
2383 fprintf(stderr
, " name == base %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2388 /* expecting p == .stamp[.gz] */
2391 #ifdef DEBUG_LIST_FILES
2392 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2401 /* base.gz is not allowed */
2402 #ifdef DEBUG_LIST_FILES
2403 fprintf(stderr
, " got base.gz - failed [%u]\n", __LINE__
);
2408 /* got base.stamp[.gz] */
2412 char *x
= strchr(*stamp
, '.');
2413 if (x
!= NULL
) *x
= '\0';
2416 #ifdef DEBUG_LIST_FILES
2417 fprintf(stderr
, " got base.stamp%s - OK\n", isgz
? ".gz" : "");
2421 else if (flags
& MODULE_NAME_STYLE_FORMAT_BES
)
2423 #ifdef DEBUG_LIST_FILES
2424 fprintf(stderr
, " MODULE_NAME_STYLE_FORMAT_BES\n");
2428 #ifdef DEBUG_LIST_FILES
2429 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2436 if (strneq_len(p
, ext
, extlen
))
2438 #ifdef DEBUG_LIST_FILES
2439 fprintf(stderr
, " ext match failed [%u]\n", __LINE__
);
2444 /* expecting p == .ext[.stamp][.gz] */
2449 /* name == base.ext OK if src is true */
2450 #ifdef DEBUG_LIST_FILES
2451 fprintf(stderr
, " name == base.ext %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2456 /* expecting p == .stamp[.gz] */
2459 #ifdef DEBUG_LIST_FILES
2460 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2469 /* base.ext.gz is not allowed */
2470 #ifdef DEBUG_LIST_FILES
2471 fprintf(stderr
, " got base.ext.gz - failed [%u]\n", __LINE__
);
2476 /* got base.ext.stamp[.gz] */
2480 char *x
= strchr(*stamp
, '.');
2481 if (x
!= NULL
) *x
= '\0';
2484 #ifdef DEBUG_LIST_FILES
2485 fprintf(stderr
, " got base.ext.stamp%s - OK\n", isgz
? ".gz" : "");
2489 else if (flags
& MODULE_NAME_STYLE_FORMAT_BSE
)
2491 #ifdef DEBUG_LIST_FILES
2492 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");
2497 #ifdef DEBUG_LIST_FILES
2498 fprintf(stderr
, " expecting dot - failed [%u]\n", __LINE__
);
2505 if (streq_len(p
, ext
, extlen
))
2510 /* name == base.ext OK if src is true */
2511 #ifdef DEBUG_LIST_FILES
2512 fprintf(stderr
, " name == base.ext %s [%u]\n", src
? "OK" : "failed", __LINE__
);
2518 /* expecting p == stamp.ext[.gz] */
2522 if (strneq_len(z
- extlen
, ext
, extlen
))
2524 #ifdef DEBUG_LIST_FILES
2525 fprintf(stderr
, " ext match (%s) isgz failed [%u]\n", z
-extlen
, __LINE__
);
2532 if (strneq_len(z
+ 1, ext
, extlen
))
2534 #ifdef DEBUG_LIST_FILES
2535 fprintf(stderr
, " ext match (%s) failed [%u]\n", z
, __LINE__
);
2541 /* got base.stamp.ext[.gz] */
2545 char *x
= strchr(*stamp
, '.');
2546 if (x
!= NULL
) *x
= '\0';
2549 #ifdef DEBUG_LIST_FILES
2550 fprintf(stderr
, " got base.stamp.ext%s - OK\n", isgz
? ".gz" : "");
2555 #ifdef DEBUG_LIST_FILES
2556 fprintf(stderr
, " unknown format - failed\n");
2563 * Find files in a directory (dir) that all have a common prefix (base).
2564 * Bits in flags further control the search.
2566 * MODULE_NAME_STYLE_STAMP_SEQ means a numeric sequence number is expected, although not required.
2567 * E.g. foo.log foo.log.0
2569 * MODULE_NAME_STYLE_STAMP_SEC also means a numeric sequence number is required following an 'T' character.
2570 * The numeric value is the file's timestamp in seconds. E.g foo.log.T1335200452
2572 * MODULE_NAME_STYLE_STAMP_UTC requires a date/time component as the file's timestamp.
2573 * E.g. foo.2012-04-06T15:30:00Z
2575 * MODULE_NAME_STYLE_STAMP_UTC_B requires a date/time component as the file's timestamp.
2576 * E.g. foo.20120406T153000Z
2578 * MODULE_NAME_STYLE_STAMP_LCL requires a date/time component as the file's timestamp.
2579 * E.g. foo.2012-04-06T15:30:00-7
2581 * MODULE_NAME_STYLE_STAMP_LCL_B requires a date/time component as the file's timestamp.
2582 * E.g. foo.20120406T153000-07
2585 _parse_stamp_style(char *stamp
, uint32_t flags
, uint32_t *sp
, time_t *tp
)
2592 long utc_offset
= 0;
2595 /* check for NULL (no stamp) */
2596 if (stamp
== NULL
) return STAMP_STYLE_NULL
;
2598 /* check for MODULE_NAME_STYLE_STAMP_SEC (foo.T12345678) */
2599 if (stamp
[0] == 'T')
2601 n
= atoi(stamp
+ 1);
2602 if ((n
== 0) && strcmp(stamp
+ 1, "0")) return STAMP_STYLE_INVALID
;
2603 if (tp
!= NULL
) *tp
= (time_t)n
;
2605 return STAMP_STYLE_SEC
;
2608 /* check for MODULE_NAME_STYLE_STAMP_SEQ (foo.0 or foo.2.gz) */
2610 for (i
= 0; digits
&& (stamp
[i
] != '\0'); i
++) digits
= (stamp
[i
] >= '0') && (stamp
[i
] <= '9');
2612 if (!digits
&& (streq(stamp
+ i
, ".gz"))) digits
= true;
2617 if (sp
!= NULL
) *sp
= (uint32_t)n
;
2618 return STAMP_STYLE_SEQ
;
2621 /* check for MODULE_NAME_STYLE_STAMP_UTC, UTC_B, LCL, or LCL_B */
2622 memset(&t
, 0, sizeof(t
));
2626 if ((flags
& MODULE_NAME_STYLE_STAMP_UTC
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL
))
2628 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
);
2630 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
))
2632 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
);
2636 #ifdef DEBUG_LIST_FILES
2637 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2639 return STAMP_STYLE_INVALID
;
2644 #ifdef DEBUG_LIST_FILES
2645 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2647 return STAMP_STYLE_INVALID
;
2654 else if ((zone
== '-') || (zone
== '+'))
2656 if (n
>= 8) utc_offset
+= (3600 * h
);
2657 if (n
>= 9) utc_offset
+= (60 * m
);
2658 if (n
== 10) utc_offset
+= s
;
2659 if (zone
== '+') utc_offset
*= -1;
2661 else if ((zone
>= 'A') && (zone
<= 'Z'))
2663 if (zone
< 'J') utc_offset
= 3600 * ((zone
- 'A') + 1);
2664 else if ((zone
>= 'K') && (zone
<= 'M')) utc_offset
= 3600 * (zone
- 'A');
2665 else if (zone
<= 'Y') utc_offset
= -3600 * ((zone
- 'N') + 1);
2667 else if ((zone
>= 'a') && (zone
<= 'z'))
2669 if (zone
< 'j') utc_offset
= 3600 * ((zone
- 'a') + 1);
2670 else if ((zone
>= 'k') && (zone
<= 'm')) utc_offset
= 3600 * (zone
- 'a');
2671 else if (zone
<= 'y') utc_offset
= -3600 * ((zone
- 'n') + 1);
2675 #ifdef DEBUG_LIST_FILES
2676 fprintf(stderr
, "_parse_stamp_style fail %u\n", __LINE__
);
2678 return STAMP_STYLE_INVALID
;
2683 t
.tm_sec
+= utc_offset
;
2686 if ((zone
== 'J') || (zone
== 'j')) ftime
= mktime(&t
);
2687 else ftime
= timegm(&t
);
2689 if (tp
!= NULL
) *tp
= ftime
;
2691 return STAMP_STYLE_ISO8601
;
2694 asl_out_file_list_t
*
2695 asl_list_log_files(const char *dir
, const char *base
, const char *ext
, uint32_t flags
, bool src
)
2699 char path
[MAXPATHLEN
];
2704 asl_out_file_list_t
*out
, *x
, *y
;
2706 #ifdef DEBUG_LIST_FILES
2707 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");
2710 if (dir
== NULL
) return NULL
;
2711 if (base
== NULL
) return NULL
;
2716 if (d
== NULL
) return NULL
;
2718 while (NULL
!= (ent
= readdir(d
)))
2722 bool stat_ok
= false;
2724 check
= _check_file_name(ent
->d_name
, base
, ext
, flags
, src
, &stamp
);
2725 if (!check
) continue;
2730 pstyle
= _parse_stamp_style(stamp
, flags
, &seq
, &ftime
);
2733 if (pstyle
== STAMP_STYLE_INVALID
) continue;
2735 snprintf(path
, sizeof(path
), "%s/%s", dir
, ent
->d_name
);
2736 memset(&sb
, 0, sizeof(sb
));
2737 if (lstat(path
, &sb
) == 0)
2739 /* ignore symlinks (created for basestamp / symlink files) */
2741 if ((sb
.st_mode
& S_IFMT
) == S_IFLNK
) continue;
2744 fstyle
= STAMP_STYLE_NULL
;
2745 if (flags
& MODULE_NAME_STYLE_STAMP_SEC
) fstyle
= STAMP_STYLE_SEC
;
2746 else if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
) fstyle
= STAMP_STYLE_SEQ
;
2747 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL
)) fstyle
= STAMP_STYLE_ISO8601
;
2748 else if ((flags
& MODULE_NAME_STYLE_STAMP_UTC_B
) || (flags
& MODULE_NAME_STYLE_STAMP_LCL_B
)) fstyle
= STAMP_STYLE_ISO8601
;
2750 #ifdef DEBUG_LIST_FILES
2751 fprintf(stderr
, "%s %s %u fstyle %u pstyle %u\n", __func__
, path
, __LINE__
, fstyle
, pstyle
);
2755 * accept the file if:
2756 * style is STAMP_STYLE_NULL (no timestamp)
2757 * src is true and style is STAMP_STYLE_SEC
2758 * actual style matches the style implied by the input flags
2762 if (pstyle
== STAMP_STYLE_NULL
) check
= true;
2763 if ((pstyle
== STAMP_STYLE_SEC
) && src
) check
= true;
2764 if (pstyle
== fstyle
) check
= true;
2768 #ifdef DEBUG_LIST_FILES
2769 fprintf(stderr
, "%s reject %s at line %u\n", __func__
, path
, __LINE__
);
2774 x
= (asl_out_file_list_t
*)calloc(1, sizeof(asl_out_file_list_t
));
2777 asl_out_file_list_free(out
);
2781 x
->name
= strdup(ent
->d_name
);
2788 x
->size
= sb
.st_size
;
2789 if (pstyle
== STAMP_STYLE_SEQ
)
2791 x
->ftime
= sb
.st_birthtimespec
.tv_sec
;
2792 if (x
->ftime
== 0) x
->ftime
= sb
.st_mtimespec
.tv_sec
;
2796 if (pstyle
== STAMP_STYLE_SEQ
)
2798 #ifdef DEBUG_LIST_FILES
2799 fprintf(stderr
, "asl_list_log_files SEQ %s %u %ld\n", path
, x
->seq
, x
->ftime
);
2805 else if ((x
->seq
== IndexNull
) || ((x
->seq
> out
->seq
) && (out
->seq
!= IndexNull
)))
2813 for (y
= out
; y
!= NULL
; y
= y
->next
)
2815 if (y
->next
== NULL
)
2821 else if ((x
->seq
> y
->next
->seq
) && (y
->next
->seq
!= IndexNull
))
2834 #ifdef DEBUG_LIST_FILES
2835 fprintf(stderr
, "asl_list_log_files TIME %s %ld\n", path
, x
->ftime
);
2841 else if (x
->ftime
< out
->ftime
)
2849 for (y
= out
; y
!= NULL
; y
= y
->next
)
2851 if (y
->next
== NULL
)
2857 else if (x
->ftime
< y
->next
->ftime
)
2875 * List the source files for an output asl_out_dst_data_t
2877 asl_out_file_list_t
*
2878 asl_list_src_files(asl_out_dst_data_t
*dst
)
2881 #ifdef DEBUG_LIST_FILES
2882 fprintf(stderr
, "asl_list_src_files\n");
2885 if (dst
== NULL
) return NULL
;
2886 if (dst
->path
== NULL
) return NULL
;
2887 if (dst
->base
== NULL
) return NULL
;
2890 * MODULE_FLAG_EXTERNAL means some process other than syslogd writes the file.
2891 * We check for its existence, and that it is non-zero in size.
2893 if (dst
->flags
& MODULE_FLAG_EXTERNAL
)
2897 memset(&sb
, 0, sizeof(struct stat
));
2899 if (stat(dst
->path
, &sb
) == 0)
2901 if (S_ISREG(sb
.st_mode
) && (sb
.st_size
!= 0))
2903 asl_out_file_list_t
*out
= (asl_out_file_list_t
*)calloc(1, sizeof(asl_out_file_list_t
));
2906 char *p
= strrchr(dst
->path
, '/');
2907 if (p
== NULL
) p
= dst
->path
;
2909 out
->name
= strdup(p
);
2910 out
->ftime
= sb
.st_birthtimespec
.tv_sec
;
2911 if (out
->ftime
== 0) out
->ftime
= sb
.st_mtimespec
.tv_sec
;
2920 if ((dst
->rotate_dir
== NULL
) && (dst
->flags
& MODULE_FLAG_BASESTAMP
) && ((dst
->flags
& MODULE_FLAG_COMPRESS
) == 0))
2922 /* files do not move to a dest dir, get renamed, or get compressed - nothing to do */
2927 * MODULE_NAME_STYLE_STAMP_SEC format is used for sequenced (MODULE_NAME_STYLE_STAMP_SEQ) files.
2928 * aslmanager converts the file names.
2930 flags
= dst
->style_flags
;
2931 if (flags
& MODULE_NAME_STYLE_STAMP_SEQ
)
2933 flags
&= ~MODULE_NAME_STYLE_STAMP_SEQ
;
2934 flags
|= MODULE_NAME_STYLE_STAMP_SEC
;
2937 return asl_list_log_files(dst
->dir
, dst
->base
, dst
->ext
, flags
, true);
2941 * List the destination files for an output asl_out_dst_data_t
2943 asl_out_file_list_t
*
2944 asl_list_dst_files(asl_out_dst_data_t
*dst
)
2947 #ifdef DEBUG_LIST_FILES
2948 fprintf(stderr
, "asl_list_dst_files\n");
2951 if (dst
== NULL
) return NULL
;
2952 if (dst
->path
== NULL
) return NULL
;
2953 if (dst
->base
== NULL
) return NULL
;
2955 dst_dir
= dst
->rotate_dir
;
2956 if (dst_dir
== NULL
) dst_dir
= dst
->dir
;
2958 return asl_list_log_files(dst_dir
, dst
->base
, dst
->ext
, dst
->style_flags
, false);
2962 asl_secure_open_dir(const char *path
)
2967 if (path
== NULL
) return -1;
2968 if (path
[0] != '/') return -1;
2970 path_parts
= explode(path
+ 1, "/");
2971 if (path_parts
== NULL
) return 0;
2973 fd
= open("/", O_RDONLY
| O_NOFOLLOW
, 0);
2976 free_string_list(path_parts
);
2980 for (i
= 0; path_parts
[i
] != NULL
; i
++)
2982 int fd_next
, status
;
2985 fd_next
= openat(fd
, path_parts
[i
], O_RDONLY
| O_NOFOLLOW
, 0);
2990 free_string_list(path_parts
);
2994 memset(&sb
, 0, sizeof(sb
));
2996 status
= fstat(fd
, &sb
);
2999 free_string_list(path_parts
);
3003 if (!S_ISDIR(sb
.st_mode
))
3005 free_string_list(path_parts
);
3010 free_string_list(path_parts
);
3015 asl_secure_chown_chmod_dir(const char *path
, uid_t uid
, gid_t gid
, mode_t mode
)
3019 fd
= asl_secure_open_dir(path
);
3020 if (fd
< 0) return fd
;
3022 status
= fchown(fd
, uid
, gid
);
3029 if (mode
>= 0) status
= fchmod(fd
, mode
);
3032 if (status
< 0) return -1;