Deleted Added
full compact
logger.c (63402) logger.c (70100)
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
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

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)logger.c 8.1 (Berkeley) 6/6/93";
43#endif
44static const char rcsid[] =
45 "$FreeBSD: head/usr.bin/logger/logger.c 63402 2000-07-18 08:56:54Z dwmalone $";
45 "$FreeBSD: head/usr.bin/logger/logger.c 70100 2000-12-16 18:33:08Z ume $";
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51
52#include <ctype.h>
53#include <err.h>

--- 6 unchanged lines hidden (view full) ---

60#define SYSLOG_NAMES
61#include <syslog.h>
62
63int decode __P((char *, CODE *));
64int pencode __P((char *));
65static void logmessage __P((int, char *, char *));
66static void usage __P((void));
67
46#endif /* not lint */
47
48#include <sys/types.h>
49#include <sys/socket.h>
50#include <netinet/in.h>
51
52#include <ctype.h>
53#include <err.h>

--- 6 unchanged lines hidden (view full) ---

60#define SYSLOG_NAMES
61#include <syslog.h>
62
63int decode __P((char *, CODE *));
64int pencode __P((char *));
65static void logmessage __P((int, char *, char *));
66static void usage __P((void));
67
68struct socks {
69 int sock;
70 int addrlen;
71 struct sockaddr_storage addr;
72};
73
74#ifdef INET6
75int family = PF_UNSPEC; /* protocol family (IPv4, IPv6 or both) */
76#else
77int family = PF_INET; /* protocol family (IPv4 only) */
78#endif
79int send_to_all = 0; /* send message to all IPv4/IPv6 addresses */
80
68/*
69 * logger -- read and log utility
70 *
71 * Reads from an input and arranges to write the result on the system
72 * log.
73 */
74int
75main(argc, argv)
76 int argc;
77 char *argv[];
78{
79 int ch, logflags, pri;
80 char *tag, *host, buf[1024];
81
82 tag = NULL;
83 host = NULL;
84 pri = LOG_NOTICE;
85 logflags = 0;
86 unsetenv("TZ");
81/*
82 * logger -- read and log utility
83 *
84 * Reads from an input and arranges to write the result on the system
85 * log.
86 */
87int
88main(argc, argv)
89 int argc;
90 char *argv[];
91{
92 int ch, logflags, pri;
93 char *tag, *host, buf[1024];
94
95 tag = NULL;
96 host = NULL;
97 pri = LOG_NOTICE;
98 logflags = 0;
99 unsetenv("TZ");
87 while ((ch = getopt(argc, argv, "f:h:ip:st:")) != -1)
100 while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
88 switch((char)ch) {
101 switch((char)ch) {
102 case '4':
103 family = PF_INET;
104 break;
105#ifdef INET6
106 case '6':
107 family = PF_INET6;
108 break;
109#endif
110 case 'A':
111 send_to_all++;
112 break;
89 case 'f': /* file to log */
90 if (freopen(optarg, "r", stdin) == NULL)
91 err(1, "%s", optarg);
92 break;
93 case 'h': /* hostname to deliver to */
94 host = optarg;
95 break;
96 case 'i': /* log process id also */

--- 48 unchanged lines hidden (view full) ---

145}
146
147/*
148 * Send the message to syslog, either on the local host, or on a remote host
149 */
150void
151logmessage(int pri, char *host, char *buf)
152{
113 case 'f': /* file to log */
114 if (freopen(optarg, "r", stdin) == NULL)
115 err(1, "%s", optarg);
116 break;
117 case 'h': /* hostname to deliver to */
118 host = optarg;
119 break;
120 case 'i': /* log process id also */

--- 48 unchanged lines hidden (view full) ---

169}
170
171/*
172 * Send the message to syslog, either on the local host, or on a remote host
173 */
174void
175logmessage(int pri, char *host, char *buf)
176{
153 static int sock = -1;
154 static struct sockaddr_in sin;
177 static struct socks *socks;
178 static int nsock = 0;
179 struct addrinfo hints, *res, *r;
155 char *line;
180 char *line;
156 int len;
181 int maxs, len, sock, error, i, lsent;
157
158 if (host == NULL) {
159 syslog(pri, "%s", buf);
160 return;
161 }
162
182
183 if (host == NULL) {
184 syslog(pri, "%s", buf);
185 return;
186 }
187
163 if (sock == -1) { /* set up socket stuff */
164 struct servent *sp;
165 struct hostent *hp = NULL;
166
167 sin.sin_family = AF_INET;
168
169 if ((sp = getservbyname("syslog", "udp")) == NULL)
170 warnx ("syslog/udp: unknown service"); /* not fatal */
171 sin.sin_port = (sp == NULL ? htons(514) : sp->s_port);
172
188 if (nsock <= 0) { /* set up socket stuff */
173 /* resolve hostname */
189 /* resolve hostname */
174 if (!(inet_aton(host, &sin.sin_addr)) &&
175 (hp = gethostbyname(host)) == NULL)
176 errx(1, "unknown host: %s", host);
177 if (hp != NULL)
178 memcpy(&sin.sin_addr, hp->h_addr, sizeof(sin.sin_addr));
179
180 sock = socket(PF_INET, SOCK_DGRAM, 0);
181 if (sock < 0)
190 memset(&hints, 0, sizeof(hints));
191 hints.ai_family = family;
192 hints.ai_socktype = SOCK_DGRAM;
193 error = getaddrinfo(host, "syslog", &hints, &res);
194 if (error == EAI_SERVICE) {
195 warnx ("syslog/udp: unknown service"); /* not fatal */
196 error = getaddrinfo(host, "514", &hints, &res);
197 }
198 if (error)
199 errx(1, "%s: %s", gai_strerror(error), host);
200 /* count max number of sockets we may open */
201 for (maxs = 0, r = res; r; r = r->ai_next, maxs++);
202 socks = malloc(maxs * sizeof(struct socks));
203 if (!socks)
204 errx(1, "couldn't allocate memory for sockets");
205 for (r = res; r; r = r->ai_next) {
206 sock = socket(r->ai_family, r->ai_socktype,
207 r->ai_protocol);
208 if (sock < 0)
209 continue;
210 memcpy(&socks[nsock].addr, r->ai_addr, r->ai_addrlen);
211 socks[nsock].addrlen = r->ai_addrlen;
212 socks[nsock++].sock = sock;
213 }
214 freeaddrinfo(res);
215 if (nsock <= 0)
182 errx(1, "socket");
183 }
184
185 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
186 errx(1, "asprintf");
187
216 errx(1, "socket");
217 }
218
219 if ((len = asprintf(&line, "<%d>%s", pri, buf)) == -1)
220 errx(1, "asprintf");
221
188 if (sendto(sock, line, len, 0, (struct sockaddr *)&sin, sizeof(sin))
189 < len)
222 for (i = 0; i < nsock; ++i) {
223 lsent = sendto(socks[i].sock, line, len, 0,
224 (struct sockaddr *)&socks[i].addr,
225 socks[i].addrlen);
226 if (lsent == len && !send_to_all)
227 break;
228 }
229 if (lsent != len)
190 warnx ("sendmsg");
191
192 free(line);
193}
194
195/*
196 * Decode a symbolic name to a numeric value
197 */

--- 38 unchanged lines hidden (view full) ---

236
237 return (-1);
238}
239
240static void
241usage()
242{
243 (void)fprintf(stderr, "usage: %s\n",
230 warnx ("sendmsg");
231
232 free(line);
233}
234
235/*
236 * Decode a symbolic name to a numeric value
237 */

--- 38 unchanged lines hidden (view full) ---

276
277 return (-1);
278}
279
280static void
281usage()
282{
283 (void)fprintf(stderr, "usage: %s\n",
244 "logger [-is] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
284 "logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
245 );
246 exit(1);
247}
285 );
286 exit(1);
287}