Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / network / common / C / logger.c
blobd75c095db50a4d8a9abc686646aeb306e5bf2cb0
1 /*
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
7 * are met:
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
31 * SUCH DAMAGE.
34 static const char copyright[] __attribute__((used)) =
35 "$VER: logger v1.0"
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>
45 #include <ctype.h>
46 #if !defined(__AROS__)
47 #include <err.h>
48 #endif
49 #include <netdb.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
55 #include <proto/socket.h>
56 #include <proto/miami.h>
58 #include <proto/exec.h>
59 #include <dos/dos.h>
60 struct Library *SocketBase;
61 struct Library *MiamiBase;
63 #define SYSLOG_NAMES
64 #include <syslog.h>
66 #define D(x)
68 int decode(char *, CODE *);
69 int pencode(char *);
70 static void logmessage(int, char *, char *);
71 static void usage(void);
73 struct socks {
74 int sock;
75 int addrlen;
76 struct sockaddr addr;
79 #ifdef INET6
80 int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
81 #else
82 int family = PF_INET; /* protocol family (IPv4 only) */
83 #endif
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
90 * log.
92 int
93 main(int argc, char *argv[])
95 int ch, logflags, pri;
96 char *tag, *host, buf[1024];
98 #if defined(__AROS__)
99 if (!(SocketBase = OpenLibrary("bsdsocket.library", 3)))
101 return RETURN_FAIL;
103 if (!(SocketBase = OpenLibrary("miami.library", 0)))
105 return RETURN_FAIL;
107 #endif
109 tag = NULL;
110 host = NULL;
111 pri = LOG_USER | LOG_NOTICE;
112 logflags = 0;
113 while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
114 switch((char)ch) {
115 case '4':
116 family = PF_INET;
117 break;
118 #ifdef INET6
119 case '6':
120 family = PF_INET6;
121 break;
122 #endif
123 case 'A':
124 send_to_all++;
125 break;
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);
132 #else
133 //return RETURN_FAIL;
134 #endif
136 break;
137 case 'h': /* hostname to deliver to */
138 host = optarg;
139 break;
140 case 'i': /* log process id also */
141 logflags |= LOG_PID;
142 break;
143 case 'p': /* priority */
144 pri = pencode(optarg);
145 break;
146 case 's': /* log to standard error */
147 logflags |= LOG_PERROR;
148 break;
149 case 't': /* tag */
150 tag = optarg;
151 break;
152 case '?':
153 default:
154 usage();
156 argc -= optind;
157 argv += optind;
159 /* setup for logging */
160 /* TODO: NicJA - openlog() & getlogin()?? */
161 #if !defined(__AROS__)
162 openlog(tag ? tag : getlogin(), logflags, 0);
163 #else
164 if (tag) {
165 // ignored
167 //openlog(tag, logflags, 0);
168 #endif
169 (void) fclose(stdout);
171 /* log input line if appropriate */
172 if (argc > 0) {
173 char *p, *endp;
174 size_t len;
176 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
177 len = strlen(*argv);
178 if (p + len > endp && p > buf) {
179 logmessage(pri, host, buf);
180 p = buf;
182 if (len > sizeof(buf) - 1)
183 logmessage(pri, host, *argv++);
184 else {
185 if (p != buf)
186 *p++ = ' ';
187 bcopy(*argv++, p, len);
188 *(p += len) = '\0';
191 if (p != buf)
192 logmessage(pri, host, buf);
193 } else
194 while (fgets(buf, sizeof(buf), stdin) != NULL)
195 logmessage(pri, host, buf);
196 exit(0);
200 * Send the message to syslog, either on the local host, or on a remote host
202 void
203 logmessage(int pri, char *host, char *buf)
205 static struct socks *socks;
206 static int nsock = 0;
207 struct addrinfo hints, *res, *r;
208 char *line;
209 int maxs, len, sock, error, i, lsent;
211 if (host == NULL) {
212 D(fprintf(stderr, "Logging text: %s\n", buf);)
213 syslog(pri, "%s", buf);
214 return;
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 */
227 #endif
228 error = getaddrinfo(host, "514", &hints, &res);
230 if (error)
232 /* TODO: NicJA - We dont have errx() */
233 #if !defined(__AROS__)
234 errx(1, "%s: %s", gai_strerror(error), host);
235 #endif
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));
240 if (!socks)
242 /* TODO: NicJA - We dont have errx() */
243 #if !defined(__AROS__)
244 errx(1, "couldn't allocate memory for sockets");
245 #endif
248 for (r = res; r; r = r->ai_next) {
249 sock = socket(r->ai_family, r->ai_socktype,
250 r->ai_protocol);
251 if (sock < 0)
252 continue;
253 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
254 socks[nsock].addrlen = r->ai_addrlen;
255 socks[nsock++].sock = sock;
257 freeaddrinfo(res);
258 if (nsock <= 0)
260 /* TODO: NicJA - We dont have errx() */
261 #if !defined(__AROS__)
262 errx(1, "socket");
263 #endif
267 #if defined(__AROS__)
268 char line_tmp[256];
269 sprintf(line_tmp, "<%d>%s", pri, buf);
270 len = strlen(line_tmp + 1);
271 line = line_tmp;
272 if (len > 0)
273 #else
274 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
275 #endif
277 /* TODO: NicJA - We dont have errx() */
278 #if !defined(__AROS__)
279 errx(1, "asprintf");
280 #endif
283 lsent = -1;
284 for (i = 0; i < nsock; ++i) {
285 lsent = sendto(socks[i].sock, line, len, 0,
286 (struct sockaddr *)&socks[i].addr,
287 socks[i].addrlen);
288 if (lsent == len && !send_to_all)
289 break;
291 if (lsent != len) {
292 if (lsent == -1)
294 /* TODO: NicJA - We dont have warn() */
295 #if !defined(__AROS__)
296 warn ("sendto");
297 #endif
299 else
301 /* TODO: NicJA - We dont have warnx() */
302 #if !defined(__AROS__)
303 warnx ("sendto: short send - %d bytes", lsent);
304 #endif
308 #ifndef __AROS__
309 free(line);
310 #endif
314 * Decode a symbolic name to a numeric value
317 pencode(char *s)
319 char *save;
320 int fac, lev;
322 for (save = s; *s && *s != '.'; ++s);
323 if (*s) {
324 *s = '\0';
325 fac = decode(save, facilitynames);
326 if (fac < 0)
328 /* TODO: NicJA - We dont have errx() */
329 #if !defined(__AROS__)
330 errx(1, "unknown facility name: %s", save);
331 #endif
333 *s++ = '.';
335 else {
336 fac = 0;
337 s = save;
339 lev = decode(s, prioritynames);
340 if (lev < 0)
342 /* TODO: NicJA - We dont have errx() */
343 #if !defined(__AROS__)
344 errx(1, "unknown priority name: %s", save);
345 #endif
347 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
351 decode(char *name, CODE *codetab)
353 CODE *c;
355 if (isdigit(*name))
356 return (atoi(name));
358 for (c = codetab; c->c_name; c++)
359 if (!strcasecmp(name, c->c_name))
360 return (c->c_val);
362 return (-1);
365 static void
366 usage(void)
368 (void)fprintf(stderr, "usage: %s\n",
369 "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
371 exit(1);