]>
Commit | Line | Data |
---|---|---|
b16a592a | 1 | /* |
db78b1bd | 2 | * Copyright (c) 2004-2011 Apple Inc. All rights reserved. |
b16a592a A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
5dd30d76 A |
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. | |
b16a592a A |
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, | |
5dd30d76 A |
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. | |
b16a592a A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
81582353 A |
24 | #include <TargetConditionals.h> |
25 | ||
26 | #if TARGET_IPHONE_SIMULATOR | |
27 | struct _not_empty; | |
28 | #else | |
29 | ||
b16a592a A |
30 | #include <sys/types.h> |
31 | #include <sys/stat.h> | |
32 | #include <sys/socket.h> | |
33 | #include <sys/un.h> | |
34 | #include <sys/uio.h> | |
35 | #include <stdio.h> | |
36 | #include <stdlib.h> | |
37 | #include <string.h> | |
38 | #include <fcntl.h> | |
39 | #include <errno.h> | |
40 | #include <unistd.h> | |
41 | #include "daemon.h" | |
42 | ||
43 | #define forever for(;;) | |
44 | ||
45 | #define MY_ID "klog_in" | |
db78b1bd | 46 | #define BUFF_SIZE 4096 |
b16a592a | 47 | |
db78b1bd A |
48 | static char inbuf[BUFF_SIZE]; |
49 | static int bx; | |
50 | static int kfd = -1; | |
51 | static dispatch_source_t in_src; | |
52 | static dispatch_queue_t in_queue; | |
b16a592a | 53 | |
db78b1bd A |
54 | void |
55 | klog_in_acceptdata(int fd) | |
b16a592a | 56 | { |
db78b1bd A |
57 | ssize_t len; |
58 | uint32_t i; | |
59 | char *p, *q; | |
f3df4c03 | 60 | asl_msg_t *m; |
b16a592a | 61 | |
db78b1bd A |
62 | len = read(fd, inbuf + bx, BUFF_SIZE - bx); |
63 | if (len <= 0) return; | |
b16a592a | 64 | |
db78b1bd A |
65 | p = inbuf; |
66 | q = p + bx; | |
67 | ||
68 | for (i = 0; i < len; i++, q++) | |
b16a592a | 69 | { |
db78b1bd A |
70 | if (*q == '\n') |
71 | { | |
72 | *q = '\0'; | |
73 | m = asl_input_parse(p, q - p, NULL, SOURCE_KERN); | |
81582353 | 74 | process_message(m, SOURCE_KERN); |
db78b1bd A |
75 | p = q + 1; |
76 | } | |
b16a592a A |
77 | } |
78 | ||
db78b1bd A |
79 | if (p != inbuf) |
80 | { | |
81 | memmove(inbuf, p, BUFF_SIZE - bx - 1); | |
82 | bx = q - p; | |
83 | } | |
b16a592a A |
84 | } |
85 | ||
86 | int | |
db78b1bd | 87 | klog_in_init() |
b16a592a | 88 | { |
db78b1bd A |
89 | static dispatch_once_t once; |
90 | ||
91 | dispatch_once(&once, ^{ | |
92 | in_queue = dispatch_queue_create(MY_ID, NULL); | |
93 | }); | |
94 | ||
b16a592a | 95 | asldebug("%s: init\n", MY_ID); |
db78b1bd | 96 | if (kfd >= 0) return 0; |
b16a592a | 97 | |
db78b1bd A |
98 | kfd = open(_PATH_KLOG, O_RDONLY, 0); |
99 | if (kfd < 0) | |
b16a592a A |
100 | { |
101 | asldebug("%s: couldn't open %s: %s\n", MY_ID, _PATH_KLOG, strerror(errno)); | |
102 | return -1; | |
103 | } | |
104 | ||
db78b1bd | 105 | if (fcntl(kfd, F_SETFL, O_NONBLOCK) < 0) |
b16a592a | 106 | { |
db78b1bd A |
107 | close(kfd); |
108 | kfd = -1; | |
109 | asldebug("%s: couldn't set O_NONBLOCK for fd %d (%s): %s\n", MY_ID, kfd, _PATH_KLOG, strerror(errno)); | |
b16a592a A |
110 | return -1; |
111 | } | |
112 | ||
db78b1bd A |
113 | in_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)kfd, 0, in_queue); |
114 | dispatch_source_set_event_handler(in_src, ^{ klog_in_acceptdata(kfd); }); | |
115 | ||
116 | dispatch_resume(in_src); | |
117 | return 0; | |
b16a592a A |
118 | } |
119 | ||
b16a592a A |
120 | int |
121 | klog_in_close(void) | |
122 | { | |
db78b1bd A |
123 | if (kfd < 0) return 1; |
124 | ||
125 | dispatch_source_cancel(in_src); | |
126 | dispatch_release(in_src); | |
127 | in_src = NULL; | |
b16a592a | 128 | |
db78b1bd A |
129 | close(kfd); |
130 | kfd = -1; | |
b16a592a A |
131 | |
132 | return 0; | |
133 | } | |
a83ff38a A |
134 | |
135 | int | |
136 | klog_in_reset(void) | |
137 | { | |
138 | return 0; | |
139 | } | |
81582353 A |
140 | |
141 | #endif /* !TARGET_IPHONE_SIMULATOR */ |