]> git.saurik.com Git - apple/syslog.git/blob - libsystem_asl.tproj/src/asl_client.c
syslog-349.1.1.tar.gz
[apple/syslog.git] / libsystem_asl.tproj / src / asl_client.c
1 /*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <string.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <unistd.h>
30 #include <stdarg.h>
31 #include <syslog.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <time.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <libkern/OSAtomic.h>
38 #include <crt_externs.h>
39 #include <asl.h>
40 #include <asl_private.h>
41 #include <asl_ipc.h>
42 #include <asl_core.h>
43 #include <asl_client.h>
44
45 #define PUBLIC_OPT_MASK 0x000000ff
46
47 /* private asl_file SPI */
48 __private_extern__ ASL_STATUS asl_file_open_write_fd(int descriptor, asl_file_t **s);
49
50 /* private asl SPI */
51 __private_extern__ ASL_STATUS asl_client_internal_send(asl_object_t client, asl_object_t msg, void *addr);
52
53 #pragma mark -
54 #pragma mark asl_client_t
55
56 static void
57 _asl_client_free_internal(asl_client_t *client)
58 {
59 uint32_t i;
60
61 if (client == NULL) return;
62
63 if (client->kvdict != NULL) asl_msg_release(client->kvdict);
64 client->kvdict = NULL;
65
66 if (client->aslfile != NULL) asl_file_close(client->aslfile);
67 client->aslfile = NULL;
68
69 for (i = 0; i < client->out_count; i++)
70 {
71 free(client->out_list[i].mfmt);
72 free(client->out_list[i].tfmt);
73 }
74
75 free(client->out_list);
76 client->out_list = NULL;
77
78 free(client);
79 }
80
81 asl_client_t *
82 asl_client_open(const char *ident, const char *facility, uint32_t opts)
83 {
84 asl_client_t *client = (asl_client_t *)calloc(1, sizeof(asl_client_t));
85 if (client == NULL)
86 {
87 errno = ENOMEM;
88 return NULL;
89 }
90
91 client->asl_type = ASL_TYPE_CLIENT;
92 client->refcount = 1;
93
94 client->kvdict = asl_msg_new(ASL_TYPE_MSG);
95 if (client->kvdict == NULL)
96 {
97 asl_client_release(client);
98 errno = ENOMEM;
99 return NULL;
100 }
101
102 client->options = opts & PUBLIC_OPT_MASK;
103
104 client->pid = getpid();
105 client->uid = getuid();
106 client->gid = getgid();
107
108 if (ident != NULL)
109 {
110 asl_msg_set_key_val(client->kvdict, ASL_KEY_SENDER, ident);
111 }
112 else
113 {
114 char *name = *(*_NSGetArgv());
115 if (name != NULL)
116 {
117 char *x = strrchr(name, '/');
118 if (x != NULL) x++;
119 else x = name;
120 asl_msg_set_key_val(client->kvdict, ASL_KEY_SENDER, x);
121 }
122 }
123
124 if (facility != NULL)
125 {
126 asl_msg_set_key_val(client->kvdict, ASL_KEY_FACILITY, facility);
127 }
128 else if (client->uid == 0)
129 {
130 asl_msg_set_key_val(client->kvdict, ASL_KEY_FACILITY, asl_syslog_faciliy_num_to_name(LOG_DAEMON));
131 }
132 else
133 {
134 asl_msg_set_key_val(client->kvdict, ASL_KEY_FACILITY, asl_syslog_faciliy_num_to_name(LOG_USER));
135 }
136
137 client->filter = ASL_FILTER_MASK_UPTO(ASL_LEVEL_NOTICE);
138
139 client->filter |= EVAL_ACTIVE;
140 if (!(opts & ASL_OPT_SHIM_NO_ASL)) client->filter |= EVAL_SEND_ASL;
141 if (!(opts & ASL_OPT_SHIM_NO_TRACE)) client->filter |= EVAL_SEND_TRACE;
142
143 if (client->options & ASL_OPT_STDERR)
144 {
145 /* only add stderr if it is valid */
146 if (fcntl(STDERR_FILENO, F_GETFD) >= 0)
147 {
148 asl_client_add_output_file(client, fileno(stderr), ASL_MSG_FMT_STD, ASL_TIME_FMT_LCL, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG), ASL_ENCODE_SAFE);
149 }
150 else
151 {
152 /* stderr has been closed, ignore ASL_OPT_STDERR flag */
153 client->options &= ~ASL_OPT_STDERR;
154 }
155 }
156
157 return client;
158 }
159
160 asl_client_t *
161 asl_client_open_from_file(int descriptor, const char *ident, const char *facility)
162 {
163 uint32_t status;
164 asl_client_t *client = asl_client_open(ident, facility, 0);
165 if (client == NULL) return NULL;
166
167 client->filter = ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG);
168 client->filter |= (EVAL_ACTIVE | EVAL_SEND_ASL);
169
170 status = asl_file_open_write_fd(descriptor, &(client->aslfile));
171 if (status != ASL_STATUS_OK)
172 {
173 _asl_client_free_internal(client);
174 return NULL;
175 }
176
177 client->aslfileid = 1;
178
179 return client;
180 }
181
182 asl_client_t *
183 asl_client_retain(asl_client_t *client)
184 {
185 if (client == NULL) return NULL;
186 asl_retain((asl_object_t)client);
187 return client;
188 }
189
190 void
191 asl_client_release(asl_client_t *client)
192 {
193 if (client == NULL) return;
194 asl_release((asl_object_t)client);
195 }
196
197 #pragma mark -
198 #pragma mark database access
199
200 ASL_STATUS
201 asl_client_send(asl_client_t *client, asl_msg_t *msg)
202 {
203 return asl_client_internal_send((asl_object_t)client, (asl_object_t)msg, __builtin_return_address(0));
204 }
205
206 static asl_msg_list_t *
207 _do_server_match(asl_msg_list_t *qlist, size_t *last, size_t start, size_t count, uint32_t duration, int dir)
208 {
209 char *str, *res = NULL;
210 uint32_t len, reslen, status;
211 uint64_t last64, start64, count64;
212 kern_return_t kstatus;
213 asl_msg_list_t *out;
214 caddr_t vmstr;
215 mach_port_t asl_server_port = asl_core_get_service_port(0);
216
217 if (asl_server_port == MACH_PORT_NULL) return NULL;
218
219 str = NULL;
220 if (qlist == NULL)
221 {
222 asprintf(&str, "0\n");
223 len = 3;
224 }
225 else
226 {
227 str = asl_msg_list_to_string(qlist, &len);
228 }
229
230 if (str == NULL) return NULL;
231
232 kstatus = vm_allocate(mach_task_self(), (vm_address_t *)&vmstr, len, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_ASL));
233 if (kstatus != KERN_SUCCESS) return NULL;
234
235 memmove(vmstr, str, len);
236 free(str);
237
238 last64 = 0;
239 start64 = start;
240 count64 = count;
241
242 kstatus = _asl_server_match(asl_server_port, vmstr, len, start64, count64, duration, dir, (caddr_t *)&res, &reslen, &last64, (int *)&status);
243 *last = last64;
244
245 out = asl_msg_list_from_string(res);
246 vm_deallocate(mach_task_self(), (vm_address_t)res, reslen);
247
248 return out;
249 }
250
251 static asl_msg_list_t *
252 _do_server_search(asl_msg_t *q)
253 {
254 asl_msg_list_t *out;
255 char *qstr, *str, *res = NULL;
256 uint32_t len, reslen = 0, status = ASL_STATUS_OK;
257 uint64_t cmax = 0;
258 kern_return_t kstatus;
259 caddr_t vmstr;
260 mach_port_t asl_server_port = asl_core_get_service_port(0);
261
262 if (asl_server_port == MACH_PORT_NULL) return NULL;
263
264 len = 0;
265 qstr = asl_msg_to_string(q, &len);
266
267 str = NULL;
268 if (qstr == NULL)
269 {
270 asprintf(&str, "0\n");
271 len = 3;
272 }
273 else
274 {
275 asprintf(&str, "1\n%s\n", qstr);
276 len += 3;
277 free(qstr);
278 }
279
280 if (str == NULL) return NULL;
281
282 kstatus = vm_allocate(mach_task_self(), (vm_address_t *)&vmstr, len, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_ASL));
283 if (kstatus != KERN_SUCCESS) return NULL;
284
285 memmove(vmstr, str, len);
286 free(str);
287
288 kstatus = _asl_server_query_2(asl_server_port, vmstr, len, 0, 0, 0, (caddr_t *)&res, &reslen, &cmax, (int *)&status);
289 if (kstatus != KERN_SUCCESS) return NULL;
290
291 out = asl_msg_list_from_string(res);
292 vm_deallocate(mach_task_self(), (vm_address_t)res, reslen);
293
294 return out;
295 }
296
297 static asl_msg_list_t *
298 _do_store_match(asl_msg_list_t *qlist, size_t *last, size_t start, size_t count, uint32_t duration, int32_t direction)
299 {
300 asl_msg_list_t *out;
301 uint32_t status;
302 uint64_t l64 = 0, s64;
303 asl_store_t *store = NULL;
304
305 uint32_t len;
306 char *str = asl_msg_list_to_string(qlist, &len);
307 free(str);
308
309 status = asl_store_open_read(NULL, &store);
310 if (status != 0) return NULL;
311 if (store == NULL) return NULL;
312
313 s64 = start;
314 out = asl_store_match(store, qlist, &l64, s64, count, duration, direction);
315 *last = l64;
316
317 asl_store_close(store);
318
319 return out;
320 }
321
322 static asl_msg_list_t *
323 _do_store_search(asl_msg_t *query)
324 {
325 asl_msg_list_t *out, *qlist = NULL;
326 uint32_t status;
327 uint16_t op;
328 uint64_t last = 0, start = 0;
329 asl_store_t *store = NULL;
330 const char *val = NULL;
331
332 /* check for "ASLMessageId >[=] n" and set start_id */
333 status = asl_msg_lookup(query, ASL_KEY_MSG_ID, &val, &op);
334 if ((status == 0) && (val != NULL) && (op & ASL_QUERY_OP_GREATER))
335 {
336 if (op & ASL_QUERY_OP_EQUAL) start = atoll(val);
337 else start = atoll(val) + 1;
338 }
339
340 status = asl_store_open_read(NULL, &store);
341 if (status != 0) return NULL;
342 if (store == NULL) return NULL;
343
344 if (query != NULL)
345 {
346 qlist = asl_msg_list_new();
347 asl_msg_list_append(qlist, query);
348 }
349
350 out = asl_store_match(store, qlist, &last, start, 0, 0, 1);
351 asl_store_close(store);
352
353 asl_msg_list_release(qlist);
354 return out;
355 }
356
357 asl_msg_list_t *
358 asl_client_match(asl_client_t *client, asl_msg_list_t *qlist, size_t *last, size_t start, size_t count, uint32_t duration, int32_t direction)
359 {
360 if (asl_store_location() == ASL_STORE_LOCATION_FILE) return _do_store_match(qlist, last, start, count, duration, direction);
361 return _do_server_match(qlist, last, start, count, duration, direction);
362 }
363
364 asl_msg_list_t *
365 asl_client_search(asl_client_t *client, asl_msg_t *query)
366 {
367 if (asl_store_location() == ASL_STORE_LOCATION_FILE) return _do_store_search(query);
368 return _do_server_search(query);
369 }
370
371
372 #pragma mark -
373 #pragma mark output control
374
375 /*
376 * Returns last filter value, or -1 on error.
377 * Note that this allows ASL_FILTER_MASK_TUNNEL (0x100) to be set.
378 * That is SPI that's used by some clients.
379 */
380 int
381 asl_client_set_filter(asl_client_t *client, int filter)
382 {
383 if (client == NULL) return -1;
384
385 uint32_t allbits = client->filter;
386 int last = allbits & (~EVAL_ACTION_MASK);
387 client->filter = (allbits & EVAL_ACTION_MASK) | (filter & (~EVAL_ACTION_MASK));
388 return last;
389 }
390
391 /* SPI */
392 uint32_t
393 asl_client_set_control(asl_client_t *client, uint32_t filter)
394 {
395 if (client == NULL) return UINT32_MAX;
396
397 uint32_t last = client->filter;
398 client->filter = filter;
399 return last;
400 }
401
402 uint32_t
403 asl_client_get_control(asl_client_t *client)
404 {
405 if (client == NULL) return UINT32_MAX;
406 return client->filter;
407 }
408
409 ASL_STATUS
410 asl_client_add_output_file(asl_client_t *client, int descriptor, const char *mfmt, const char *tfmt, int filter, int text_encoding)
411 {
412 uint32_t i;
413
414 if (client == NULL) return ASL_STATUS_FAILED;
415
416 for (i = 0; i < client->out_count; i++)
417 {
418 if (client->out_list[i].fd == descriptor)
419 {
420 /* update message format, time format, filter, and text encoding */
421 free(client->out_list[i].mfmt);
422 client->out_list[i].mfmt = NULL;
423 if (mfmt != NULL) client->out_list[i].mfmt = strdup(mfmt);
424
425 free(client->out_list[i].tfmt);
426 client->out_list[i].tfmt = NULL;
427 if (tfmt != NULL) client->out_list[i].tfmt = strdup(tfmt);
428
429 client->out_list[i].encoding = text_encoding;
430 client->out_list[i].filter = filter;
431
432 return ASL_STATUS_OK;
433 }
434 }
435
436 if (client->out_count == 0) client->out_list = NULL;
437 client->out_list = (asl_out_file_t *)reallocf(client->out_list, (1 + client->out_count) * sizeof(asl_out_file_t));
438
439 if (client->out_list == NULL) return ASL_STATUS_FAILED;
440
441 client->out_list[client->out_count].fd = descriptor;
442 client->out_list[client->out_count].encoding = text_encoding;
443 client->out_list[client->out_count].filter = filter;
444 client->out_list[client->out_count].mfmt = NULL;
445 if (mfmt != NULL) client->out_list[client->out_count].mfmt = strdup(mfmt);
446 client->out_list[client->out_count].tfmt = NULL;
447 if (tfmt != NULL) client->out_list[client->out_count].tfmt = strdup(tfmt);
448
449 client->out_count++;
450
451 return ASL_STATUS_OK;
452 }
453
454 /* returns last filter value, or -1 on error */
455 int
456 asl_client_set_output_file_filter(asl_client_t *client, int descriptor, int filter)
457 {
458 uint32_t i;
459 int last = 0;
460
461 if (client == NULL) return -1;
462
463 for (i = 0; i < client->out_count; i++)
464 {
465 if (client->out_list[i].fd == descriptor)
466 {
467 /* update filter */
468 last = client->out_list[i].filter;
469 client->out_list[i].filter = filter;
470 break;
471 }
472 }
473
474 return last;
475 }
476
477 ASL_STATUS
478 asl_client_remove_output_file(asl_client_t *client, int descriptor)
479 {
480 uint32_t i;
481 int x;
482
483 if (client == NULL) return ASL_STATUS_INVALID_ARG;
484
485 if (client->out_count == 0) return ASL_STATUS_OK;
486
487 x = -1;
488 for (i = 0; i < client->out_count; i++)
489 {
490 if (client->out_list[i].fd == descriptor)
491 {
492 x = i;
493 break;
494 }
495 }
496
497 if (x == -1) return ASL_STATUS_OK;
498
499 free(client->out_list[x].mfmt);
500 free(client->out_list[x].tfmt);
501
502 for (i = x + 1; i < client->out_count; i++, x++)
503 {
504 client->out_list[x] = client->out_list[i];
505 }
506
507 client->out_count--;
508
509 if (client->out_count == 0)
510 {
511 free(client->out_list);
512 client->out_list = NULL;
513 }
514 else
515 {
516 client->out_list = (asl_out_file_t *)reallocf(client->out_list, client->out_count * sizeof(asl_out_file_t));
517
518 if (client->out_list == NULL)
519 {
520 client->out_count = 0;
521 return ASL_STATUS_FAILED;
522 }
523 }
524
525 return ASL_STATUS_OK;
526 }
527
528 #pragma mark -
529 #pragma mark dictionary access
530
531 asl_msg_t *
532 asl_client_kvdict(asl_client_t *client)
533 {
534 if (client == NULL) return NULL;
535 return client->kvdict;
536 }
537
538 #pragma mark -
539 #pragma mark asl_object support
540
541 static void
542 _jump_dealloc(asl_object_private_t *obj)
543 {
544 _asl_client_free_internal((asl_client_t *)obj);
545 }
546
547 static void
548 _jump_append(asl_object_private_t *obj, asl_object_private_t *newobj, void *addr)
549 {
550 int type = asl_get_type((asl_object_t)newobj);
551
552 if (type == ASL_TYPE_LIST)
553 {
554 asl_msg_t *msg;
555 asl_msg_list_reset_iteration((asl_msg_list_t *)newobj, 0);
556 while (NULL != (msg = asl_msg_list_next((asl_msg_list_t *)newobj)))
557 {
558 if (asl_client_internal_send((asl_object_t)obj, (asl_object_t)msg, addr) != ASL_STATUS_OK) return;
559 }
560 }
561 else if ((type == ASL_TYPE_MSG) || (type == ASL_TYPE_QUERY))
562 {
563 asl_client_internal_send((asl_object_t)obj, (asl_object_t)newobj, addr);
564 }
565 }
566
567 static asl_object_private_t *
568 _jump_search(asl_object_private_t *obj, asl_object_private_t *query)
569 {
570 int type = asl_get_type((asl_object_t)query);
571 if ((query != NULL) && (type != ASL_TYPE_MSG) && (type != ASL_TYPE_QUERY)) return NULL;
572
573 return (asl_object_private_t *)asl_client_search((asl_client_t *)obj, (asl_msg_t *)query);
574 }
575
576 static asl_object_private_t *
577 _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)
578 {
579 asl_msg_list_t *out = NULL;
580 int type = asl_get_type((asl_object_t)qlist);
581
582 if ((qlist != NULL) && (type != ASL_TYPE_LIST)) return NULL;
583
584 out = asl_client_match((asl_client_t *)obj, (asl_msg_list_t *)qlist, last, start, count, duration, dir);
585 return (asl_object_private_t *)out;
586 }
587
588 __private_extern__ const asl_jump_table_t *
589 asl_client_jump_table()
590 {
591 static const asl_jump_table_t jump =
592 {
593 .alloc = NULL,
594 .dealloc = &_jump_dealloc,
595 .set_key_val_op = NULL,
596 .unset_key = NULL,
597 .get_val_op_for_key = NULL,
598 .get_key_val_op_at_index = NULL,
599 .count = NULL,
600 .next = NULL,
601 .prev = NULL,
602 .get_object_at_index = NULL,
603 .set_iteration_index = NULL,
604 .remove_object_at_index = NULL,
605 .append = &_jump_append,
606 .prepend = NULL,
607 .search = &_jump_search,
608 .match = &_jump_match
609 };
610
611 return &jump;
612 }
613