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