]> git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/udp_in.c
syslog-148.8.tar.gz
[apple/syslog.git] / syslogd.tproj / udp_in.c
1 /*
2 * Copyright (c) 2004-2011 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 <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <sys/un.h>
30 #include <sys/uio.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <netdb.h>
38 #include "daemon.h"
39
40 #define forever for(;;)
41
42 #define UDP_SOCKET_NAME "NetworkListener"
43 #define MY_ID "udp_in"
44 #define MAXLINE 4096
45
46 #define MAXSOCK 16
47 static int nsock = 0;
48 static int ufd[MAXSOCK];
49 static dispatch_source_t ufd_src[MAXSOCK];
50
51 static char uline[MAXLINE + 1];
52
53 static dispatch_source_t in_src[MAXSOCK];
54 static dispatch_queue_t in_queue;
55
56 #define FMT_LEGACY 0
57 #define FMT_ASL 1
58
59 void
60 udp_in_acceptmsg(int fd)
61 {
62 socklen_t fromlen;
63 ssize_t len;
64 struct sockaddr_storage from;
65 char fromstr[64], *r, *p;
66 struct sockaddr_in *s4;
67 struct sockaddr_in6 *s6;
68 aslmsg m;
69
70 fromlen = sizeof(struct sockaddr_storage);
71 memset(&from, 0, fromlen);
72
73 len = recvfrom(fd, uline, MAXLINE, 0, (struct sockaddr *)&from, &fromlen);
74 if (len <= 0) return;
75
76 fromstr[0] = '\0';
77 r = NULL;
78
79 if (from.ss_family == AF_INET)
80 {
81 s4 = (struct sockaddr_in *)&from;
82 inet_ntop(from.ss_family, &(s4->sin_addr), fromstr, 64);
83 r = fromstr;
84 asldebug("%s: fd %d recvfrom %s len %d\n", MY_ID, fd, fromstr, len);
85 }
86 else if (from.ss_family == AF_INET6)
87 {
88 s6 = (struct sockaddr_in6 *)&from;
89 inet_ntop(from.ss_family, &(s6->sin6_addr), fromstr, 64);
90 r = fromstr;
91 asldebug("%s: fd %d recvfrom %s len %d\n", MY_ID, fd, fromstr, len);
92 }
93
94 uline[len] = '\0';
95
96 p = strrchr(uline, '\n');
97 if (p != NULL) *p = '\0';
98
99 m = asl_input_parse(uline, len, r, SOURCE_UDP_SOCKET);
100 dispatch_async(global.work_queue, ^{ process_message(m, SOURCE_UDP_SOCKET); });
101 }
102
103 int
104 udp_in_init()
105 {
106 int i, rbufsize, len, fd;
107 launch_data_t sockets_dict, fd_array, fd_dict;
108 static dispatch_once_t once;
109
110 dispatch_once(&once, ^{
111 in_queue = dispatch_queue_create(MY_ID, NULL);
112 });
113
114 asldebug("%s: init\n", MY_ID);
115 if (nsock > 0) return 0;
116
117 if (global.launch_dict == NULL)
118 {
119 asldebug("%s: launchd dict is NULL\n", MY_ID);
120 return -1;
121 }
122
123 sockets_dict = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_SOCKETS);
124 if (sockets_dict == NULL)
125 {
126 asldebug("%s: launchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID);
127 return -1;
128 }
129
130 fd_array = launch_data_dict_lookup(sockets_dict, UDP_SOCKET_NAME);
131 if (fd_array == NULL)
132 {
133 asldebug("%s: launchd lookup of UDP_SOCKET_NAME failed\n", MY_ID);
134 return -1;
135 }
136
137 nsock = launch_data_array_get_count(fd_array);
138 if (nsock <= 0)
139 {
140 asldebug("%s: launchd fd array is empty\n", MY_ID);
141 return -1;
142 }
143
144 for (i = 0; i < nsock; i++)
145 {
146 ufd[i] = -1;
147
148 fd_dict = launch_data_array_get_index(fd_array, i);
149 if (fd_dict == NULL)
150 {
151 asldebug("%s: launchd file discriptor array element 0 is NULL\n", MY_ID);
152 return -1;
153 }
154
155 fd = launch_data_get_fd(fd_dict);
156
157 rbufsize = 128 * 1024;
158 len = sizeof(rbufsize);
159
160 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0)
161 {
162 asldebug("%s: couldn't set receive buffer size for file descriptor %d: %s\n", MY_ID, fd, strerror(errno));
163 }
164
165 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
166 {
167 asldebug("%s: couldn't set O_NONBLOCK for file descriptor %d: %s\n", MY_ID, fd, strerror(errno));
168 }
169
170 ufd[i] = fd;
171
172 in_src[i] = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)fd, 0, in_queue);
173 dispatch_source_set_event_handler(in_src[i], ^{ udp_in_acceptmsg(fd); });
174
175 dispatch_resume(in_src[i]);
176 }
177
178 return 0;
179 }
180
181 /* N.B. Does NOT close fds. They "belong" to launchd. */
182 int
183 udp_in_close(void)
184 {
185 int i;
186
187 if (nsock == 0) return -1;
188
189 for (i = 0; i < nsock; i++)
190 {
191 if (ufd_src[i] != NULL)
192 {
193 dispatch_source_cancel(in_src[i]);
194 dispatch_release(in_src[i]);
195 in_src[i] = NULL;
196 }
197
198 if (ufd[i] != -1)
199 {
200 ufd[i] = -1;
201 }
202 }
203
204 nsock = 0;
205
206 return 0;
207 }
208
209 int
210 udp_in_reset(void)
211 {
212 if (udp_in_close() != 0) return -1;
213 return udp_in_init();
214 }