]> git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/klog_in.c
syslog-13.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 * "Portions Copyright (c) 2004 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <sys/uio.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <fcntl.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include "daemon.h"
37
38 #define forever for(;;)
39
40 #define MY_ID "klog_in"
41 #define MAXLINE 4096
42
43 static int kfd = -1;
44
45 static int kx = 0;
46 static char kline[MAXLINE + 1];
47
48 asl_msg_t *
49 klog_in_acceptmsg(int fd)
50 {
51 int n;
52 char c;
53
54 n = read(fd, &c, 1);
55
56 while ((n == 1) && (c != '\n'))
57 {
58 if (kx < MAXLINE) kline[kx++] = c;
59 n = read(fd, &c, 1);
60 }
61
62 if (kx == 0) return NULL;
63
64 n = kx - 1;
65 kline[kx] = '\0';
66 kx = 0;
67
68 return asl_syslog_input_convert(kline, n, NULL, 1);
69 }
70
71 int
72 klog_in_init(void)
73 {
74 asldebug("%s: init\n", MY_ID);
75 if (kfd >= 0) return kfd;
76
77 kfd = open(_PATH_KLOG, O_RDONLY, 0);
78 if (kfd < 0)
79 {
80 asldebug("%s: couldn't open %s: %s\n", MY_ID, _PATH_KLOG, strerror(errno));
81 return -1;
82 }
83
84 if (fcntl(kfd, F_SETFL, O_NONBLOCK) < 0)
85 {
86 close(kfd);
87 kfd = -1;
88 asldebug("%s: couldn't set O_NONBLOCK for fd %d (%s): %s\n", MY_ID, kfd, _PATH_KLOG, strerror(errno));
89 return -1;
90 }
91
92 return aslevent_addfd(kfd, klog_in_acceptmsg, NULL, NULL);
93 }
94
95 int
96 klog_in_reset(void)
97 {
98 return 0;
99 }
100
101 int
102 klog_in_close(void)
103 {
104 if (kfd < 0) return 1;
105
106 close(kfd);
107 kfd = -1;
108
109 return 0;
110 }