]>
Commit | Line | Data |
---|---|---|
b16a592a A |
1 | /* |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
02b408bf 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, | |
02b408bf 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/fcntl.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 <unistd.h> | |
34 | #include <errno.h> | |
35 | #include "daemon.h" | |
36 | ||
37 | #define MY_ID "bsd_in" | |
38 | #define MAXLINE 1024 | |
39 | ||
40 | static int sock = -1; | |
41 | ||
42 | asl_msg_t * | |
43 | bsd_in_acceptmsg(int fd) | |
44 | { | |
45 | uint32_t len; | |
46 | int n; | |
47 | char line[MAXLINE + 1]; | |
48 | struct sockaddr_un sun; | |
49 | ||
50 | len = sizeof(struct sockaddr_un); | |
51 | n = recvfrom(fd, line, MAXLINE, 0, (struct sockaddr *)&sun, &len); | |
52 | ||
53 | if (n <= 0) return NULL; | |
54 | ||
55 | line[n] = '\0'; | |
56 | return asl_syslog_input_convert(line, n, NULL, 0); | |
57 | } | |
58 | ||
59 | int | |
60 | bsd_in_init(void) | |
61 | { | |
62 | struct sockaddr_un sun; | |
63 | int rbufsize; | |
64 | int len; | |
65 | ||
66 | asldebug("%s: init\n", MY_ID); | |
67 | if (sock >= 0) return sock; | |
68 | ||
69 | unlink(_PATH_SYSLOG_IN); | |
70 | sock = socket(AF_UNIX, SOCK_DGRAM, 0); | |
71 | if (sock < 0) | |
72 | { | |
73 | asldebug("%s: couldn't create socket for %s: %s\n", MY_ID, _PATH_SYSLOG_IN, strerror(errno)); | |
74 | return -1; | |
75 | } | |
76 | ||
77 | asldebug("%s: creating %s for fd %d\n", MY_ID, _PATH_SYSLOG_IN, sock); | |
78 | ||
79 | memset(&sun, 0, sizeof(sun)); | |
80 | sun.sun_family = AF_UNIX; | |
81 | strcpy(sun.sun_path, _PATH_SYSLOG_IN); | |
82 | ||
83 | len = sizeof(struct sockaddr_un); | |
84 | if (bind(sock, (struct sockaddr *)&sun, len) < 0) | |
85 | { | |
86 | asldebug("%s: couldn't bind socket %d for %s: %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
87 | close(sock); | |
88 | sock = -1; | |
89 | return -1; | |
90 | } | |
91 | ||
92 | rbufsize = 128 * 1024; | |
93 | len = sizeof(rbufsize); | |
94 | ||
95 | if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0) | |
96 | { | |
97 | asldebug("%s: couldn't set receive buffer size for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
98 | close(sock); | |
99 | sock = -1; | |
100 | return -1; | |
101 | } | |
102 | ||
103 | if (fcntl(sock, F_SETFL, O_NONBLOCK) < 0) | |
104 | { | |
105 | asldebug("%s: couldn't set O_NONBLOCK for socket %d (%s): %s\n", MY_ID, sock, _PATH_SYSLOG_IN, strerror(errno)); | |
106 | close(sock); | |
107 | sock = -1; | |
108 | return -1; | |
109 | } | |
110 | ||
111 | chmod(_PATH_SYSLOG_IN, 0666); | |
112 | ||
113 | return aslevent_addfd(sock, bsd_in_acceptmsg, NULL, NULL); | |
114 | } | |
115 | ||
116 | int | |
117 | bsd_in_reset(void) | |
118 | { | |
119 | return 0; | |
120 | } | |
121 | ||
122 | int | |
123 | bsd_in_close(void) | |
124 | { | |
125 | if (sock < 0) return 1; | |
126 | ||
127 | close(sock); | |
128 | unlink(_PATH_SYSLOG_IN); | |
129 | return 0; | |
130 | } |