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

(-)usr.bin/logger/logger.c (-4 / +8 lines)
Lines 72-82 Link Here
72
{
72
{
73
	int ch, logflags, pri;
73
	int ch, logflags, pri;
74
	char *tag, buf[1024];
74
	char *tag, *host, buf[1024];
75
75
76
	tag = NULL;
76
	tag = NULL;
77
	host = NULL;
77
	pri = LOG_NOTICE;
78
	pri = LOG_NOTICE;
78
	logflags = 0;
79
	logflags = 0;
79
	unsetenv("TZ");
80
	unsetenv("TZ");
80
	while ((ch = getopt(argc, argv, "f:ip:st:")) != -1)
81
	while ((ch = getopt(argc, argv, "f:ip:st:h:")) != -1)
81
		switch((char)ch) {
82
		switch((char)ch) {
82
		case 'f':		/* file to log */
83
		case 'f':		/* file to log */
Lines 96-99 Link Here
96
			tag = optarg;
97
			tag = optarg;
97
			break;
98
			break;
99
		case 'h':
100
			host = optarg;
101
			break;
98
		case '?':
102
		case '?':
99
		default:
103
		default:
Lines 128-135 Link Here
128
		}
132
		}
129
		if (p != buf)
133
		if (p != buf)
130
			syslog(pri, "%s", buf);
134
			syslogh(pri, host, "%s", buf);
131
	} else
135
	} else
132
		while (fgets(buf, sizeof(buf), stdin) != NULL)
136
		while (fgets(buf, sizeof(buf), stdin) != NULL)
133
			syslog(pri, "%s", buf);
137
			syslogh(pri, host, "%s", buf);
134
	exit(0);
138
	exit(0);
135
}
139
}
(-)usr.bin/logger/logger.1 (+3 lines)
Lines 72-73 Link Here
72
The default is ``user.notice.''
72
The default is ``user.notice.''
73
.It Fl h Ar host
74
Send the message directly to the specified host. This allows you to use
75
logger on a machine with no syslogd running, for example.
73
.It Fl t Ar tag 
76
.It Fl t Ar tag 
(-)lib/libc/gen/syslog.3 (+17 lines)
Lines 39-40 Link Here
39
.Nm vsyslog ,
39
.Nm vsyslog ,
40
.Nm syslogh ,
41
.Nm vsyslogh ,
40
.Nm openlog ,
42
.Nm openlog ,
Lines 51-52 Link Here
51
.Ft void
53
.Ft void
54
.Fn syslogh "int priority" "const char *host" "const char *message" "..."
55
.Ft void
56
.Fn vsyslogh "int priority" "const char *host" "const char *message" "va_list args"
57
.Ft void
52
.Fn openlog "const char *ident" "int logopt" "int facility"
58
.Fn openlog "const char *ident" "int logopt" "int facility"
Lines 84-85 Link Here
84
.Xr varargs 3 .
90
.Xr varargs 3 .
91
.Pp
92
.Fn syslogh
93
and
94
.Fn vsyslogh
95
send the message directly to the specified
96
.Fa host ,
97
allowing you to bypass
98
the local syslog-daemon. If the sending fails, it will still be attempted
99
to log the message locally, as if
100
.Fa host
101
was NULL.
85
.Pp
102
.Pp
(-)/usr/src/lib/libc/gen/syslog.c (-23 / +121 lines)
Lines 47-48 Link Here
47
#include <netdb.h>
47
#include <netdb.h>
48
#include <netinet/in.h>
48
49
Lines 108-132 Link Here
108
109
109
/*
110
static const char * logtohost(host, tbuf, cnt)
110
 * syslog, vsyslog --
111
	const char *host;
111
 *	print message on log file; output is intended for syslogd(8).
112
	char *tbuf;
112
 */
113
	int   cnt;
113
void
114
#if __STDC__
115
syslog(int pri, const char *fmt, ...)
116
#else
117
syslog(pri, fmt, va_alist)
118
	int pri;
119
	char *fmt;
120
	va_dcl
121
#endif
122
{
114
{
123
	va_list ap;
115
	struct hostent *hp;
116
	static int finet;
117
	static struct sockaddr_in addr;
118
119
	/* initialize */
120
	if(!finet) {
121
		finet = socket(AF_INET, SOCK_DGRAM, 0);
122
		if(finet == -1) {
123
			finet = 0;
124
			return strerror(errno);
125
		}
126
	}
127
	if(!addr.sin_family) { 
128
		struct servent *sp;
129
		sp = getservbyname("syslog", "udp");
130
		if(sp == NULL)
131
			return "syslog/udp: unknown service";
132
		addr.sin_port = sp->s_port;
133
		addr.sin_family = AF_INET;
134
	}
124
135
125
#if __STDC__
136
	hp = gethostbyname(host);
126
	va_start(ap, fmt);
137
	if(hp == NULL)
127
#else
138
		return hstrerror(h_errno);
128
	va_start(ap);
139
	memmove(&addr.sin_addr, hp->h_addr, hp->h_length);
129
#endif
140
130
	vsyslog(pri, fmt, ap);
141
	if(sendto(finet, tbuf, cnt, 0, (struct sockaddr *)&addr,
131
	va_end(ap);
142
						sizeof(addr)) != cnt)
143
		return strerror(errno);
144
145
	return NULL; /* success */
132
}
146
}
Lines 134-138 Link Here
134
void
148
void
135
vsyslog(pri, fmt, ap)
149
vsyslogh(pri, host, fmt, ap)
136
	int pri;
150
	int pri;
137
	register const char *fmt;
151
	register const char *fmt;
152
	const char *host;
138
	va_list ap;
153
	va_list ap;
Lines 240-241 Link Here
240
255
256
	/* If host is specified, try sending the message to it */
257
	if(host) {
258
		const char *err;
259
		err = logtohost(host, tbuf, cnt);
260
		if(err == NULL)
261
			return; /* sent  successfully */
262
		else if(sizeof(tbuf) - cnt > 5) {
263
			/* if there is any room in tbuf, append the */
264
			/* hostname and the error message from the  */
265
			/* logtohost, for it to be logged locally   */
266
			strncat(tbuf + cnt++ - 1, " ", 1);
267
			strncat(tbuf + cnt - 1, host, sizeof(tbuf) - cnt);
268
			cnt += strlen(host);
269
			if(sizeof(tbuf) - cnt > 3) {
270
				strncat(tbuf + cnt++ - 1, ":", 1);
271
				strncat(tbuf + cnt - 1, err,
272
						sizeof(tbuf) - cnt);
273
				cnt += strlen(err);
274
			}
275
		}
276
	}
277
241
	/* Get connected, output the message to the local logger. */
278
	/* Get connected, output the message to the local logger. */
Lines 258-260 Link Here
258
	 * Output the message to the console; don't worry about blocking,
295
	 * Output the message to the console; don't worry about blocking,
259
	 * if console blocks everything will.  Make sure the error reported
296
	 * if console blocks, everything will.  Make sure the error reported
260
	 * is the one from the syslogd failure.
297
	 * is the one from the syslogd failure.
Lines 276-277 Link Here
276
}
313
}
314
315
/*
316
 * syslog, vsyslog --
317
 *	print message on log file; output is intended for syslogd(8).
318
 */
319
void
320
#if __STDC__
321
syslogh(int pri, const char *host, const char *fmt, ...)
322
#else
323
syslogh(pri, host, fmt, va_alist)
324
	int pri;
325
	char *fmt;
326
	const char *host;
327
	va_dcl
328
#endif
329
{
330
	va_list ap;
331
332
#if __STDC__
333
	va_start(ap, fmt);
334
#else
335
	va_start(ap);
336
#endif
337
	vsyslogh(pri, host, fmt, ap);
338
	va_end(ap);
339
}
340
341
/*
342
 * syslog, vsyslog --
343
 *	print message on log file; output is intended for syslogd(8).
344
 */
345
void
346
#if __STDC__
347
syslog(int pri, const char *fmt, ...)
348
#else
349
syslog(pri, fmt, va_alist)
350
	int pri;
351
	char *fmt;
352
	va_dcl
353
#endif
354
{
355
	va_list ap;
356
357
#if __STDC__
358
	va_start(ap, fmt);
359
#else
360
	va_start(ap);
361
#endif
362
	vsyslogh(pri, NULL, fmt, ap);
363
	va_end(ap);
364
}
365
366
void
367
vsyslog(pri, fmt, ap)
368
	int pri;
369
	register const char *fmt;
370
	va_list ap;
371
{
372
	vsyslogh(pri, NULL, fmt, ap);
373
}
374
277
static void
375
static void
(-)sys/sys/syslog.h (+3 lines)
Lines 195-198 Link Here
195
void	syslog __P((int, const char *, ...)) __printflike(2, 3);
195
void	syslog __P((int, const char *, ...)) __printflike(2, 3);
196
void	vsyslog __P((int, const char *, _BSD_VA_LIST_)) __printflike(2, 0);
196
void	vsyslog __P((int, const char *, _BSD_VA_LIST_)) __printflike(2, 0);
197
void	syslogh __P((int, const char *, const char *, ...)) __printflike(3, 4);
198
void	vsyslogh __P((int, const char *, const char *, _BSD_VA_LIST_))
199
							__printflike(3, 0);
197
__END_DECLS
200
__END_DECLS

Return to bug 11294