2 * Copyright (c) 2007-2010 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@
25 #include "asl_mini_memory.h"
29 #include <sys/errno.h>
31 #include <sys/types.h>
33 #include <asl_private.h>
36 #define DEFAULT_MAX_RECORDS 256
37 #define MEM_STRING_HEADER_SIZE 8
38 #define CFLOG_LOCAL_TIME_KEY "CFLog Local Time"
39 #define CFLOG_THREAD_KEY "CFLog Thread"
41 #define forever for(;;)
42 extern time_t asl_parse_time(const char *str
);
43 extern int asl_msg_cmp(asl_msg_t
*a
, asl_msg_t
*b
);
46 asl_mini_memory_statistics(asl_mini_memory_t
*s
, aslmsg
*msg
)
53 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
54 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
56 out
= asl_new(ASL_TYPE_MSG
);
57 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
59 size
= sizeof(asl_mini_memory_t
);
60 size
+= ((s
->record_count
+ 1) * sizeof(mini_mem_record_t
));
62 for (i
= 0; i
< s
->string_count
; i
++)
64 size
+= MEM_STRING_HEADER_SIZE
;
65 if (((mini_mem_string_t
*)s
->string_cache
[i
])->str
!= NULL
) size
+= (strlen(((mini_mem_string_t
*)s
->string_cache
[i
])->str
) + 1);
68 snprintf(str
, sizeof(str
), "%llu", size
);
69 asl_set(out
, "Size", str
);
72 for (i
= 0; i
< s
->record_count
; i
++) if (s
->record
[i
]->mid
!= 0) n
++;
74 snprintf(str
, sizeof(str
), "%u", n
);
75 asl_set(out
, "RecordCount", str
);
77 snprintf(str
, sizeof(str
), "%u", s
->string_count
);
78 asl_set(out
, "StringCount", str
);
85 asl_mini_memory_close(asl_mini_memory_t
*s
)
89 if (s
== NULL
) return ASL_STATUS_OK
;
91 if (s
->record
!= NULL
)
93 for (i
= 0; i
< s
->record_count
; i
++)
95 if (s
->record
[i
] != NULL
) free(s
->record
[i
]);
103 if (s
->buffer_record
!= NULL
) free(s
->buffer_record
);
105 if (s
->string_cache
!= NULL
)
107 for (i
= 0; i
< s
->string_count
; i
++)
109 if (s
->string_cache
[i
] != NULL
) free(s
->string_cache
[i
]);
110 s
->string_cache
[i
] = NULL
;
113 free(s
->string_cache
);
114 s
->string_cache
= NULL
;
119 return ASL_STATUS_OK
;
123 asl_mini_memory_open(uint32_t max_records
, asl_mini_memory_t
**s
)
125 asl_mini_memory_t
*out
;
128 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
130 if (max_records
== 0) max_records
= DEFAULT_MAX_RECORDS
;
132 out
= calloc(1, sizeof(asl_mini_memory_t
));
133 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
135 out
->record_count
= max_records
;
136 out
->record
= (mini_mem_record_t
**)calloc(max_records
, sizeof(mini_mem_record_t
*));
137 if (out
->record
== NULL
)
140 return ASL_STATUS_NO_MEMORY
;
143 for (i
= 0; i
< max_records
; i
++)
145 out
->record
[i
] = (mini_mem_record_t
*)calloc(1, sizeof(mini_mem_record_t
));
146 if (out
->record
[i
] == NULL
)
148 asl_mini_memory_close(out
);
149 return ASL_STATUS_NO_MEMORY
;
153 out
->buffer_record
= (mini_mem_record_t
*)calloc(1, sizeof(mini_mem_record_t
));
154 if (out
->buffer_record
== NULL
)
156 asl_mini_memory_close(out
);
157 return ASL_STATUS_NO_MEMORY
;
163 return ASL_STATUS_OK
;
166 static mini_mem_string_t
*
167 mem_string_new(const char *str
, uint32_t len
, uint32_t hash
)
169 mini_mem_string_t
*out
;
172 if (str
== NULL
) return NULL
;
174 ss
= MEM_STRING_HEADER_SIZE
+ len
+ 1;
175 out
= (mini_mem_string_t
*)calloc(1, ss
);
176 if (out
== NULL
) return NULL
;
180 memcpy(out
->str
, str
, len
);
186 * Find the first hash greater than or equal to a given hash in the string cache.
187 * Return s->string_count if hash is greater that or equal to last hash in the string cache.
188 * Caller must check if the hashes match or not.
190 * This routine is used both to find strings in the cache and to determine where to insert
191 * new strings. Note that the caller needs to do extra work after calling this routine.
194 asl_mini_memory_string_cache_search_hash(asl_mini_memory_t
*s
, uint32_t hash
)
196 uint32_t top
, bot
, mid
, range
;
197 mini_mem_string_t
*ms
;
199 if (s
->string_count
== 0) return 0;
200 if (s
->string_count
== 1)
202 ms
= (mini_mem_string_t
*)s
->string_cache
[0];
203 if (hash
< ms
->hash
) return 0;
207 top
= s
->string_count
- 1;
214 ms
= (mini_mem_string_t
*)s
->string_cache
[mid
];
216 if (hash
== ms
->hash
)
220 ms
= (mini_mem_string_t
*)s
->string_cache
[mid
- 1];
221 if (hash
!= ms
->hash
) break;
229 ms
= (mini_mem_string_t
*)s
->string_cache
[mid
];
230 if (hash
< ms
->hash
) top
= mid
;
235 mid
= bot
+ (range
/ 2);
238 ms
= (mini_mem_string_t
*)s
->string_cache
[bot
];
239 if (hash
<= ms
->hash
) return bot
;
241 ms
= (mini_mem_string_t
*)s
->string_cache
[top
];
242 if (hash
<= ms
->hash
) return top
;
244 return s
->string_count
;
248 * Search the string cache.
249 * If the string is in the cache, increment refcount and return it.
250 * If the string is not in cache and create flag is on, create a new string.
251 * Otherwise, return NULL.
253 static mini_mem_string_t
*
254 asl_mini_memory_string_retain(asl_mini_memory_t
*s
, const char *str
, int create
)
256 uint32_t i
, where
, hash
, len
;
258 if (s
== NULL
) return NULL
;
259 if (str
== NULL
) return NULL
;
262 /* check the cache */
263 hash
= asl_core_string_hash(str
, len
);
264 where
= asl_mini_memory_string_cache_search_hash(s
, hash
);
266 /* asl_mini_memory_string_cache_search_hash just tells us where to look */
267 if (where
< s
->string_count
)
269 while (((mini_mem_string_t
*)(s
->string_cache
[where
]))->hash
== hash
)
271 if (!strcmp(str
, ((mini_mem_string_t
*)(s
->string_cache
[where
]))->str
))
273 ((mini_mem_string_t
*)(s
->string_cache
[where
]))->refcount
++;
274 return s
->string_cache
[where
];
282 if (create
== 0) return NULL
;
284 /* create a new mini_mem_string_t and insert into the cache at index 'where' */
285 if (s
->string_count
== 0)
287 s
->string_cache
= (void **)calloc(1, sizeof(void *));
291 s
->string_cache
= (void **)reallocf(s
->string_cache
, (s
->string_count
+ 1) * sizeof(void *));
292 for (i
= s
->string_count
; i
> where
; i
--) s
->string_cache
[i
] = s
->string_cache
[i
- 1];
295 if (s
->string_cache
== NULL
)
302 s
->string_cache
[where
] = mem_string_new(str
, len
, hash
);
304 return s
->string_cache
[where
];
308 asl_mini_memory_string_release(asl_mini_memory_t
*s
, mini_mem_string_t
*m
)
312 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
313 if (m
== NULL
) return ASL_STATUS_OK
;
315 if (m
->refcount
> 0) m
->refcount
--;
316 if (m
->refcount
> 0) return ASL_STATUS_OK
;
318 where
= asl_mini_memory_string_cache_search_hash(s
, m
->hash
);
319 if (((mini_mem_string_t
*)(s
->string_cache
[where
]))->hash
!= m
->hash
) return ASL_STATUS_OK
;
321 while (s
->string_cache
[where
] != m
)
323 if (((mini_mem_string_t
*)(s
->string_cache
[where
]))->hash
!= m
->hash
) return ASL_STATUS_OK
;
326 if (where
>= s
->string_count
) return ASL_STATUS_OK
;
329 for (i
= where
+ 1; i
< s
->string_count
; i
++) s
->string_cache
[i
- 1] = s
->string_cache
[i
];
334 if (s
->string_count
== 0)
336 free(s
->string_cache
);
337 s
->string_cache
= NULL
;
338 return ASL_STATUS_OK
;
341 s
->string_cache
= (void **)reallocf(s
->string_cache
, s
->string_count
* sizeof(void *));
342 if (s
->string_cache
== NULL
)
345 return ASL_STATUS_NO_MEMORY
;
348 return ASL_STATUS_OK
;
352 * Release all a record's strings and reset it's values
355 asl_mini_memory_record_clear(asl_mini_memory_t
*s
, mini_mem_record_t
*r
)
359 if (s
== NULL
) return;
360 if (r
== NULL
) return;
362 asl_mini_memory_string_release(s
, r
->sender
);
363 asl_mini_memory_string_release(s
, r
->facility
);
364 asl_mini_memory_string_release(s
, r
->message
);
366 for (i
= 0; i
< r
->kvcount
; i
++) asl_mini_memory_string_release(s
, r
->kvlist
[i
]);
368 if (r
->kvlist
!= NULL
) free(r
->kvlist
);
369 memset(r
, 0, sizeof(mini_mem_record_t
));
373 asl_mini_memory_record_free(asl_mini_memory_t
*s
, mini_mem_record_t
*r
)
375 asl_mini_memory_record_clear(s
, r
);
380 * Encode an aslmsg as a record structure.
381 * Creates and caches strings.
384 asl_mini_memory_message_encode(asl_mini_memory_t
*s
, aslmsg msg
)
387 mini_mem_string_t
*k
, *v
;
388 mini_mem_record_t
*r
;
389 const char *key
, *val
;
391 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
392 if (s
->buffer_record
== NULL
) return ASL_STATUS_INVALID_STORE
;
393 if (msg
== NULL
) return ASL_STATUS_INVALID_MESSAGE
;
395 r
= s
->buffer_record
;
397 memset(r
, 0, sizeof(mini_mem_record_t
));
400 r
->level
= ASL_LEVEL_DEBUG
;
402 r
->time
= (uint64_t)-1;
407 for (x
= asl_msg_fetch((asl_msg_t
*)msg
, 0, &key
, &val
, NULL
); x
!= IndexNull
; x
= asl_msg_fetch((asl_msg_t
*)msg
, x
, &key
, &val
, NULL
))
409 if (key
== NULL
) continue;
411 else if (!strcmp(key
, ASL_KEY_TIME
))
413 if (val
!= NULL
) r
->time
= asl_parse_time(val
);
415 else if (!strcmp(key
, ASL_KEY_SENDER
))
417 if (val
!= NULL
) r
->sender
= asl_mini_memory_string_retain(s
, val
, 1);
419 else if (!strcmp(key
, ASL_KEY_PID
))
421 if (val
!= NULL
) r
->pid
= atoi(val
);
423 else if (!strcmp(key
, ASL_KEY_LEVEL
))
425 if (val
!= NULL
) r
->level
= atoi(val
);
427 else if (!strcmp(key
, ASL_KEY_MSG
))
429 if (val
!= NULL
) r
->message
= asl_mini_memory_string_retain(s
, val
, 1);
431 else if (!strcmp(key
, ASL_KEY_FACILITY
))
433 if (val
!= NULL
) r
->facility
= asl_mini_memory_string_retain(s
, val
, 1);
435 else if (!strcmp(key
, ASL_KEY_MSG_ID
))
440 else if (!strcmp(key
, ASL_KEY_TIME_NSEC
))
445 else if (!strcmp(key
, ASL_KEY_HOST
))
450 else if (!strcmp(key
, ASL_KEY_REF_PID
))
455 else if (!strcmp(key
, ASL_KEY_REF_PROC
))
460 else if (!strcmp(key
, ASL_KEY_SESSION
))
465 else if (!strcmp(key
, ASL_KEY_UID
))
470 else if (!strcmp(key
, ASL_KEY_GID
))
475 else if (!strcmp(key
, ASL_KEY_READ_UID
))
480 else if (!strcmp(key
, ASL_KEY_READ_GID
))
485 else if (!strcmp(key
, CFLOG_LOCAL_TIME_KEY
))
490 else if (!strcmp(key
, CFLOG_THREAD_KEY
))
497 k
= asl_mini_memory_string_retain(s
, key
, 1);
498 if (k
== NULL
) continue;
501 if (val
!= NULL
) v
= asl_mini_memory_string_retain(s
, val
, 1);
505 r
->kvlist
= (mini_mem_string_t
**)calloc(2, sizeof(mini_mem_string_t
*));
509 r
->kvlist
= (mini_mem_string_t
**)realloc(r
->kvlist
, (r
->kvcount
+ 2) * sizeof(mini_mem_string_t
*));
512 if (r
->kvlist
== NULL
)
514 asl_mini_memory_record_clear(s
, r
);
515 return ASL_STATUS_NO_MEMORY
;
518 r
->kvlist
[r
->kvcount
++] = k
;
519 r
->kvlist
[r
->kvcount
++] = v
;
523 return ASL_STATUS_OK
;
527 asl_mini_memory_save(asl_mini_memory_t
*s
, aslmsg msg
, uint64_t *mid
)
530 mini_mem_record_t
*t
;
532 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
533 if (s
->buffer_record
== NULL
) return ASL_STATUS_INVALID_STORE
;
535 /* asl_mini_memory_message_encode creates and caches strings */
536 status
= asl_mini_memory_message_encode(s
, msg
);
537 if (status
!= ASL_STATUS_OK
) return status
;
539 s
->buffer_record
->mid
= s
->next_id
;
542 /* clear the first record */
543 t
= s
->record
[s
->record_first
];
544 asl_mini_memory_record_clear(s
, t
);
546 /* add the new record to the record list (swap in the buffer record) */
547 s
->record
[s
->record_first
] = s
->buffer_record
;
548 s
->buffer_record
= t
;
550 /* record list is a circular queue */
552 if (s
->record_first
>= s
->record_count
) s
->record_first
= 0;
554 *mid
= s
->buffer_record
->mid
;
560 * Decodes a record structure.
563 asl_mini_memory_message_decode(asl_mini_memory_t
*s
, mini_mem_record_t
*r
, aslmsg
*out
)
568 const char *key
, *val
;
570 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
571 if (r
== NULL
) return ASL_STATUS_INVALID_ARG
;
572 if (out
== NULL
) return ASL_STATUS_INVALID_ARG
;
576 msg
= asl_new(ASL_TYPE_MSG
);
577 if (msg
== NULL
) return ASL_STATUS_NO_MEMORY
;
580 snprintf(tmp
, sizeof(tmp
), "%u", r
->mid
);
581 asl_set(msg
, ASL_KEY_MSG_ID
, tmp
);
584 snprintf(tmp
, sizeof(tmp
), "%u", r
->level
);
585 asl_set(msg
, ASL_KEY_LEVEL
, tmp
);
588 if (r
->time
!= (uint64_t)-1)
590 snprintf(tmp
, sizeof(tmp
), "%llu", r
->time
);
591 asl_set(msg
, ASL_KEY_TIME
, tmp
);
595 if (r
->sender
!= NULL
)
597 asl_set(msg
, ASL_KEY_SENDER
, r
->sender
->str
);
601 if (r
->facility
!= NULL
)
603 asl_set(msg
, ASL_KEY_FACILITY
, r
->facility
->str
);
609 snprintf(tmp
, sizeof(tmp
), "%d", r
->pid
);
610 asl_set(msg
, ASL_KEY_PID
, tmp
);
614 if (r
->message
!= NULL
)
616 asl_set(msg
, ASL_KEY_MSG
, r
->message
->str
);
619 /* Key - Value List */
620 for (i
= 0; i
< r
->kvcount
; i
++)
625 if ((r
->kvlist
[i
] != NULL
) && (r
->kvlist
[i
]->str
!= NULL
)) key
= r
->kvlist
[i
]->str
;
627 if ((r
->kvlist
[i
] != NULL
) && (r
->kvlist
[i
]->str
!= NULL
)) val
= r
->kvlist
[i
]->str
;
629 if (key
!= NULL
) asl_set(msg
, key
, val
);
633 return ASL_STATUS_OK
;
637 asl_mini_memory_fetch(asl_mini_memory_t
*s
, uint64_t mid
, aslmsg
*msg
)
641 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
642 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
644 for (i
= 0; i
< s
->record_count
; i
++)
646 if (s
->record
[i
]->mid
== 0) break;
647 if (s
->record
[i
]->mid
== mid
) return asl_mini_memory_message_decode(s
, s
->record
[i
], msg
);
650 return ASL_STATUS_INVALID_ID
;
653 static mini_mem_record_t
*
654 asl_mini_memory_query_to_record(asl_mini_memory_t
*s
, asl_msg_t
*q
, uint32_t *type
)
656 mini_mem_record_t
*out
;
658 mini_mem_string_t
*mkey
, *mval
;
659 const char *key
, *val
;
661 if (type
== NULL
) return NULL
;
665 *type
= ASL_QUERY_MATCH_ERROR
;
669 /* NULL query matches anything */
670 *type
= ASL_QUERY_MATCH_TRUE
;
671 if (q
== NULL
) return NULL
;
672 if (asl_msg_count((asl_msg_t
*)q
) == 0) return NULL
;
675 /* we can only do fast match on equality tests */
676 *type
= ASL_QUERY_MATCH_SLOW
;
678 for (x
= asl_msg_fetch((asl_msg_t
*)q
, 0, NULL
, NULL
, &op
); x
!= IndexNull
; x
= asl_msg_fetch((asl_msg_t
*)q
, x
, NULL
, NULL
, &op
))
680 if (op
!= ASL_QUERY_OP_EQUAL
) return NULL
;
683 out
= (mini_mem_record_t
*)calloc(1, sizeof(mini_mem_record_t
));
686 *type
= ASL_QUERY_MATCH_ERROR
;
690 for (x
= asl_msg_fetch((asl_msg_t
*)q
, 0, &key
, &val
, &op
); x
!= IndexNull
; x
= asl_msg_fetch((asl_msg_t
*)q
, x
, &key
, &val
, &op
))
692 if (key
== NULL
) continue;
694 else if (!strcmp(key
, ASL_KEY_MSG_ID
))
696 if (val
== NULL
) continue;
698 if (*type
& ASL_QUERY_MATCH_MSG_ID
)
700 asl_mini_memory_record_free(s
, out
);
701 *type
= ASL_QUERY_MATCH_SLOW
;
705 *type
|= ASL_QUERY_MATCH_MSG_ID
;
706 out
->mid
= atoll(val
);
708 else if (!strcmp(key
, ASL_KEY_TIME
))
710 if (val
== NULL
) continue;
712 if (*type
& ASL_QUERY_MATCH_TIME
)
714 asl_mini_memory_record_free(s
, out
);
715 *type
= ASL_QUERY_MATCH_SLOW
;
719 *type
|= ASL_QUERY_MATCH_TIME
;
720 out
->time
= asl_parse_time(val
);
722 else if (!strcmp(key
, ASL_KEY_LEVEL
))
724 if (val
== NULL
) continue;
726 if (*type
& ASL_QUERY_MATCH_LEVEL
)
728 asl_mini_memory_record_free(s
, out
);
729 *type
= ASL_QUERY_MATCH_SLOW
;
733 *type
|= ASL_QUERY_MATCH_LEVEL
;
734 out
->level
= atoi(val
);
736 else if (!strcmp(key
, ASL_KEY_PID
))
738 if (val
== NULL
) continue;
740 if (*type
& ASL_QUERY_MATCH_PID
)
742 asl_mini_memory_record_free(s
, out
);
743 *type
= ASL_QUERY_MATCH_SLOW
;
747 *type
|= ASL_QUERY_MATCH_PID
;
748 out
->pid
= atoi(val
);
750 else if (!strcmp(key
, ASL_KEY_SENDER
))
752 if (val
== NULL
) continue;
754 if (*type
& ASL_QUERY_MATCH_SENDER
)
756 asl_mini_memory_record_free(s
, out
);
757 *type
= ASL_QUERY_MATCH_SLOW
;
761 *type
|= ASL_QUERY_MATCH_SENDER
;
762 out
->sender
= asl_mini_memory_string_retain(s
, val
, 0);
763 if (out
->sender
== NULL
)
765 asl_mini_memory_record_free(s
, out
);
766 *type
= ASL_QUERY_MATCH_FALSE
;
770 else if (!strcmp(key
, ASL_KEY_FACILITY
))
772 if (val
== NULL
) continue;
774 if (*type
& ASL_QUERY_MATCH_FACILITY
)
776 asl_mini_memory_record_free(s
, out
);
777 *type
= ASL_QUERY_MATCH_SLOW
;
781 *type
|= ASL_QUERY_MATCH_FACILITY
;
782 out
->facility
= asl_mini_memory_string_retain(s
, val
, 0);
783 if (out
->facility
== NULL
)
785 asl_mini_memory_record_free(s
, out
);
786 *type
= ASL_QUERY_MATCH_FALSE
;
790 else if (!strcmp(key
, ASL_KEY_MSG
))
792 if (val
== NULL
) continue;
794 if (*type
& ASL_QUERY_MATCH_MESSAGE
)
796 asl_mini_memory_record_free(s
, out
);
797 *type
= ASL_QUERY_MATCH_SLOW
;
801 *type
|= ASL_QUERY_MATCH_MESSAGE
;
802 out
->message
= asl_mini_memory_string_retain(s
, val
, 0);
803 if (out
->message
== NULL
)
805 asl_mini_memory_record_free(s
, out
);
806 *type
= ASL_QUERY_MATCH_FALSE
;
812 mkey
= asl_mini_memory_string_retain(s
, key
, 0);
815 asl_mini_memory_record_free(s
, out
);
816 *type
= ASL_QUERY_MATCH_FALSE
;
820 for (i
= 0; i
< out
->kvcount
; i
+= 2)
822 if (out
->kvlist
[i
] == mkey
)
824 asl_mini_memory_record_free(s
, out
);
825 *type
= ASL_QUERY_MATCH_SLOW
;
830 mval
= asl_mini_memory_string_retain(s
, val
, 0);
832 if (out
->kvcount
== 0)
834 out
->kvlist
= (mini_mem_string_t
**)calloc(2, sizeof(mini_mem_string_t
*));
838 out
->kvlist
= (mini_mem_string_t
**)realloc(out
->kvlist
, (out
->kvcount
+ 2) * sizeof(mini_mem_string_t
*));
841 if (out
->kvlist
== NULL
)
843 asl_mini_memory_record_free(s
, out
);
844 *type
= ASL_QUERY_MATCH_ERROR
;
848 out
->kvlist
[out
->kvcount
++] = mkey
;
849 out
->kvlist
[out
->kvcount
++] = mval
;
857 asl_mini_memory_fast_match(asl_mini_memory_t
*s
, mini_mem_record_t
*r
, uint32_t qtype
, mini_mem_record_t
*q
)
861 if (s
== NULL
) return 0;
862 if (r
== NULL
) return 0;
863 if (q
== NULL
) return 1;
865 if ((qtype
& ASL_QUERY_MATCH_MSG_ID
) && (q
->mid
!= r
->mid
)) return 0;
866 if ((qtype
& ASL_QUERY_MATCH_TIME
) && (q
->time
!= r
->time
)) return 0;
867 if ((qtype
& ASL_QUERY_MATCH_LEVEL
) && (q
->level
!= r
->level
)) return 0;
868 if ((qtype
& ASL_QUERY_MATCH_PID
) && (q
->pid
!= r
->pid
)) return 0;
869 if ((qtype
& ASL_QUERY_MATCH_SENDER
) && (q
->sender
!= r
->sender
)) return 0;
870 if ((qtype
& ASL_QUERY_MATCH_FACILITY
) && (q
->facility
!= r
->facility
)) return 0;
871 if ((qtype
& ASL_QUERY_MATCH_MESSAGE
) && (q
->message
!= r
->message
)) return 0;
873 for (i
= 0; i
< q
->kvcount
; i
+= 2)
875 for (j
= 0; j
< r
->kvcount
; j
+= 2)
877 if (q
->kvlist
[i
] == r
->kvlist
[j
])
879 if (q
->kvlist
[i
+ 1] == r
->kvlist
[j
+ 1]) break;
884 if (j
>= r
->kvcount
) return 0;
891 asl_mini_memory_slow_match(asl_mini_memory_t
*s
, mini_mem_record_t
*r
, mini_mem_record_t
*q
, aslmsg rawq
)
897 status
= asl_mini_memory_message_decode(s
, r
, &rawm
);
898 if (status
!= ASL_STATUS_OK
) return 0;
901 if (asl_msg_cmp((asl_msg_t
*)rawq
, (asl_msg_t
*)rawm
) != 0) status
= 1;
907 asl_mini_memory_match(asl_mini_memory_t
*s
, aslresponse query
, aslresponse
*res
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
)
909 uint32_t status
, i
, where
, start
, j
, do_match
, did_match
, rescount
, *qtype
;
910 mini_mem_record_t
**qp
;
913 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
914 if (res
== NULL
) return ASL_STATUS_INVALID_ARG
;
921 if ((query
== NULL
) || ((query
!= NULL
) && (query
->count
== 0)))
927 qp
= (mini_mem_record_t
**)calloc(query
->count
, sizeof(mini_mem_record_t
*));
928 if (qp
== NULL
) return ASL_STATUS_NO_MEMORY
;
930 qtype
= (uint32_t *)calloc(query
->count
, sizeof(uint32_t));
934 return ASL_STATUS_NO_MEMORY
;
938 for (i
= 0; i
< query
->count
; i
++)
940 qp
[i
] = asl_mini_memory_query_to_record(s
, query
->msg
[i
], &(qtype
[i
]));
941 if (qtype
[i
] == ASL_QUERY_MATCH_ERROR
)
943 for (j
= 0; j
< i
; j
++) asl_mini_memory_record_free(s
, qp
[j
]);
946 return ASL_STATUS_FAILED
;
949 if (qtype
[i
] != ASL_QUERY_MATCH_TRUE
) do_match
= 1;
953 for (i
= 0; i
< s
->record_count
; i
++)
957 where
= (s
->record_first
+ i
) % s
->record_count
;
958 if (s
->record
[where
]->mid
== 0) continue;
959 if (s
->record
[where
]->mid
>= start_id
) break;
963 where
= ((s
->record_count
- (i
+ 1)) + s
->record_first
) % s
->record_count
;
964 if (s
->record
[where
]->mid
== 0) continue;
965 if (s
->record
[where
]->mid
<= start_id
) break;
969 if (i
>= s
->record_count
)
973 for (i
= 0; i
< query
->count
; i
++) asl_mini_memory_record_free(s
, qp
[i
]);
978 return ASL_STATUS_OK
;
984 * loop through records
986 for (i
= 0; i
< s
->record_count
; i
++)
988 if (s
->record
[where
]->mid
== 0)
993 if (where
>= s
->record_count
) where
= 0;
997 if (where
== 0) where
= s
->record_count
- 1;
1001 if (where
== s
->record_first
) break;
1005 s
->record
[where
]->flags
&= ASL_MINI_MSG_FLAG_SEARCH_CLEAR
;
1006 *last_id
= s
->record
[where
]->mid
;
1013 for (j
= 0; (j
< query
->count
) && (did_match
== 0); j
++)
1015 if (qtype
[j
] == ASL_QUERY_MATCH_TRUE
)
1019 else if (qtype
[j
] == ASL_QUERY_MATCH_FALSE
)
1023 else if (qtype
[j
] == ASL_QUERY_MATCH_SLOW
)
1025 did_match
= asl_mini_memory_slow_match(s
, s
->record
[where
], qp
[j
], (aslmsg
)query
->msg
[j
]);
1029 did_match
= asl_mini_memory_fast_match(s
, s
->record
[where
], qtype
[j
], qp
[j
]);
1036 s
->record
[where
]->flags
|= ASL_MINI_MSG_FLAG_SEARCH_MATCH
;
1038 if ((count
!= 0) && (rescount
>= count
)) break;
1044 if (where
>= s
->record_count
) where
= 0;
1048 if (where
== 0) where
= s
->record_count
- 1;
1052 if (where
== s
->record_first
) break;
1057 for (i
= 0; i
< query
->count
; i
++) asl_mini_memory_record_free(s
, qp
[i
]);
1063 if (rescount
== 0) return ASL_STATUS_OK
;
1065 *res
= (asl_msg_list_t
*)calloc(1, sizeof(asl_msg_list_t
));
1066 if (*res
== NULL
) return ASL_STATUS_NO_MEMORY
;
1068 (*res
)->count
= rescount
;
1070 (*res
)->msg
= (asl_msg_t
**)calloc(rescount
, sizeof(asl_msg_t
*));
1071 if ((*res
)->msg
== NULL
)
1075 return ASL_STATUS_NO_MEMORY
;
1081 if (s
->record
[where
]->flags
& ASL_MINI_MSG_FLAG_SEARCH_MATCH
)
1083 s
->record
[where
]->flags
&= ASL_MINI_MSG_FLAG_SEARCH_CLEAR
;
1085 status
= asl_mini_memory_message_decode(s
, s
->record
[where
], &m
);
1086 if (status
!= ASL_STATUS_OK
)
1088 aslresponse_free(*res
);
1093 (*res
)->msg
[(*res
)->curr
++] = (asl_msg_t
*)m
;
1094 if ((*res
)->curr
== rescount
) break;
1100 if (where
>= s
->record_count
) where
= 0;
1104 if (where
== 0) where
= s
->record_count
- 1;
1108 if (where
== s
->record_first
) break;
1112 return ASL_STATUS_OK
;