X-Git-Url: https://git.saurik.com/apple/syslog.git/blobdiff_plain/b16a592aee171655b85f8e45c0604c38494ab8ea..5222c21d179b0b291b379bab771199072df3ea35:/syslogd.tproj/klog_in.c diff --git a/syslogd.tproj/klog_in.c b/syslogd.tproj/klog_in.c index 5edd1d7..d048738 100644 --- a/syslogd.tproj/klog_in.c +++ b/syslogd.tproj/klog_in.c @@ -1,27 +1,32 @@ /* - * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2004-2011 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * - * "Portions Copyright (c) 2004 Apple Computer, Inc. All Rights - * Reserved. This file contains Original Code and/or Modifications of - * Original Code as defined in and that are subject to the Apple Public - * Source License Version 1.0 (the 'License'). You may not use this file - * except in compliance with the License. Please obtain a copy of the - * License at http://www.apple.com/publicsource and read it before using - * this file. + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License." + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ +#include + +#if TARGET_IPHONE_SIMULATOR +struct _not_empty; +#else + #include #include #include @@ -38,41 +43,57 @@ #define forever for(;;) #define MY_ID "klog_in" -#define MAXLINE 4096 +#define BUFF_SIZE 4096 +static char inbuf[BUFF_SIZE]; +static int bx; static int kfd = -1; +static dispatch_source_t in_src; +static dispatch_queue_t in_queue; -static int kx = 0; -static char kline[MAXLINE + 1]; - -asl_msg_t * -klog_in_acceptmsg(int fd) +void +klog_in_acceptdata(int fd) { - int n; - char c; + ssize_t len; + uint32_t i; + char *p, *q; + asl_msg_t *m; - n = read(fd, &c, 1); + len = read(fd, inbuf + bx, BUFF_SIZE - bx); + if (len <= 0) return; - while ((n == 1) && (c != '\n')) + p = inbuf; + q = p + bx; + + for (i = 0; i < len; i++, q++) { - if (kx < MAXLINE) kline[kx++] = c; - n = read(fd, &c, 1); + if (*q == '\n') + { + *q = '\0'; + m = asl_input_parse(p, q - p, NULL, SOURCE_KERN); + process_message(m, SOURCE_KERN); + p = q + 1; + } } - if (kx == 0) return NULL; - - n = kx - 1; - kline[kx] = '\0'; - kx = 0; - - return asl_syslog_input_convert(kline, n, NULL, 1); + if (p != inbuf) + { + memmove(inbuf, p, BUFF_SIZE - bx - 1); + bx = q - p; + } } int -klog_in_init(void) +klog_in_init() { + static dispatch_once_t once; + + dispatch_once(&once, ^{ + in_queue = dispatch_queue_create(MY_ID, NULL); + }); + asldebug("%s: init\n", MY_ID); - if (kfd >= 0) return kfd; + if (kfd >= 0) return 0; kfd = open(_PATH_KLOG, O_RDONLY, 0); if (kfd < 0) @@ -89,12 +110,10 @@ klog_in_init(void) return -1; } - return aslevent_addfd(kfd, klog_in_acceptmsg, NULL, NULL); -} + in_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)kfd, 0, in_queue); + dispatch_source_set_event_handler(in_src, ^{ klog_in_acceptdata(kfd); }); -int -klog_in_reset(void) -{ + dispatch_resume(in_src); return 0; } @@ -103,8 +122,20 @@ klog_in_close(void) { if (kfd < 0) return 1; + dispatch_source_cancel(in_src); + dispatch_release(in_src); + in_src = NULL; + close(kfd); kfd = -1; return 0; } + +int +klog_in_reset(void) +{ + return 0; +} + +#endif /* !TARGET_IPHONE_SIMULATOR */