/*
- * Copyright (c) 2007-2008 Apple Inc. All rights reserved.
+ * Copyright (c) 2007-2010 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
*/
#include <asl_core.h>
-#include <asl_mini_memory.h>
-#include <unistd.h>
+#include "asl_mini_memory.h"
+#include <unistd.h>x
#include <stdlib.h>
#include <stdio.h>
#include <sys/errno.h>
#include <sys/types.h>
#include <time.h>
#include <asl_private.h>
+#include <asl_msg.h>
#define DEFAULT_MAX_RECORDS 256
#define MEM_STRING_HEADER_SIZE 8
if (s == NULL) return ASL_STATUS_INVALID_STORE;
if (msg == NULL) return ASL_STATUS_INVALID_ARG;
- out = (aslmsg)calloc(1, sizeof(asl_msg_t));
+ out = asl_new(ASL_TYPE_MSG);
if (out == NULL) return ASL_STATUS_NO_MEMORY;
size = sizeof(asl_mini_memory_t);
* Creates and caches strings.
*/
static uint32_t
-asl_mini_memory_message_encode(asl_mini_memory_t *s, asl_msg_t *msg, mini_mem_record_t *r)
+asl_mini_memory_message_encode(asl_mini_memory_t *s, aslmsg msg)
{
- uint32_t i;
+ uint32_t x;
mini_mem_string_t *k, *v;
+ mini_mem_record_t *r;
+ const char *key, *val;
if (s == NULL) return ASL_STATUS_INVALID_STORE;
+ if (s->buffer_record == NULL) return ASL_STATUS_INVALID_STORE;
if (msg == NULL) return ASL_STATUS_INVALID_MESSAGE;
- if (r == NULL) return ASL_STATUS_INVALID_ARG;
+
+ r = s->buffer_record;
memset(r, 0, sizeof(mini_mem_record_t));
r->pid = -1;
r->time = (uint64_t)-1;
- for (i = 0; i < msg->count; i++)
+ key = NULL;
+ val = NULL;
+
+ 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))
{
- if (msg->key[i] == NULL) continue;
+ if (key == NULL) continue;
- else if (!strcmp(msg->key[i], ASL_KEY_TIME))
+ else if (!strcmp(key, ASL_KEY_TIME))
{
- if (msg->val[i] != NULL) r->time = asl_parse_time(msg->val[i]);
+ if (val != NULL) r->time = asl_parse_time(val);
}
- else if (!strcmp(msg->key[i], ASL_KEY_SENDER))
+ else if (!strcmp(key, ASL_KEY_SENDER))
{
- if (msg->val[i] != NULL) r->sender = asl_mini_memory_string_retain(s, msg->val[i], 1);
+ if (val != NULL) r->sender = asl_mini_memory_string_retain(s, val, 1);
}
- else if (!strcmp(msg->key[i], ASL_KEY_PID))
+ else if (!strcmp(key, ASL_KEY_PID))
{
- if (msg->val[i] != NULL) r->pid = atoi(msg->val[i]);
+ if (val != NULL) r->pid = atoi(val);
}
- else if (!strcmp(msg->key[i], ASL_KEY_LEVEL))
+ else if (!strcmp(key, ASL_KEY_LEVEL))
{
- if (msg->val[i] != NULL) r->level = atoi(msg->val[i]);
+ if (val != NULL) r->level = atoi(val);
}
- else if (!strcmp(msg->key[i], ASL_KEY_MSG))
+ else if (!strcmp(key, ASL_KEY_MSG))
{
- if (msg->val[i] != NULL) r->message = asl_mini_memory_string_retain(s, msg->val[i], 1);
+ if (val != NULL) r->message = asl_mini_memory_string_retain(s, val, 1);
}
- else if (!strcmp(msg->key[i], ASL_KEY_FACILITY))
+ else if (!strcmp(key, ASL_KEY_FACILITY))
{
- if (msg->val[i] != NULL) r->facility = asl_mini_memory_string_retain(s, msg->val[i], 1);
+ if (val != NULL) r->facility = asl_mini_memory_string_retain(s, val, 1);
}
- else if (!strcmp(msg->key[i], ASL_KEY_MSG_ID))
+ else if (!strcmp(key, ASL_KEY_MSG_ID))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_TIME_NSEC))
+ else if (!strcmp(key, ASL_KEY_TIME_NSEC))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_HOST))
+ else if (!strcmp(key, ASL_KEY_HOST))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_REF_PID))
+ else if (!strcmp(key, ASL_KEY_REF_PID))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_REF_PROC))
+ else if (!strcmp(key, ASL_KEY_REF_PROC))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_SESSION))
+ else if (!strcmp(key, ASL_KEY_SESSION))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_UID))
+ else if (!strcmp(key, ASL_KEY_UID))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_GID))
+ else if (!strcmp(key, ASL_KEY_GID))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_READ_UID))
+ else if (!strcmp(key, ASL_KEY_READ_UID))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], ASL_KEY_READ_GID))
+ else if (!strcmp(key, ASL_KEY_READ_GID))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], CFLOG_LOCAL_TIME_KEY))
+ else if (!strcmp(key, CFLOG_LOCAL_TIME_KEY))
{
/* Ignore */
continue;
}
- else if (!strcmp(msg->key[i], CFLOG_THREAD_KEY))
+ else if (!strcmp(key, CFLOG_THREAD_KEY))
{
/* Ignore */
continue;
}
else
{
- k = asl_mini_memory_string_retain(s, msg->key[i], 1);
+ k = asl_mini_memory_string_retain(s, key, 1);
if (k == NULL) continue;
v = NULL;
- if (msg->val[i] != NULL) v = asl_mini_memory_string_retain(s, msg->val[i], 1);
+ if (val != NULL) v = asl_mini_memory_string_retain(s, val, 1);
if (r->kvcount == 0)
{
if (s->buffer_record == NULL) return ASL_STATUS_INVALID_STORE;
/* asl_mini_memory_message_encode creates and caches strings */
- status = asl_mini_memory_message_encode(s, msg, s->buffer_record);
+ status = asl_mini_memory_message_encode(s, msg);
if (status != ASL_STATUS_OK) return status;
s->buffer_record->mid = s->next_id;
* Decodes a record structure.
*/
static uint32_t
-asl_mini_memory_message_decode(asl_mini_memory_t *s, mini_mem_record_t *r, asl_msg_t **out)
+asl_mini_memory_message_decode(asl_mini_memory_t *s, mini_mem_record_t *r, aslmsg *out)
{
- uint32_t i, n;
- asl_msg_t *msg;
+ uint32_t i;
+ aslmsg msg;
+ char tmp[64];
+ const char *key, *val;
if (s == NULL) return ASL_STATUS_INVALID_STORE;
if (r == NULL) return ASL_STATUS_INVALID_ARG;
*out = NULL;
- msg = (asl_msg_t *)calloc(1, sizeof(asl_msg_t));
+ msg = asl_new(ASL_TYPE_MSG);
if (msg == NULL) return ASL_STATUS_NO_MEMORY;
- msg->type = ASL_TYPE_MSG;
- /* Level and Message ID are always set */
- msg->count = 2;
-
- if (r->time != (uint64_t)-1) msg->count++;
- if (r->sender != NULL) msg->count++;
- if (r->facility != NULL) msg->count++;
- if (r->pid != -1) msg->count++;
- if (r->message != NULL) msg->count++;
-
- msg->count += (r->kvcount / 2);
-
- msg->key = (char **)calloc(msg->count, sizeof(char *));
- if (msg->key == NULL)
- {
- free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- msg->val = (char **)calloc(msg->count, sizeof(char *));
- if (msg->val == NULL)
- {
- free(msg->key);
- free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- n = 0;
-
/* Message ID */
- msg->key[n] = strdup(ASL_KEY_MSG_ID);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- asprintf(&(msg->val[n]), "%lu", r->mid);
- if (msg->val[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
- n++;
+ snprintf(tmp, sizeof(tmp), "%u", r->mid);
+ asl_set(msg, ASL_KEY_MSG_ID, tmp);
/* Level */
- msg->key[n] = strdup(ASL_KEY_LEVEL);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- asprintf(&(msg->val[n]), "%u", r->level);
- if (msg->val[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
- n++;
+ snprintf(tmp, sizeof(tmp), "%u", r->level);
+ asl_set(msg, ASL_KEY_LEVEL, tmp);
/* Time */
if (r->time != (uint64_t)-1)
{
- msg->key[n] = strdup(ASL_KEY_TIME);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- asprintf(&(msg->val[n]), "%llu", r->time);
- if (msg->val[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
- n++;
+ snprintf(tmp, sizeof(tmp), "%llu", r->time);
+ asl_set(msg, ASL_KEY_TIME, tmp);
}
/* Sender */
if (r->sender != NULL)
{
- msg->key[n] = strdup(ASL_KEY_SENDER);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- msg->val[n] = strdup(r->sender->str);
- n++;
+ asl_set(msg, ASL_KEY_SENDER, r->sender->str);
}
/* Facility */
if (r->facility != NULL)
{
- msg->key[n] = strdup(ASL_KEY_FACILITY);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- msg->val[n] = strdup(r->facility->str);
- n++;
+ asl_set(msg, ASL_KEY_FACILITY, r->facility->str);
}
/* PID */
if (r->pid != -1)
{
- msg->key[n] = strdup(ASL_KEY_PID);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- asprintf(&(msg->val[n]), "%d", r->pid);
- if (msg->val[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
- n++;
+ snprintf(tmp, sizeof(tmp), "%d", r->pid);
+ asl_set(msg, ASL_KEY_PID, tmp);
}
/* Message */
if (r->message != NULL)
{
- msg->key[n] = strdup(ASL_KEY_MSG);
- if (msg->key[n] == NULL)
- {
- asl_free(msg);
- return ASL_STATUS_NO_MEMORY;
- }
-
- msg->val[n] = strdup(r->message->str);
- n++;
+ asl_set(msg, ASL_KEY_MSG, r->message->str);
}
/* Key - Value List */
for (i = 0; i < r->kvcount; i++)
{
- if ((r->kvlist[i] != NULL) && (r->kvlist[i]->str != NULL)) msg->key[n] = strdup(r->kvlist[i]->str);
+ key = NULL;
+ val = NULL;
+
+ if ((r->kvlist[i] != NULL) && (r->kvlist[i]->str != NULL)) key = r->kvlist[i]->str;
i++;
- if ((r->kvlist[i] != NULL) && (r->kvlist[i]->str != NULL)) msg->val[n] = strdup(r->kvlist[i]->str);
- n++;
+ if ((r->kvlist[i] != NULL) && (r->kvlist[i]->str != NULL)) val = r->kvlist[i]->str;
+
+ if (key != NULL) asl_set(msg, key, val);
}
*out = msg;
asl_mini_memory_query_to_record(asl_mini_memory_t *s, asl_msg_t *q, uint32_t *type)
{
mini_mem_record_t *out;
- uint32_t i, j;
- mini_mem_string_t *key, *val;
+ uint32_t i, x, op;
+ mini_mem_string_t *mkey, *mval;
+ const char *key, *val;
if (type == NULL) return NULL;
/* NULL query matches anything */
*type = ASL_QUERY_MATCH_TRUE;
if (q == NULL) return NULL;
- if (q->count == 0) return NULL;
+ if (asl_msg_count((asl_msg_t *)q) == 0) return NULL;
/* we can only do fast match on equality tests */
*type = ASL_QUERY_MATCH_SLOW;
- if (q->op != NULL)
+
+ 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))
{
- for (i = 0; i < q->count; i++) if (q->op[i] != ASL_QUERY_OP_EQUAL) return NULL;
+ if (op != ASL_QUERY_OP_EQUAL) return NULL;
}
out = (mini_mem_record_t *)calloc(1, sizeof(mini_mem_record_t));
return NULL;
}
- for (i = 0; i < q->count; i++)
+ 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))
{
- if (q->key[i] == NULL) continue;
+ if (key == NULL) continue;
- else if (!strcmp(q->key[i], ASL_KEY_MSG_ID))
+ else if (!strcmp(key, ASL_KEY_MSG_ID))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_MSG_ID)
{
}
*type |= ASL_QUERY_MATCH_MSG_ID;
- out->mid = atoll(q->val[i]);
+ out->mid = atoll(val);
}
- else if (!strcmp(q->key[i], ASL_KEY_TIME))
+ else if (!strcmp(key, ASL_KEY_TIME))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_TIME)
{
}
*type |= ASL_QUERY_MATCH_TIME;
- out->time = asl_parse_time(q->val[i]);
+ out->time = asl_parse_time(val);
}
- else if (!strcmp(q->key[i], ASL_KEY_LEVEL))
+ else if (!strcmp(key, ASL_KEY_LEVEL))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_LEVEL)
{
}
*type |= ASL_QUERY_MATCH_LEVEL;
- out->level = atoi(q->val[i]);
+ out->level = atoi(val);
}
- else if (!strcmp(q->key[i], ASL_KEY_PID))
+ else if (!strcmp(key, ASL_KEY_PID))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_PID)
{
}
*type |= ASL_QUERY_MATCH_PID;
- out->pid = atoi(q->val[i]);
+ out->pid = atoi(val);
}
- else if (!strcmp(q->key[i], ASL_KEY_SENDER))
+ else if (!strcmp(key, ASL_KEY_SENDER))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_SENDER)
{
}
*type |= ASL_QUERY_MATCH_SENDER;
- out->sender = asl_mini_memory_string_retain(s, q->val[i], 0);
+ out->sender = asl_mini_memory_string_retain(s, val, 0);
if (out->sender == NULL)
{
asl_mini_memory_record_free(s, out);
return NULL;
}
}
- else if (!strcmp(q->key[i], ASL_KEY_FACILITY))
+ else if (!strcmp(key, ASL_KEY_FACILITY))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_FACILITY)
{
}
*type |= ASL_QUERY_MATCH_FACILITY;
- out->facility = asl_mini_memory_string_retain(s, q->val[i], 0);
+ out->facility = asl_mini_memory_string_retain(s, val, 0);
if (out->facility == NULL)
{
asl_mini_memory_record_free(s, out);
return NULL;
}
}
- else if (!strcmp(q->key[i], ASL_KEY_MSG))
+ else if (!strcmp(key, ASL_KEY_MSG))
{
- if (q->val[i] == NULL) continue;
+ if (val == NULL) continue;
if (*type & ASL_QUERY_MATCH_MESSAGE)
{
}
*type |= ASL_QUERY_MATCH_MESSAGE;
- out->message = asl_mini_memory_string_retain(s, q->val[i], 0);
+ out->message = asl_mini_memory_string_retain(s, val, 0);
if (out->message == NULL)
{
asl_mini_memory_record_free(s, out);
}
else
{
- key = asl_mini_memory_string_retain(s, q->key[i], 0);
- if (key == NULL)
+ mkey = asl_mini_memory_string_retain(s, key, 0);
+ if (mkey == NULL)
{
asl_mini_memory_record_free(s, out);
*type = ASL_QUERY_MATCH_FALSE;
return NULL;
}
- for (j = 0; j < out->kvcount; j += 2)
+ for (i = 0; i < out->kvcount; i += 2)
{
- if (out->kvlist[j] == key)
+ if (out->kvlist[i] == mkey)
{
asl_mini_memory_record_free(s, out);
*type = ASL_QUERY_MATCH_SLOW;
}
}
- val = asl_mini_memory_string_retain(s, q->val[i], 0);
+ mval = asl_mini_memory_string_retain(s, val, 0);
if (out->kvcount == 0)
{
return NULL;
}
- out->kvlist[out->kvcount++] = key;
- out->kvlist[out->kvcount++] = val;
+ out->kvlist[out->kvcount++] = mkey;
+ out->kvlist[out->kvcount++] = mval;
}
}
}
static uint32_t
-asl_mini_memory_slow_match(asl_mini_memory_t *s, mini_mem_record_t *r, mini_mem_record_t *q, asl_msg_t *rawq)
+asl_mini_memory_slow_match(asl_mini_memory_t *s, mini_mem_record_t *r, mini_mem_record_t *q, aslmsg rawq)
{
- asl_msg_t *rawm;
+ aslmsg rawm;
uint32_t status;
rawm = NULL;
if (status != ASL_STATUS_OK) return 0;
status = 0;
- if (asl_msg_cmp(rawq, rawm) != 0) status = 1;
+ if (asl_msg_cmp((asl_msg_t *)rawq, (asl_msg_t *)rawm) != 0) status = 1;
asl_free(rawm);
return status;
}
{
uint32_t status, i, where, start, j, do_match, did_match, rescount, *qtype;
mini_mem_record_t **qp;
- asl_msg_t *m;
+ aslmsg m;
if (s == NULL) return ASL_STATUS_INVALID_STORE;
if (res == NULL) return ASL_STATUS_INVALID_ARG;
{
did_match = 1;
}
+ else if (qtype[j] == ASL_QUERY_MATCH_FALSE)
+ {
+ did_match = 0;
+ }
else if (qtype[j] == ASL_QUERY_MATCH_SLOW)
{
- did_match = asl_mini_memory_slow_match(s, s->record[where], qp[j], query->msg[j]);
+ did_match = asl_mini_memory_slow_match(s, s->record[where], qp[j], (aslmsg)query->msg[j]);
}
else
{
return status;
}
- (*res)->msg[(*res)->curr++] = m;
+ (*res)->msg[(*res)->curr++] = (asl_msg_t *)m;
if ((*res)->curr == rescount) break;
}