]> git.saurik.com Git - apple/syslog.git/blame - syslogd.tproj/bsd_in.c
syslog-385.tar.gz
[apple/syslog.git] / syslogd.tproj / bsd_in.c
CommitLineData
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
81582353
A
24#include <TargetConditionals.h>
25
b16a592a
A
26#include <sys/types.h>
27#include <sys/stat.h>
28#include <sys/socket.h>
29#include <sys/fcntl.h>
30#include <sys/un.h>
31#include <sys/uio.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <errno.h>
37#include "daemon.h"
38
5dd30d76 39#define BSD_SOCKET_NAME "BSDSystemLogger"
b16a592a 40#define MY_ID "bsd_in"
5dd30d76 41#define MAXLINE 4096
b16a592a
A
42
43static int sock = -1;
db78b1bd
A
44static dispatch_source_t in_src;
45static dispatch_queue_t in_queue;
b16a592a 46
db78b1bd 47void
b16a592a
A
48bsd_in_acceptmsg(int fd)
49{
50 uint32_t len;
51 int n;
db78b1bd 52 char line[MAXLINE];
b16a592a 53 struct sockaddr_un sun;
f3df4c03 54 asl_msg_t *m;
b16a592a
A
55
56 len = sizeof(struct sockaddr_un);
57 n = recvfrom(fd, line, MAXLINE, 0, (struct sockaddr *)&sun, &len);
58
db78b1bd 59 if (n <= 0) return;
b16a592a
A
60
61 line[n] = '\0';
5dd30d76 62
db78b1bd 63 m = asl_input_parse(line, n, NULL, SOURCE_BSD_SOCKET);
81582353 64 process_message(m, SOURCE_BSD_SOCKET);
b16a592a
A
65}
66
67int
db78b1bd 68bsd_in_init()
b16a592a 69{
b16a592a
A
70 int rbufsize;
71 int len;
5dd30d76 72 launch_data_t sockets_dict, fd_array, fd_dict;
db78b1bd 73 static dispatch_once_t once;
81582353 74
86a8bcf5 75#if TARGET_OS_SIMULATOR
81582353
A
76 const char *_PATH_SYSLOG_IN = getenv("IOS_SIMULATOR_SYSLOG_SOCKET");
77#endif
78
db78b1bd
A
79 dispatch_once(&once, ^{
80 in_queue = dispatch_queue_create(MY_ID, NULL);
81 });
b16a592a
A
82
83 asldebug("%s: init\n", MY_ID);
db78b1bd 84 if (sock >= 0) return -1;
b16a592a 85
57b0aad2 86 if (global.launch_dict == NULL)
b16a592a 87 {
c4fdb7d1 88 asldebug("%s: launchd dict is NULL\n", MY_ID);
b16a592a
A
89 return -1;
90 }
91
57b0aad2 92 sockets_dict = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_SOCKETS);
5dd30d76
A
93 if (sockets_dict == NULL)
94 {
c4fdb7d1 95 asldebug("%s: launchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID);
5dd30d76
A
96 return -1;
97 }
b16a592a 98
5dd30d76
A
99 fd_array = launch_data_dict_lookup(sockets_dict, BSD_SOCKET_NAME);
100 if (fd_array == NULL)
101 {
c4fdb7d1 102 asldebug("%s: launchd lookup of BSD_SOCKET_NAME failed\n", MY_ID);
5dd30d76
A
103 return -1;
104 }
b16a592a 105
5dd30d76
A
106 len = launch_data_array_get_count(fd_array);
107 if (len <= 0)
b16a592a 108 {
c4fdb7d1 109 asldebug("%s: launchd fd array is empty\n", MY_ID);
b16a592a
A
110 return -1;
111 }
112
5dd30d76
A
113 if (len > 1)
114 {
c4fdb7d1 115 asldebug("%s: warning! launchd fd array has %d sockets\n", MY_ID, len);
5dd30d76
A
116 }
117
118 fd_dict = launch_data_array_get_index(fd_array, 0);
119 if (fd_dict == NULL)
120 {
c4fdb7d1 121 asldebug("%s: launchd file discriptor array element 0 is NULL\n", MY_ID);
5dd30d76
A
122 return -1;
123 }
124
125 sock = launch_data_get_fd(fd_dict);
126
b16a592a
A
127 rbufsize = 128 * 1024;
128 len = sizeof(rbufsize);
5dd30d76 129
b16a592a
A
130 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0)
131 {
132 asldebug("%s: couldn't set receive buffer size for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno));
133 close(sock);
134 sock = -1;
135 return -1;
136 }
137
138 if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0)
139 {
140 asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno));
141 close(sock);
142 sock = -1;
143 return -1;
144 }
145
db78b1bd
A
146 in_src = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)sock, 0, in_queue);
147 dispatch_source_set_event_handler(in_src, ^{ bsd_in_acceptmsg(sock); });
f3df4c03 148
db78b1bd
A
149 dispatch_resume(in_src);
150 return 0;
b16a592a
A
151}
152
153int
a83ff38a 154bsd_in_close(void)
b16a592a 155{
a83ff38a
A
156 if (sock < 0) return 1;
157
db78b1bd
A
158 dispatch_source_cancel(in_src);
159 dispatch_release(in_src);
160 in_src = NULL;
161
a83ff38a
A
162 close(sock);
163 sock = -1;
164
b16a592a
A
165 return 0;
166}
167
168int
a83ff38a 169bsd_in_reset(void)
b16a592a 170{
b16a592a
A
171 return 0;
172}