2 * Copyright (c) 2007-2009 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 2007 Apple Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
30 #include <sys/types.h>
34 #include <asl_private.h>
36 #include <asl_store.h>
39 extern time_t asl_parse_time(const char *str
);
40 extern uint64_t asl_file_cursor(asl_file_t
*s
);
41 extern uint32_t asl_file_match_start(asl_file_t
*s
, uint64_t start_id
, int32_t direction
);
42 extern uint32_t asl_file_match_next(asl_file_t
*s
, aslresponse query
, asl_msg_t
**msg
, uint64_t *last_id
, int32_t direction
, int32_t ruid
, int32_t rgid
);
44 #define SECONDS_PER_DAY 86400
47 * The ASL Store is organized as a set of files in a common directory.
48 * Files are prefixed by the date (YYYY.MM.DD) of their contents.
50 * Messages with no access controls are saved in YYYY.MM.DD.asl
51 * Messages with access limited to UID uuu are saved in YYYY.MM.DD.Uuuu.asl
52 * Messages with access limited to GID ggg are saved in YYYY.MM.DD.Gggg.asl
53 * Messages with access limited to UID uuu and GID ggg are saved in YYYY.MM.DD.Uuuu.Gggg.asl
55 * Messages that have a value for ASLExpireTime are saved in BB.YYYY.MM.DD.asl
56 * where the timestamp is the "Best Before" date of the file. Access controls
57 * are implemented as above with Uuuu and Gggg in the file name. Note that the
58 * Best Before files are for the last day of the month, so a single file contains
59 * messages that expire in that month.
61 * An external tool runs daily and deletes "old" files.
70 memset(&ctm
, 0, sizeof(struct tm
));
73 if (localtime_r((const time_t *)&now
, &ctm
) == NULL
) return 0;
83 * The base directory contains a data file which stores
86 * | MAX_ID (uint64_t) |
90 asl_store_open_write(const char *basedir
, asl_store_t
**s
)
100 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
102 start
= _asl_start_today();
103 if (start
== 0) return ASL_STATUS_FAILED
;
105 if (basedir
== NULL
) basedir
= PATH_ASL_STORE
;
107 memset(&sb
, 0, sizeof(struct stat
));
108 if (stat(basedir
, &sb
) != 0) return ASL_STATUS_INVALID_STORE
;
109 if ((sb
.st_mode
& S_IFDIR
) == 0) return ASL_STATUS_INVALID_STORE
;
112 asprintf(&path
, "%s/%s", basedir
, FILE_ASL_STORE_DATA
);
113 if (path
== NULL
) return ASL_STATUS_NO_MEMORY
;
117 memset(&sb
, 0, sizeof(struct stat
));
118 if (stat(path
, &sb
) != 0)
123 return ASL_STATUS_FAILED
;
126 sd
= fopen(path
, "w+");
129 if (sd
== NULL
) return ASL_STATUS_FAILED
;
133 /* Create new StoreData file (8 bytes ID + 4 bytes flags) */
135 if (fwrite(&last_id
, sizeof(uint64_t), 1, sd
) != 1)
138 return ASL_STATUS_WRITE_FAILED
;
142 if (fwrite(&flags
, sizeof(uint32_t), 1, sd
) != 1)
145 return ASL_STATUS_WRITE_FAILED
;
150 sd
= fopen(path
, "r+");
153 if (sd
== NULL
) return ASL_STATUS_FAILED
;
154 if (fread(&last_id
, sizeof(uint64_t), 1, sd
) != 1)
157 return ASL_STATUS_READ_FAILED
;
160 last_id
= asl_core_ntohq(last_id
);
163 out
= (asl_store_t
*)calloc(1, sizeof(asl_store_t
));
167 return ASL_STATUS_NO_MEMORY
;
170 if (basedir
== NULL
) out
->base_dir
= strdup(PATH_ASL_STORE
);
171 else out
->base_dir
= strdup(basedir
);
173 if (out
->base_dir
== NULL
)
177 return ASL_STATUS_NO_MEMORY
;
180 out
->start_today
= start
;
181 out
->start_tomorrow
= out
->start_today
+ SECONDS_PER_DAY
;
183 out
->next_id
= last_id
+ 1;
185 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
187 memset(&out
->file_cache
[i
], 0, sizeof(asl_cached_file_t
));
188 out
->file_cache
[i
].u
= -1;
189 out
->file_cache
[i
].g
= -1;
193 return ASL_STATUS_OK
;
197 asl_store_statistics(asl_store_t
*s
, aslmsg
*msg
)
201 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
202 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
204 out
= (aslmsg
)calloc(1, sizeof(asl_msg_t
));
205 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
207 /* does nothing for now */
210 return ASL_STATUS_OK
;
214 asl_store_open_read(const char *basedir
, asl_store_t
**s
)
219 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
221 if (basedir
== NULL
) basedir
= PATH_ASL_STORE
;
223 memset(&sb
, 0, sizeof(struct stat
));
224 if (stat(basedir
, &sb
) != 0) return ASL_STATUS_INVALID_STORE
;
225 if ((sb
.st_mode
& S_IFDIR
) == 0) return ASL_STATUS_INVALID_STORE
;
227 out
= (asl_store_t
*)calloc(1, sizeof(asl_store_t
));
228 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
230 if (basedir
== NULL
) out
->base_dir
= strdup(PATH_ASL_STORE
);
231 else out
->base_dir
= strdup(basedir
);
233 if (out
->base_dir
== NULL
)
236 return ASL_STATUS_NO_MEMORY
;
240 return ASL_STATUS_OK
;
244 asl_store_max_file_size(asl_store_t
*s
, size_t max
)
246 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
248 s
->max_file_size
= max
;
249 return ASL_STATUS_OK
;
253 asl_store_file_closeall(asl_store_t
*s
)
257 if (s
== NULL
) return;
259 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
261 if (s
->file_cache
[i
].f
!= NULL
) asl_file_close(s
->file_cache
[i
].f
);
262 s
->file_cache
[i
].f
= NULL
;
263 if (s
->file_cache
[i
].path
!= NULL
) free(s
->file_cache
[i
].path
);
264 s
->file_cache
[i
].path
= NULL
;
265 s
->file_cache
[i
].u
= -1;
266 s
->file_cache
[i
].g
= -1;
267 s
->file_cache
[i
].bb
= 0;
268 s
->file_cache
[i
].ts
= 0;
273 asl_store_close(asl_store_t
*s
)
275 if (s
== NULL
) return ASL_STATUS_OK
;
277 if (s
->base_dir
!= NULL
) free(s
->base_dir
);
279 asl_store_file_closeall(s
);
280 if (s
->storedata
!= NULL
) fclose(s
->storedata
);
284 return ASL_STATUS_OK
;
288 asl_store_signal_sweep(asl_store_t
*s
)
295 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
297 asprintf(&str
, "%s/%s", s
->base_dir
, FILE_ASL_STORE_SWEEP_SEMAPHORE
);
298 if (str
== NULL
) return ASL_STATUS_NO_MEMORY
;
300 semfd
= open(str
, O_WRONLY
| O_CREAT
| O_NONBLOCK
, 0644);
303 if (semfd
< 0) return ASL_STATUS_WRITE_FAILED
;
305 status
= ASL_STATUS_OK
;
307 /* write the current message ID in the SweepStore file */
308 xid
= asl_core_htonq(s
->next_id
- 1);
309 if (write(semfd
, &xid
, sizeof(uint64_t)) != sizeof(uint64_t)) status
= ASL_STATUS_WRITE_FAILED
;
316 * Sweep the file cache.
317 * Close any files that have not been used in the last FILE_CACHE_TTL seconds.
318 * Returns least recently used or unused cache slot.
321 asl_store_file_cache_lru(asl_store_t
*s
, time_t now
, uint32_t ignorex
)
326 if (s
== NULL
) return 0;
329 min
= now
- FILE_CACHE_TTL
;
331 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
333 if ((i
!= ignorex
) && (s
->file_cache
[i
].ts
< min
))
335 asl_file_close(s
->file_cache
[i
].f
);
336 s
->file_cache
[i
].f
= NULL
;
337 if (s
->file_cache
[i
].path
!= NULL
) free(s
->file_cache
[i
].path
);
338 s
->file_cache
[i
].path
= NULL
;
339 s
->file_cache
[i
].u
= -1;
340 s
->file_cache
[i
].g
= -1;
341 s
->file_cache
[i
].bb
= 0;
342 s
->file_cache
[i
].ts
= 0;
345 if (s
->file_cache
[i
].ts
< s
->file_cache
[x
].ts
) x
= i
;
352 asl_store_sweep_file_cache(asl_store_t
*s
)
354 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
356 asl_store_file_cache_lru(s
, time(NULL
), FILE_CACHE_SIZE
);
357 return ASL_STATUS_OK
;
361 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
)
369 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
371 /* see if the file is already open and in the cache */
372 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
374 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
))
376 s
->file_cache
[i
].ts
= now
;
377 *f
= s
->file_cache
[i
].f
;
378 if (check_cache
== 1) asl_store_file_cache_lru(s
, now
, i
);
379 return ASL_STATUS_OK
;
392 asprintf(&path
, "%s/%s.asl", s
->base_dir
, tstring
);
398 asprintf(&path
, "%s/%s.G%d.asl", s
->base_dir
, tstring
, g
);
407 asprintf(&path
, "%s/%s.U%d.asl", s
->base_dir
, tstring
, u
);
413 asprintf(&path
, "%s/%s.U%d.G%u.asl", s
->base_dir
, tstring
, u
, g
);
417 if (path
== NULL
) return ASL_STATUS_NO_MEMORY
;
420 status
= asl_file_open_write(path
, m
, u
, g
, &out
);
421 if (status
!= ASL_STATUS_OK
)
427 x
= asl_store_file_cache_lru(s
, now
, FILE_CACHE_SIZE
);
428 if (s
->file_cache
[x
].f
!= NULL
) asl_file_close(s
->file_cache
[x
].f
);
429 if (s
->file_cache
[x
].path
!= NULL
) free(s
->file_cache
[x
].path
);
431 s
->file_cache
[x
].f
= out
;
432 s
->file_cache
[x
].path
= path
;
433 s
->file_cache
[x
].u
= ruid
;
434 s
->file_cache
[x
].g
= rgid
;
435 s
->file_cache
[x
].bb
= bb
;
436 s
->file_cache
[x
].ts
= time(NULL
);
440 return ASL_STATUS_OK
;
444 asl_store_file_path(asl_store_t
*s
, asl_file_t
*f
)
448 if (s
== NULL
) return NULL
;
450 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
452 if (s
->file_cache
[i
].f
== f
)
454 if (s
->file_cache
[i
].path
== NULL
) return NULL
;
455 return strdup(s
->file_cache
[i
].path
);
463 asl_store_file_close(asl_store_t
*s
, asl_file_t
*f
)
467 if (s
== NULL
) return;
468 if (f
== NULL
) return;
470 for (i
= 0; i
< FILE_CACHE_SIZE
; i
++)
472 if (s
->file_cache
[i
].f
== f
)
474 asl_file_close(s
->file_cache
[i
].f
);
475 s
->file_cache
[i
].f
= NULL
;
476 if (s
->file_cache
[i
].path
!= NULL
) free(s
->file_cache
[i
].path
);
477 s
->file_cache
[i
].path
= NULL
;
478 s
->file_cache
[i
].u
= -1;
479 s
->file_cache
[i
].g
= -1;
480 s
->file_cache
[i
].bb
= 0;
481 s
->file_cache
[i
].ts
= 0;
488 asl_store_save(asl_store_t
*s
, aslmsg msg
)
491 time_t msg_time
, now
, bb
;
492 char *path
, *tmp_path
, *tstring
, *scratch
;
497 uint32_t status
, check_cache
, signal_sweep
, len
;
501 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
502 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
507 if ((s
->last_write
+ FILE_CACHE_TTL
) <= now
) check_cache
= 1;
512 val
= asl_get(msg
, ASL_KEY_TIME
);
513 if (val
== NULL
) msg_time
= now
;
514 else msg_time
= asl_parse_time(val
);
516 if (msg_time
>= s
->start_tomorrow
)
518 if (now
>= s
->start_tomorrow
)
523 asl_store_file_closeall(s
);
526 * _asl_start_today should never fail, but if it does,
527 * just push forward one day. That will probably be correct, and if
528 * it isn't, the next message that gets saved will push it ahead again
529 * until we get to the right date.
531 s
->start_today
= _asl_start_today();
532 if (s
->start_today
== 0) s
->start_today
= s
->start_tomorrow
;
534 s
->start_tomorrow
= s
->start_today
+ SECONDS_PER_DAY
;
538 val
= asl_get(msg
, ASL_KEY_READ_UID
);
540 if (val
!= NULL
) ruid
= atoi(val
);
542 val
= asl_get(msg
, ASL_KEY_READ_GID
);
544 if (val
!= NULL
) rgid
= atoi(val
);
547 val
= asl_get(msg
, ASL_KEY_EXPIRE_TIME
);
551 msg_time
= asl_parse_time(val
);
554 if (fseeko(s
->storedata
, 0, SEEK_SET
) != 0) return ASL_STATUS_WRITE_FAILED
;
556 xid
= asl_core_htonq(s
->next_id
);
557 if (fwrite(&xid
, sizeof(uint64_t), 1, s
->storedata
) != 1) return ASL_STATUS_WRITE_FAILED
;
564 if (localtime_r((const time_t *)&msg_time
, &ctm
) == NULL
) return ASL_STATUS_FAILED
;
570 * This supports 12 monthy "Best Before" buckets.
571 * We advance the actual expiry time to day zero of the following month.
572 * mktime() is clever enough to know that you actually mean the last day
573 * of the previous month. What we get back from localtime is the last
574 * day of the month in which the message expires, which we use in the name.
584 if (localtime_r((const time_t *)&bb
, &ctm
) == NULL
) return ASL_STATUS_FAILED
;
585 asprintf(&tstring
, "BB.%d.%02d.%02d", ctm
.tm_year
+ 1900, ctm
.tm_mon
+ 1, ctm
.tm_mday
);
589 asprintf(&tstring
, "%d.%02d.%02d", ctm
.tm_year
+ 1900, ctm
.tm_mon
+ 1, ctm
.tm_mday
);
592 if (tstring
== NULL
) return ASL_STATUS_NO_MEMORY
;
594 status
= asl_store_file_open_write(s
, tstring
, ruid
, rgid
, bb
, &f
, now
, check_cache
);
598 if (status
!= ASL_STATUS_OK
) return status
;
600 status
= asl_file_save(f
, msg
, &xid
);
601 if (status
!= ASL_STATUS_OK
) return status
;
603 fsize
= asl_file_size(f
);
604 ftime
= asl_file_ctime(f
);
606 /* if file is larger than max_file_size, rename it and touch semaphore file in the store */
607 if ((s
->max_file_size
!= 0) && (fsize
> s
->max_file_size
))
610 status
= ASL_STATUS_OK
;
612 path
= asl_store_file_path(s
, f
);
614 asl_store_file_close(s
, f
);
621 if ((len
>= 4) && (!strcmp(path
+ len
- 4, ".asl")))
623 /* rename xxxxxxx.asl to xxxxxxx.timestamp.asl */
624 scratch
= strdup(path
);
627 scratch
[len
- 4] = '\0';
628 asprintf(&tmp_path
, "%s.%llu.asl", scratch
, ftime
);
635 /* append timestamp */
636 asprintf(&tmp_path
, "%s.%llu", path
, ftime
);
639 if (tmp_path
== NULL
)
641 status
= ASL_STATUS_NO_MEMORY
;
645 if (rename(path
, tmp_path
) != 0) status
= ASL_STATUS_FAILED
;
653 if (signal_sweep
!= 0) asl_store_signal_sweep(s
);
659 asl_store_match_timeout(asl_store_t
*s
, aslresponse query
, aslresponse
*res
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
, uint32_t usec
)
666 asl_file_list_t
*files
;
668 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
669 if (res
== NULL
) return ASL_STATUS_INVALID_ARG
;
674 * Open all readable files
676 dp
= opendir(s
->base_dir
);
677 if (dp
== NULL
) return ASL_STATUS_READ_FAILED
;
679 while ((dent
= readdir(dp
)) != NULL
)
681 if (dent
->d_name
[0] == '.') continue;
684 asprintf(&path
, "%s/%s", s
->base_dir
, dent
->d_name
);
686 /* 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 */
687 status
= asl_file_open_read(path
, &f
);
688 if (path
!= NULL
) free(path
);
689 if ((status
!= ASL_STATUS_OK
) || (f
== NULL
)) continue;
691 files
= asl_file_list_add(files
, f
);
696 status
= asl_file_list_match_timeout(files
, query
, res
, last_id
, start_id
, count
, direction
, usec
);
697 asl_file_list_close(files
);
702 asl_store_match(asl_store_t
*s
, aslresponse query
, aslresponse
*res
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
)
704 return asl_store_match_timeout(s
, query
, res
, last_id
, start_id
, count
, direction
, 0);
708 asl_store_match_start(asl_store_t
*s
, uint64_t start_id
, int32_t direction
)
715 asl_file_list_t
*files
;
717 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
719 if (s
->work
!= NULL
) asl_file_list_match_end(s
->work
);
725 * Open all readable files
727 dp
= opendir(s
->base_dir
);
728 if (dp
== NULL
) return ASL_STATUS_READ_FAILED
;
730 while ((dent
= readdir(dp
)) != NULL
)
732 if (dent
->d_name
[0] == '.') continue;
735 asprintf(&path
, "%s/%s", s
->base_dir
, dent
->d_name
);
737 /* 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 */
738 status
= asl_file_open_read(path
, &f
);
739 if (path
!= NULL
) free(path
);
740 if ((status
!= ASL_STATUS_OK
) || (f
== NULL
)) continue;
742 files
= asl_file_list_add(files
, f
);
747 s
->work
= asl_file_list_match_start(files
, start_id
, direction
);
748 if (s
->work
== NULL
) return ASL_STATUS_FAILED
;
750 return ASL_STATUS_OK
;
754 asl_store_match_next(asl_store_t
*s
, aslresponse query
, aslresponse
*res
, uint32_t count
)
756 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
757 if (s
->work
== NULL
) return ASL_STATUS_OK
;
759 return asl_file_list_match_next(s
->work
, query
, res
, count
);