View | Details | Raw Unified | Return to bug 19821
Collapse All | Expand All

(-)logger.c (-9 / +83 lines)
Lines 42-50 Link Here
42
static char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
42
static char sccsid[] = "@(#)logger.c	8.1 (Berkeley) 6/6/93";
43
#endif
43
#endif
44
static const char rcsid[] =
44
static const char rcsid[] =
45
	"$Id: logger.c,v 1.2.2.3 1997/09/15 08:32:18 jkh Exp $";
45
	"$Id: logger.c,v 1.2 1998/01/29 17:28:54 nick Exp $";
46
#endif /* not lint */
46
#endif /* not lint */
47
47
48
#include <sys/types.h>
49
#include <sys/uio.h>
50
#include <netinet/in.h>
51
#include <sys/socket.h>
52
#include <netdb.h>
53
48
#include <ctype.h>
54
#include <ctype.h>
49
#include <err.h>
55
#include <err.h>
50
#include <stdio.h>
56
#include <stdio.h>
Lines 57-64 Link Here
57
63
58
int	decode __P((char *, CODE *));
64
int	decode __P((char *, CODE *));
59
int	pencode __P((char *));
65
int	pencode __P((char *));
66
void	logmessage __P((int, char *, char *));
60
static void	usage __P((void));
67
static void	usage __P((void));
61
68
69
#define MAXBUF 1024
70
62
/*
71
/*
63
 * logger -- read and log utility
72
 * logger -- read and log utility
64
 *
73
 *
Lines 71-83 Link Here
71
	char *argv[];
80
	char *argv[];
72
{
81
{
73
	int ch, logflags, pri;
82
	int ch, logflags, pri;
74
	char *tag, buf[1024];
83
	char *tag, *host, buf[MAXBUF];
75
84
76
	tag = NULL;
85
	host = tag = NULL;
77
	pri = LOG_NOTICE;
86
	pri = LOG_NOTICE;
78
	logflags = 0;
87
	logflags = 0;
79
	unsetenv("TZ");
88
	unsetenv("TZ");
80
	while ((ch = getopt(argc, argv, "f:ip:st:")) != -1)
89
	while ((ch = getopt(argc, argv, "f:h:ip:st:")) != -1)
81
		switch((char)ch) {
90
		switch((char)ch) {
82
		case 'f':		/* file to log */
91
		case 'f':		/* file to log */
83
			if (freopen(optarg, "r", stdin) == NULL)
92
			if (freopen(optarg, "r", stdin) == NULL)
Lines 95-100 Link Here
95
		case 't':		/* tag */
104
		case 't':		/* tag */
96
			tag = optarg;
105
			tag = optarg;
97
			break;
106
			break;
107
		case 'h':
108
			host = optarg;	/* hostname to deliver to */
109
			break;
98
		case '?':
110
		case '?':
99
		default:
111
		default:
100
			usage();
112
			usage();
Lines 114-124 Link Here
114
		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
126
		for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
115
			len = strlen(*argv);
127
			len = strlen(*argv);
116
			if (p + len > endp && p > buf) {
128
			if (p + len > endp && p > buf) {
117
				syslog(pri, "%s", buf);
129
				logmessage(pri, host, buf);
118
				p = buf;
130
				p = buf;
119
			}
131
			}
120
			if (len > sizeof(buf) - 1)
132
			if (len > sizeof(buf) - 1)
121
				syslog(pri, "%s", *argv++);
133
				logmessage(pri, host, *argv++);
122
			else {
134
			else {
123
				if (p != buf)
135
				if (p != buf)
124
					*p++ = ' ';
136
					*p++ = ' ';
Lines 127-140 Link Here
127
			}
139
			}
128
		}
140
		}
129
		if (p != buf)
141
		if (p != buf)
130
			syslog(pri, "%s", buf);
142
			logmessage(pri, host, buf);
131
	} else
143
	} else
132
		while (fgets(buf, sizeof(buf), stdin) != NULL)
144
		while (fgets(buf, sizeof(buf), stdin) != NULL)
133
			syslog(pri, "%s", buf);
145
			logmessage(pri, host, buf);
134
	exit(0);
146
	exit(0);
135
}
147
}
136
148
137
/*
149
/*
150
 *  Send the message to syslog, either on the local host, or on a remote host
151
 */
152
void 
153
logmessage(int pri, char *host, char *buf)
154
{
155
	static int sock = -1;
156
	static struct sockaddr_in sin;
157
158
	struct msghdr msg;
159
	struct iovec iov[1];
160
	ssize_t size;
161
	struct in_addr in;
162
	struct servent *sp;
163
	struct hostent *hp = NULL;
164
	char line[MAXBUF + sizeof("<nnnnnnnnnnn>")];	/* int is always < 11 chars */
165
166
	if (host == NULL) {
167
		syslog(pri, "%s", buf);
168
		return;
169
	}
170
171
	if (sock == -1) {	/* set up socket stuff */
172
		if ((sp = getservbyname("syslog", "udp")) == NULL)
173
			warnx ("syslog/udp: unknown service");	/* not fatal */
174
175
		/* resolve hostname */
176
		if (!(inet_aton (host, &in)) && !(hp = gethostbyname(host))) {
177
			errx (1, "unknown host: %s", host);
178
		}
179
		if (hp != NULL)
180
			memcpy ((void *)&in.s_addr, hp->h_addr, 
181
				sizeof(struct in_addr));
182
183
		/* set up struct sockaddr_in */
184
		sin.sin_family = AF_INET;
185
		sin.sin_port = (sp == NULL ? 514 : sp->s_port);
186
		memcpy ((void *)&sin.sin_addr, (void *)&in.s_addr,
187
			sizeof(struct in_addr));
188
189
		sock = socket (PF_INET, SOCK_DGRAM, 0);
190
		if (sock < 0)
191
			errx(1, "socket");
192
	}
193
194
	msg.msg_name = (void *)&sin;
195
	msg.msg_namelen = sizeof sin;
196
	msg.msg_iov = iov;
197
	msg.msg_iovlen = 0;
198
	msg.msg_control = 0;
199
	msg.msg_controllen = 0;
200
	msg.msg_flags = 0;
201
202
	snprintf (line, sizeof (line) - 1, "<%d>%s", pri, buf);
203
	
204
	iov[msg.msg_iovlen].iov_base = line;
205
	iov[msg.msg_iovlen++].iov_len = strlen(line);
206
	                        
207
	if (sendmsg (sock, &msg, 0) < strlen(line))
208
		warnx ("sendmsg");
209
}
210
211
/*
138
 *  Decode a symbolic name to a numeric value
212
 *  Decode a symbolic name to a numeric value
139
 */
213
 */
140
int
214
int
Lines 183-188 Link Here
183
usage()
257
usage()
184
{
258
{
185
	(void)fprintf(stderr,
259
	(void)fprintf(stderr,
186
	    "usage: logger [-is] [-f file] [-p pri] [-t tag] [message ...]\n");
260
	    "usage: logger [-is] [-f file] [-p pri] [-t tag] [-h host] [message ...]\n");
187
	exit(1);
261
	exit(1);
188
}
262
}
(-)logger.1 (+5 lines)
Lines 43-48 Link Here
43
.Op Fl f Ar file
43
.Op Fl f Ar file
44
.Op Fl p Ar pri
44
.Op Fl p Ar pri
45
.Op Fl t Ar tag
45
.Op Fl t Ar tag
46
.Op Fl h Ar host
46
.Op Ar message ...
47
.Op Ar message ...
47
.Sh DESCRIPTION
48
.Sh DESCRIPTION
48
.Nm Logger
49
.Nm Logger
Lines 73-78 Link Here
73
.It Fl t Ar tag 
74
.It Fl t Ar tag 
74
Mark every line in the log with the specified
75
Mark every line in the log with the specified
75
.Ar tag  .
76
.Ar tag  .
77
.It Fl h Ar host 
78
Send the message to the remote system 
79
.Ar host
80
instead of logging it locally.
76
.It Ar message
81
.It Ar message
77
Write the message to log; if not specified, and the
82
Write the message to log; if not specified, and the
78
.Fl f
83
.Fl f

Return to bug 19821