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_memory.h"
29 #include <sys/errno.h>
31 #include <sys/types.h>
33 #include <asl_private.h>
35 #define DEFAULT_MAX_RECORDS 2000
36 #define MEM_STRING_HEADER_SIZE 8
38 #define forever for(;;)
39 extern time_t asl_parse_time(const char *str
);
40 extern int asl_msg_cmp(asl_msg_t
*a
, asl_msg_t
*b
);
43 asl_memory_statistics(asl_memory_t
*s
, aslmsg
*msg
)
50 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
51 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
53 out
= asl_new(ASL_TYPE_MSG
);
54 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
56 size
= sizeof(asl_memory_t
);
57 size
+= ((s
->record_count
+ 1) * sizeof(mem_record_t
));
59 for (i
= 0; i
< s
->string_count
; i
++)
61 size
+= MEM_STRING_HEADER_SIZE
;
62 if (((mem_string_t
*)s
->string_cache
[i
])->str
!= NULL
) size
+= (strlen(((mem_string_t
*)s
->string_cache
[i
])->str
) + 1);
65 snprintf(str
, sizeof(str
), "%llu", size
);
66 asl_set(out
, "Size", str
);
69 for (i
= 0; i
< s
->record_count
; i
++) if (s
->record
[i
]->mid
!= 0) n
++;
71 snprintf(str
, sizeof(str
), "%u", n
);
72 asl_set(out
, "RecordCount", str
);
74 snprintf(str
, sizeof(str
), "%u", s
->string_count
);
75 asl_set(out
, "StringCount", str
);
82 asl_memory_close(asl_memory_t
*s
)
86 if (s
== NULL
) return ASL_STATUS_OK
;
88 if (s
->record
!= NULL
)
90 for (i
= 0; i
< s
->record_count
; i
++)
92 if (s
->record
[i
] != NULL
) free(s
->record
[i
]);
100 if (s
->buffer_record
!= NULL
) free(s
->buffer_record
);
102 if (s
->string_cache
!= NULL
)
104 for (i
= 0; i
< s
->string_count
; i
++)
106 if (s
->string_cache
[i
] != NULL
) free(s
->string_cache
[i
]);
107 s
->string_cache
[i
] = NULL
;
110 free(s
->string_cache
);
111 s
->string_cache
= NULL
;
116 return ASL_STATUS_OK
;
120 asl_memory_open(uint32_t max_records
, asl_memory_t
**s
)
125 if (s
== NULL
) return ASL_STATUS_INVALID_ARG
;
127 if (max_records
== 0) max_records
= DEFAULT_MAX_RECORDS
;
129 out
= calloc(1, sizeof(asl_memory_t
));
130 if (out
== NULL
) return ASL_STATUS_NO_MEMORY
;
132 out
->record_count
= max_records
;
133 out
->record
= (mem_record_t
**)calloc(max_records
, sizeof(mem_record_t
*));
134 if (out
->record
== NULL
)
137 return ASL_STATUS_NO_MEMORY
;
140 for (i
= 0; i
< max_records
; i
++)
142 out
->record
[i
] = (mem_record_t
*)calloc(1, sizeof(mem_record_t
));
143 if (out
->record
[i
] == NULL
)
145 asl_memory_close(out
);
146 return ASL_STATUS_NO_MEMORY
;
150 out
->buffer_record
= (mem_record_t
*)calloc(1, sizeof(mem_record_t
));
151 if (out
->buffer_record
== NULL
)
153 asl_memory_close(out
);
154 return ASL_STATUS_NO_MEMORY
;
158 return ASL_STATUS_OK
;
161 static mem_string_t
*
162 mem_string_new(const char *str
, uint32_t len
, uint32_t hash
)
167 if (str
== NULL
) return NULL
;
169 ss
= MEM_STRING_HEADER_SIZE
+ len
+ 1;
170 out
= (mem_string_t
*)calloc(1, ss
);
171 if (out
== NULL
) return NULL
;
175 memcpy(out
->str
, str
, len
);
181 * Find the first hash greater than or equal to a given hash in the string cache.
182 * Return s->string_count if hash is greater that or equal to last hash in the string cache.
183 * Caller must check if the hashes match or not.
185 * This routine is used both to find strings in the cache and to determine where to insert
186 * new strings. Note that the caller needs to do extra work after calling this routine.
189 asl_memory_string_cache_search_hash(asl_memory_t
*s
, uint32_t hash
)
191 uint32_t top
, bot
, mid
, range
;
194 if (s
->string_count
== 0) return 0;
195 if (s
->string_count
== 1)
197 ms
= (mem_string_t
*)s
->string_cache
[0];
198 if (hash
< ms
->hash
) return 0;
202 range
= top
= s
->string_count
- 1;
208 ms
= (mem_string_t
*)s
->string_cache
[mid
];
210 if (hash
== ms
->hash
)
214 ms
= (mem_string_t
*)s
->string_cache
[mid
- 1];
215 if (hash
!= ms
->hash
) break;
223 ms
= (mem_string_t
*)s
->string_cache
[mid
];
224 if (hash
< ms
->hash
) top
= mid
;
229 mid
= bot
+ (range
/ 2);
232 ms
= (mem_string_t
*)s
->string_cache
[bot
];
233 if (hash
<= ms
->hash
) return bot
;
235 ms
= (mem_string_t
*)s
->string_cache
[top
];
236 if (hash
<= ms
->hash
) return top
;
238 return s
->string_count
;
242 * Search the string cache.
243 * If the string is in the cache, increment refcount and return it.
244 * If the string is not in cache and create flag is on, create a new string.
245 * Otherwise, return NULL.
247 static mem_string_t
*
248 asl_memory_string_retain(asl_memory_t
*s
, const char *str
, int create
)
250 uint32_t i
, where
, hash
, len
;
253 if (s
== NULL
) return NULL
;
254 if (str
== NULL
) return NULL
;
257 /* check the cache */
258 hash
= asl_core_string_hash(str
, len
);
259 where
= asl_memory_string_cache_search_hash(s
, hash
);
261 /* asl_memory_string_cache_search_hash just tells us where to look */
262 if (where
< s
->string_count
)
264 while (((mem_string_t
*)(s
->string_cache
[where
]))->hash
== hash
)
266 if (!strcmp(str
, ((mem_string_t
*)(s
->string_cache
[where
]))->str
))
268 ((mem_string_t
*)(s
->string_cache
[where
]))->refcount
++;
269 return s
->string_cache
[where
];
277 if (create
== 0) return NULL
;
279 /* create a new mem_string_t and insert into the cache at index 'where' */
280 if (s
->string_count
== 0)
282 s
->string_cache
= (void **)calloc(1, sizeof(void *));
286 s
->string_cache
= (void **)reallocf(s
->string_cache
, (s
->string_count
+ 1) * sizeof(void *));
287 for (i
= s
->string_count
; i
> where
; i
--) s
->string_cache
[i
] = s
->string_cache
[i
- 1];
290 if (s
->string_cache
== NULL
)
296 new = mem_string_new(str
, len
, hash
);
297 if (new == NULL
) return NULL
;
299 s
->string_cache
[where
] = new;
302 return s
->string_cache
[where
];
306 asl_memory_string_release(asl_memory_t
*s
, mem_string_t
*m
)
310 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
311 if (m
== NULL
) return ASL_STATUS_OK
;
313 if (m
->refcount
> 0) m
->refcount
--;
314 if (m
->refcount
> 0) return ASL_STATUS_OK
;
316 where
= asl_memory_string_cache_search_hash(s
, m
->hash
);
317 if (((mem_string_t
*)(s
->string_cache
[where
]))->hash
!= m
->hash
) return ASL_STATUS_OK
;
319 while (s
->string_cache
[where
] != m
)
321 if (((mem_string_t
*)(s
->string_cache
[where
]))->hash
!= m
->hash
) return ASL_STATUS_OK
;
324 if (where
>= s
->string_count
) return ASL_STATUS_OK
;
327 for (i
= where
+ 1; i
< s
->string_count
; i
++) s
->string_cache
[i
- 1] = s
->string_cache
[i
];
332 if (s
->string_count
== 0)
334 free(s
->string_cache
);
335 s
->string_cache
= NULL
;
336 return ASL_STATUS_OK
;
339 s
->string_cache
= (void **)reallocf(s
->string_cache
, s
->string_count
* sizeof(void *));
340 if (s
->string_cache
== NULL
)
343 return ASL_STATUS_NO_MEMORY
;
346 return ASL_STATUS_OK
;
350 * Release all a record's strings and reset it's values
353 asl_memory_record_clear(asl_memory_t
*s
, mem_record_t
*r
)
357 if (s
== NULL
) return;
358 if (r
== NULL
) return;
360 asl_memory_string_release(s
, r
->host
);
361 asl_memory_string_release(s
, r
->sender
);
362 asl_memory_string_release(s
, r
->sender_mach_uuid
);
363 asl_memory_string_release(s
, r
->facility
);
364 asl_memory_string_release(s
, r
->message
);
365 asl_memory_string_release(s
, r
->refproc
);
366 asl_memory_string_release(s
, r
->session
);
368 for (i
= 0; i
< r
->kvcount
; i
++) asl_memory_string_release(s
, r
->kvlist
[i
]);
370 if (r
->kvlist
!= NULL
) free(r
->kvlist
);
371 memset(r
, 0, sizeof(mem_record_t
));
375 asl_memory_record_free(asl_memory_t
*s
, mem_record_t
*r
)
377 asl_memory_record_clear(s
, r
);
382 * Encode an aslmsg as a record structure.
383 * Creates and caches strings.
386 asl_memory_message_encode(asl_memory_t
*s
, aslmsg msg
)
391 const char *key
, *val
;
393 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
394 if (s
->buffer_record
== NULL
) return ASL_STATUS_INVALID_STORE
;
395 if (msg
== NULL
) return ASL_STATUS_INVALID_MESSAGE
;
397 r
= s
->buffer_record
;
399 memset(r
, 0, sizeof(mem_record_t
));
402 r
->level
= ASL_LEVEL_DEBUG
;
408 r
->time
= (uint64_t)-1;
409 r
->nano
= (uint32_t)-1;
414 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
))
416 if (key
== NULL
) continue;
418 else if (!strcmp(key
, ASL_KEY_TIME
))
420 if (val
!= NULL
) r
->time
= asl_parse_time(val
);
422 else if (!strcmp(key
, ASL_KEY_TIME_NSEC
))
424 if (val
!= NULL
) r
->nano
= atoi(val
);
426 else if (!strcmp(key
, ASL_KEY_HOST
))
428 if (val
!= NULL
) r
->host
= asl_memory_string_retain(s
, val
, 1);
430 else if (!strcmp(key
, ASL_KEY_SENDER
))
432 if (val
!= NULL
) r
->sender
= asl_memory_string_retain(s
, val
, 1);
434 else if (!strcmp(key
, ASL_KEY_PID
))
436 if (val
!= NULL
) r
->pid
= atoi(val
);
438 else if (!strcmp(key
, ASL_KEY_REF_PID
))
440 if (val
!= NULL
) r
->refpid
= atoi(val
);
442 else if (!strcmp(key
, ASL_KEY_UID
))
444 if (val
!= NULL
) r
->uid
= atoi(val
);
446 else if (!strcmp(key
, ASL_KEY_GID
))
448 if (val
!= NULL
) r
->gid
= atoi(val
);
450 else if (!strcmp(key
, ASL_KEY_LEVEL
))
452 if (val
!= NULL
) r
->level
= atoi(val
);
454 else if (!strcmp(key
, ASL_KEY_MSG
))
456 if (val
!= NULL
) r
->message
= asl_memory_string_retain(s
, val
, 1);
458 else if (!strcmp(key
, ASL_KEY_SENDER_MACH_UUID
))
460 if (val
!= NULL
) r
->sender_mach_uuid
= asl_memory_string_retain(s
, val
, 1);
462 else if (!strcmp(key
, ASL_KEY_FACILITY
))
464 if (val
!= NULL
) r
->facility
= asl_memory_string_retain(s
, val
, 1);
466 else if (!strcmp(key
, ASL_KEY_REF_PROC
))
468 if (val
!= NULL
) r
->refproc
= asl_memory_string_retain(s
, val
, 1);
470 else if (!strcmp(key
, ASL_KEY_SESSION
))
472 if (val
!= NULL
) r
->session
= asl_memory_string_retain(s
, val
, 1);
474 else if (!strcmp(key
, ASL_KEY_READ_UID
))
476 if (((r
->flags
& ASL_MSG_FLAG_READ_UID_SET
) == 0) && (val
!= NULL
))
479 r
->flags
|= ASL_MSG_FLAG_READ_UID_SET
;
482 else if (!strcmp(key
, ASL_KEY_READ_GID
))
484 if (((r
->flags
& ASL_MSG_FLAG_READ_GID_SET
) == 0) && (val
!= NULL
))
487 r
->flags
|= ASL_MSG_FLAG_READ_GID_SET
;
490 else if (!strcmp(key
, ASL_KEY_MSG_ID
))
497 k
= asl_memory_string_retain(s
, key
, 1);
498 if (k
== NULL
) continue;
501 if (val
!= NULL
) v
= asl_memory_string_retain(s
, val
, 1);
505 r
->kvlist
= (mem_string_t
**)calloc(2, sizeof(mem_string_t
*));
509 r
->kvlist
= (mem_string_t
**)reallocf(r
->kvlist
, (r
->kvcount
+ 2) * sizeof(mem_string_t
*));
512 if (r
->kvlist
== NULL
)
514 asl_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_memory_save(asl_memory_t
*s
, aslmsg msg
, uint64_t *mid
)
532 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
533 if (s
->buffer_record
== NULL
) return ASL_STATUS_INVALID_STORE
;
535 /* asl_memory_message_encode creates and caches strings */
536 status
= asl_memory_message_encode(s
, msg
);
537 if (status
!= ASL_STATUS_OK
) return status
;
541 s
->buffer_record
->mid
= *mid
;
545 s
->buffer_record
->mid
= asl_core_new_msg_id(0);
546 *mid
= s
->buffer_record
->mid
;
549 /* clear the first record */
550 t
= s
->record
[s
->record_first
];
551 asl_memory_record_clear(s
, t
);
553 /* add the new record to the record list (swap in the buffer record) */
554 s
->record
[s
->record_first
] = s
->buffer_record
;
555 s
->buffer_record
= t
;
557 /* record list is a circular queue */
559 if (s
->record_first
>= s
->record_count
) s
->record_first
= 0;
565 * Decodes a record structure.
568 asl_memory_message_decode(asl_memory_t
*s
, mem_record_t
*r
, aslmsg
*out
)
573 const char *key
, *val
;
575 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
576 if (r
== NULL
) return ASL_STATUS_INVALID_ARG
;
577 if (out
== NULL
) return ASL_STATUS_INVALID_ARG
;
581 msg
= asl_new(ASL_TYPE_MSG
);
582 if (msg
== NULL
) return ASL_STATUS_NO_MEMORY
;
585 snprintf(tmp
, sizeof(tmp
), "%llu", r
->mid
);
586 asl_set(msg
, ASL_KEY_MSG_ID
, tmp
);
589 snprintf(tmp
, sizeof(tmp
), "%u", r
->level
);
590 asl_set(msg
, ASL_KEY_LEVEL
, tmp
);
593 if (r
->time
!= (uint64_t)-1)
595 snprintf(tmp
, sizeof(tmp
), "%llu", r
->time
);
596 asl_set(msg
, ASL_KEY_TIME
, tmp
);
600 if (r
->nano
!= (uint32_t)-1)
602 snprintf(tmp
, sizeof(tmp
), "%u", r
->nano
);
603 asl_set(msg
, ASL_KEY_TIME_NSEC
, tmp
);
609 asl_set(msg
, ASL_KEY_HOST
, r
->host
->str
);
613 if (r
->sender
!= NULL
)
615 asl_set(msg
, ASL_KEY_SENDER
, r
->sender
->str
);
618 /* Sender mach UUID */
619 if (r
->sender_mach_uuid
!= NULL
)
621 asl_set(msg
, ASL_KEY_SENDER_MACH_UUID
, r
->sender_mach_uuid
->str
);
625 if (r
->facility
!= NULL
)
627 asl_set(msg
, ASL_KEY_FACILITY
, r
->facility
->str
);
631 if (r
->refproc
!= NULL
)
633 asl_set(msg
, ASL_KEY_REF_PROC
, r
->refproc
->str
);
637 if (r
->session
!= NULL
)
639 asl_set(msg
, ASL_KEY_SESSION
, r
->session
->str
);
645 snprintf(tmp
, sizeof(tmp
), "%d", r
->pid
);
646 asl_set(msg
, ASL_KEY_PID
, tmp
);
652 snprintf(tmp
, sizeof(tmp
), "%d", r
->refpid
);
653 asl_set(msg
, ASL_KEY_REF_PID
, tmp
);
659 snprintf(tmp
, sizeof(tmp
), "%d", r
->uid
);
660 asl_set(msg
, ASL_KEY_UID
, tmp
);
666 snprintf(tmp
, sizeof(tmp
), "%d", r
->gid
);
667 asl_set(msg
, ASL_KEY_GID
, tmp
);
671 if (r
->message
!= NULL
)
673 asl_set(msg
, ASL_KEY_MSG
, r
->message
->str
);
677 if (r
->flags
& ASL_MSG_FLAG_READ_UID_SET
)
679 snprintf(tmp
, sizeof(tmp
), "%d", r
->ruid
);
680 asl_set(msg
, ASL_KEY_READ_UID
, tmp
);
684 if (r
->flags
& ASL_MSG_FLAG_READ_GID_SET
)
686 snprintf(tmp
, sizeof(tmp
), "%d", r
->rgid
);
687 asl_set(msg
, ASL_KEY_READ_GID
, tmp
);
690 /* Key - Value List */
691 for (i
= 0; i
< r
->kvcount
; i
++)
696 if ((r
->kvlist
[i
] != NULL
) && (r
->kvlist
[i
]->str
!= NULL
)) key
= r
->kvlist
[i
]->str
;
698 if ((r
->kvlist
[i
] != NULL
) && (r
->kvlist
[i
]->str
!= NULL
)) val
= r
->kvlist
[i
]->str
;
700 if (key
!= NULL
) asl_set(msg
, key
, val
);
704 return ASL_STATUS_OK
;
708 asl_memory_fetch(asl_memory_t
*s
, uint64_t mid
, aslmsg
*msg
, int32_t ruid
, int32_t rgid
)
712 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
713 if (msg
== NULL
) return ASL_STATUS_INVALID_ARG
;
715 for (i
= 0; i
< s
->record_count
; i
++)
717 if (s
->record
[i
]->mid
== 0) break;
719 if (s
->record
[i
]->mid
== mid
)
721 status
= asl_core_check_access(s
->record
[i
]->ruid
, s
->record
[i
]->rgid
, ruid
, rgid
, s
->record
[i
]->flags
);
722 if (status
!= ASL_STATUS_OK
) return status
;
723 return asl_memory_message_decode(s
, s
->record
[i
], msg
);
727 return ASL_STATUS_INVALID_ID
;
730 static mem_record_t
*
731 asl_memory_query_to_record(asl_memory_t
*s
, aslmsg q
, uint32_t *type
)
735 mem_string_t
*mkey
, *mval
;
736 const char *key
, *val
;
738 if (type
== NULL
) return NULL
;
742 *type
= ASL_QUERY_MATCH_ERROR
;
746 /* NULL query matches anything */
747 *type
= ASL_QUERY_MATCH_TRUE
;
748 if (q
== NULL
) return NULL
;
749 if (asl_msg_count((asl_msg_t
*)q
) == 0) return NULL
;
752 /* we can only do fast match on equality tests */
753 *type
= ASL_QUERY_MATCH_SLOW
;
755 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
))
757 if (op
!= ASL_QUERY_OP_EQUAL
) return NULL
;
760 out
= (mem_record_t
*)calloc(1, sizeof(mem_record_t
));
763 *type
= ASL_QUERY_MATCH_ERROR
;
767 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
))
769 if (key
== NULL
) continue;
771 else if (!strcmp(key
, ASL_KEY_MSG_ID
))
773 if (val
== NULL
) continue;
775 if (*type
& ASL_QUERY_MATCH_MSG_ID
)
777 asl_memory_record_free(s
, out
);
778 *type
= ASL_QUERY_MATCH_SLOW
;
782 *type
|= ASL_QUERY_MATCH_MSG_ID
;
783 out
->mid
= atoll(val
);
785 else if (!strcmp(key
, ASL_KEY_TIME
))
787 if (val
== NULL
) continue;
789 if (*type
& ASL_QUERY_MATCH_TIME
)
791 asl_memory_record_free(s
, out
);
792 *type
= ASL_QUERY_MATCH_SLOW
;
796 *type
|= ASL_QUERY_MATCH_TIME
;
797 out
->time
= asl_parse_time(val
);
799 else if (!strcmp(key
, ASL_KEY_TIME_NSEC
))
801 if (val
== NULL
) continue;
803 if (*type
& ASL_QUERY_MATCH_NANO
)
805 asl_memory_record_free(s
, out
);
806 *type
= ASL_QUERY_MATCH_SLOW
;
810 *type
|= ASL_QUERY_MATCH_NANO
;
811 out
->nano
= atoll(val
);
813 else if (!strcmp(key
, ASL_KEY_LEVEL
))
815 if (val
== NULL
) continue;
817 if (*type
& ASL_QUERY_MATCH_LEVEL
)
819 asl_memory_record_free(s
, out
);
820 *type
= ASL_QUERY_MATCH_SLOW
;
824 *type
|= ASL_QUERY_MATCH_LEVEL
;
825 out
->level
= atoi(val
);
827 else if (!strcmp(key
, ASL_KEY_PID
))
829 if (val
== NULL
) continue;
831 if (*type
& ASL_QUERY_MATCH_PID
)
833 asl_memory_record_free(s
, out
);
834 *type
= ASL_QUERY_MATCH_SLOW
;
838 *type
|= ASL_QUERY_MATCH_PID
;
839 out
->pid
= atoi(val
);
841 else if (!strcmp(key
, ASL_KEY_UID
))
843 if (val
== NULL
) continue;
845 if (*type
& ASL_QUERY_MATCH_UID
)
847 asl_memory_record_free(s
, out
);
848 *type
= ASL_QUERY_MATCH_SLOW
;
852 *type
|= ASL_QUERY_MATCH_UID
;
853 out
->uid
= atoi(val
);
855 else if (!strcmp(key
, ASL_KEY_GID
))
857 if (val
== NULL
) continue;
859 if (*type
& ASL_QUERY_MATCH_GID
)
861 asl_memory_record_free(s
, out
);
862 *type
= ASL_QUERY_MATCH_SLOW
;
866 *type
|= ASL_QUERY_MATCH_GID
;
867 out
->gid
= atoi(val
);
869 else if (!strcmp(key
, ASL_KEY_READ_UID
))
871 if (val
== NULL
) continue;
873 if (*type
& ASL_QUERY_MATCH_RUID
)
875 asl_memory_record_free(s
, out
);
876 *type
= ASL_QUERY_MATCH_SLOW
;
880 *type
|= ASL_QUERY_MATCH_RUID
;
881 out
->ruid
= atoi(val
);
883 else if (!strcmp(key
, ASL_KEY_READ_GID
))
885 if (val
== NULL
) continue;
887 if (*type
& ASL_QUERY_MATCH_RGID
)
889 asl_memory_record_free(s
, out
);
890 *type
= ASL_QUERY_MATCH_SLOW
;
894 *type
|= ASL_QUERY_MATCH_RGID
;
895 out
->rgid
= atoi(val
);
897 else if (!strcmp(key
, ASL_KEY_REF_PID
))
899 if (val
== NULL
) continue;
901 if (*type
& ASL_QUERY_MATCH_REF_PID
)
903 asl_memory_record_free(s
, out
);
904 *type
= ASL_QUERY_MATCH_SLOW
;
908 *type
|= ASL_QUERY_MATCH_REF_PID
;
909 out
->refpid
= atoi(val
);
911 else if (!strcmp(key
, ASL_KEY_HOST
))
913 if (val
== NULL
) continue;
915 if (*type
& ASL_QUERY_MATCH_HOST
)
917 asl_memory_record_free(s
, out
);
918 *type
= ASL_QUERY_MATCH_SLOW
;
922 *type
|= ASL_QUERY_MATCH_HOST
;
923 out
->host
= asl_memory_string_retain(s
, val
, 0);
924 if (out
->host
== NULL
)
926 asl_memory_record_free(s
, out
);
927 *type
= ASL_QUERY_MATCH_FALSE
;
931 else if (!strcmp(key
, ASL_KEY_SENDER
))
933 if (val
== NULL
) continue;
935 if (*type
& ASL_QUERY_MATCH_SENDER
)
937 asl_memory_record_free(s
, out
);
938 *type
= ASL_QUERY_MATCH_SLOW
;
942 *type
|= ASL_QUERY_MATCH_SENDER
;
943 out
->sender
= asl_memory_string_retain(s
, val
, 0);
944 if (out
->sender
== NULL
)
946 asl_memory_record_free(s
, out
);
947 *type
= ASL_QUERY_MATCH_FALSE
;
951 else if (!strcmp(key
, ASL_KEY_SENDER_MACH_UUID
))
953 if (val
== NULL
) continue;
955 if (*type
& ASL_QUERY_MATCH_SMUUID
)
957 asl_memory_record_free(s
, out
);
958 *type
= ASL_QUERY_MATCH_SLOW
;
962 *type
|= ASL_QUERY_MATCH_SMUUID
;
963 out
->sender
= asl_memory_string_retain(s
, val
, 0);
964 if (out
->sender_mach_uuid
== NULL
)
966 asl_memory_record_free(s
, out
);
967 *type
= ASL_QUERY_MATCH_FALSE
;
971 else if (!strcmp(key
, ASL_KEY_FACILITY
))
973 if (val
== NULL
) continue;
975 if (*type
& ASL_QUERY_MATCH_FACILITY
)
977 asl_memory_record_free(s
, out
);
978 *type
= ASL_QUERY_MATCH_SLOW
;
982 *type
|= ASL_QUERY_MATCH_FACILITY
;
983 out
->facility
= asl_memory_string_retain(s
, val
, 0);
984 if (out
->facility
== NULL
)
986 asl_memory_record_free(s
, out
);
987 *type
= ASL_QUERY_MATCH_FALSE
;
991 else if (!strcmp(key
, ASL_KEY_MSG
))
993 if (val
== NULL
) continue;
995 if (*type
& ASL_QUERY_MATCH_MESSAGE
)
997 asl_memory_record_free(s
, out
);
998 *type
= ASL_QUERY_MATCH_SLOW
;
1002 *type
|= ASL_QUERY_MATCH_MESSAGE
;
1003 out
->message
= asl_memory_string_retain(s
, val
, 0);
1004 if (out
->message
== NULL
)
1006 asl_memory_record_free(s
, out
);
1007 *type
= ASL_QUERY_MATCH_FALSE
;
1011 else if (!strcmp(key
, ASL_KEY_REF_PROC
))
1013 if (val
== NULL
) continue;
1015 if (*type
& ASL_QUERY_MATCH_REF_PROC
)
1017 asl_memory_record_free(s
, out
);
1018 *type
= ASL_QUERY_MATCH_SLOW
;
1022 *type
|= ASL_QUERY_MATCH_REF_PROC
;
1023 out
->refproc
= asl_memory_string_retain(s
, val
, 0);
1024 if (out
->refproc
== NULL
)
1026 asl_memory_record_free(s
, out
);
1027 *type
= ASL_QUERY_MATCH_FALSE
;
1031 else if (!strcmp(key
, ASL_KEY_SESSION
))
1033 if (val
== NULL
) continue;
1035 if (*type
& ASL_QUERY_MATCH_SESSION
)
1037 asl_memory_record_free(s
, out
);
1038 *type
= ASL_QUERY_MATCH_SLOW
;
1042 *type
|= ASL_QUERY_MATCH_SESSION
;
1043 out
->session
= asl_memory_string_retain(s
, val
, 0);
1044 if (out
->session
== NULL
)
1046 asl_memory_record_free(s
, out
);
1047 *type
= ASL_QUERY_MATCH_FALSE
;
1053 mkey
= asl_memory_string_retain(s
, key
, 0);
1056 asl_memory_record_free(s
, out
);
1057 *type
= ASL_QUERY_MATCH_FALSE
;
1061 for (i
= 0; i
< out
->kvcount
; i
+= 2)
1063 if (out
->kvlist
[i
] == mkey
)
1065 asl_memory_record_free(s
, out
);
1066 *type
= ASL_QUERY_MATCH_SLOW
;
1071 mval
= asl_memory_string_retain(s
, val
, 0);
1073 if (out
->kvcount
== 0)
1075 out
->kvlist
= (mem_string_t
**)calloc(2, sizeof(mem_string_t
*));
1079 out
->kvlist
= (mem_string_t
**)reallocf(out
->kvlist
, (out
->kvcount
+ 2) * sizeof(mem_string_t
*));
1082 if (out
->kvlist
== NULL
)
1084 asl_memory_record_free(s
, out
);
1085 *type
= ASL_QUERY_MATCH_ERROR
;
1089 out
->kvlist
[out
->kvcount
++] = mkey
;
1090 out
->kvlist
[out
->kvcount
++] = mval
;
1098 asl_memory_fast_match(asl_memory_t
*s
, mem_record_t
*r
, uint32_t qtype
, mem_record_t
*q
)
1102 if (s
== NULL
) return 0;
1103 if (r
== NULL
) return 0;
1104 if (q
== NULL
) return 1;
1106 if ((qtype
& ASL_QUERY_MATCH_MSG_ID
) && (q
->mid
!= r
->mid
)) return 0;
1107 if ((qtype
& ASL_QUERY_MATCH_TIME
) && (q
->time
!= r
->time
)) return 0;
1108 if ((qtype
& ASL_QUERY_MATCH_NANO
) && (q
->nano
!= r
->nano
)) return 0;
1109 if ((qtype
& ASL_QUERY_MATCH_LEVEL
) && (q
->level
!= r
->level
)) return 0;
1110 if ((qtype
& ASL_QUERY_MATCH_PID
) && (q
->pid
!= r
->pid
)) return 0;
1111 if ((qtype
& ASL_QUERY_MATCH_UID
) && (q
->uid
!= r
->uid
)) return 0;
1112 if ((qtype
& ASL_QUERY_MATCH_GID
) && (q
->gid
!= r
->gid
)) return 0;
1113 if ((qtype
& ASL_QUERY_MATCH_RUID
) && (q
->ruid
!= r
->ruid
)) return 0;
1114 if ((qtype
& ASL_QUERY_MATCH_RGID
) && (q
->rgid
!= r
->rgid
)) return 0;
1115 if ((qtype
& ASL_QUERY_MATCH_REF_PID
) && (q
->refpid
!= r
->refpid
)) return 0;
1116 if ((qtype
& ASL_QUERY_MATCH_HOST
) && (q
->host
!= r
->host
)) return 0;
1117 if ((qtype
& ASL_QUERY_MATCH_SENDER
) && (q
->sender
!= r
->sender
)) return 0;
1118 if ((qtype
& ASL_QUERY_MATCH_SMUUID
) && (q
->sender_mach_uuid
!= r
->sender_mach_uuid
)) return 0;
1119 if ((qtype
& ASL_QUERY_MATCH_FACILITY
) && (q
->facility
!= r
->facility
)) return 0;
1120 if ((qtype
& ASL_QUERY_MATCH_MESSAGE
) && (q
->message
!= r
->message
)) return 0;
1121 if ((qtype
& ASL_QUERY_MATCH_REF_PROC
) && (q
->refproc
!= r
->refproc
)) return 0;
1122 if ((qtype
& ASL_QUERY_MATCH_SESSION
) && (q
->session
!= r
->session
)) return 0;
1124 for (i
= 0; i
< q
->kvcount
; i
+= 2)
1126 for (j
= 0; j
< r
->kvcount
; j
+= 2)
1128 if (q
->kvlist
[i
] == r
->kvlist
[j
])
1130 if (q
->kvlist
[i
+ 1] == r
->kvlist
[j
+ 1]) break;
1135 if (j
>= r
->kvcount
) return 0;
1142 asl_memory_slow_match(asl_memory_t
*s
, mem_record_t
*r
, aslmsg rawq
)
1148 status
= asl_memory_message_decode(s
, r
, &rawm
);
1149 if (status
!= ASL_STATUS_OK
) return 0;
1152 if (asl_msg_cmp((asl_msg_t
*)rawq
, (asl_msg_t
*)rawm
) != 0) status
= 1;
1158 asl_memory_match_restricted_uuid(asl_memory_t
*s
, aslresponse query
, aslresponse
*res
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
, int32_t ruid
, int32_t rgid
, const char *uuid_str
)
1160 uint32_t status
, i
, where
, start
, j
, do_match
, did_match
, rescount
, *qtype
;
1164 if (s
== NULL
) return ASL_STATUS_INVALID_STORE
;
1165 if (res
== NULL
) return ASL_STATUS_INVALID_ARG
;
1171 if ((query
== NULL
) || ((query
!= NULL
) && (query
->count
== 0)))
1177 qp
= (mem_record_t
**)calloc(query
->count
, sizeof(mem_record_t
*));
1178 if (qp
== NULL
) return ASL_STATUS_NO_MEMORY
;
1180 qtype
= (uint32_t *)calloc(query
->count
, sizeof(uint32_t));
1184 return ASL_STATUS_NO_MEMORY
;
1188 for (i
= 0; i
< query
->count
; i
++)
1190 qp
[i
] = asl_memory_query_to_record(s
, (aslmsg
)query
->msg
[i
], &(qtype
[i
]));
1191 if (qtype
[i
] == ASL_QUERY_MATCH_ERROR
)
1193 for (j
= 0; j
< i
; j
++) asl_memory_record_free(s
, qp
[j
]);
1196 return ASL_STATUS_FAILED
;
1199 if (qtype
[i
] != ASL_QUERY_MATCH_TRUE
) do_match
= 1;
1203 for (i
= 0; i
< s
->record_count
; i
++)
1207 where
= (s
->record_first
+ i
) % s
->record_count
;
1208 if (s
->record
[where
]->mid
== 0) continue;
1209 if (s
->record
[where
]->mid
>= start_id
) break;
1213 where
= ((s
->record_count
- (i
+ 1)) + s
->record_first
) % s
->record_count
;
1214 if (s
->record
[where
]->mid
== 0) continue;
1215 if (s
->record
[where
]->mid
<= start_id
) break;
1219 if (i
>= s
->record_count
)
1223 for (i
= 0; i
< query
->count
; i
++) asl_memory_record_free(s
, qp
[i
]);
1228 return ASL_STATUS_OK
;
1234 * loop through records
1236 for (i
= 0; i
< s
->record_count
; i
++)
1238 status
= ASL_STATUS_INVALID_ID
;
1239 if (s
->record
[where
]->mid
!= 0) status
= asl_core_check_access(s
->record
[where
]->ruid
, s
->record
[where
]->rgid
, ruid
, rgid
, s
->record
[where
]->flags
);
1241 if ((status
== ASL_STATUS_OK
) && (uuid_str
!= NULL
))
1243 if (s
->record
[where
]->sender_mach_uuid
== NULL
) status
= ASL_STATUS_INVALID_ID
;
1244 else if (strcmp(s
->record
[where
]->sender_mach_uuid
->str
, uuid_str
) != 0) status
= ASL_STATUS_INVALID_ID
;
1247 if (status
!= ASL_STATUS_OK
)
1252 if (where
>= s
->record_count
) where
= 0;
1256 if (where
== 0) where
= s
->record_count
- 1;
1260 if (where
== s
->record_first
) break;
1264 s
->record
[where
]->flags
&= ASL_MSG_FLAG_SEARCH_CLEAR
;
1265 *last_id
= s
->record
[where
]->mid
;
1272 for (j
= 0; (j
< query
->count
) && (did_match
== 0); j
++)
1274 if (qtype
[j
] == ASL_QUERY_MATCH_TRUE
)
1278 else if (qtype
[j
] == ASL_QUERY_MATCH_FALSE
)
1282 else if (qtype
[j
] == ASL_QUERY_MATCH_SLOW
)
1284 did_match
= asl_memory_slow_match(s
, s
->record
[where
], (aslmsg
)query
->msg
[j
]);
1288 did_match
= asl_memory_fast_match(s
, s
->record
[where
], qtype
[j
], qp
[j
]);
1295 s
->record
[where
]->flags
|= ASL_MSG_FLAG_SEARCH_MATCH
;
1297 if ((count
!= 0) && (rescount
>= count
)) break;
1303 if (where
>= s
->record_count
) where
= 0;
1307 if (where
== 0) where
= s
->record_count
- 1;
1311 if (where
== s
->record_first
) break;
1316 for (i
= 0; i
< query
->count
; i
++) asl_memory_record_free(s
, qp
[i
]);
1322 if (rescount
== 0) return ASL_STATUS_OK
;
1324 *res
= (asl_msg_list_t
*)calloc(1, sizeof(asl_msg_list_t
));
1325 if (*res
== NULL
) return ASL_STATUS_NO_MEMORY
;
1327 (*res
)->count
= rescount
;
1329 (*res
)->msg
= (asl_msg_t
**)calloc(rescount
, sizeof(asl_msg_t
*));
1330 if ((*res
)->msg
== NULL
)
1334 return ASL_STATUS_NO_MEMORY
;
1340 if (s
->record
[where
]->flags
& ASL_MSG_FLAG_SEARCH_MATCH
)
1342 s
->record
[where
]->flags
&= ASL_MSG_FLAG_SEARCH_CLEAR
;
1344 status
= asl_memory_message_decode(s
, s
->record
[where
], &m
);
1345 if (status
!= ASL_STATUS_OK
)
1347 aslresponse_free(*res
);
1352 (*res
)->msg
[(*res
)->curr
++] = (asl_msg_t
*)m
;
1353 if ((*res
)->curr
== rescount
) break;
1359 if (where
>= s
->record_count
) where
= 0;
1363 if (where
== 0) where
= s
->record_count
- 1;
1367 if (where
== s
->record_first
) break;
1371 return ASL_STATUS_OK
;
1375 asl_memory_match(asl_memory_t
*s
, aslresponse query
, aslresponse
*res
, uint64_t *last_id
, uint64_t start_id
, uint32_t count
, int32_t direction
, int32_t ruid
, int32_t rgid
)
1377 return asl_memory_match_restricted_uuid(s
, query
, res
, last_id
, start_id
, count
, direction
, ruid
, rgid
, NULL
);