2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static const char copyright
[] __attribute__((used
)) =
36 "Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.\n"
37 "Copygigjt (c) 2005 - 2006 Pavel Fedin";
39 #include <sys/cdefs.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
46 #if !defined(__AROS__)
55 #include <proto/socket.h>
56 #include <proto/miami.h>
58 #include <proto/exec.h>
60 struct Library
*SocketBase
;
61 struct Library
*MiamiBase
;
68 int decode(char *, CODE
*);
70 static void logmessage(int, char *, char *);
71 static void usage(void);
80 int family
= PF_UNSPEC
; /* protocol family (IPv4, IPv6 or both) */
82 int family
= PF_INET
; /* protocol family (IPv4 only) */
84 int send_to_all
= 0; /* send message to all IPv4/IPv6 addresses */
87 * logger -- read and log utility
89 * Reads from an input and arranges to write the result on the system
93 main(int argc
, char *argv
[])
95 int ch
, logflags
, pri
;
96 char *tag
, *host
, buf
[1024];
99 if (!(SocketBase
= OpenLibrary("bsdsocket.library", 3)))
103 if (!(SocketBase
= OpenLibrary("miami.library", 0)))
111 pri
= LOG_USER
| LOG_NOTICE
;
113 while ((ch
= getopt(argc
, argv
, "46Af:h:ip:st:")) != -1)
126 case 'f': /* file to log */
127 if (freopen(optarg
, "r", stdin
) == NULL
)
129 /* TODO: NicJA - we dont have err() */
130 #if !defined(__AROS__)
131 err(1, "%s", optarg
);
133 //return RETURN_FAIL;
137 case 'h': /* hostname to deliver to */
140 case 'i': /* log process id also */
143 case 'p': /* priority */
144 pri
= pencode(optarg
);
146 case 's': /* log to standard error */
147 logflags
|= LOG_PERROR
;
159 /* setup for logging */
160 /* TODO: NicJA - openlog() & getlogin()?? */
161 #if !defined(__AROS__)
162 openlog(tag
? tag
: getlogin(), logflags
, 0);
167 //openlog(tag, logflags, 0);
169 (void) fclose(stdout
);
171 /* log input line if appropriate */
176 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
;) {
178 if (p
+ len
> endp
&& p
> buf
) {
179 logmessage(pri
, host
, buf
);
182 if (len
> sizeof(buf
) - 1)
183 logmessage(pri
, host
, *argv
++);
187 bcopy(*argv
++, p
, len
);
192 logmessage(pri
, host
, buf
);
194 while (fgets(buf
, sizeof(buf
), stdin
) != NULL
)
195 logmessage(pri
, host
, buf
);
200 * Send the message to syslog, either on the local host, or on a remote host
203 logmessage(int pri
, char *host
, char *buf
)
205 static struct socks
*socks
;
206 static int nsock
= 0;
207 struct addrinfo hints
, *res
, *r
;
209 int maxs
, len
, sock
, error
, i
, lsent
;
212 D(fprintf(stderr
, "Logging text: %s\n", buf
);)
213 syslog(pri
, "%s", buf
);
217 if (nsock
<= 0) { /* set up socket stuff */
218 /* resolve hostname */
219 memset(&hints
, 0, sizeof(hints
));
220 hints
.ai_family
= family
;
221 hints
.ai_socktype
= SOCK_DGRAM
;
222 error
= getaddrinfo(host
, "syslog", &hints
, &res
);
223 if (error
== EAI_SERVICE
) {
224 /* TODO: NicJA - We dont have warnx() */
225 #if !defined(__AROS__)
226 warnx("syslog/udp: unknown service"); /* not fatal */
228 error
= getaddrinfo(host
, "514", &hints
, &res
);
232 /* TODO: NicJA - We dont have errx() */
233 #if !defined(__AROS__)
234 errx(1, "%s: %s", gai_strerror(error
), host
);
237 /* count max number of sockets we may open */
238 for (maxs
= 0, r
= res
; r
; r
= r
->ai_next
, maxs
++);
239 socks
= malloc(maxs
* sizeof(struct socks
));
242 /* TODO: NicJA - We dont have errx() */
243 #if !defined(__AROS__)
244 errx(1, "couldn't allocate memory for sockets");
248 for (r
= res
; r
; r
= r
->ai_next
) {
249 sock
= socket(r
->ai_family
, r
->ai_socktype
,
253 memcpy(&socks
[nsock
].addr
, r
->ai_addr
, r
->ai_addrlen
);
254 socks
[nsock
].addrlen
= r
->ai_addrlen
;
255 socks
[nsock
++].sock
= sock
;
260 /* TODO: NicJA - We dont have errx() */
261 #if !defined(__AROS__)
267 #if defined(__AROS__)
269 sprintf(line_tmp
, "<%d>%s", pri
, buf
);
270 len
= strlen(line_tmp
+ 1);
274 if ((len
= asprintf(&line
, "<%d>%s", pri
, buf
)) == -1)
277 /* TODO: NicJA - We dont have errx() */
278 #if !defined(__AROS__)
284 for (i
= 0; i
< nsock
; ++i
) {
285 lsent
= sendto(socks
[i
].sock
, line
, len
, 0,
286 (struct sockaddr
*)&socks
[i
].addr
,
288 if (lsent
== len
&& !send_to_all
)
294 /* TODO: NicJA - We dont have warn() */
295 #if !defined(__AROS__)
301 /* TODO: NicJA - We dont have warnx() */
302 #if !defined(__AROS__)
303 warnx ("sendto: short send - %d bytes", lsent
);
314 * Decode a symbolic name to a numeric value
322 for (save
= s
; *s
&& *s
!= '.'; ++s
);
325 fac
= decode(save
, facilitynames
);
328 /* TODO: NicJA - We dont have errx() */
329 #if !defined(__AROS__)
330 errx(1, "unknown facility name: %s", save
);
339 lev
= decode(s
, prioritynames
);
342 /* TODO: NicJA - We dont have errx() */
343 #if !defined(__AROS__)
344 errx(1, "unknown priority name: %s", save
);
347 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
351 decode(char *name
, CODE
*codetab
)
358 for (c
= codetab
; c
->c_name
; c
++)
359 if (!strcasecmp(name
, c
->c_name
))
368 (void)fprintf(stderr
, "usage: %s\n",
369 "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"