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