Add a new command-line option, -P, which allows the user to specify

an arbitrary port to which syslog messages are to be sent.

PR:		bin/54026
Submitted by:	Oliver Fromme
MFC after:	10 days
This commit is contained in:
bms 2006-08-02 13:21:44 +00:00
parent 87adc023e9
commit 14416210a8
2 changed files with 27 additions and 11 deletions

View File

@ -43,6 +43,7 @@
.Op Fl 46Ais
.Op Fl f Ar file
.Op Fl h Ar host
.Op Fl P Ar port
.Op Fl p Ar pri
.Op Fl t Ar tag
.Op Ar message ...
@ -82,6 +83,16 @@ Log the specified file.
Send the message to the remote system
.Ar host
instead of logging it locally.
.It Fl P Ar port
Send the message to the specified
.Ar port
number on a remote system,
which can be specified as a service name
or as a decimal number.
The default is ``syslog''.
If an unknown service name is used,
.Nm
prints a warning and falls back to port 514.
.It Fl p Ar pri
Enter the message with the specified priority.
The priority may be specified numerically or as a ``facility.level''

View File

@ -63,7 +63,7 @@ __FBSDID("$FreeBSD$");
int decode(char *, CODE *);
int pencode(char *);
static void logmessage(int, char *, char *);
static void logmessage(int, char *, char *, char *);
static void usage(void);
struct socks {
@ -89,14 +89,15 @@ int
main(int argc, char *argv[])
{
int ch, logflags, pri;
char *tag, *host, buf[1024];
char *tag, *host, *svcname, buf[1024];
tag = NULL;
host = NULL;
svcname = "syslog";
pri = LOG_USER | LOG_NOTICE;
logflags = 0;
unsetenv("TZ");
while ((ch = getopt(argc, argv, "46Af:h:ip:st:")) != -1)
while ((ch = getopt(argc, argv, "46Af:h:iP:p:st:")) != -1)
switch((char)ch) {
case '4':
family = PF_INET;
@ -119,6 +120,9 @@ main(int argc, char *argv[])
case 'i': /* log process id also */
logflags |= LOG_PID;
break;
case 'P': /* service name or port number */
svcname = optarg;
break;
case 'p': /* priority */
pri = pencode(optarg);
break;
@ -147,11 +151,11 @@ main(int argc, char *argv[])
for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
len = strlen(*argv);
if (p + len > endp && p > buf) {
logmessage(pri, host, buf);
logmessage(pri, host, svcname, buf);
p = buf;
}
if (len > sizeof(buf) - 1)
logmessage(pri, host, *argv++);
logmessage(pri, host, svcname, *argv++);
else {
if (p != buf)
*p++ = ' ';
@ -160,10 +164,10 @@ main(int argc, char *argv[])
}
}
if (p != buf)
logmessage(pri, host, buf);
logmessage(pri, host, svcname, buf);
} else
while (fgets(buf, sizeof(buf), stdin) != NULL)
logmessage(pri, host, buf);
logmessage(pri, host, svcname, buf);
exit(0);
}
@ -171,7 +175,7 @@ main(int argc, char *argv[])
* Send the message to syslog, either on the local host, or on a remote host
*/
void
logmessage(int pri, char *host, char *buf)
logmessage(int pri, char *host, char *svcname, char *buf)
{
static struct socks *socks;
static int nsock = 0;
@ -189,9 +193,9 @@ logmessage(int pri, char *host, char *buf)
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(host, "syslog", &hints, &res);
error = getaddrinfo(host, svcname, &hints, &res);
if (error == EAI_SERVICE) {
warnx("syslog/udp: unknown service"); /* not fatal */
warnx("%s/udp: unknown service", svcname);
error = getaddrinfo(host, "514", &hints, &res);
}
if (error)
@ -282,7 +286,8 @@ static void
usage(void)
{
(void)fprintf(stderr, "usage: %s\n",
"logger [-46Ais] [-f file] [-h host] [-p pri] [-t tag] [message ...]"
"logger [-46Ais] [-f file] [-h host] [-P port] [-p pri] [-t tag]\n"
" [message ...]"
);
exit(1);
}