]> git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/klog_in.c
syslog-69.0.3.tar.gz
[apple/syslog.git] / syslogd.tproj / klog_in.c
1 /*
2 * Copyright (c) 2004-2008 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 <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"
40 #define MAXLINE 4096
41
42 static int kx = 0;
43 static char kline[MAXLINE + 1];
44
45 asl_msg_t *
46 klog_in_acceptmsg(int fd)
47 {
48 int n;
49 char c;
50
51 n = read(fd, &c, 1);
52
53 while ((n == 1) && (c != '\n'))
54 {
55 if (kx < MAXLINE) kline[kx++] = c;
56 n = read(fd, &c, 1);
57 }
58
59 if (kx == 0) return NULL;
60
61 n = kx - 1;
62 kline[kx] = '\0';
63 kx = 0;
64
65 return asl_input_parse(kline, n, NULL, 1);
66 }
67
68 int
69 klog_in_init(void)
70 {
71 asldebug("%s: init\n", MY_ID);
72 if (global.kfd >= 0) return global.kfd;
73
74 global.kfd = open(_PATH_KLOG, O_RDONLY, 0);
75 if (global.kfd < 0)
76 {
77 asldebug("%s: couldn't open %s: %s\n", MY_ID, _PATH_KLOG, strerror(errno));
78 return -1;
79 }
80
81 if (fcntl(global.kfd, F_SETFL, O_NONBLOCK) < 0)
82 {
83 close(global.kfd);
84 global.kfd = -1;
85 asldebug("%s: couldn't set O_NONBLOCK for fd %d (%s): %s\n", MY_ID, global.kfd, _PATH_KLOG, strerror(errno));
86 return -1;
87 }
88
89 return aslevent_addfd(global.kfd, ADDFD_FLAGS_LOCAL, klog_in_acceptmsg, NULL, NULL);
90 }
91
92 int
93 klog_in_reset(void)
94 {
95 return 0;
96 }
97
98 int
99 klog_in_close(void)
100 {
101 if (global.kfd < 0) return 1;
102
103 close(global.kfd);
104 global.kfd = -1;
105
106 return 0;
107 }