2 * Copyright (c) 2007-2011 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@
29 #include <sys/types.h>
33 #include <asl_private.h>
35 #include <asl_store.h>
38 extern uint64_t asl_file_cursor(asl_file_t
*s
);
39 extern uint32_t asl_file_match_start(asl_file_t
*s
, uint64_t start_id
, int32_t direction
);
40 extern uint32_t asl_file_match_next(asl_file_t
*s
, asl_msg_list_t
*qlist
, asl_msg_t
**msg
, uint64_t *last_id
, int32_t direction
, int32_t ruid
, int32_t rgid
);
41 extern int asl_file_create(const char *path
, uid_t uid
, gid_t gid
, mode_t mode
);
43 #define SECONDS_PER_DAY 86400
46 * The ASL Store is organized as a set of files in a common directory.
47 * Files are prefixed by the date (YYYY.MM.DD) of their contents.
49 * Messages with no access controls are saved in YYYY.MM.DD.asl
50 * Messages with access limited to UID uuu are saved in YYYY.MM.DD.Uuuu.asl
51 * Messages with access limited to GID ggg are saved in YYYY.MM.DD.Gggg.asl
52 * Messages with access limited to UID uuu and GID ggg are saved in YYYY.MM.DD.Uuuu.Gggg.asl
54 * Messages that have a value for ASLExpireTime are saved in BB.YYYY.MM.DD.asl
55 * where the timestamp is the "Best Before" date of the file. Access controls
56 * are implemented as above with Uuuu and Gggg in the file name. Note that the
57 * Best Before files are for the last day of the month, so a single file contains
58 * messages that expire in that month.
60 * An external tool runs daily and deletes "old" files.
69 memset(&ctm
, 0, sizeof(struct tm
));
72 if (localtime_r((const time_t *)&now
, &ctm
) == NULL
) return 0;
82 * The base directory contains a data file which stores
85 * | MAX_ID (uint64_t) |
89 asl_store_open_write(const char *basedir
, asl_store_t
**s
)
94 char path
[MAXPATHLEN
];
99 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
101 start
= _asl_start_today();
102 if (start
== 0) return ASL_STATUS_FAILED
;
104 if (basedir
== NULL
) basedir
= PATH_ASL_STORE
;
106 memset(&sb
, 0, sizeof(struct stat
));
107 if (stat(basedir
, &sb
) != 0)
109 if (errno
!= ENOENT
) return ASL_STATUS_INVALID_STORE
;
110 if (mkdir(basedir
, 0755) != 0) return ASL_STATUS_WRITE_FAILED
;
114 if (!S_ISDIR(sb
.st_mode
)) return ASL_STATUS_INVALID_STORE
;
117 snprintf(path
, sizeof(path
), "%s/%s", basedir
, FILE_ASL_STORE_DATA
);
121 memset(&sb
, 0, sizeof(struct stat
));
122 if (stat(path
, &sb
) != 0)
124 if (errno
!= ENOENT
) return ASL_STATUS_FAILED
;
126 sd
= fopen(path
, "w+");
128 if (sd
== NULL
) return ASL_STATUS_FAILED
;
132 /* Create new StoreData file (8 bytes ID + 4 bytes flags) */
134 if (fwrite(&last_id
, sizeof(uint64_t), 1, sd
) != 1)
137 return ASL_STATUS_WRITE_FAILED
;
141 if (fwrite(&flags
, sizeof(uint32_t), 1, sd
) != 1)
144 return ASL_STATUS_WRITE_FAILED
;
152 sd
= fopen(path
, "r+");
154 if (sd
== NULL
) return ASL_STATUS_FAILED
;
155 if (fread(&last_id
, sizeof(uint64_t), 1, sd
) != 1)
158 return ASL_STATUS_READ_FAILED
;
161 last_id
= asl_core_ntohq(last_id
);
164 out
= (asl_store_t
*)calloc(1, sizeof(asl_store_t
));
168 return ASL_STATUS_NO_MEMORY
;
171 out
->asl_type
= ASL_TYPE_STORE
;
174 if (basedir
== NULL
) out
->base_dir
= strdup(PATH_ASL_STORE
);
175 else out
->base_dir
= strdup(basedir
);
177 if (out
->base_dir
== NULL
)
181 return ASL_STATUS_NO_MEMORY
;
184 out
->start_today
= start
;
185 out
->start_tomorrow
= out
->start_today
+ SECONDS_PER_DAY
;
187 out
->next_id
= last_id
+ 1;
189 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
191 memset(&out
->file_cache
[i
], 0, sizeof(asl_cached_file_t
));
192 out
->file_cache
[i
].u
= -1;
193 out
->file_cache
[i
].g
= -1;
197 return ASL_STATUS_OK
;
201 asl_store_set_flags(asl_store_t
*s
, uint32_t flags
)
203 if (s
== NULL
) return 0;
204 uint32_t oldflags
= s
->flags
;
210 asl_store_statistics(asl_store_t
*s
, asl_msg_t
**msg
)
214 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
215 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
217 out
= asl_msg_new(ASL_TYPE_MSG
);
218 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
220 /* does nothing for now */
223 return ASL_STATUS_OK
;
227 asl_store_open_read(const char *basedir
, asl_store_t
**s
)
232 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
234 if (basedir
== NULL
) basedir
= PATH_ASL_STORE
;
236 memset(&sb
, 0, sizeof(struct stat
));
237 if (stat(basedir
, &sb
) != 0) return ASL_STATUS_INVALID_STORE
;
238 if (!S_ISDIR(sb
.st_mode
)) return ASL_STATUS_INVALID_STORE
;
240 out
= (asl_store_t
*)calloc(1, sizeof(asl_store_t
));
241 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
243 out
->asl_type
= ASL_TYPE_STORE
;
246 if (basedir
== NULL
) out
->base_dir
= strdup(PATH_ASL_STORE
);
247 else out
->base_dir
= strdup(basedir
);
249 if (out
->base_dir
== NULL
)
252 return ASL_STATUS_NO_MEMORY
;
256 return ASL_STATUS_OK
;
260 asl_store_max_file_size(asl_store_t
*s
, size_t max
)
262 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
264 s
->max_file_size
= max
;
265 return ASL_STATUS_OK
;
268 __private_extern__
void
269 asl_store_file_closeall(asl_store_t
*s
)
273 if (s
== NULL
) return;
275 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
277 if (s
->file_cache
[i
].f
!= NULL
) asl_file_close(s
->file_cache
[i
].f
);
278 s
->file_cache
[i
].f
= NULL
;
279 if (s
->file_cache
[i
].path
!= NULL
) free(s
->file_cache
[i
].path
);
280 s
->file_cache
[i
].path
= NULL
;
281 s
->file_cache
[i
].u
= -1;
282 s
->file_cache
[i
].g
= -1;
283 s
->file_cache
[i
].bb
= 0;
284 s
->file_cache
[i
].ts
= 0;
289 asl_store_retain(asl_store_t
*s
)
291 if (s
== NULL
) return NULL
;
292 asl_retain((asl_object_t
)s
);
297 asl_store_release(asl_store_t
*s
)
299 if (s
== NULL
) return;
300 asl_release((asl_object_t
)s
);
304 asl_store_close(asl_store_t
*s
)
306 if (s
== NULL
) return ASL_STATUS_OK
;
307 asl_release((asl_object_t
)s
);
308 return ASL_STATUS_OK
;
312 _asl_store_free_internal(asl_store_t
*s
)
314 if (s
== NULL
) return;
316 if (s
->base_dir
!= NULL
) free(s
->base_dir
);
318 asl_store_file_closeall(s
);
319 if (s
->storedata
!= NULL
) fclose(s
->storedata
);
325 * Sweep the file cache.
326 * Close any files that have not been used in the last FILE_CACHE_TTL seconds.
327 * Returns least recently used or unused cache slot.
330 asl_store_file_cache_lru(asl_store_t
*s
, time_t now
, uint32_t ignorex
)
335 if (s
== NULL
) return 0;
338 min
= now
- FILE_CACHE_TTL
;
340 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
342 if ((i
!= ignorex
) && (s
->file_cache
[i
].ts
< min
))
344 asl_file_close(s
->file_cache
[i
].f
);
345 s
->file_cache
[i
].f
= NULL
;
346 if (s
->file_cache
[i
].path
!= NULL
) free(s
->file_cache
[i
].path
);
347 s
->file_cache
[i
].path
= NULL
;
348 s
->file_cache
[i
].u
= -1;
349 s
->file_cache
[i
].g
= -1;
350 s
->file_cache
[i
].bb
= 0;
351 s
->file_cache
[i
].ts
= 0;
354 if (s
->file_cache
[i
].ts
< s
->file_cache
[x
].ts
) x
= i
;
361 asl_store_sweep_file_cache(asl_store_t
*s
)
363 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
365 asl_store_file_cache_lru(s
, time(NULL
), FILE_CACHE_SIZE
);
366 return ASL_STATUS_OK
;
370 asl_store_make_ug_path(const char *dir
, const char *base
, const char *ext
, uid_t ruid
, gid_t rgid
, uid_t
*u
, gid_t
*g
, mode_t
*m
)
382 if (ext
== NULL
) asprintf(&path
, "%s/%s", dir
, base
);
383 else asprintf(&path
, "%s/%s.%s", dir
, base
, ext
);
389 if (ext
== NULL
) asprintf(&path
, "%s/%s.G%d", dir
, base
, *g
);
390 else asprintf(&path
, "%s/%s.G%d.%s", dir
, base
, *g
, ext
);
399 if (ext
== NULL
) asprintf(&path
, "%s/%s.U%d", dir
, base
, *u
);
400 else asprintf(&path
, "%s/%s.U%d.%s", dir
, base
, *u
, ext
);
406 if (ext
== NULL
) asprintf(&path
, "%s/%s.U%d.G%d", dir
, base
, *u
, *g
);
407 else asprintf(&path
, "%s/%s.U%d.G%u.%s", dir
, base
, *u
, *g
, ext
);
415 asl_store_file_open_write(asl_store_t
*s
, char *tstring
, int32_t ruid
, int32_t rgid
, time_t bb
, asl_file_t
**f
, time_t now
, uint32_t check_cache
)
425 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
427 /* see if the file is already open and in the cache */
428 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
430 if ((s
->file_cache
[i
].u
== ruid
) && (s
->file_cache
[i
].g
== rgid
) && (s
->file_cache
[i
].bb
== bb
) && (s
->file_cache
[i
].f
!= NULL
))
432 s
->file_cache
[i
].ts
= now
;
433 *f
= s
->file_cache
[i
].f
;
434 if (check_cache
== 1) asl_store_file_cache_lru(s
, now
, i
);
435 return ASL_STATUS_OK
;
442 path
= asl_store_make_ug_path(s
->base_dir
, tstring
, "asl", (uid_t
)ruid
, (gid_t
)rgid
, &u
, &g
, &m
);
443 if (path
== NULL
) return ASL_STATUS_NO_MEMORY
;
446 status
= asl_file_open_write(path
, m
, u
, g
, &out
);
447 if (status
!= ASL_STATUS_OK
)
453 x
= asl_store_file_cache_lru(s
, now
, FILE_CACHE_SIZE
);
454 if (s
->file_cache
[x
].f
!= NULL
) asl_file_close(s
->file_cache
[x
].f
);
455 if (s
->file_cache
[x
].path
!= NULL
) free(s
->file_cache
[x
].path
);
457 s
->file_cache
[x
].f
= out
;
458 s
->file_cache
[x
].path
= path
;
459 s
->file_cache
[x
].u
= ruid
;
460 s
->file_cache
[x
].g
= rgid
;
461 s
->file_cache
[x
].bb
= bb
;
462 s
->file_cache
[x
].ts
= time(NULL
);
466 return ASL_STATUS_OK
;
469 __private_extern__
char *
470 asl_store_file_path(asl_store_t
*s
, asl_file_t
*f
)
474 if (s
== NULL
) return NULL
;
476 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
478 if (s
->file_cache
[i
].f
== f
)
480 if (s
->file_cache
[i
].path
== NULL
) return NULL
;
481 return strdup(s
->file_cache
[i
].path
);
488 __private_extern__
void
489 asl_store_file_close(asl_store_t
*s
, asl_file_t
*f
)
493 if (s
== NULL
) return;
494 if (f
== NULL
) return;
496 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
498 if (s
->file_cache
[i
].f
== f
)
500 asl_file_close(s
->file_cache
[i
].f
);
501 s
->file_cache
[i
].f
= NULL
;
502 if (s
->file_cache
[i
].path
!= NULL
) free(s
->file_cache
[i
].path
);
503 s
->file_cache
[i
].path
= NULL
;
504 s
->file_cache
[i
].u
= -1;
505 s
->file_cache
[i
].g
= -1;
506 s
->file_cache
[i
].bb
= 0;
507 s
->file_cache
[i
].ts
= 0;
514 asl_store_save(asl_store_t
*s
, asl_msg_t
*msg
)
517 time_t msg_time
, now
, bb
;
519 char tstring
[128], tmp_path
[MAXPATHLEN
];
524 uint32_t status
, check_cache
, trigger_aslmanager
, len
;
528 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
529 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
534 if ((s
->last_write
+ FILE_CACHE_TTL
) <= now
) check_cache
= 1;
536 trigger_aslmanager
= 0;
541 if (asl_msg_lookup(msg
, ASL_KEY_TIME
, &val
, NULL
) != 0) msg_time
= now
;
542 else if (val
== NULL
) msg_time
= now
;
543 else msg_time
= asl_core_parse_time(val
, NULL
);
545 if (msg_time
>= s
->start_tomorrow
)
547 if (now
>= s
->start_tomorrow
)
551 asl_store_file_closeall(s
);
554 * _asl_start_today should never fail, but if it does,
555 * just push forward one day. That will probably be correct, and if
556 * it isn't, the next message that gets saved will push it ahead again
557 * until we get to the right date.
559 s
->start_today
= _asl_start_today();
560 if (s
->start_today
== 0) s
->start_today
= s
->start_tomorrow
;
562 s
->start_tomorrow
= s
->start_today
+ SECONDS_PER_DAY
;
568 if ((s
->flags
& ASL_STORE_FLAG_NO_ACLS
) == 0)
571 if ((asl_msg_lookup(msg
, ASL_KEY_READ_UID
, &val
, NULL
) == 0) && (val
!= NULL
)) ruid
= atoi(val
);
574 if ((asl_msg_lookup(msg
, ASL_KEY_READ_GID
, &val
, NULL
) == 0) && (val
!= NULL
)) rgid
= atoi(val
);
578 if ((s
->flags
& ASL_STORE_FLAG_NO_TTL
) == 0)
581 if ((asl_msg_lookup(msg
, ASL_KEY_EXPIRE_TIME
, &val
, NULL
) == 0) && (val
!= NULL
))
584 msg_time
= asl_core_parse_time(val
, NULL
);
588 if (fseeko(s
->storedata
, 0, SEEK_SET
) != 0) return ASL_STATUS_WRITE_FAILED
;
590 xid
= asl_core_htonq(s
->next_id
);
591 if (fwrite(&xid
, sizeof(uint64_t), 1, s
->storedata
) != 1) return ASL_STATUS_WRITE_FAILED
;
594 fflush(s
->storedata
);
601 if (localtime_r((const time_t *)&msg_time
, &ctm
) == NULL
) return ASL_STATUS_FAILED
;
606 * This supports 12 monthly "Best Before" buckets.
607 * We advance the actual expiry time to day zero of the following month.
608 * mktime() is clever enough to know that you actually mean the last day
609 * of the previous month. What we get back from localtime is the last
610 * day of the month in which the message expires, which we use in the name.
620 if (localtime_r((const time_t *)&bb
, &ctm
) == NULL
) return ASL_STATUS_FAILED
;
621 snprintf(tstring
, sizeof(tstring
), "BB.%d.%02d.%02d", ctm
.tm_year
+ 1900, ctm
.tm_mon
+ 1, ctm
.tm_mday
);
625 snprintf(tstring
, sizeof(tstring
), "%d.%02d.%02d", ctm
.tm_year
+ 1900, ctm
.tm_mon
+ 1, ctm
.tm_mday
);
628 status
= asl_store_file_open_write(s
, tstring
, ruid
, rgid
, bb
, &f
, now
, check_cache
);
629 if (status
!= ASL_STATUS_OK
) return status
;
631 status
= asl_file_save(f
, msg
, &xid
);
632 if (status
!= ASL_STATUS_OK
) return status
;
634 fsize
= asl_file_size(f
);
635 ftime
= asl_file_ctime(f
);
637 /* if file is larger than max_file_size, rename it and trigger aslmanager */
638 if ((s
->max_file_size
!= 0) && (fsize
> s
->max_file_size
))
640 trigger_aslmanager
= 1;
641 status
= ASL_STATUS_OK
;
643 path
= asl_store_file_path(s
, f
);
645 asl_store_file_close(s
, f
);
650 if ((len
>= 4) && (!strcmp(path
+ len
- 4, ".asl")))
652 /* rename xxxxxxx.asl to xxxxxxx.timestamp.asl */
653 char scratch
[MAXPATHLEN
];
654 snprintf(scratch
, sizeof(scratch
), "%s", path
);
655 scratch
[len
- 4] = '\0';
656 snprintf(tmp_path
, sizeof(tmp_path
), "%s.%llu.asl", scratch
, ftime
);
660 /* append timestamp */
661 snprintf(tmp_path
, sizeof(tmp_path
), "%s.%llu", path
, ftime
);
664 if (rename(path
, tmp_path
) != 0) status
= ASL_STATUS_FAILED
;
670 if (trigger_aslmanager
!= 0) asl_trigger_aslmanager();
676 asl_store_mkdir(asl_store_t
*s
, const char *dir
, mode_t m
)
678 char tstring
[MAXPATHLEN
];
682 snprintf(tstring
, sizeof(tstring
), "%s/%s", s
->base_dir
, dir
);
684 memset(&sb
, 0, sizeof(struct stat
));
685 status
= stat(tstring
, &sb
);
689 /* must be a directory */
690 if (!S_ISDIR(sb
.st_mode
)) return ASL_STATUS_INVALID_STORE
;
696 /* doesn't exist - create it */
697 if (mkdir(tstring
, m
) != 0) return ASL_STATUS_WRITE_FAILED
;
701 /* stat failed for some other reason */
702 return ASL_STATUS_FAILED
;
706 return ASL_STATUS_OK
;
710 asl_store_open_aux(asl_store_t
*s
, asl_msg_t
*msg
, int *out_fd
, char **url
)
715 char tstring
[128], dir
[128];
724 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
725 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
726 if (out_fd
== NULL
) return ASL_STATUS_INVALID_ARG
;
727 if (url
== NULL
) return ASL_STATUS_INVALID_ARG
;
729 msg_time
= time(NULL
);
733 if ((s
->flags
& ASL_STORE_FLAG_NO_ACLS
) == 0)
736 if ((asl_msg_lookup(msg
, ASL_KEY_READ_UID
, &val
, NULL
) == 0) && (val
!= NULL
)) ruid
= atoi(val
);
739 if ((asl_msg_lookup(msg
, ASL_KEY_READ_GID
, &val
, NULL
) == 0) && (val
!= NULL
)) rgid
= atoi(val
);
743 if ((s
->flags
& ASL_STORE_FLAG_NO_TTL
) == 0)
746 if ((asl_msg_lookup(msg
, ASL_KEY_EXPIRE_TIME
, &val
, NULL
) == 0) && (val
!= NULL
))
749 msg_time
= asl_core_parse_time(val
, NULL
);
753 if (localtime_r((const time_t *)&msg_time
, &ctm
) == NULL
) return ASL_STATUS_FAILED
;
758 * This supports 12 monthly "Best Before" buckets.
759 * We advance the actual expiry time to day zero of the following month.
760 * mktime() is clever enough to know that you actually mean the last day
761 * of the previous month. What we get back from localtime is the last
762 * day of the month in which the message expires, which we use in the name.
772 if (localtime_r((const time_t *)&bb
, &ctm
) == NULL
) return ASL_STATUS_FAILED
;
773 snprintf(dir
, sizeof(dir
), "BB.AUX.%d.%02d.%02d", ctm
.tm_year
+ 1900, ctm
.tm_mon
+ 1, ctm
.tm_mday
);
777 snprintf(dir
, sizeof(dir
), "AUX.%d.%02d.%02d", ctm
.tm_year
+ 1900, ctm
.tm_mon
+ 1, ctm
.tm_mday
);
780 status
= asl_store_mkdir(s
, dir
, 0755);
781 if (status
!= ASL_STATUS_OK
) return status
;
786 snprintf(tstring
, sizeof(tstring
), "%s/%llu", dir
, fid
);
791 path
= asl_store_make_ug_path(s
->base_dir
, tstring
, NULL
, ruid
, rgid
, &u
, &g
, &m
);
792 if (path
== NULL
) return ASL_STATUS_NO_MEMORY
;
794 fd
= asl_file_create(path
, u
, g
, m
);
799 return ASL_STATUS_WRITE_FAILED
;
802 /* URL is file://<path> */
804 asprintf(url
, "file://%s", path
);
813 asl_store_match(asl_store_t
*s
, asl_msg_list_t
*qlist
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, uint32_t duration
, int32_t direction
)
819 char path
[MAXPATHLEN
];
820 asl_file_list_t
*files
;
823 if (s
== NULL
) return NULL
;
828 * Open all readable files
830 dp
= opendir(s
->base_dir
);
831 if (dp
== NULL
) return NULL
;
833 while ((dent
= readdir(dp
)) != NULL
)
835 if (dent
->d_name
[0] == '.') continue;
837 snprintf(path
, sizeof(path
), "%s/%s", s
->base_dir
, dent
->d_name
);
839 /* NB asl_file_open_read will fail if path is NULL, if the file is not an ASL store file, or if it isn't readable */
840 status
= asl_file_open_read(path
, &f
);
841 if ((status
!= ASL_STATUS_OK
) || (f
== NULL
)) continue;
843 files
= asl_file_list_add(files
, f
);
848 res
= asl_file_list_match(files
, qlist
, last_id
, start_id
, count
, duration
, direction
);
849 asl_file_list_close(files
);
854 * PRIVATE FOR DEV TOOLS SUPPORT
855 * DO NOT USE THIS INTERFACE OTHERWISE
857 * This is only called by a client that compiled with a 10.9 SDK, but is running
858 * with an new 10.10 libasl.
860 * Only searches the ASL database, so the store (first parameter) is ignored.
862 * The query and result are old-style message lists.
873 asl_store_match_timeout(void *ignored
, void *query_v1
, void **result_v1
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
, uint32_t usec
)
875 asl_store_t
*asldb
= NULL
;
876 asl_msg_list_v1_t
*listv1
;
877 asl_msg_list_t
*qlist
= NULL
;
880 if (result_v1
== NULL
) return ASL_STATUS_FAILED
;
883 status
= asl_store_open_read(NULL
, &asldb
);
884 if (status
!= ASL_STATUS_OK
) return status
;
886 /* convert query_v1 into an asl_msg_list_t */
887 listv1
= (asl_msg_list_v1_t
*)query_v1
;
890 if (listv1
->count
> 0) qlist
= (asl_msg_list_t
*)asl_new(ASL_TYPE_LIST
);
892 for (listv1
->curr
= 0; listv1
->curr
< listv1
->count
; listv1
->curr
++)
894 asl_append((asl_object_t
)qlist
, (asl_object_t
)listv1
->msg
[listv1
->curr
]);
898 asl_msg_list_t
*result
= asl_store_match(asldb
, qlist
, last_id
, start_id
, count
, usec
, direction
);
899 asl_release((asl_object_t
)asldb
);
900 asl_release((asl_object_t
)qlist
);
902 if (result
== NULL
) return ASL_STATUS_OK
;
904 n
= asl_count((asl_object_t
)result
);
907 asl_release((asl_object_t
)result
);
908 return ASL_STATUS_OK
;
911 listv1
= (asl_msg_list_v1_t
*)calloc(1, sizeof(asl_msg_list_v1_t
));
914 asl_release((asl_object_t
)result
);
915 return ASL_STATUS_NO_MEMORY
;
919 listv1
->msg
= (void **)calloc(listv1
->count
, sizeof(void *));
923 asl_release((asl_object_t
)result
);
924 return ASL_STATUS_NO_MEMORY
;
927 for (listv1
->curr
= 0; listv1
->curr
< listv1
->count
; listv1
->curr
++)
929 listv1
->msg
[listv1
->curr
] = asl_retain(asl_next((asl_object_t
)result
));
935 asl_release((asl_object_t
)result
);
936 return ASL_STATUS_OK
;
940 asl_store_match_start(asl_store_t
*s
, uint64_t start_id
, int32_t direction
)
946 char path
[MAXPATHLEN
];
947 asl_file_list_t
*files
;
949 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
951 if (s
->work
!= NULL
) asl_file_list_match_end(s
->work
);
957 * Open all readable files
959 dp
= opendir(s
->base_dir
);
960 if (dp
== NULL
) return ASL_STATUS_READ_FAILED
;
962 while ((dent
= readdir(dp
)) != NULL
)
964 if (dent
->d_name
[0] == '.') continue;
966 snprintf(path
, sizeof(path
), "%s/%s", s
->base_dir
, dent
->d_name
);
969 * NB asl_file_open_read will fail if path is NULL,
970 * if it is not an ASL store file, or if it isn't readable.
973 status
= asl_file_open_read(path
, &f
);
974 if ((status
!= ASL_STATUS_OK
) || (f
== NULL
)) continue;
976 files
= asl_file_list_add(files
, f
);
981 s
->work
= asl_file_list_match_start(files
, start_id
, direction
);
982 if (s
->work
== NULL
) return ASL_STATUS_FAILED
;
984 return ASL_STATUS_OK
;
988 asl_store_match_next(asl_store_t
*s
, asl_msg_list_t
*qlist
, asl_msg_list_t
**res
, uint32_t count
)
990 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
991 if (s
->work
== NULL
) return ASL_STATUS_OK
;
993 return asl_file_list_match_next(s
->work
, qlist
, res
, count
);
997 #pragma mark asl_object support
1000 _jump_dealloc(asl_object_private_t
*obj
)
1002 _asl_store_free_internal((asl_store_t
*)obj
);
1005 static asl_object_private_t
*
1006 _jump_next(asl_object_private_t
*obj
)
1008 asl_store_t
*s
= (asl_store_t
*)obj
;
1009 asl_msg_list_t
*list
;
1010 asl_msg_t
*out
= NULL
;
1013 if (s
== NULL
) return NULL
;
1014 if (s
->curr
== SIZE_MAX
) return NULL
;
1017 list
= asl_store_match(s
, NULL
, &last
, s
->curr
, 1, 0, 1);
1025 out
= asl_msg_list_get_index(list
, 0);
1026 asl_msg_list_release(list
);
1028 return (asl_object_private_t
*)out
;
1031 static asl_object_private_t
*
1032 _jump_prev(asl_object_private_t
*obj
)
1034 asl_store_t
*s
= (asl_store_t
*)obj
;
1035 asl_msg_list_t
*list
;
1036 asl_msg_t
*out
= NULL
;
1039 if (s
== NULL
) return NULL
;
1040 if (s
->curr
== 0) return NULL
;
1043 if (s
->curr
== 0) return NULL
;
1045 list
= asl_store_match(s
, NULL
, &last
, s
->curr
, 1, 0, -1);
1053 out
= asl_msg_list_get_index(list
, 0);
1054 asl_msg_list_release(list
);
1056 return (asl_object_private_t
*)out
;
1060 _jump_set_iteration_index(asl_object_private_t
*obj
, size_t n
)
1062 asl_store_t
*s
= (asl_store_t
*)obj
;
1063 if (s
== NULL
) return;
1069 _jump_append(asl_object_private_t
*obj
, asl_object_private_t
*newobj
)
1071 asl_store_t
*s
= (asl_store_t
*)obj
;
1072 int type
= asl_get_type((asl_object_t
)newobj
);
1073 if (s
== NULL
) return;
1074 if (s
->flags
& ASL_FILE_FLAG_READ
) return;
1076 if (type
== ASL_TYPE_LIST
)
1079 asl_msg_list_reset_iteration((asl_msg_list_t
*)newobj
, 0);
1080 while (NULL
!= (msg
= asl_msg_list_next((asl_msg_list_t
*)newobj
)))
1082 if (asl_store_save(s
, msg
) != ASL_STATUS_OK
) return;
1085 else if ((type
== ASL_TYPE_MSG
) || (type
== ASL_TYPE_QUERY
))
1087 asl_store_save(s
, (asl_msg_t
*)newobj
);
1091 static asl_object_private_t
*
1092 _jump_search(asl_object_private_t
*obj
, asl_object_private_t
*query
)
1094 asl_store_t
*s
= (asl_store_t
*)obj
;
1095 int type
= asl_get_type((asl_object_t
)query
);
1096 asl_msg_list_t
*out
= NULL
;
1097 asl_msg_list_t
*ql
= NULL
;
1102 out
= asl_store_match(s
, NULL
, &last
, 0, 0, 0, 1);
1104 else if (type
== ASL_TYPE_LIST
)
1106 out
= asl_store_match(s
, (asl_msg_list_t
*)query
, &last
, 0, 0, 0, 1);
1108 else if ((type
== ASL_TYPE_MSG
) || (type
== ASL_TYPE_QUERY
))
1110 ql
= asl_msg_list_new();
1111 asl_msg_list_append(ql
, query
);
1113 out
= asl_store_match(s
, ql
, &last
, 0, 0, 0, 1);
1114 asl_msg_list_release(ql
);
1117 return (asl_object_private_t
*)out
;
1120 static asl_object_private_t
*
1121 _jump_match(asl_object_private_t
*obj
, asl_object_private_t
*qlist
, size_t *last
, size_t start
, size_t count
, uint32_t duration
, int32_t dir
)
1124 asl_msg_list_t
*out
;
1126 out
= asl_store_match((asl_store_t
*)obj
, (asl_msg_list_t
*)qlist
, &x
, start
, count
, duration
, dir
);
1128 return (asl_object_private_t
*)out
;
1131 __private_extern__
const asl_jump_table_t
*
1132 asl_store_jump_table()
1134 static const asl_jump_table_t jump
=
1137 .dealloc
= &_jump_dealloc
,
1138 .set_key_val_op
= NULL
,
1140 .get_val_op_for_key
= NULL
,
1141 .get_key_val_op_at_index
= NULL
,
1143 .next
= &_jump_next
,
1144 .prev
= &_jump_prev
,
1145 .get_object_at_index
= NULL
,
1146 .set_iteration_index
= &_jump_set_iteration_index
,
1147 .remove_object_at_index
= NULL
,
1148 .append
= &_jump_append
,
1150 .search
= &_jump_search
,
1151 .match
= &_jump_match