]> git.saurik.com Git - apple/syslog.git/blame - syslogd.tproj/klog_in.c
syslog-100.2.tar.gz
[apple/syslog.git] / syslogd.tproj / klog_in.c
CommitLineData
b16a592a 1/*
57b0aad2 2 * Copyright (c) 2004-2008 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"
40#define MAXLINE 4096
41
b16a592a
A
42static int kx = 0;
43static char kline[MAXLINE + 1];
44
45asl_msg_t *
46klog_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
5dd30d76 65 return asl_input_parse(kline, n, NULL, 1);
b16a592a
A
66}
67
68int
69klog_in_init(void)
70{
71 asldebug("%s: init\n", MY_ID);
57b0aad2 72 if (global.kfd >= 0) return global.kfd;
b16a592a 73
57b0aad2
A
74 global.kfd = open(_PATH_KLOG, O_RDONLY, 0);
75 if (global.kfd < 0)
b16a592a
A
76 {
77 asldebug("%s: couldn't open %s: %s\n", MY_ID, _PATH_KLOG, strerror(errno));
78 return -1;
79 }
80
57b0aad2 81 if (fcntl(global.kfd, F_SETFL, O_NONBLOCK) < 0)
b16a592a 82 {
57b0aad2
A
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));
b16a592a
A
86 return -1;
87 }
88
c4fdb7d1 89 return aslevent_addfd(SOURCE_KERN, global.kfd, ADDFD_FLAGS_LOCAL, klog_in_acceptmsg, NULL, NULL);
b16a592a
A
90}
91
92int
93klog_in_reset(void)
94{
95 return 0;
96}
97
98int
99klog_in_close(void)
100{
57b0aad2 101 if (global.kfd < 0) return 1;
b16a592a 102
57b0aad2
A
103 close(global.kfd);
104 global.kfd = -1;
b16a592a
A
105
106 return 0;
107}